1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <OpenGl_GlCore11.hxx>
23 #include <OpenGl_Polygon.hxx>
25 #include <OpenGl_telem_util.hxx>
26 #include <OpenGl_TextureBox.hxx>
28 #include <OpenGl_AspectFace.hxx>
29 #include <OpenGl_Structure.hxx>
33 #if (defined(_WIN32) || defined(__WIN32__))
45 typedef EXTRA_VERTEX* extra_vertex;
49 NCollection_Vector<void *> tmesh_sequence;
50 GLenum triangle_type; /* FSXXX OPTI */
54 static void bgntriangulate( const TEL_POLYGON_DATA *, void (APIENTRY*)() );
55 static void endtriangulate(void);
57 #ifndef GLU_VERSION_1_2
58 #define GLUtesselator GLUtriangulatorObj
59 void gluTessBeginContour();
60 void gluTessBeginPolygon();
61 void gluTessEndPolygon();
62 void gluTessEndContour();
63 #define GLU_TESS_BEGIN 100100
64 #define GLU_TESS_VERTEX 100101
65 #define GLU_TESS_END 100102
66 #define GLU_TESS_ERROR 100103
67 #define GLU_TESS_COMBINE 100105
70 /*----------------------------------------------------------------------*/
72 void OpenGl_Polygon::draw_polygon (const Handle(OpenGl_Workspace) &AWorkspace, Tint front_lighting_model) const
79 tel_texture_coord pvt;
82 pvc = myData.vcolours;
83 pvn = myData.vnormals;
84 pvt = myData.vtexturecoord;
86 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
89 ptr = myData.vertices;
91 glColor3fv( pfc->rgb );
92 if ( front_lighting_model )
93 glNormal3fv( myData.fnormal.xyz );
95 if( myData.reverse_order ) glFrontFace( GL_CW );
97 if (myData.num_vertices == 3) glBegin(GL_TRIANGLES);
98 else if(myData.num_vertices == 4) glBegin(GL_QUADS);
99 else glBegin(GL_POLYGON);
100 if( front_lighting_model )
104 if (pvt && (AWorkspace->NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
105 for( i=0; i<myData.num_vertices; i++, ptr++ )
107 glNormal3fv( pvn[i].xyz );
108 glTexCoord2fv( pvt[i].xy );
109 glVertex3fv( ptr->xyz );
112 for( i=0; i<myData.num_vertices; i++, ptr++ )
114 glNormal3fv( pvn[i].xyz );
115 glVertex3fv( ptr->xyz );
120 for( i=0; i<myData.num_vertices; i++, ptr++ )
122 glVertex3fv( ptr->xyz );
130 for( i=0; i<myData.num_vertices; i++, ptr++ )
132 glColor3fv( pvc[i].rgb );
133 glVertex3fv( ptr->xyz );
138 for( i=0; i<myData.num_vertices; i++, ptr++ )
140 glVertex3fv( ptr->xyz );
145 if( myData.reverse_order ) glFrontFace( GL_CCW );
149 /*----------------------------------------------------------------------*/
151 /* JWR - allow varying the size */
153 static const TEL_POLYGON_DATA *DaTa;
154 static GLUtesselator *tripak = 0;
157 out_bgntmesh( GLenum triangle_type )
159 NCollection_Vector<SEQ_> *dis = DaTa->dsply;
162 #ifdef JWR_DEC_TRIFAN_BUG
163 aSeq.triangle_type = GL_POLYGON;
167 aSeq.triangle_type = triangle_type;
169 glBegin(triangle_type);
173 /*----------------------------------------------------------------------*/
176 out_vert1( void *data )
178 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
180 s.tmesh_sequence.Append(data);
182 if ( data < (void *)0xffff ) {
185 glVertex3fv( DaTa->vertices[a].xyz );
188 extra_vertex b = (extra_vertex) data;
190 glVertex3fv( b->vert );
195 /*----------------------------------------------------------------------*/
198 out_vert2( void *data )
200 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
202 s.tmesh_sequence.Append(data);
204 if ( data < (void *)0xffff ) {
207 glColor3fv( DaTa->vcolours[a].rgb );
208 glVertex3fv( DaTa->vertices[a].xyz );
211 extra_vertex b = (extra_vertex) data;
213 glColor3fv( DaTa->vcolours[(b->ind)].rgb );
214 glVertex3fv( b->vert );
218 /*----------------------------------------------------------------------*/
221 out_vert3( void *data )
223 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
225 s.tmesh_sequence.Append(data);
227 if ( data <= (void *)0xffff ) {
230 glNormal3fv( DaTa->vnormals[a].xyz );
231 glVertex3fv( DaTa->vertices[a].xyz);
234 extra_vertex b = (extra_vertex) data;
236 glNormal3fv( DaTa->vnormals[(b->ind)].xyz );
237 glVertex3fv( b->vert );
241 /*----------------------------------------------------------------------*/
244 mycombine( GLdouble coords[3], int *data, GLfloat w[4], void **dataout)
246 extra_vertex new_vertex = new EXTRA_VERTEX();
248 new_vertex->vert[0] = ( float )coords[0];
249 new_vertex->vert[1] = ( float )coords[1];
250 new_vertex->vert[2] = ( float )coords[2];
251 new_vertex->ind = *data;
252 *dataout = new_vertex;
255 /*----------------------------------------------------------------------*/
263 /*----------------------------------------------------------------------*/
266 out_error( GLenum error )
268 printf( "POLYGON : %s\n", (char *) gluErrorString(error) );
271 /*----------------------------------------------------------------------*/
274 bgntriangulate(const TEL_POLYGON_DATA *d, void ( APIENTRY * out_ver)() )
278 tripak = gluNewTess();
280 #if defined(linux) && !defined(NOGLUfuncptr)
281 gluTessCallback( tripak, GLU_TESS_BEGIN, (_GLUfuncptr)(out_bgntmesh) );
282 gluTessCallback( tripak, GLU_TESS_VERTEX, out_ver );
283 gluTessCallback( tripak, GLU_TESS_END, out_endtmesh );
284 gluTessCallback( tripak, GLU_TESS_ERROR, (_GLUfuncptr)(out_error) );
285 gluTessCallback( tripak, GLU_TESS_COMBINE, (_GLUfuncptr)(mycombine) );
287 gluTessCallback( tripak, GLU_TESS_BEGIN, (void (APIENTRY*)())out_bgntmesh );
288 gluTessCallback( tripak, GLU_TESS_VERTEX, (void (APIENTRY*)())out_ver );
289 gluTessCallback( tripak, GLU_TESS_END, (void (APIENTRY*)())out_endtmesh );
290 gluTessCallback( tripak, GLU_TESS_ERROR, (void (APIENTRY*)())out_error );
291 gluTessCallback( tripak, GLU_TESS_COMBINE, (void (APIENTRY*)())mycombine );
295 /*----------------------------------------------------------------------*/
301 gluDeleteTess(tripak);
304 /*----------------------------------------------------------------------*/
306 void OpenGl_Polygon::draw_polygon_concav (const Handle(OpenGl_Workspace) &AWorkspace, Tint front_lighting_model) const
315 pfc = myData.fcolour;
316 pvc = myData.vcolours;
317 pvn = myData.vnormals;
319 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
322 ptr = myData.vertices;
326 glColor3fv( pfc->rgb );
327 if ( front_lighting_model )
328 glNormal3fv( myData.fnormal.xyz );
330 if( myData.reverse_order ) glFrontFace( GL_CW );
334 if( front_lighting_model )
338 bgntriangulate(&myData, (void (APIENTRY*)())out_vert3);
342 bgntriangulate(&myData, (void (APIENTRY*)())out_vert1);
349 bgntriangulate(&myData, (void (APIENTRY*)())out_vert2);
353 bgntriangulate(&myData, (void (APIENTRY*)())out_vert1);
356 gluTessBeginPolygon( tripak, NULL );
357 gluTessBeginContour( tripak);
359 for( i=0; i<myData.num_vertices; i++, ptr++ )
361 xyz[0] = ptr->xyz[0];
362 xyz[1] = ptr->xyz[1];
363 xyz[2] = ptr->xyz[2];
365 gluTessVertex( tripak, xyz,(void * ) i );
368 double v[ 3 ] = {ptr -> xyz[ 0 ], ptr -> xyz[ 1 ], ptr -> xyz[ 2 ]};
369 gluTessVertex ( tripak, v, ( void* )i );
373 gluTessEndContour( tripak );
374 gluTessEndPolygon( tripak );
379 if( front_lighting_model )
381 draw_tmesh( pvn? 3 : 1 );
385 draw_tmesh( pvc? 2 : 1 );
389 if( myData.reverse_order ) glFrontFace( GL_CCW );
392 /*----------------------------------------------------------------------*/
394 void OpenGl_Polygon::draw_edges (const TEL_COLOUR *edge_colour, const Aspect_InteriorStyle interior_style, const Handle(OpenGl_Workspace) &AWorkspace) const
396 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace( Standard_True );
398 if ( interior_style != Aspect_IS_HIDDENLINE && aspect_face->Context().Edge == TOff )
401 glDisable(GL_LIGHTING);
402 const GLboolean texture_on = IsTextureEnabled();
403 if (texture_on) DisableTexture();
406 const OpenGl_AspectLine *aspect_line_old = AWorkspace->SetAspectLine( aspect_face->AspectEdge() );
407 AWorkspace->AspectLine( Standard_True );
410 tel_point ptr = myData.vertices;
412 glColor3fv( edge_colour->rgb );
414 glBegin(GL_LINE_LOOP);
415 for( i=0; i<myData.num_vertices; i++, ptr++ )
417 glVertex3fv( ptr->xyz );
421 // Restore line context
422 AWorkspace->SetAspectLine( aspect_line_old );
424 if (texture_on) EnableTexture();
427 /*----------------------------------------------------------------------*/
429 void OpenGl_Polygon::draw_tmesh ( Tint v ) const
435 NCollection_Vector<SEQ_> *dis = myData.dsply;
436 for( i = 0; i < dis->Length(); i++ )
438 s = &(dis->ChangeValue(i));
440 glBegin(s->triangle_type);
445 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
447 if ( s->tmesh_sequence(j) < (void *)0xffff )
448 glVertex3fv( myData.vertices[ (long)s->tmesh_sequence.Value(j) ].xyz );
450 b = (extra_vertex) s->tmesh_sequence(j);
451 glVertex3fv( b->vert );
459 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
461 if ( s->tmesh_sequence(j) < (void *)0xffff ) {
462 glColor3fv( myData.vcolours[ (long) s->tmesh_sequence(j) ].rgb );
463 glVertex3fv( myData.vertices[ (long) s->tmesh_sequence(j) ].xyz );
465 b = (extra_vertex) s->tmesh_sequence(j);
466 glColor3fv( myData.vcolours[(b->ind)].rgb);
467 glVertex3fv( b->vert );
474 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
476 if ( s->tmesh_sequence(j) < (void *)0xffff ) {
477 glNormal3fv( myData.vnormals[ (long) s->tmesh_sequence(j) ].xyz);
478 glVertex3fv( myData.vertices[ (long) s->tmesh_sequence(j) ].xyz);
480 b = (extra_vertex) s->tmesh_sequence(j);
481 glNormal3fv( myData.vnormals[(b->ind)].xyz);
482 glVertex3fv( b->vert );
492 /*----------------------------------------------------------------------*/
494 OpenGl_Polygon::OpenGl_Polygon (const Graphic3d_Array1OfVertex& AListVertex,
495 const Graphic3d_TypeOfPolygon AType)
497 const Standard_Integer nv = AListVertex.Length();
499 myData.num_vertices = nv;
501 myData.vertices = new TEL_POINT[nv];
502 memcpy( myData.vertices, &AListVertex(AListVertex.Lower()), nv*sizeof(TEL_POINT) );
504 myData.vertex_flag = TEL_VT_NONE;
505 myData.vnormals = NULL;
507 myData.vcolours = NULL;
509 myData.vtexturecoord = NULL;
511 myData.reverse_order = 0;
513 myData.facet_flag = TEL_FA_NONE;
514 TelGetPolygonNormal( myData.vertices, NULL, nv, myData.fnormal.xyz );
516 myData.fcolour = NULL;
518 #if defined(__sgi) || defined(IRIX)
519 // Pb with tesselator on sgi
520 myData.shape_flag = TEL_SHAPE_CONVEX;
524 case Graphic3d_TOP_UNKNOWN :
525 myData.shape_flag = TEL_SHAPE_UNKNOWN;
527 case Graphic3d_TOP_COMPLEX :
528 myData.shape_flag = TEL_SHAPE_COMPLEX;
530 case Graphic3d_TOP_CONCAVE :
531 myData.shape_flag = TEL_SHAPE_CONCAVE;
533 //case Graphic3d_TOP_CONVEX :
535 myData.shape_flag = TEL_SHAPE_CONVEX;
540 myData.dsply = new NCollection_Vector<SEQ_>();
543 /*----------------------------------------------------------------------*/
545 OpenGl_Polygon::~OpenGl_Polygon ()
548 delete myData.fcolour;
549 if( myData.vertices )
550 delete[] myData.vertices;
551 if( myData.vcolours )
552 delete[] myData.vcolours;
553 if( myData.vnormals )
554 delete[] myData.vnormals;
555 if ( myData.vtexturecoord )
556 delete myData.vtexturecoord;
562 for( i = 0; i < myData.dsply->Length(); i++ )
564 for ( j = 0; j < myData.dsply->Value(i).tmesh_sequence.Length() ; j++ )
566 if ( myData.dsply->Value(i).tmesh_sequence(j) >= (void *)0xffff )
567 delete myData.dsply->Value(i).tmesh_sequence(j);
575 /*----------------------------------------------------------------------*/
577 void OpenGl_Polygon::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
579 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace( Standard_True );
581 Tint front_lighting_model = aspect_face->Context().IntFront.color_mask;
582 const Aspect_InteriorStyle interior_style = aspect_face->Context().InteriorStyle;
583 const TEL_COLOUR *interior_colour = &aspect_face->Context().IntFront.matcol;
584 const TEL_COLOUR *edge_colour = &aspect_face->AspectEdge()->Color();
586 // Use highlight colous
587 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
589 edge_colour = interior_colour = AWorkspace->HighlightColor;
590 front_lighting_model = 0;
593 if( interior_style != Aspect_IS_EMPTY && AWorkspace->DegenerateModel < 2 )
595 if ( front_lighting_model )
596 glEnable(GL_LIGHTING);
598 glDisable(GL_LIGHTING);
600 glColor3fv( interior_colour->rgb );
602 if( myData.shape_flag != TEL_SHAPE_CONVEX )
603 draw_polygon_concav( AWorkspace, front_lighting_model );
605 draw_polygon( AWorkspace, front_lighting_model );
608 /* OCC11904 -- Temporarily disable environment mapping */
609 glPushAttrib(GL_ENABLE_BIT);
610 glDisable(GL_TEXTURE_1D);
611 glDisable(GL_TEXTURE_2D);
613 switch ( AWorkspace->DegenerateModel )
616 draw_edges ( edge_colour, interior_style, AWorkspace );
618 case 3: /* marker degeneration */
622 glPopAttrib(); /* skt: GL_ENABLE_BIT*/
625 /*----------------------------------------------------------------------*/