From ad9a03da5c53612470b06bebc77a3abfd206e868 Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 30 Mar 2004 22:02:29 +0000 Subject: [PATCH] support dynamic geoms properly with display lists --- .../src/glstuff/glGraphicsStateGuardian_src.I | 24 +++++ .../glstuff/glGraphicsStateGuardian_src.cxx | 102 ++++-------------- .../src/glstuff/glGraphicsStateGuardian_src.h | 2 + panda/src/gobj/geomSprite.cxx | 15 +++ panda/src/gobj/geomSprite.h | 2 + 5 files changed, 66 insertions(+), 79 deletions(-) diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.I b/panda/src/glstuff/glGraphicsStateGuardian_src.I index 8b4698edb9..9a8b62885a 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.I +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.I @@ -17,6 +17,30 @@ //////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////// +// Function: CLP(GraphicsStateGuardian)::draw_display_list +// Access: Public +// Description: If the GeomContext is non-NULL and contains a valid +// display list, uses it to draw the geometry if +// appropriate, and returns true. If the display list +// is absent or cannot be used for some reason, does +// nothing and returns false. +//////////////////////////////////////////////////////////////////// +INLINE bool CLP(GraphicsStateGuardian):: +draw_display_list(GeomContext *gc) { + if (gc != (GeomContext *)NULL && _vertex_colors_enabled) { + _draw_primitive_pcollector.start(); + CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); + GLP(CallList)(ggc->_index); +#ifdef DO_PSTATS + _vertices_display_list_pcollector.add_level(ggc->_num_verts); +#endif + _draw_primitive_pcollector.stop(); + return true; + } + return false; +} + //////////////////////////////////////////////////////////////////// // Function: CLP(GraphicsStateGuardian)::report_errors // Access: Public, Static diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index bc51c8757e..336e5821a1 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -652,14 +652,7 @@ draw_point(GeomPoint *geom, GeomContext *gc) { GLCAT.spam() << "draw_point()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -725,14 +718,7 @@ draw_line(GeomLine *geom, GeomContext *gc) { GLCAT.spam() << "draw_line()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -808,14 +794,7 @@ draw_linestrip(GeomLinestrip *geom, GeomContext *gc) { GLCAT.spam() << "draw_linestrip()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1190,14 +1169,7 @@ draw_polygon(GeomPolygon *geom, GeomContext *gc) { GLCAT.spam() << "draw_polygon()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1280,14 +1252,7 @@ draw_tri(GeomTri *geom, GeomContext *gc) { GLCAT.spam() << "draw_tri()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1367,14 +1332,7 @@ draw_quad(GeomQuad *geom, GeomContext *gc) { GLCAT.spam() << "draw_quad()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1452,14 +1410,7 @@ draw_tristrip(GeomTristrip *geom, GeomContext *gc) { GLCAT.spam() << "draw_tristrip()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1560,14 +1511,7 @@ draw_trifan(GeomTrifan *geom, GeomContext *gc) { GLCAT.spam() << "draw_trifan()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1668,14 +1612,7 @@ draw_sphere(GeomSphere *geom, GeomContext *gc) { GLCAT.spam() << "draw_sphere()" << endl; #endif - if (gc != (GeomContext *)NULL) { - _draw_primitive_pcollector.start(); - CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc); - GLP(CallList)(ggc->_index); -#ifdef DO_PSTATS - _vertices_display_list_pcollector.add_level(ggc->_num_verts); -#endif - _draw_primitive_pcollector.stop(); + if (draw_display_list(gc)) { return; } @@ -1841,6 +1778,20 @@ release_texture(TextureContext *tc) { //////////////////////////////////////////////////////////////////// GeomContext *CLP(GraphicsStateGuardian):: prepare_geom(Geom *geom) { + if (!_vertex_colors_enabled) { + // We can't build a display list (or play back a display list) if + // its color is overridden with a scene graph color. Maybe if we + // take advantage of the OpenGL color matrix we can do this, but + // for now we'll just ignore it. + return NULL; + } + + if (geom->is_dynamic()) { + // If the Geom is dynamic in some way, we shouldn't try to + // display-list it. + return NULL; + } + CLP(GeomContext) *ggc = new CLP(GeomContext)(geom); ggc->_index = GLP(GenLists)(1); if (GLCAT.is_debug()) { @@ -1854,16 +1805,12 @@ prepare_geom(Geom *geom) { return NULL; } - /* // We need to temporarily force normals and UV's on, so the display // list will have them built in. bool old_normals_enabled = _normals_enabled; bool old_texturing_enabled = _texturing_enabled; - bool old_vertex_colors_enabled = _vertex_colors_enabled; _normals_enabled = true; _texturing_enabled = true; - _vertex_colors_enabled = true; - */ #ifdef DO_PSTATS // Count up the number of vertices we're about to render, by @@ -1891,11 +1838,8 @@ prepare_geom(Geom *geom) { ggc->_num_verts = (int)(num_verts + 0.5); #endif - /* _normals_enabled = old_normals_enabled; _texturing_enabled = old_texturing_enabled; - _vertex_colors_enabled = old_vertex_colors_enabled; - */ report_my_gl_errors(); return ggc; diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.h b/panda/src/glstuff/glGraphicsStateGuardian_src.h index 0c436eee33..8324417c04 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.h +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.h @@ -66,6 +66,8 @@ public: virtual void draw_trifan(GeomTrifan *geom, GeomContext *gc); virtual void draw_sphere(GeomSphere *geom, GeomContext *gc); + INLINE bool draw_display_list(GeomContext *gc); + virtual TextureContext *prepare_texture(Texture *tex); virtual void apply_texture(TextureContext *tc); virtual void release_texture(TextureContext *tc); diff --git a/panda/src/gobj/geomSprite.cxx b/panda/src/gobj/geomSprite.cxx index 8f8e32ab0f..a0fddd1fcb 100644 --- a/panda/src/gobj/geomSprite.cxx +++ b/panda/src/gobj/geomSprite.cxx @@ -82,6 +82,21 @@ draw_immediate(GraphicsStateGuardianBase *gsg, GeomContext *gc) { gsg->draw_sprite(this, gc); } +//////////////////////////////////////////////////////////////////// +// Function: GeomSprite::is_dynamic +// Access: Public, Virtual +// Description: Returns true if the Geom has any dynamic properties +// that are expected to change from one frame to the +// next, or false if the Geom is largely static. For +// now, this is the same thing as asking whether its +// vertices are indexed. +//////////////////////////////////////////////////////////////////// +bool GeomSprite:: +is_dynamic() const { + // Sprites are always dynamic. + return true; +} + //////////////////////////////////////////////////////////////////// // Function: GeomSprite::write_datagram // Access: Public diff --git a/panda/src/gobj/geomSprite.h b/panda/src/gobj/geomSprite.h index f4303681bf..d78d199b85 100644 --- a/panda/src/gobj/geomSprite.h +++ b/panda/src/gobj/geomSprite.h @@ -43,6 +43,8 @@ public: virtual Geom *explode() const { return new GeomSprite(*this); } + virtual bool is_dynamic() const; + static float get_frustum_top() { return 1.0f; } static float get_frustum_bottom() { return -1.0f; } static float get_frustum_left() { return -1.0f; }