Commit 144466480c0d5f19b95db412a919004641d5d4be
authorMichel Pollet <buserror@gmail.com>
Mon, 21 May 2012 00:30:37 +0000 (01:30 +0100)
committerMichel Pollet <buserror@gmail.com>
Mon, 21 May 2012 00:30:37 +0000 (01:30 +0100)
Allows having a custom callback set for geometries

Signed-off-by: Michel Pollet <buserror@gmail.com>
7 files changed:
examples/board_reprap/src/c3/c3context.c
examples/board_reprap/src/c3/c3context.h
examples/board_reprap/src/c3/c3driver_geometry.h
examples/board_reprap/src/c3/c3geometry.c
examples/board_reprap/src/c3/c3geometry.h
examples/board_reprap/src/reprap_gl.c
examples/board_reprap/src/reprap_gl.h

index 6c287585f4c89cfd5364bd7ae30b31ed66f7d1dd..1f9bc4c94e36d401a3b7fcd83484040f030748a1 100644 (file)
@@ -47,6 +47,15 @@ c3context_init(
        return c;
 }
 
+void
+c3context_dispose(
+               c3context_p c)
+{
+       c3object_dispose(c->root);
+       c3geometry_array_free(&c->projected);
+       free(c);
+}
+
 void
 c3context_prepare(
                c3context_p c)
@@ -67,6 +76,6 @@ c3context_draw(
        c3context_prepare(c);
        for (int gi = 0; gi < c->projected.count; gi++) {
                c3geometry_p g = c->projected.e[gi];
-               C3_DRIVER(c, geometry_draw, g);
+               c3geometry_draw(g);
        }
 }
index 8d42887557548fcaf499037a3ec412c66b42797a..42f9c8d1b58bf2d96f090281b299619b7dd8ff4c 100644 (file)
@@ -46,6 +46,10 @@ c3context_init(
                int w,
                int h);
 
