1 // Created on: 2011-08-05
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_Workspace.hxx>
25 #include <OpenGl_AspectLine.hxx>
26 #include <OpenGl_AspectFace.hxx>
27 #include <OpenGl_AspectMarker.hxx>
28 #include <OpenGl_AspectText.hxx>
34 /* OCC22218 NOTE: project dependency on gl2ps is specified by macro */
37 /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
38 Pragma comment for gl2ps.lib is defined only here. */
40 #pragma comment( lib, "gl2ps.lib" )
44 #include <Aspect_PolygonOffsetMode.hxx>
45 #include <OpenGl_View.hxx>
47 /*----------------------------------------------------------------------*/
49 static void TelUpdatePolygonOffsets( const TEL_POFFSET_PARAM *pdata )
51 if ( ( pdata->mode & Aspect_POM_Fill ) == Aspect_POM_Fill )
52 glEnable ( GL_POLYGON_OFFSET_FILL );
54 glDisable ( GL_POLYGON_OFFSET_FILL );
56 if ( ( pdata->mode & Aspect_POM_Line ) == Aspect_POM_Line )
57 glEnable ( GL_POLYGON_OFFSET_LINE );
59 glDisable( GL_POLYGON_OFFSET_LINE );
61 if ( ( pdata->mode & Aspect_POM_Point ) == Aspect_POM_Point )
62 glEnable ( GL_POLYGON_OFFSET_POINT );
64 glDisable( GL_POLYGON_OFFSET_POINT );
66 glPolygonOffset( pdata->factor, pdata->units );
69 /*----------------------------------------------------------------------*/
71 void OpenGl_Workspace::UpdateMaterial( const int flag )
73 // Case of hidden line
74 if (AspectFace_set->InteriorStyle() == Aspect_IS_HIDDENLINE)
76 myAspectFaceHl = *AspectFace_set; // copy all values including line edge aspect
77 myAspectFaceHl.ChangeIntFront().matcol = BackgroundColor();
78 myAspectFaceHl.ChangeIntFront().color_mask = 0;
79 myAspectFaceHl.ChangeIntFront().color_mask = 0;
81 AspectFace_set = &myAspectFaceHl;
85 const OPENGL_SURF_PROP *prop = NULL;
87 if ( flag == TEL_FRONT_MATERIAL )
89 prop = &AspectFace_set->IntFront();
90 face = GL_FRONT_AND_BACK;
94 prop = &AspectFace_set->IntBack();
98 // Handling transparency
99 if ( (NamedStatus & OPENGL_NS_2NDPASSDO) == 0 )
101 if ( myUseTransparency && prop->trans != 1.0F )
103 // Render transparent
104 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106 glDepthMask (GL_FALSE);
111 if ( (NamedStatus & OPENGL_NS_ANTIALIASING) == 0 )
113 glBlendFunc (GL_ONE, GL_ZERO);
114 glDisable (GL_BLEND);
116 glDepthMask (GL_TRUE);
120 // Obtaining reflection mode flags to update GL material properties
121 const unsigned int aReflectionMode = prop->color_mask;
123 // Do not update material properties in case of zero reflection mode,
124 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray()
126 if ( !aReflectionMode ) return;
128 static float mAmb[4];
129 static float mDiff[4];
130 static float mSpec[4];
131 static float mEmsv[4];
134 static const float defspeccol[4] = { 1.F, 1.F, 1.F, 1.F };
137 if ( NamedStatus & OPENGL_NS_RESMAT )
140 if( aReflectionMode & OPENGL_AMBIENT_MASK )
142 const float *c = prop->isphysic? prop->ambcol.rgb : prop->matcol.rgb;
144 mAmb[0] = prop->amb * c[0];
145 mAmb[1] = prop->amb * c[1];
146 mAmb[2] = prop->amb * c[2];
156 // Diffusion component
157 if( aReflectionMode & OPENGL_DIFFUSE_MASK )
159 const float *c = prop->isphysic? prop->difcol.rgb : prop->matcol.rgb;
161 mDiff[0] = prop->diff * c[0];
162 mDiff[1] = prop->diff * c[1];
163 mDiff[2] = prop->diff * c[2];
173 if (NamedStatus & OPENGL_NS_2NDPASSDO)
175 mDiff[3] = prop->env_reflexion;
179 if (myUseTransparency) mDiff[3] = prop->trans;
180 // If the material reflects the environment scene, the second pass is needed
181 if (prop->env_reflexion != 0.0) NamedStatus |= OPENGL_NS_2NDPASSNEED;
184 // Specular component
185 if( aReflectionMode & OPENGL_SPECULAR_MASK )
187 const float *c = prop->isphysic? prop->speccol.rgb : defspeccol;
189 mSpec[0] = prop->spec * c[0];
190 mSpec[1] = prop->spec * c[1];
191 mSpec[2] = prop->spec * c[2];
200 // Emissive component
201 if( aReflectionMode & OPENGL_EMISSIVE_MASK )
203 const float *c = prop->isphysic? prop->emscol.rgb : prop->matcol.rgb;
205 mEmsv[0] = prop->emsv * c[0];
206 mEmsv[1] = prop->emsv * c[1];
207 mEmsv[2] = prop->emsv * c[2];
216 /* Coeficient de brillance */
219 glMaterialfv(face, GL_AMBIENT, mAmb );
220 glMaterialfv(face, GL_DIFFUSE, mDiff );
221 glMaterialfv(face, GL_SPECULAR, mSpec);
222 glMaterialfv(face, GL_EMISSION, mEmsv);
223 glMaterialf(face, GL_SHININESS, mShin);
225 NamedStatus &= ~OPENGL_NS_RESMAT;
228 // Set Material Optimize
232 if( aReflectionMode & OPENGL_AMBIENT_MASK )
234 const float *c = prop->isphysic? prop->ambcol.rgb : prop->matcol.rgb;
236 if (mAmb[0] != prop->amb * c[0] ||
237 mAmb[1] != prop->amb * c[1] ||
238 mAmb[2] != prop->amb * c[2] )
240 mAmb[0] = prop->amb * c[0];
241 mAmb[1] = prop->amb * c[1];
242 mAmb[2] = prop->amb * c[2];
245 glMaterialfv(face, GL_AMBIENT, mAmb);
250 if ( mAmb[0] != 0.F || mAmb[1] != 0.F || mAmb[2] != 0.F )
257 glMaterialfv(face, GL_AMBIENT, mAmb);
261 // Diffusion component
262 if( aReflectionMode & OPENGL_DIFFUSE_MASK )
264 const float *c = prop->isphysic? prop->difcol.rgb : prop->matcol.rgb;
266 if (mDiff[0] != prop->diff * c[0] ||
267 mDiff[1] != prop->diff * c[1] ||
268 mDiff[2] != prop->diff * c[2] ||
269 mDiff[3] != ((NamedStatus & OPENGL_NS_2NDPASSDO)? prop->env_reflexion : (myUseTransparency? prop->trans : 1.0F)))
271 mDiff[0] = prop->diff * c[0];
272 mDiff[1] = prop->diff * c[1];
273 mDiff[2] = prop->diff * c[2];
276 if (NamedStatus & OPENGL_NS_2NDPASSDO)
278 mDiff[3] = prop->env_reflexion;
282 if (myUseTransparency) mDiff[3] = prop->trans;
283 // If the material reflects the environment scene, the second pass is needed
284 if (prop->env_reflexion != 0.0) NamedStatus |= OPENGL_NS_2NDPASSNEED;
287 glMaterialfv(face, GL_DIFFUSE, mDiff );
292 Tfloat newDiff3 = 1.F;
294 if (NamedStatus & OPENGL_NS_2NDPASSDO)
296 newDiff3 = prop->env_reflexion;
300 if (myUseTransparency) newDiff3 = prop->trans;
301 // If the material reflects the environment scene, the second pass is needed
302 if (prop->env_reflexion != 0.0) NamedStatus |= OPENGL_NS_2NDPASSNEED;
305 /* OCC19915: Even if diffuse reflectance is disabled,
306 still trying to update the current transparency if it
307 differs from the previous value */
308 if ( mDiff[0] != 0.F || mDiff[1] != 0.F || mDiff[2] != 0.F || fabs(mDiff[3] - newDiff3) > 0.01F )
315 glMaterialfv(face, GL_DIFFUSE, mDiff);
319 // Specular component
320 if( aReflectionMode & OPENGL_SPECULAR_MASK )
322 const float *c = prop->isphysic? prop->speccol.rgb : defspeccol;
324 if (mSpec[0] != prop->spec * c[0] ||
325 mSpec[1] != prop->spec * c[1] ||
326 mSpec[2] != prop->spec * c[2])
328 mSpec[0] = prop->spec * c[0];
329 mSpec[1] = prop->spec * c[1];
330 mSpec[2] = prop->spec * c[2];
333 glMaterialfv(face, GL_SPECULAR, mSpec);
338 if ( mSpec[0] != 0.F || mSpec[1] != 0.F || mSpec[2] != 0.F )
345 glMaterialfv(face, GL_SPECULAR, mSpec);
349 // Emissive component
350 if( aReflectionMode & OPENGL_EMISSIVE_MASK )
352 const float *c = prop->isphysic? prop->emscol.rgb : prop->matcol.rgb;
354 if (mEmsv[0] != prop->emsv * c[0] ||
355 mEmsv[1] != prop->emsv * c[1] ||
356 mEmsv[2] != prop->emsv * c[2])
358 mEmsv[0] = prop->emsv * c[0];
359 mEmsv[1] = prop->emsv * c[1];
360 mEmsv[2] = prop->emsv * c[2];
363 glMaterialfv(face, GL_EMISSION, mEmsv);
368 if ( mEmsv[0] != 0.F || mEmsv[1] != 0.F || mEmsv[2] != 0.F )
375 glMaterialfv(face, GL_EMISSION, mEmsv);
379 // Shining coefficient
380 if( mShin != prop->shine )
383 glMaterialf(face, GL_SHININESS, mShin);
388 /*----------------------------------------------------------------------*/
390 const OpenGl_AspectLine * OpenGl_Workspace::SetAspectLine(const OpenGl_AspectLine *AnAspect)
392 const OpenGl_AspectLine *AspectLine_old = AspectLine_set;
393 AspectLine_set = AnAspect;
394 return AspectLine_old;
397 /*----------------------------------------------------------------------*/
399 const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace(const OpenGl_AspectFace *AnAspect)
401 const OpenGl_AspectFace *AspectFace_old = AspectFace_set;
402 AspectFace_set = AnAspect;
403 return AspectFace_old;
406 /*----------------------------------------------------------------------*/
408 const OpenGl_AspectMarker * OpenGl_Workspace::SetAspectMarker(const OpenGl_AspectMarker *AnAspect)
410 const OpenGl_AspectMarker *AspectMarker_old = AspectMarker_set;
411 AspectMarker_set = AnAspect;
412 return AspectMarker_old;
415 /*----------------------------------------------------------------------*/
417 const OpenGl_AspectText * OpenGl_Workspace::SetAspectText(const OpenGl_AspectText *AnAspect)
419 const OpenGl_AspectText *AspectText_old = AspectText_set;
420 AspectText_set = AnAspect;
421 return AspectText_old;
424 /*----------------------------------------------------------------------*/
426 const OpenGl_Matrix * OpenGl_Workspace::SetViewMatrix(const OpenGl_Matrix *AMatrix)
428 const OpenGl_Matrix *ViewMatrix_old = ViewMatrix_applied;
429 ViewMatrix_applied = AMatrix;
432 OpenGl_Transposemat3( &lmat, StructureMatrix_applied );
434 glMatrixMode (GL_MODELVIEW);
436 OpenGl_Multiplymat3 (&rmat, &lmat, ViewMatrix_applied);
437 glLoadMatrixf ((const GLfloat* )rmat.mat);
439 return ViewMatrix_old;
442 /*----------------------------------------------------------------------*/
444 const OpenGl_Matrix * OpenGl_Workspace::SetStructureMatrix(const OpenGl_Matrix *AMatrix)
446 const OpenGl_Matrix *StructureMatrix_old = StructureMatrix_applied;
447 StructureMatrix_applied = AMatrix;
450 OpenGl_Transposemat3( &lmat, AMatrix );
452 glMatrixMode (GL_MODELVIEW);
454 OpenGl_Multiplymat3 (&rmat, &lmat, ViewMatrix_applied);
455 glLoadMatrixf ((const GLfloat* )rmat.mat);
457 return StructureMatrix_old;
460 /*----------------------------------------------------------------------*/
462 const OpenGl_AspectLine * OpenGl_Workspace::AspectLine(const Standard_Boolean WithApply)
464 if ( WithApply && (AspectLine_set != AspectLine_applied) )
466 glColor3fv(AspectLine_set->Color().rgb);
468 if ( !AspectLine_applied || (AspectLine_set->Type() != AspectLine_applied->Type() ) )
470 myDisplay->SetTypeOfLine(AspectLine_set->Type());
473 if ( !AspectLine_applied || ( AspectLine_set->Width() != AspectLine_applied->Width() ) )
475 glLineWidth( (GLfloat)AspectLine_set->Width() );
477 gl2psLineWidth( (GLfloat)AspectLine_set->Width() );
481 AspectLine_applied = AspectLine_set;
483 return AspectLine_set;
486 /*----------------------------------------------------------------------*/
488 const OpenGl_AspectFace* OpenGl_Workspace::AspectFace (const Standard_Boolean theToApply)
490 if (!theToApply || (AspectFace_set == AspectFace_applied))
492 return AspectFace_set;
495 const Aspect_InteriorStyle anIntstyle = AspectFace_set->InteriorStyle();
496 if (AspectFace_applied == NULL || AspectFace_applied->InteriorStyle() != anIntstyle)
500 case Aspect_IS_EMPTY:
501 case Aspect_IS_HOLLOW:
503 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
506 case Aspect_IS_HATCH:
508 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
509 myDisplay->SetTypeOfHatch (AspectFace_applied != NULL ? AspectFace_applied->Hatch() : TEL_HS_SOLID);
512 case Aspect_IS_SOLID:
513 case Aspect_IS_HIDDENLINE:
515 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
516 glDisable (GL_POLYGON_STIPPLE);
519 case Aspect_IS_POINT: //szvgl - no corresponding enumeration item Aspect_IS_POINT // = 5
521 glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
527 if (anIntstyle == Aspect_IS_HATCH)
529 const Tint hatchstyle = AspectFace_set->Hatch();
530 if (AspectFace_applied == NULL || AspectFace_applied->Hatch() != hatchstyle)
532 myDisplay->SetTypeOfHatch(hatchstyle);
536 if (!ActiveView()->Backfacing())
538 const Tint aCullingMode = AspectFace_set->CullingMode();
539 if (AspectFace_applied == NULL || AspectFace_applied->CullingMode() != aCullingMode)
541 switch ((TelCullMode )aCullingMode)
545 glDisable (GL_CULL_FACE);
550 glCullFace (GL_FRONT);
551 glEnable (GL_CULL_FACE);
556 glCullFace (GL_BACK);
557 glEnable (GL_CULL_FACE);
564 // Aspect_POM_None means: do not change current settings
565 if ((AspectFace_set->PolygonOffset().mode & Aspect_POM_None) != Aspect_POM_None)
567 if (PolygonOffset_applied == NULL
568 || PolygonOffset_applied->mode != AspectFace_set->PolygonOffset().mode
569 || PolygonOffset_applied->factor != AspectFace_set->PolygonOffset().factor
570 || PolygonOffset_applied->units != AspectFace_set->PolygonOffset().units)
572 PolygonOffset_applied = &AspectFace_set->PolygonOffset();
573 TelUpdatePolygonOffsets (PolygonOffset_applied);
577 UpdateMaterial (TEL_FRONT_MATERIAL);
578 if (AspectFace_set->DistinguishingMode() == TOn)
580 UpdateMaterial (TEL_BACK_MATERIAL);
583 if ((NamedStatus & OPENGL_NS_FORBIDSETTEX) == 0)
585 if (AspectFace_set->DoTextureMap())
587 EnableTexture (AspectFace_set->TextureRes (this),
588 AspectFace_set->TextureParams());
596 AspectFace_applied = AspectFace_set;
597 return AspectFace_set;
600 /*----------------------------------------------------------------------*/
602 const OpenGl_AspectMarker* OpenGl_Workspace::AspectMarker (const Standard_Boolean theToApply)
604 if (theToApply && (AspectMarker_set != AspectMarker_applied))
606 if (!AspectMarker_applied || (AspectMarker_set->Scale() != AspectMarker_applied->Scale()))
608 glPointSize (AspectMarker_set->Scale());
610 gl2psPointSize (AspectMarker_set->Scale());
613 AspectMarker_applied = AspectMarker_set;
615 return AspectMarker_set;
618 /*----------------------------------------------------------------------*/
620 const OpenGl_AspectText* OpenGl_Workspace::AspectText (const Standard_Boolean theWithApply)
624 AspectText_applied = AspectText_set;
625 TextParam_applied = TextParam_set;
628 return AspectText_set;