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.
20 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_Polygon.hxx>
24 #include <OpenGl_telem_util.hxx>
26 #include <OpenGl_AspectFace.hxx>
27 #include <OpenGl_Structure.hxx>
28 #include <OpenGl_Workspace.hxx>
32 #if (defined(_WIN32) || defined(__WIN32__))
44 typedef EXTRA_VERTEX* extra_vertex;
48 NCollection_Vector<void *> tmesh_sequence;
49 GLenum triangle_type; /* FSXXX OPTI */
53 static void bgntriangulate( const TEL_POLYGON_DATA *, void (APIENTRY*)() );
54 static void endtriangulate(void);
56 #ifndef GLU_VERSION_1_2
57 #define GLUtesselator GLUtriangulatorObj
58 void gluTessBeginContour();
59 void gluTessBeginPolygon();
60 void gluTessEndPolygon();
61 void gluTessEndContour();
62 #define GLU_TESS_BEGIN 100100
63 #define GLU_TESS_VERTEX 100101
64 #define GLU_TESS_END 100102
65 #define GLU_TESS_ERROR 100103
66 #define GLU_TESS_COMBINE 100105
69 /*----------------------------------------------------------------------*/
71 void OpenGl_Polygon::draw_polygon (const Handle(OpenGl_Workspace) &AWorkspace, Tint front_lighting_model) const
78 tel_texture_coord pvt;
81 pvc = myData.vcolours;
82 pvn = myData.vnormals;
83 pvt = myData.vtexturecoord;
85 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
88 ptr = myData.vertices;
90 glColor3fv( pfc->rgb );
91 if ( front_lighting_model )
92 glNormal3fv( myData.fnormal.xyz );
94 if( myData.reverse_order ) glFrontFace( GL_CW );
96 if (myData.num_vertices == 3) glBegin(GL_TRIANGLES);
97 else if(myData.num_vertices == 4) glBegin(GL_QUADS);
98 else glBegin(GL_POLYGON);
99 if( front_lighting_model )
103 if (pvt && (AWorkspace->NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
104 for( i=0; i<myData.num_vertices; i++, ptr++ )
106 glNormal3fv( pvn[i].xyz );
107 glTexCoord2fv( pvt[i].xy );
108 glVertex3fv( ptr->xyz );
111 for( i=0; i<myData.num_vertices; i++, ptr++ )
113 glNormal3fv( pvn[i].xyz );
114 glVertex3fv( ptr->xyz );
119 for( i=0; i<myData.num_vertices; i++, ptr++ )
121 glVertex3fv( ptr->xyz );
129 for( i=0; i<myData.num_vertices; i++, ptr++ )
131 glColor3fv( pvc[i].rgb );
132 glVertex3fv( ptr->xyz );
137 for( i=0; i<myData.num_vertices; i++, ptr++ )
139 glVertex3fv( ptr->xyz );
144 if( myData.reverse_order ) glFrontFace( GL_CCW );
148 /*----------------------------------------------------------------------*/
150 /* JWR - allow varying the size */
152 static const TEL_POLYGON_DATA *DaTa;
153 static GLUtesselator *tripak = 0;
156 out_bgntmesh( GLenum triangle_type )
158 NCollection_Vector<SEQ_> *dis = DaTa->dsply;
161 #ifdef JWR_DEC_TRIFAN_BUG
162 aSeq.triangle_type = GL_POLYGON;
166 aSeq.triangle_type = triangle_type;
168 glBegin(triangle_type);
172 /*----------------------------------------------------------------------*/
175 out_vert1( void *data )
177 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
179 s.tmesh_sequence.Append(data);
181 if ( data < (void *)0xffff ) {
184 glVertex3fv( DaTa->vertices[a].xyz );
187 extra_vertex b = (extra_vertex) data;
189 glVertex3fv( b->vert );
194 /*----------------------------------------------------------------------*/
197 out_vert2( void *data )
199 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
201 s.tmesh_sequence.Append(data);
203 if ( data < (void *)0xffff ) {
206 glColor3fv( DaTa->vcolours[a].rgb );
207 glVertex3fv( DaTa->vertices[a].xyz );
210 extra_vertex b = (extra_vertex) data;
212 glColor3fv( DaTa->vcolours[(b->ind)].rgb );
213 glVertex3fv( b->vert );
217 /*----------------------------------------------------------------------*/
220 out_vert3( void *data )
222 SEQ_ &s = DaTa->dsply->ChangeValue(DaTa->dsply->Length() - 1);
224 s.tmesh_sequence.Append(data);
226 if ( data <= (void *)0xffff ) {
229 glNormal3fv( DaTa->vnormals[a].xyz );
230 glVertex3fv( DaTa->vertices[a].xyz);
233 extra_vertex b = (extra_vertex) data;
235 glNormal3fv( DaTa->vnormals[(b->ind)].xyz );
236 glVertex3fv( b->vert );
240 /*----------------------------------------------------------------------*/
243 mycombine( GLdouble coords[3], int *data, GLfloat w[4], void **dataout)
245 extra_vertex new_vertex = new EXTRA_VERTEX();
247 new_vertex->vert[0] = ( float )coords[0];
248 new_vertex->vert[1] = ( float )coords[1];
249 new_vertex->vert[2] = ( float )coords[2];
250 new_vertex->ind = *data;
251 *dataout = new_vertex;
254 /*----------------------------------------------------------------------*/
262 /*----------------------------------------------------------------------*/
265 out_error( GLenum error )
267 printf( "POLYGON : %s\n", (char *) gluErrorString(error) );
270 /*----------------------------------------------------------------------*/
273 bgntriangulate(const TEL_POLYGON_DATA *d, void ( APIENTRY * out_ver)() )
277 tripak = gluNewTess();
279 #if defined(linux) && !defined(NOGLUfuncptr)
280 gluTessCallback( tripak, GLU_TESS_BEGIN, (_GLUfuncptr)(out_bgntmesh) );
281 gluTessCallback( tripak, GLU_TESS_VERTEX, out_ver );
282 gluTessCallback( tripak, GLU_TESS_END, out_endtmesh );
283 gluTessCallback( tripak, GLU_TESS_ERROR, (_GLUfuncptr)(out_error) );
284 gluTessCallback( tripak, GLU_TESS_COMBINE, (_GLUfuncptr)(mycombine) );
286 gluTessCallback( tripak, GLU_TESS_BEGIN, (void (APIENTRY*)())out_bgntmesh );
287 gluTessCallback( tripak, GLU_TESS_VERTEX, (void (APIENTRY*)())out_ver );
288 gluTessCallback( tripak, GLU_TESS_END, (void (APIENTRY*)())out_endtmesh );
289 gluTessCallback( tripak, GLU_TESS_ERROR, (void (APIENTRY*)())out_error );
290 gluTessCallback( tripak, GLU_TESS_COMBINE, (void (APIENTRY*)())mycombine );
294 /*----------------------------------------------------------------------*/
300 gluDeleteTess(tripak);
303 /*----------------------------------------------------------------------*/
305 void OpenGl_Polygon::draw_polygon_concav (const Handle(OpenGl_Workspace) &AWorkspace, Tint front_lighting_model) const
314 pfc = myData.fcolour;
315 pvc = myData.vcolours;
316 pvn = myData.vnormals;
318 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
321 ptr = myData.vertices;
325 glColor3fv( pfc->rgb );
326 if ( front_lighting_model )
327 glNormal3fv( myData.fnormal.xyz );
329 if( myData.reverse_order ) glFrontFace( GL_CW );
333 if( front_lighting_model )
337 bgntriangulate(&myData, (void (APIENTRY*)())out_vert3);
341 bgntriangulate(&myData, (void (APIENTRY*)())out_vert1);
348 bgntriangulate(&myData, (void (APIENTRY*)())out_vert2);
352 bgntriangulate(&myData, (void (APIENTRY*)())out_vert1);
355 gluTessBeginPolygon( tripak, NULL );
356 gluTessBeginContour( tripak);
358 for( i=0; i<myData.num_vertices; i++, ptr++ )
360 xyz[0] = ptr->xyz[0];
361 xyz[1] = ptr->xyz[1];
362 xyz[2] = ptr->xyz[2];
364 gluTessVertex( tripak, xyz,(void * ) i );
367 double v[ 3 ] = {ptr -> xyz[ 0 ], ptr -> xyz[ 1 ], ptr -> xyz[ 2 ]};
368 gluTessVertex ( tripak, v, ( void* )i );
372 gluTessEndContour( tripak );
373 gluTessEndPolygon( tripak );
378 if( front_lighting_model )
380 draw_tmesh( pvn? 3 : 1 );
384 draw_tmesh( pvc? 2 : 1 );
388 if( myData.reverse_order ) glFrontFace( GL_CCW );
391 /*----------------------------------------------------------------------*/
393 void OpenGl_Polygon::draw_edges (const TEL_COLOUR* theEdgeColor,
394 const Aspect_InteriorStyle theInteriorStyle,
395 const Handle(OpenGl_Workspace)& theWorkspace) const
397 const OpenGl_AspectFace* anAspectFace = theWorkspace->AspectFace (Standard_True);
399 if (theInteriorStyle != Aspect_IS_HIDDENLINE
400 && anAspectFace->Edge == TOff)
405 glDisable (GL_LIGHTING);
406 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
409 const OpenGl_AspectLine* aPrevAspectLine = theWorkspace->SetAspectLine (anAspectFace->AspectEdge());
410 theWorkspace->AspectLine (Standard_True);
412 glColor3fv (theEdgeColor->rgb);
414 glBegin (GL_LINE_LOOP);
415 tel_point ptr = myData.vertices;
416 for (Tint i = 0; i < myData.num_vertices; i++, ptr++)
418 glVertex3fv (ptr->xyz);
422 // Restore line context
423 theWorkspace->SetAspectLine (aPrevAspectLine);
425 theWorkspace->EnableTexture (aPrevTexture);
428 /*----------------------------------------------------------------------*/
430 void OpenGl_Polygon::draw_tmesh ( Tint v ) const
436 NCollection_Vector<SEQ_> *dis = myData.dsply;
437 for( i = 0; i < dis->Length(); i++ )
439 s = &(dis->ChangeValue(i));
441 glBegin(s->triangle_type);
446 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
448 if ( s->tmesh_sequence(j) < (void *)0xffff )
449 glVertex3fv( myData.vertices[ (long)s->tmesh_sequence.Value(j) ].xyz );
451 b = (extra_vertex) s->tmesh_sequence(j);
452 glVertex3fv( b->vert );
460 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
462 if ( s->tmesh_sequence(j) < (void *)0xffff ) {
463 glColor3fv( myData.vcolours[ (long) s->tmesh_sequence(j) ].rgb );
464 glVertex3fv( myData.vertices[ (long) s->tmesh_sequence(j) ].xyz );
466 b = (extra_vertex) s->tmesh_sequence(j);
467 glColor3fv( myData.vcolours[(b->ind)].rgb);
468 glVertex3fv( b->vert );
475 for( j = 0, k = 0; j < s->tmesh_sequence.Length(); j++ )
477 if ( s->tmesh_sequence(j) < (void *)0xffff ) {
478 glNormal3fv( myData.vnormals[ (long) s->tmesh_sequence(j) ].xyz);
479 glVertex3fv( myData.vertices[ (long) s->tmesh_sequence(j) ].xyz);
481 b = (extra_vertex) s->tmesh_sequence(j);
482 glNormal3fv( myData.vnormals[(b->ind)].xyz);
483 glVertex3fv( b->vert );
493 /*----------------------------------------------------------------------*/
495 OpenGl_Polygon::OpenGl_Polygon (const Graphic3d_Array1OfVertex& AListVertex,
496 const Graphic3d_TypeOfPolygon AType)
498 const Standard_Integer nv = AListVertex.Length();
500 myData.num_vertices = nv;
502 myData.vertices = new TEL_POINT[nv];
503 memcpy( myData.vertices, &AListVertex(AListVertex.Lower()), nv*sizeof(TEL_POINT) );
505 myData.vertex_flag = TEL_VT_NONE;
506 myData.vnormals = NULL;
508 myData.vcolours = NULL;
510 myData.vtexturecoord = NULL;
512 myData.reverse_order = 0;
514 myData.facet_flag = TEL_FA_NONE;
515 TelGetPolygonNormal( myData.vertices, NULL, nv, myData.fnormal.xyz );
517 myData.fcolour = NULL;
519 #if defined(__sgi) || defined(IRIX)
520 // Pb with tesselator on sgi
521 myData.shape_flag = TEL_SHAPE_CONVEX;
525 case Graphic3d_TOP_UNKNOWN :
526 myData.shape_flag = TEL_SHAPE_UNKNOWN;
528 case Graphic3d_TOP_COMPLEX :
529 myData.shape_flag = TEL_SHAPE_COMPLEX;
531 case Graphic3d_TOP_CONCAVE :
532 myData.shape_flag = TEL_SHAPE_CONCAVE;
534 //case Graphic3d_TOP_CONVEX :
536 myData.shape_flag = TEL_SHAPE_CONVEX;
541 myData.dsply = new NCollection_Vector<SEQ_>();
544 /*----------------------------------------------------------------------*/
546 OpenGl_Polygon::~OpenGl_Polygon ()
549 delete myData.fcolour;
550 if( myData.vertices )
551 delete[] myData.vertices;
552 if( myData.vcolours )
553 delete[] myData.vcolours;
554 if( myData.vnormals )
555 delete[] myData.vnormals;
556 if ( myData.vtexturecoord )
557 delete myData.vtexturecoord;
563 for( i = 0; i < myData.dsply->Length(); i++ )
565 for ( j = 0; j < myData.dsply->Value(i).tmesh_sequence.Length() ; j++ )
567 if ( myData.dsply->Value(i).tmesh_sequence(j) >= (void *)0xffff )
568 delete myData.dsply->Value(i).tmesh_sequence(j);
576 /*----------------------------------------------------------------------*/
578 void OpenGl_Polygon::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
580 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace( Standard_True );
582 Tint front_lighting_model = aspect_face->IntFront.color_mask;
583 const Aspect_InteriorStyle interior_style = aspect_face->InteriorStyle;
584 const TEL_COLOUR *interior_colour = &aspect_face->IntFront.matcol;
585 const TEL_COLOUR *edge_colour = &aspect_face->AspectEdge()->Color();
587 // Use highlight colous
588 if ( AWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT )
590 edge_colour = interior_colour = AWorkspace->HighlightColor;
591 front_lighting_model = 0;
594 if( interior_style != Aspect_IS_EMPTY && AWorkspace->DegenerateModel < 2 )
596 if ( front_lighting_model )
597 glEnable(GL_LIGHTING);
599 glDisable(GL_LIGHTING);
601 glColor3fv( interior_colour->rgb );
603 if( myData.shape_flag != TEL_SHAPE_CONVEX )
604 draw_polygon_concav( AWorkspace, front_lighting_model );
606 draw_polygon( AWorkspace, front_lighting_model );
609 /* OCC11904 -- Temporarily disable environment mapping */
610 glPushAttrib(GL_ENABLE_BIT);
611 glDisable(GL_TEXTURE_1D);
612 glDisable(GL_TEXTURE_2D);
614 switch ( AWorkspace->DegenerateModel )
617 draw_edges ( edge_colour, interior_style, AWorkspace );
619 case 3: /* marker degeneration */
623 glPopAttrib(); /* skt: GL_ENABLE_BIT*/
626 void OpenGl_Polygon::Release (const Handle(OpenGl_Context)& theContext)