From: Michel Pollet Date: Mon, 21 May 2012 00:30:37 +0000 (+0100) Subject: reprap: c3 update X-Git-Tag: v1.0~121 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=144466480c0d5f19b95db412a919004641d5d4be;p=sx%2Fsimavr.git reprap: c3 update Allows having a custom callback set for geometries Signed-off-by: Michel Pollet --- diff --git a/examples/board_reprap/src/c3/c3context.c b/examples/board_reprap/src/c3/c3context.c index 6c28758..1f9bc4c 100644 --- a/examples/board_reprap/src/c3/c3context.c +++ b/examples/board_reprap/src/c3/c3context.c @@ -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); } } diff --git a/examples/board_reprap/src/c3/c3context.h b/examples/board_reprap/src/c3/c3context.h index 8d42887..42f9c8d 100644 --- a/examples/board_reprap/src/c3/c3context.h +++ b/examples/board_reprap/src/c3/c3context.h @@ -46,6 +46,10 @@ c3context_init( int w, int h); +void +c3context_dispose( + c3context_p c); + // Reproject geometry for dirty objects void c3context_prepare( diff --git a/examples/board_reprap/src/c3/c3driver_geometry.h b/examples/board_reprap/src/c3/c3driver_geometry.h index 5a6d49b..bfdbb2e 100644 --- a/examples/board_reprap/src/c3/c3driver_geometry.h +++ b/examples/board_reprap/src/c3/c3driver_geometry.h @@ -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; diff --git a/examples/board_reprap/src/c3/c3geometry.c b/examples/board_reprap/src/c3/c3geometry.c index d1f2994..98c6ee9 100644 --- a/examples/board_reprap/src/c3/c3geometry.c +++ b/examples/board_reprap/src/c3/c3geometry.c @@ -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); +} diff --git a/examples/board_reprap/src/c3/c3geometry.h b/examples/board_reprap/src/c3/c3geometry.h index 2bfaa07..a6b5c5e 100644 --- a/examples/board_reprap/src/c3/c3geometry.h +++ b/examples/board_reprap/src/c3/c3geometry.h @@ -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); diff --git a/examples/board_reprap/src/reprap_gl.c b/examples/board_reprap/src/reprap_gl.c index fc6d16f..73494da 100644 --- a/examples/board_reprap/src/reprap_gl.c +++ b/examples/board_reprap/src/reprap_gl.c @@ -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; } diff --git a/examples/board_reprap/src/reprap_gl.h b/examples/board_reprap/src/reprap_gl.h index c0e85e8..20ef0aa 100644 --- a/examples/board_reprap/src/reprap_gl.h +++ b/examples/board_reprap/src/reprap_gl.h @@ -31,4 +31,7 @@ gl_init( int gl_runloop(); +void +gl_dispose(); + #endif /* __REPRAP_GL_H___ */