@@ -26,6 +26,10 @@ static void CALLBACK tessEndCB() {
2626 glEnd ();
2727}
2828
29+ static void CALLBACK tessEdgeFlagCB (GLboolean flag ) {
30+ glEdgeFlag (flag );
31+ }
32+
2933static void CALLBACK tessVertexCB (void * data ) {
3034 Wsgl_tess_vertex * v = (Wsgl_tess_vertex * )data ;
3135 if (v -> has_norm ) {
@@ -83,6 +87,9 @@ void wsgl_draw_tess_polygon(Wsgl_tess_vertex *vertices, int num_vertices, int re
8387 int normal_indices [MAX_VERTICES ];
8488 int n_vertices = 0 ;
8589 int n_normals = 0 ;
90+ GLboolean orig_depth_mask ;
91+ GLfloat cur_color [4 ];
92+ int has_transparency = 0 ;
8693
8794 tess = gluNewTess ();
8895 if (!tess ) return ;
@@ -92,6 +99,25 @@ void wsgl_draw_tess_polygon(Wsgl_tess_vertex *vertices, int num_vertices, int re
9299 gluTessCallback (tess , GLU_TESS_VERTEX , (void (CALLBACK * )())tessVertexCB );
93100 gluTessCallback (tess , GLU_TESS_ERROR , (void (CALLBACK * )())tessErrorCB );
94101 gluTessCallback (tess , GLU_TESS_COMBINE , (void (CALLBACK * )())tessCombineCB );
102+ gluTessCallback (tess , GLU_TESS_EDGE_FLAG , (void (CALLBACK * )())tessEdgeFlagCB );
103+
104+ /* Determine if depth writing should be disabled for order-independent transparency */
105+ glGetBooleanv (GL_DEPTH_WRITEMASK , & orig_depth_mask );
106+
107+ if (num_vertices > 0 && vertices [0 ].apply_cb ) {
108+ if (vertices [0 ].colr_type == PMODEL_RGBA && vertices [0 ].colr .direct .rgba .alpha < 1.0f ) {
109+ has_transparency = 1 ;
110+ }
111+ } else {
112+ glGetFloatv (GL_CURRENT_COLOR , cur_color );
113+ if (cur_color [3 ] < 1.0f ) {
114+ has_transparency = 1 ;
115+ }
116+ }
117+
118+ if (has_transparency && orig_depth_mask == GL_TRUE ) {
119+ glDepthMask (GL_FALSE );
120+ }
95121
96122 gluTessBeginPolygon (tess , NULL );
97123 gluTessBeginContour (tess );
@@ -115,6 +141,13 @@ void wsgl_draw_tess_polygon(Wsgl_tess_vertex *vertices, int num_vertices, int re
115141 gluTessEndPolygon (tess );
116142 gluDeleteTess (tess );
117143
144+ /* Restore default edge flag state for subsequent rendering */
145+ glEdgeFlag (GL_TRUE );
146+
147+ if (has_transparency && orig_depth_mask == GL_TRUE ) {
148+ glDepthMask (GL_TRUE );
149+ }
150+
118151 if (record_geom_flag && n_vertices > 0 ) {
119152 wsgl_add_geometry (GEOM_FACE , vertex_indices , normal_indices , n_vertices );
120153 }
0 commit comments