+void
+c3context_dispose(
+               c3context_p c);
+
 // Reproject geometry for dirty objects
 void
 c3context_prepare(
index 5a6d49bc532e66e058326501ed80984833bc590d..bfdbb2eae8f207d0b43a3e2e2bf3a9f1e717a13d 100644 (file)
@@ -34,6 +34,9 @@ typedef struct c3driver_geometry_t {
        void (*prepare)(
                        struct c3geometry_t * geometry,
                        const struct c3driver_geometry_t *d);
+       void (*draw)(
+                       struct c3geometry_t * geometry,
+                       const struct c3driver_geometry_t *d);
 } c3driver_geometry_t, *c3driver_geometry_p;
 
 
index d1f2994ae074939e6a62b365e18474d22d419ac5..98c6ee9818c6ebd16371fbd2590c843f1fe443e7 100644 (file)
@@ -63,9 +63,20 @@ _c3geometry_prepare(
 //     C3_DRIVER_INHERITED(g, d, prepare);
 }
 
+static void
+_c3geometry_draw(
+               c3geometry_p g,
+               const struct c3driver_geometry_t *d)
+{
+       if (g->object && g->object->context)
+               C3_DRIVER(g->object->context, geometry_draw, g);
+//     C3_DRIVER_INHERITED(g, d, draw);
+}
+
 const  c3driver_geometry_t c3geometry_driver = {
        .dispose = _c3geometry_dispose,
        .prepare = _c3geometry_prepare,
+       .draw = _c3geometry_draw,
 };
 
 c3geometry_p
@@ -95,6 +106,24 @@ c3geometry_init(
        return g;
 }
 
+c3driver_geometry_p
+c3geometry_get_custom(
+               c3geometry_p g )
+{
+       if (g->custom)
+               return (c3driver_geometry_p)g->driver[0];
+       int cnt = 0;
+       for (int di = 0; g->driver[di]; di++)
+               cnt++;
+       c3driver_geometry_p * newd = malloc(sizeof(c3driver_geometry_p) * (cnt + 2));
+       memcpy(&newd[1], g->driver, (cnt + 1) * sizeof(c3driver_geometry_p));
+       newd[0] = malloc(sizeof(c3driver_geometry_t));
+       memset(newd[0], 0, sizeof(c3driver_geometry_t));
+       g->custom = 1;
+       g->driver = (typeof(g->driver))newd;
+       return newd[0];
+}
+
 void
 c3geometry_dispose(
                c3geometry_p g)
@@ -110,3 +139,10 @@ c3geometry_prepare(
                return;
        C3_DRIVER(g, prepare);
 }
+
+void
+c3geometry_draw(
+               c3geometry_p g )
+{
+       C3_DRIVER(g, draw);
+}
index 2bfaa079ba1d4bb40b264e6ee0e6fa9b11201b43..a6b5c5e1638e28b9b2b250b6f81544b0958609ba 100644 (file)
@@ -54,11 +54,14 @@ typedef union {
 
 typedef struct c3geometry_t {
        c3geometry_type_t       type;   // GL_LINES etc
-       int                                     dirty : 1, texture : 1;
+       int                                     dirty : 1,
+                                               texture : 1,    // has a valid material.texture
+                                               custom : 1;             // has a custom driver
        str_p                           name;   // optional
        c3material_t            mat;
        struct c3object_t * object;
        const struct c3driver_geometry_t ** driver;
+
        c3vertex_array_t        vertice;
        c3tex_array_t           textures;
        c3colorf_array_t        colorf;
@@ -66,13 +69,6 @@ typedef struct c3geometry_t {
        // projected version of the vertice
        c3vertex_array_t        projected;
        c3bbox_t                        bbox;
-
-       /*
-        * optional, geometry dependant custom draw method
-        * return nonzero will orevent the default drawing code
-        * from being called (c3context one)
-        */
-       int     (*draw)(struct c3geometry_t *);
 } c3geometry_t, *c3geometry_p;
 
 DECLARE_C_ARRAY(c3geometry_p, c3geometry_array, 4);
@@ -93,6 +89,14 @@ c3geometry_dispose(
 void
 c3geometry_prepare(
                c3geometry_p g );
+void
+c3geometry_draw(
+               c3geometry_p g );
+
+//! allocate (if not there) and return a custom driver for this geometry
+struct c3driver_geometry_t *
+c3geometry_get_custom(
+               c3geometry_p g );
 
 IMPLEMENT_C_ARRAY(c3geometry_array);
 IMPLEMENT_C_ARRAY(c3vertex_array);
index fc6d16f9f3d26d924bbdf69f8dd7c983bb64dfce..73494da4ca8ccb46f6fe6fc1f7b0c95ef127d7f7 100644 (file)
@@ -64,6 +64,7 @@ _gl_key_cb(
        switch (key) {
                case 'q':
                //      avr_vcd_stop(&vcd_file);
+                       c3context_dispose(c3);
                        exit(0);
                        break;
                case 'r':
@@ -83,7 +84,6 @@ _c3_geometry_prepare(
                const struct c3driver_context_t *d,
                c3geometry_p g)
 {
-       printf("_c3_geometry_prepare %p %d/%d!\n", g, g->type.type, g->type.subtype);
        switch(g->type.type) {
                case C3_TEXTURE_TYPE: {
                        c3texture_p t = (c3texture_p)g;
@@ -206,7 +206,7 @@ _gl_display_cb(void)                /* function called whenever redisplay needed */
        c3transform_set(head->transform.e[0], &headmove);
 
        if (c3->root->dirty) {
-               printf("reproject\n");
+       //      printf("reproject\n");
                c3context_prepare(c3);
 
                qsort(c3->projected.e, c3->projected.count,
@@ -380,9 +380,16 @@ gl_init(
        return 1;
 }
 
+void
+gl_dispose()
+{
+       c3context_dispose(c3);
+}
+
 int
 gl_runloop()
 {
        glutMainLoop();
+       gl_dispose();
        return 0;
 }
index c0e85e856e7513628e6d00a0791ad8876627ce9d..20ef0aa644ad81d4540ab52bb6f0182f7eb9166f 100644 (file)
@@ -31,4 +31,7 @@ gl_init(
 int
 gl_runloop();
 
+void
+gl_dispose();
+
 #endif /* __REPRAP_GL_H___ */