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 <Aspect_PolygonOffsetMode.hxx>
21 #include <NCollection_Vec3.hxx>
23 #include <OpenGl_AspectFace.hxx>
24 #include <OpenGl_Context.hxx>
25 #include <OpenGl_ShaderManager.hxx>
26 #include <OpenGl_ShaderProgram.hxx>
27 #include <OpenGl_Texture.hxx>
28 #include <OpenGl_Workspace.hxx>
30 #include <Graphic3d_CGroup.hxx>
31 #include <Graphic3d_ShaderProgram.hxx>
32 #include <Graphic3d_TextureMap.hxx>
33 #include <Graphic3d_TypeOfReflection.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
38 static OPENGL_SURF_PROP THE_DEFAULT_MATERIAL =
40 0.2F, 0.8F, 0.1F, 0.0F, // amb, diff, spec, emsv
41 1.0F, 10.0F, 0.0F, // trans, shine, env_reflexion
43 (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), // color_mask
44 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // ambient color
45 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // diffuse color
46 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // specular color
47 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // emissive color
48 {{ 1.0F, 1.0F, 1.0F, 1.0F }} // material color
51 static TEL_POFFSET_PARAM THE_DEFAULT_POFFSET = { Aspect_POM_Fill, 1.0F, 0.0F };
52 static const TCollection_AsciiString THE_EMPTY_KEY;
55 // =======================================================================
56 // function : convertMaterial
58 // =======================================================================
59 void OpenGl_AspectFace::convertMaterial (const CALL_DEF_MATERIAL& theMat,
60 OPENGL_SURF_PROP& theSurf)
62 theSurf.amb = theMat.IsAmbient ? theMat.Ambient : 0.0f;
63 theSurf.diff = theMat.IsDiffuse ? theMat.Diffuse : 0.0f;
64 theSurf.spec = theMat.IsSpecular ? theMat.Specular : 0.0f;
65 theSurf.emsv = theMat.IsEmission ? theMat.Emission : 0.0f;
67 theSurf.isphysic = theMat.IsPhysic ? 1 : 0; // type of material
70 theSurf.color_mask = 0;
73 theSurf.color_mask |= OPENGL_AMBIENT_MASK;
77 theSurf.color_mask |= OPENGL_DIFFUSE_MASK;
79 if (theMat.IsSpecular)
81 theSurf.color_mask |= OPENGL_SPECULAR_MASK;
83 if (theMat.IsEmission)
85 theSurf.color_mask |= OPENGL_EMISSIVE_MASK;
89 theSurf.ambcol.rgb[0] = theMat.ColorAmb.r;
90 theSurf.ambcol.rgb[1] = theMat.ColorAmb.g;
91 theSurf.ambcol.rgb[2] = theMat.ColorAmb.b;
92 theSurf.ambcol.rgb[3] = 1.0f;
95 theSurf.difcol.rgb[0] = theMat.ColorDif.r;
96 theSurf.difcol.rgb[1] = theMat.ColorDif.g;
97 theSurf.difcol.rgb[2] = theMat.ColorDif.b;
98 theSurf.difcol.rgb[3] = 1.0f;
101 theSurf.speccol.rgb[0] = theMat.ColorSpec.r;
102 theSurf.speccol.rgb[1] = theMat.ColorSpec.g;
103 theSurf.speccol.rgb[2] = theMat.ColorSpec.b;
104 theSurf.speccol.rgb[3] = 1.0f;
107 theSurf.emscol.rgb[0] = theMat.ColorEms.r;
108 theSurf.emscol.rgb[1] = theMat.ColorEms.g;
109 theSurf.emscol.rgb[2] = theMat.ColorEms.b;
110 theSurf.emscol.rgb[3] = 1.0f;
112 theSurf.shine = 128.0f * float(theMat.Shininess);
113 theSurf.env_reflexion = theMat.EnvReflexion;
115 // trans = 0. => opaque
116 // trans = 1. => transparent
117 // in OpenGl it is opposite.
118 theSurf.trans = 1.0f - theMat.Transparency;
121 // =======================================================================
122 // function : OpenGl_AspectFace
124 // =======================================================================
125 OpenGl_AspectFace::OpenGl_AspectFace()
126 : myInteriorStyle (Aspect_IS_SOLID),
128 myHatch (TEL_HS_SOLID),
129 myDistinguishingMode (TOff),
130 myCullingMode (TelCullNone),
131 myIntFront (THE_DEFAULT_MATERIAL),
132 myIntBack (THE_DEFAULT_MATERIAL),
133 myPolygonOffset (THE_DEFAULT_POFFSET),
134 myDoTextureMap (false)
137 // =======================================================================
138 // function : SetAspect
140 // =======================================================================
141 void OpenGl_AspectFace::SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect)
143 myInteriorStyle = (Aspect_InteriorStyle )theAspect.Style;
144 myEdge = theAspect.Edge ? TOn : TOff;
146 //TelInteriorStyleIndex
147 switch (theAspect.Hatch)
149 case 0: /* Aspect_HS_HORIZONTAL */
150 myHatch = TEL_HS_HORIZONTAL;
152 case 1: /* Aspect_HS_HORIZONTAL_WIDE */
153 myHatch = TEL_HS_HORIZONTAL_SPARSE;
155 case 2: /* Aspect_HS_VERTICAL */
156 myHatch = TEL_HS_VERTICAL;
158 case 3: /* Aspect_HS_VERTICAL_WIDE */
159 myHatch = TEL_HS_VERTICAL_SPARSE;
161 case 4: /* Aspect_HS_DIAGONAL_45 */
162 myHatch = TEL_HS_DIAG_45;
164 case 5: /* Aspect_HS_DIAGONAL_45_WIDE */
165 myHatch = TEL_HS_DIAG_45_SPARSE;
167 case 6: /* Aspect_HS_DIAGONAL_135 */
168 myHatch = TEL_HS_DIAG_135;
170 case 7: /* Aspect_HS_DIAGONAL_135_WIDE */
171 myHatch = TEL_HS_DIAG_135_SPARSE;
173 case 8: /* Aspect_HS_GRID */
174 myHatch = TEL_HS_GRID;
176 case 9: /* Aspect_HS_GRID_WIDE */
177 myHatch = TEL_HS_GRID_SPARSE;
179 case 10: /* Aspect_HS_GRID_DIAGONAL */
180 myHatch = TEL_HS_CROSS;
182 case 11: /* Aspect_HS_GRID_DIAGONAL_WIDE */
183 myHatch = TEL_HS_CROSS_SPARSE;
190 myDistinguishingMode = theAspect.Distinguish ? TOn : TOff;
191 myCullingMode = theAspect.BackFace ? TelCullBack : TelCullNone;
193 convertMaterial (theAspect.Front, myIntFront);
194 convertMaterial (theAspect.Back, myIntBack);
197 myIntFront.matcol.rgb[0] = (float )theAspect.IntColor.r;
198 myIntFront.matcol.rgb[1] = (float )theAspect.IntColor.g;
199 myIntFront.matcol.rgb[2] = (float )theAspect.IntColor.b;
200 myIntFront.matcol.rgb[3] = 1.0f;
202 //TelBackInteriorColour
203 myIntBack.matcol.rgb[0] = (float )theAspect.BackIntColor.r;
204 myIntBack.matcol.rgb[1] = (float )theAspect.BackIntColor.g;
205 myIntBack.matcol.rgb[2] = (float )theAspect.BackIntColor.b;
206 myIntBack.matcol.rgb[3] = 1.0f;
209 myPolygonOffset.mode = (Aspect_PolygonOffsetMode )theAspect.PolygonOffsetMode;
210 myPolygonOffset.factor = theAspect.PolygonOffsetFactor;
211 myPolygonOffset.units = theAspect.PolygonOffsetUnits;
213 CALL_DEF_CONTEXTLINE anEdgeAspect;
214 anEdgeAspect.Color.r = (float )theAspect.EdgeColor.r;
215 anEdgeAspect.Color.g = (float )theAspect.EdgeColor.g;
216 anEdgeAspect.Color.b = (float )theAspect.EdgeColor.b;
217 anEdgeAspect.LineType = (Aspect_TypeOfLine )theAspect.LineType;
218 anEdgeAspect.Width = (float )theAspect.Width;
219 myAspectEdge.SetAspect (anEdgeAspect);
221 myDoTextureMap = (theAspect.Texture.doTextureMap != 0);
223 // update texture binding
224 myTexture = theAspect.Texture.TextureMap;
226 const TCollection_AsciiString& aTextureKey = myTexture.IsNull() ? THE_EMPTY_KEY : myTexture->GetId();
227 if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey)
229 myResources.ResetTextureReadiness();
232 // update shader program binding
233 myShaderProgram = theAspect.ShaderProgram;
235 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
236 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
238 myResources.ResetShaderReadiness();
242 // =======================================================================
243 // function : SetAspect
245 // =======================================================================
246 void OpenGl_AspectFace::SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect)
248 CALL_DEF_CONTEXTFILLAREA aFaceContext;
249 Standard_Real aWidth;
250 Quantity_Color aBackIntColor;
251 Quantity_Color aEdgeColor;
252 Aspect_TypeOfLine aLType;
253 Quantity_Color aIntColor;
254 Aspect_InteriorStyle aIntStyle;
255 NCollection_Vec3<Standard_Real> aColor;
257 theAspect->Values (aIntStyle, aIntColor, aBackIntColor, aEdgeColor, aLType, aWidth);
258 aIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
260 aFaceContext.Style = int (aIntStyle);
261 aFaceContext.IntColor.r = float (aColor.r());
262 aFaceContext.IntColor.g = float (aColor.g());
263 aFaceContext.IntColor.b = float (aColor.b());
265 if (theAspect->Distinguish())
267 aBackIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
270 aFaceContext.BackIntColor.r = float (aColor.r());
271 aFaceContext.BackIntColor.g = float (aColor.g());
272 aFaceContext.BackIntColor.b = float (aColor.b());
274 aFaceContext.Edge = theAspect->Edge () ? 1:0;
275 aEdgeColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
277 aFaceContext.EdgeColor.r = float (aColor.r());
278 aFaceContext.EdgeColor.g = float (aColor.g());
279 aFaceContext.EdgeColor.b = float (aColor.b());
280 aFaceContext.LineType = int (aLType);
281 aFaceContext.Width = float (aWidth);
282 aFaceContext.Hatch = int (theAspect->HatchStyle ());
284 aFaceContext.Distinguish = theAspect->Distinguish () ? 1:0;
285 aFaceContext.BackFace = theAspect->BackFace () ? 1:0;
287 aFaceContext.Back.Shininess = float ((theAspect->BackMaterial ()).Shininess ());
288 aFaceContext.Back.Ambient = float ((theAspect->BackMaterial ()).Ambient ());
289 aFaceContext.Back.Diffuse = float ((theAspect->BackMaterial ()).Diffuse ());
290 aFaceContext.Back.Specular = float ((theAspect->BackMaterial ()).Specular ());
291 aFaceContext.Back.Transparency = float ((theAspect->BackMaterial ()).Transparency ());
292 aFaceContext.Back.Emission = float ((theAspect->BackMaterial ()).Emissive ());
295 aFaceContext.Back.IsAmbient = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0 );
296 aFaceContext.Back.IsDiffuse = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0 );
297 aFaceContext.Back.IsSpecular = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0 );
298 aFaceContext.Back.IsEmission = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0 );
301 const Graphic3d_MaterialAspect aBackMat = theAspect->BackMaterial ();
302 Standard_Boolean isBackPhys = aBackMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
303 aFaceContext.Back.IsPhysic = (isBackPhys ? 1 : 0 );
306 aFaceContext.Back.ColorSpec.r = float (((theAspect->BackMaterial ()).SpecularColor ()).Red ());
307 aFaceContext.Back.ColorSpec.g = float (((theAspect->BackMaterial ()).SpecularColor ()).Green ());
308 aFaceContext.Back.ColorSpec.b = float (((theAspect->BackMaterial ()).SpecularColor ()).Blue ());
311 aFaceContext.Back.ColorAmb.r = float (((theAspect->BackMaterial ()).AmbientColor ()).Red ());
312 aFaceContext.Back.ColorAmb.g = float (((theAspect->BackMaterial ()).AmbientColor ()).Green ());
313 aFaceContext.Back.ColorAmb.b = float (((theAspect->BackMaterial ()).AmbientColor ()).Blue ());
316 aFaceContext.Back.ColorDif.r = float (((theAspect->BackMaterial ()).DiffuseColor ()).Red ());
317 aFaceContext.Back.ColorDif.g = float (((theAspect->BackMaterial ()).DiffuseColor ()).Green ());
318 aFaceContext.Back.ColorDif.b = float (((theAspect->BackMaterial ()).DiffuseColor ()).Blue ());
321 aFaceContext.Back.ColorEms.r = float (((theAspect->BackMaterial ()).EmissiveColor ()).Red ());
322 aFaceContext.Back.ColorEms.g = float (((theAspect->BackMaterial ()).EmissiveColor ()).Green ());
323 aFaceContext.Back.ColorEms.b = float (((theAspect->BackMaterial ()).EmissiveColor ()).Blue ());
325 aFaceContext.Back.EnvReflexion = float ((theAspect->BackMaterial ()).EnvReflexion());
327 aFaceContext.Front.Shininess = float ((theAspect->FrontMaterial ()).Shininess ());
328 aFaceContext.Front.Ambient = float ((theAspect->FrontMaterial ()).Ambient ());
329 aFaceContext.Front.Diffuse = float ((theAspect->FrontMaterial ()).Diffuse ());
330 aFaceContext.Front.Specular = float ((theAspect->FrontMaterial ()).Specular ());
331 aFaceContext.Front.Transparency = float ((theAspect->FrontMaterial ()).Transparency ());
332 aFaceContext.Front.Emission = float ((theAspect->FrontMaterial ()).Emissive ());
335 aFaceContext.Front.IsAmbient = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0);
336 aFaceContext.Front.IsDiffuse = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0);
337 aFaceContext.Front.IsSpecular = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
338 aFaceContext.Front.IsEmission = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
341 const Graphic3d_MaterialAspect aFrontMat = theAspect->FrontMaterial ();
342 Standard_Boolean isFrontPhys = aFrontMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
343 aFaceContext.Front.IsPhysic = (isFrontPhys ? 1 : 0 );
346 aFaceContext.Front.ColorSpec.r = float (((theAspect->FrontMaterial ()).SpecularColor ()).Red ());
347 aFaceContext.Front.ColorSpec.g = float (((theAspect->FrontMaterial ()).SpecularColor ()).Green ());
348 aFaceContext.Front.ColorSpec.b = float (((theAspect->FrontMaterial ()).SpecularColor ()).Blue ());
351 aFaceContext.Front.ColorAmb.r = float (((theAspect->FrontMaterial ()).AmbientColor ()).Red ());
352 aFaceContext.Front.ColorAmb.g = float (((theAspect->FrontMaterial ()).AmbientColor ()).Green ());
353 aFaceContext.Front.ColorAmb.b = float (((theAspect->FrontMaterial ()).AmbientColor ()).Blue ());
356 aFaceContext.Front.ColorDif.r = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Red ());
357 aFaceContext.Front.ColorDif.g = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Green ());
358 aFaceContext.Front.ColorDif.b = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Blue ());
361 aFaceContext.Front.ColorEms.r = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Red ());
362 aFaceContext.Front.ColorEms.g = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Green ());
363 aFaceContext.Front.ColorEms.b = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Blue ());
365 aFaceContext.Front.EnvReflexion = float ((theAspect->FrontMaterial ()).EnvReflexion());
366 aFaceContext.IsDef = 1;
367 aFaceContext.Texture.TextureMap = theAspect->TextureMap();
368 aFaceContext.Texture.doTextureMap = theAspect->TextureMapState() ? 1 : 0;
370 Standard_Integer aPolyMode;
371 Standard_ShortReal aPolyFactor, aPolyUnits;
372 theAspect->PolygonOffsets (aPolyMode, aPolyFactor, aPolyUnits);
373 aFaceContext.PolygonOffsetMode = aPolyMode;
374 aFaceContext.PolygonOffsetFactor = (Standard_ShortReal)aPolyFactor;
375 aFaceContext.PolygonOffsetUnits = (Standard_ShortReal)aPolyUnits;
377 aFaceContext.ShaderProgram = theAspect->ShaderProgram();
379 SetAspect (aFaceContext);
382 // =======================================================================
385 // =======================================================================
386 void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
388 theWorkspace->SetAspectFace (this);
391 // =======================================================================
392 // function : Release
394 // =======================================================================
395 void OpenGl_AspectFace::Release (const Handle(OpenGl_Context)& theContext)
397 if (!myResources.Texture.IsNull())
399 if (!theContext.IsNull())
401 if (myResources.TextureId.IsEmpty())
403 theContext->DelayedRelease (myResources.Texture);
407 myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
408 theContext->ReleaseResource (myResources.TextureId, Standard_True);
411 myResources.Texture.Nullify();
413 myResources.TextureId.Clear();
414 myResources.ResetTextureReadiness();
416 if (!myResources.ShaderProgram.IsNull()
417 && !theContext.IsNull())
419 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
420 myResources.ShaderProgram);
422 myResources.ShaderProgramId.Clear();
423 myResources.ResetShaderReadiness();
426 // =======================================================================
427 // function : BuildTexture
429 // =======================================================================
430 void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Workspace)& theWS,
431 const Handle(Graphic3d_TextureMap)& theTexture)
433 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
435 // release old texture resource
436 if (!Texture.IsNull())
438 if (TextureId.IsEmpty())
440 aContext->DelayedRelease (Texture);
445 Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
446 aContext->ReleaseResource (TextureId);
450 TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId();
452 if (!theTexture.IsNull())
454 if (TextureId.IsEmpty() || !aContext->GetResource<Handle(OpenGl_Texture)> (TextureId, Texture))
456 Texture = new OpenGl_Texture (theTexture->GetParams());
457 Handle(Image_PixMap) anImage = theTexture->GetImage();
458 if (!anImage.IsNull())
460 Texture->Init (aContext, *anImage.operator->(), theTexture->Type());
462 if (!TextureId.IsEmpty())
464 aContext->ShareResource (TextureId, Texture);
470 // =======================================================================
471 // function : BuildShader
473 // =======================================================================
474 void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
475 const Handle(Graphic3d_ShaderProgram)& theShader)
477 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
478 if (!aContext->IsGlGreaterEqual (2, 0))
483 // release old shader program resources
484 if (!ShaderProgram.IsNull())
486 aContext->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
487 ShaderProgramId.Clear();
488 ShaderProgram.Nullify();
490 if (theShader.IsNull())
495 aContext->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);