~marten-r/widelands/feature-3d-rendering-open-gl-es-compat

« back to all changes in this revision

Viewing changes to src/graphic/render/gl_surface.cc

  • Committer: Marten Richter \
  • Date: 2013-12-07 16:34:54 UTC
  • Revision ID: marten.richter@freenet.de-20131207163454-ssy014khpxtrgvq9
Add z-Ordering for rendering, fix some drawing glitches, change api for 3d rendering

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
/**
102
102
 * Draws the outline of a rectangle
103
103
 */
104
 
void GLSurface::draw_rect3d(const Rect& rc,int32_t z, const RGBColor clr)
 
104
void GLSurface::draw_rect3d(const Rect& rc, int32_t z, const RGBColor clr)
105
105
{
106
106
        assert(g_opengl);
107
 
        glDisable(GL_BLEND);
108
 
        glDisable(GL_TEXTURE_2D);
109
 
        glLineWidth(1);
110
107
 
111
 
        glBegin(GL_LINE_LOOP); {
112
 
                glColor3ub(clr.r, clr.g, clr.b);
113
 
                glVertex3f(rc.x + 0.5f,        rc.y + 0.5f, z);
114
 
                glVertex3f(rc.x + rc.w - 0.5f, rc.y + 0.5f,z);
115
 
                glVertex3f(rc.x + rc.w - 0.5f, rc.y + rc.h - 0.5f, z);
116
 
                glVertex3f(rc.x + 0.5f,        rc.y + rc.h - 0.5f, z);
117
 
        } glEnd();
118
 
        glEnable(GL_TEXTURE_2D);
 
108
        render_tasks.insert(RenderTask(task_draw_rect, z_project *  Point3D(rc, z) , rc, z, clr ));
119
109
}
120
110
 
121
111
 
126
116
        assert(rc.x >= 0);
127
117
        assert(rc.y >= 0);
128
118
        assert(g_opengl);
129
 
        glDisable(GL_TEXTURE_2D);
130
 
        glDisable(GL_BLEND);
131
119
 
132
 
        glBegin(GL_QUADS); {
133
 
                glColor4ub(clr.r, clr.g, clr.b, clr.a);
134
 
                glVertex3f(rc.x,        rc.y, z);
135
 
                glVertex3f(rc.x + rc.w, rc.y, z);
136
 
                glVertex3f(rc.x + rc.w, rc.y + rc.h, z);
137
 
                glVertex3f(rc.x,        rc.y + rc.h, z);
138
 
        } glEnd();
139
 
        glEnable(GL_TEXTURE_2D);
 
120
        render_tasks.insert(RenderTask(task_fill_rect, z_project * Point3D(rc, z) , rc, z, clr ));
140
121
}
141
122
 
142
123
/**
265
246
void GLSurface::blit3d
266
247
        (const Point3D& dst, const Surface* image, const Rect& srcrc, Composite cm)
267
248
{
268
 
        // Note: This function is highly optimized and therefore does not restore
269
 
        // all state. It also assumes that all other glStuff restores state to make
270
 
        // this function faster.
271
 
 
272
249
        assert(g_opengl);
273
 
        const GLSurfaceTexture& surf = *static_cast<const GLSurfaceTexture*>(image);
274
 
 
275
 
        /* Set a texture scaling factor. Normally texture coordinates
276
 
        * (see glBegin()...glEnd() Block below) are given in the range 0-1
277
 
        * to avoid the calculation (and let opengl do it) the texture
278
 
        * space is modified. glMatrixMode select which matrixconst  to manipulate
279
 
        * (the texture transformation matrix in this case). glLoadIdentity()
280
 
        * resets the (selected) matrix to the identity matrix. And finally
281
 
        * glScalef() calculates the texture matrix.
282
 
        */
283
 
        glMatrixMode(GL_TEXTURE);
284
 
        glLoadIdentity();
285
 
        glScalef
286
 
                (1.0f / static_cast<GLfloat>(surf.get_tex_w()),
287
 
                 1.0f / static_cast<GLfloat>(surf.get_tex_h()), 1);
288
 
 
289
 
        // Enable Alpha blending
290
 
        if (cm == CM_Normal) {
291
 
                glEnable(GL_BLEND);
292
 
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
293
 
        } else {
294
 
                glDisable(GL_BLEND);
 
250
        render_tasks.insert(RenderTask(task_blit, z_project * dst, dst, image, srcrc, cm));
 
251
}
 
252
 
 
253
 
 
254
 
 
255
void GLSurface::start_rendering3d(Vector z_proj)
 
256
{
 
257
        z_project = z_proj;
 
258
}
 
259
 
 
260
void GLSurface::end_rendering3d()
 
