From 1d39956926c07a7625830872db830789a8dda508 Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Tue, 12 Jun 2012 18:33:15 +0100 Subject: [PATCH] c3geometry: Removed projected vertices Now uses a world aligned transform matrix. Changed randering code to use it. Still use a projection matrix for now. Signed-off-by: Michel Pollet --- examples/board_reprap/src/reprap_gl.c | 14 +++----------- examples/shared/libc3/src/c3geometry.c | 14 ++++++-------- examples/shared/libc3/src/c3geometry.h | 24 ++++++++++++------------ examples/shared/libc3/src/c3object.c | 7 +------ examples/shared/libc3/srcgl/c3gl.c | 15 ++++++++------- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/examples/board_reprap/src/reprap_gl.c b/examples/board_reprap/src/reprap_gl.c index b11f874..2ed140c 100644 --- a/examples/board_reprap/src/reprap_gl.c +++ b/examples/board_reprap/src/reprap_gl.c @@ -162,6 +162,7 @@ _gl_reshape_cb(int w, int h) _w = w; _h = h; + glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, _w, _h); gl_offscreenReshape(_w, _h); glutPostRedisplay(); @@ -238,9 +239,9 @@ _gl_display_cb(void) /* function called whenever redisplay needed */ // Set up projection matrix glMatrixMode(GL_PROJECTION); // Select projection matrix - glLoadIdentity(); // Start with an identity matrix + c3mat4 p = perspective3D(50, (float)_w / (float)_h, z_min, z_max); + glLoadMatrixf(p.n); - gluPerspective(50, (float)_w / (float)_h, z_min, z_max); #if 0 glCullFace(GL_BACK); glEnable(GL_CULL_FACE); @@ -257,14 +258,6 @@ _gl_display_cb(void) /* function called whenever redisplay needed */ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Type Of Blending To Use glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glMultMatrixf(c3context_view_get(c3)->cam.mtx.n); - glTranslatef(-c3context_view_get(c3)->cam.eye.n[VX], - -c3context_view_get(c3)->cam.eye.n[VY], - -c3context_view_get(c3)->cam.eye.n[VZ]); - - dumpError("flush"); c3context_draw(c3); @@ -290,7 +283,6 @@ _gl_display_cb(void) /* function called whenever redisplay needed */ glScalef(1, -1, 1); glTranslatef(0, -1 * _h, 0); glMatrixMode(GL_MODELVIEW); // Select modelview matrix - glLoadIdentity(); // Start with an identity matrix if (hud) c3context_draw(hud); diff --git a/examples/shared/libc3/src/c3geometry.c b/examples/shared/libc3/src/c3geometry.c index a10eda5..7f607f6 100644 --- a/examples/shared/libc3/src/c3geometry.c +++ b/examples/shared/libc3/src/c3geometry.c @@ -49,7 +49,6 @@ _c3geometry_dispose( C3_DRIVER(g->object->context, geometry_dispose, g); str_free(g->name); c3vertex_array_free(&g->vertice); - c3vertex_array_free(&g->projected); c3tex_array_free(&g->textures); c3colorf_array_free(&g->colorf); free(g); @@ -63,18 +62,17 @@ _c3geometry_project( c3mat4p m) { if (g->vertice.count) { - c3vertex_array_realloc(&g->projected, g->vertice.count); - g->projected.count = g->vertice.count; for (int vi = 0; vi < g->vertice.count; vi++) { - g->projected.e[vi] = c3mat4_mulv3(m, g->vertice.e[vi]); + c3vec3 v = c3mat4_mulv3(m, g->vertice.e[vi]); if (vi == 0) - g->bbox.min = g->bbox.max = g->projected.e[vi]; + g->bbox.min = g->bbox.max = v; else { - g->bbox.max = c3vec3_min(g->bbox.min, g->projected.e[vi]); - g->bbox.max = c3vec3_max(g->bbox.max, g->projected.e[vi]); + g->bbox.max = c3vec3_min(g->bbox.min, v); + g->bbox.max = c3vec3_max(g->bbox.max, v); } } - } + } else + g->bbox.min = g->bbox.max = c3vec3f(0,0,0); if (g->object && g->object->context) C3_DRIVER(g->object->context, geometry_project, g, m); diff --git a/examples/shared/libc3/src/c3geometry.h b/examples/shared/libc3/src/c3geometry.h index e7d0183..68831a8 100644 --- a/examples/shared/libc3/src/c3geometry.h +++ b/examples/shared/libc3/src/c3geometry.h @@ -68,8 +68,8 @@ enum { * real format of the vertices. like GL_LINES etc */ typedef struct c3geometry_type_t { - uint32_t type; - c3apiobject_t subtype; + uint32_t type; // C3_RAW_TYPE etc + c3apiobject_t subtype; // GL_LINES etc } c3geometry_type_t; /*! @@ -79,23 +79,23 @@ typedef struct c3geometry_type_t { * c3object that has the projection */ typedef struct c3geometry_t { - c3geometry_type_t type; // C3_TRIANGLE_TYPE, GL_LINES etc + c3geometry_type_t type; // geometry type int dirty : 1, custom : 1; // has a custom driver str_p name; // optional + c3apiobject_t bid; // buffer id for opengl + c3material_t mat; - struct c3object_t * object; + struct c3object_t * object; // parent object const struct c3driver_geometry_t ** driver; + c3bbox_t bbox; // world aligned bounding box c3vertex_array_t vertice; - c3tex_array_t textures; - c3colorf_array_t colorf; - c3vertex_array_t normals; - c3indices_array_t indices; - - // projected version of the vertice - c3vertex_array_t projected; - c3bbox_t bbox; + c3tex_array_t textures; // optional: texture coordinates + c3vertex_array_t normals; // optional: vertex normals + c3indices_array_t indices; // optional: vertex indices + // could go ? + c3colorf_array_t colorf; // optional: vertex colors /* * Some shared attributes diff --git a/examples/shared/libc3/src/c3object.c b/examples/shared/libc3/src/c3object.c index 04d17f1..1243e0d 100644 --- a/examples/shared/libc3/src/c3object.c +++ b/examples/shared/libc3/src/c3object.c @@ -81,21 +81,16 @@ _c3object_project( const c3driver_object_t * d, c3mat4p m) { + o->world = *m; if (!o->dirty) return; - -// c3mat4 identity = identity3D(); c3mat4 p = *m; for (int pi = 0; pi < o->transform.count; pi++) p = c3mat4_mul(&p, &o->transform.e[pi]->matrix); -// bool is_identity = c3mat4_equal(m, &identity); o->world = p; for (int gi = 0; gi < o->geometry.count; gi++) { c3geometry_p g = o->geometry.e[gi]; - c3vertex_array_clear(&g->projected); - - g->bbox.min = g->bbox.max = c3vec3f(0,0,0); c3geometry_project(g, &p); } for (int oi = 0; oi < o->objects.count; oi++) diff --git a/examples/shared/libc3/srcgl/c3gl.c b/examples/shared/libc3/srcgl/c3gl.c index 889633f..513a79b 100644 --- a/examples/shared/libc3/srcgl/c3gl.c +++ b/examples/shared/libc3/srcgl/c3gl.c @@ -237,10 +237,15 @@ _c3_geometry_draw( glColor4fv(g->mat.color.n); dumpError("glColor"); // glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, g->mat.color.n); - glVertexPointer(3, GL_FLOAT, 0, - g->projected.count ? g->projected.e : g->vertice.e); + glVertexPointer(3, GL_FLOAT, 0, g->vertice.e); glEnableClientState(GL_VERTEX_ARRAY); dumpError("GL_VERTEX_ARRAY"); + + c3mat4 eye = c3mat4_mul( + &g->object->world, + &c3context_view_get(g->object->context)->cam.mtx); + glLoadMatrixf(eye.n); + glDisable(GL_TEXTURE_2D); if (g->mat.texture) { GLuint mode = g->mat.texture->normalize ? @@ -265,15 +270,11 @@ _c3_geometry_draw( glEnableClientState(GL_NORMAL_ARRAY); } if (g->indices.count) { - // GLCHECK(glIndexPointer(GL_UNSIGNED_SHORT, 0, g->indices.e)); - // glEnableClientState(GL_INDEX_ARRAY); GLCHECK(glDrawElements((GLuint)g->type.subtype, g->indices.count, GL_UNSIGNED_SHORT, g->indices.e)); - // glDisableClientState(GL_INDEX_ARRAY); } else { - glDrawArrays((GLuint)g->type.subtype, 0, - g->projected.count ? g->projected.count : g->vertice.count); + glDrawArrays((GLuint)g->type.subtype, 0, g->vertice.count); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); -- 2.39.5