261
{
 
262
        std::multiset<RenderTask>::iterator itty;
 
263
        for (itty = render_tasks.begin(); itty != render_tasks.end(); itty++)
 
264
        {
 
265
                const RenderTask & cur=*itty;
 
266
                switch (cur.task_id) {
 
267
                case task_fill_rect: {
 
268
                        glDisable(GL_TEXTURE_2D);
 
269
                        glDisable(GL_BLEND);
 
270
 
 
271
                        glBegin(GL_QUADS); {
 
272
                                glColor4ub(cur.clr.r, cur.clr.g, cur.clr.b, cur.clr.a);
 
273
                                glVertex3f(cur.rect0.x,cur.rect0.y, cur.point0.z);
 
274
                                glVertex3f(cur.rect0.x + cur.rect0.w, cur.rect0.y, cur.point0.z);
 
275
                                glVertex3f(cur.rect0.x + cur.rect0.w, cur.rect0.y + cur.rect0.h, cur.point0.z);
 
276
                                glVertex3f(cur.rect0.x, cur.rect0.y + cur.rect0.h, cur.point0.z);
 
277
                        } glEnd();
 
278
                        glEnable(GL_TEXTURE_2D);
 
279
 
 
280
                } break;
 
281
                case task_draw_rect: {
 
282
                        glDisable(GL_BLEND);
 
283
                        glDisable(GL_TEXTURE_2D);
 
284
                        glLineWidth(1);
 
285
 
 
286
                        glBegin(GL_LINE_LOOP); {
 
287
                                glColor3ub(cur.clr.r, cur.clr.g, cur.clr.b);
 
288
                                glVertex3f(cur.rect0.x + 0.5f, cur.rect0.y + 0.5f, cur.point0.z);
 
289
                                glVertex3f(cur.rect0.x + cur.rect0.w - 0.5f, cur.rect0.y + 0.5f,cur.point0.z);
 
290
                                glVertex3f(cur.rect0.x + cur.rect0.w - 0.5f, cur.rect0.y + cur.rect0.h - 0.5f, cur.point0.z);
 
291
                                glVertex3f(cur.rect0.x + 0.5f, cur.rect0.y + cur.rect0.h - 0.5f, cur.point0.z);
 
292
                        } glEnd();
 
293
                        glEnable(GL_TEXTURE_2D);
 
294
 
 
295
                } break;
 
296
                case task_blit: {
 
297
                        // Note: This function is highly optimized and therefore does not restore
 
298
                        // all state. It also assumes that all other glStuff restores state to make
 
299
                        // this function faster.
 
300
                        const GLSurfaceTexture& surf =
 
301
                                        *static_cast<const GLSurfaceTexture*> (cur.image);
 
302
 
 
303
                        /* Set a texture scaling factor. Normally texture coordinates
 
304
                         * (see glBegin()...glEnd() Block below) are given in the range 0-1
 
305
                         * to avoid the calculation (and let opengl do it) the texture
 
306
                         * space is modified. glMatrixMode select which matrixconst  to manipulate
 
307
                         * (the texture transformation matrix in this case). glLoadIdentity()
 
308
                         * resets the (selected) matrix to the identity matrix. And finally
 
309
                         * glScalef() calculates the texture matrix.
 
310
                         */
 
311
                        glMatrixMode( GL_TEXTURE);
 
312
                        glLoadIdentity();
 
313
                        glScalef(1.0f / static_cast<GLfloat> (surf.get_tex_w()),
 
314
                                        1.0f / static_cast<GLfloat> (surf.get_tex_h()), 1);
 
315
 
 
316
                        // Enable Alpha blending
 
317
                        if (cur.cm == CM_Normal) {
 
318
                                glEnable( GL_BLEND);
 
319
                                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
320
                        } else {
 
321
                                glDisable( GL_BLEND);
 
322
                        }
 
323
 
 
324
                        glBindTexture(GL_TEXTURE_2D, surf.get_gl_texture());
 
325
 
 
326
                        glBegin( GL_QUADS);
 
327
                        {
 
328
                                // set color white, otherwise textures get mixed with color
 
329
                                glColor3f(1.0, 1.0, 1.0);
 
330
                                // top-left
 
331
                                glTexCoord2i(cur.rect0.x, cur.rect0.y);
 
332
                                glVertex3i(cur.point0.x, cur.point0.y, cur.point0.z);
 
333
                                // top-right
 
334
                                glTexCoord2i(cur.rect0.x + cur.rect0.w, cur.rect0.y);
 
335
                                glVertex3f((cur.point0.x + (int32_t) cur.rect0.w),cur.point0.y, cur.point0.z);
 
336
                                // bottom-right
 
337
                                glTexCoord2i(cur.rect0.x + cur.rect0.w, cur.rect0.y + cur.rect0.h);
 
338
                                glVertex3f((cur.point0.x + (int32_t) cur.rect0.w),
 
339
                                                (cur.point0.y + (int32_t) cur.rect0.h), cur.point0.z);
 
340
                                // bottom-left
 
341
                                glTexCoord2i(cur.rect0.x, cur.rect0.y + cur.rect0.h);
 
342
                                glVertex3f(cur.point0.x, (cur.point0.y + (int32_t) cur.rect0.h), cur.point0.z);
 
343
                        }
 
344
                        glEnd();
 
345
 
 
346
                }
 
347
                        break;
 
348
                };
 
349
 
295
350
        }
296
 
 
297
 
        glBindTexture(GL_TEXTURE_2D, surf.get_gl_texture());
298
 
 
299
 
        glBegin(GL_QUADS); {
300
 
                // set color white, otherwise textures get mixed with color
301
 
                glColor3f(1.0, 1.0, 1.0);
302
 
                // top-left
303
 
                glTexCoord2i(srcrc.x, srcrc.y);
304
 
                glVertex3i(dst.x, dst.y, dst.z);
305
 
                // top-right
306
 
                glTexCoord2i(srcrc.x + srcrc.w, srcrc.y);
307
 
                glVertex3f((dst.x + (int32_t) srcrc.w), dst.y, dst.z);
308
 
                // bottom-right
309
 
                glTexCoord2i(srcrc.x + srcrc.w, srcrc.y + srcrc.h);
310
 
                glVertex3f((dst.x + (int32_t) srcrc.w), (dst.y + (int32_t) srcrc.h), dst.z);
311
 
                // bottom-left
312
 
                glTexCoord2i(srcrc.x, srcrc.y + srcrc.h);
313
 
                glVertex3f(dst.x, (dst.y + (int32_t) srcrc.h), dst.z);
314
 
        } glEnd();
315
 
 
 
351
        render_tasks.clear();
316
352
}
317
353
 
318