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();
228 if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey)
230 myResources.ResetTexture();
233 // update shader program binding
234 myShaderProgram = theAspect.ShaderProgram;
236 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
238 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
240 myResources.ResetShader();
244 // =======================================================================
245 // function : SetAspect
247 // =======================================================================
248 void OpenGl_AspectFace::SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect)
250 CALL_DEF_CONTEXTFILLAREA aFaceContext;
251 Standard_Real aWidth;
252 Quantity_Color aBackIntColor;
253 Quantity_Color aEdgeColor;
254 Aspect_TypeOfLine aLType;
255 Quantity_Color aIntColor;
256 Aspect_InteriorStyle aIntStyle;
257 NCollection_Vec3<Standard_Real> aColor;
259 theAspect->Values (aIntStyle, aIntColor, aBackIntColor, aEdgeColor, aLType, aWidth);
260 aIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
262 aFaceContext.Style = int (aIntStyle);
263 aFaceContext.IntColor.r = float (aColor.r());
264 aFaceContext.IntColor.g = float (aColor.g());
265 aFaceContext.IntColor.b = float (aColor.b());
267 if (theAspect->Distinguish())
269 aBackIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
272 aFaceContext.BackIntColor.r = float (aColor.r());
273 aFaceContext.BackIntColor.g = float (aColor.g());
274 aFaceContext.BackIntColor.b = float (aColor.b());
276 aFaceContext.Edge = theAspect->Edge () ? 1:0;
277 aEdgeColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
279 aFaceContext.EdgeColor.r = float (aColor.r());
280 aFaceContext.EdgeColor.g = float (aColor.g());
281 aFaceContext.EdgeColor.b = float (aColor.b());
282 aFaceContext.LineType = int (aLType);
283 aFaceContext.Width = float (aWidth);
284 aFaceContext.Hatch = int (theAspect->HatchStyle ());
286 aFaceContext.Distinguish = theAspect->Distinguish () ? 1:0;
287 aFaceContext.BackFace = theAspect->BackFace () ? 1:0;
289 aFaceContext.Back.Shininess = float ((theAspect->BackMaterial ()).Shininess ());
290 aFaceContext.Back.Ambient = float ((theAspect->BackMaterial ()).Ambient ());
291 aFaceContext.Back.Diffuse = float ((theAspect->BackMaterial ()).Diffuse ());
292 aFaceContext.Back.Specular = float ((theAspect->BackMaterial ()).Specular ());
293 aFaceContext.Back.Transparency = float ((theAspect->BackMaterial ()).Transparency ());
294 aFaceContext.Back.Emission = float ((theAspect->BackMaterial ()).Emissive ());
297 aFaceContext.Back.IsAmbient = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0 );
298 aFaceContext.Back.IsDiffuse = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0 );
299 aFaceContext.Back.IsSpecular = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0 );
300 aFaceContext.Back.IsEmission = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0 );
303 const Graphic3d_MaterialAspect aBackMat = theAspect->BackMaterial ();
304 Standard_Boolean isBackPhys = aBackMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
305 aFaceContext.Back.IsPhysic = (isBackPhys ? 1 : 0 );
308 aFaceContext.Back.ColorSpec.r = float (((theAspect->BackMaterial ()).SpecularColor ()).Red ());
309 aFaceContext.Back.ColorSpec.g = float (((theAspect->BackMaterial ()).SpecularColor ()).Green ());
310 aFaceContext.Back.ColorSpec.b = float (((theAspect->BackMaterial ()).SpecularColor ()).Blue ());
313 aFaceContext.Back.ColorAmb.r = float (((theAspect->BackMaterial ()).AmbientColor ()).Red ());
314 aFaceContext.Back.ColorAmb.g = float (((theAspect->BackMaterial ()).AmbientColor ()).Green ());
315 aFaceContext.Back.ColorAmb.b = float (((theAspect->BackMaterial ()).AmbientColor ()).Blue ());
318 aFaceContext.Back.ColorDif.r = float (((theAspect->BackMaterial ()).DiffuseColor ()).Red ());
319 aFaceContext.Back.ColorDif.g = float (((theAspect->BackMaterial ()).DiffuseColor ()).Green ());
320 aFaceContext.Back.ColorDif.b = float (((theAspect->BackMaterial ()).DiffuseColor ()).Blue ());
323 aFaceContext.Back.ColorEms.r = float (((theAspect->BackMaterial ()).EmissiveColor ()).Red ());
324 aFaceContext.Back.ColorEms.g = float (((theAspect->BackMaterial ()).EmissiveColor ()).Green ());
325 aFaceContext.Back.ColorEms.b = float (((theAspect->BackMaterial ()).EmissiveColor ()).Blue ());
327 aFaceContext.Back.EnvReflexion = float ((theAspect->BackMaterial ()).EnvReflexion());
329 aFaceContext.Front.Shininess = float ((theAspect->FrontMaterial ()).Shininess ());
330 aFaceContext.Front.Ambient = float ((theAspect->FrontMaterial ()).Ambient ());
331 aFaceContext.Front.Diffuse = float ((theAspect->FrontMaterial ()).Diffuse ());
332 aFaceContext.Front.Specular = float ((theAspect->FrontMaterial ()).Specular ());
333 aFaceContext.Front.Transparency = float ((theAspect->FrontMaterial ()).Transparency ());
334 aFaceContext.Front.Emission = float ((theAspect->FrontMaterial ()).Emissive ());
337 aFaceContext.Front.IsAmbient = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0);
338 aFaceContext.Front.IsDiffuse = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0);
339 aFaceContext.Front.IsSpecular = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
340 aFaceContext.Front.IsEmission = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
343 const Graphic3d_MaterialAspect aFrontMat = theAspect->FrontMaterial ();
344 Standard_Boolean isFrontPhys = aFrontMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
345 aFaceContext.Front.IsPhysic = (isFrontPhys ? 1 : 0 );
348 aFaceContext.Front.ColorSpec.r = float (((theAspect->FrontMaterial ()).SpecularColor ()).Red ());
349 aFaceContext.Front.ColorSpec.g = float (((theAspect->FrontMaterial ()).SpecularColor ()).Green ());
350 aFaceContext.Front.ColorSpec.b = float (((theAspect->FrontMaterial ()).SpecularColor ()).Blue ());
353 aFaceContext.Front.ColorAmb.r = float (((theAspect->FrontMaterial ()).AmbientColor ()).Red ());
354 aFaceContext.Front.ColorAmb.g = float (((theAspect->FrontMaterial ()).AmbientColor ()).Green ());
355 aFaceContext.Front.ColorAmb.b = float (((theAspect->FrontMaterial ()).AmbientColor ()).Blue ());
358 aFaceContext.Front.ColorDif.r = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Red ());
359 aFaceContext.Front.ColorDif.g = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Green ());
360 aFaceContext.Front.ColorDif.b = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Blue ());
363 aFaceContext.Front.ColorEms.r = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Red ());
364 aFaceContext.Front.ColorEms.g = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Green ());
365 aFaceContext.Front.ColorEms.b = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Blue ());
367 aFaceContext.Front.EnvReflexion = float ((theAspect->FrontMaterial ()).EnvReflexion());
368 aFaceContext.IsDef = 1;
369 aFaceContext.Texture.TextureMap = theAspect->TextureMap();
370 aFaceContext.Texture.doTextureMap = theAspect->TextureMapState() ? 1 : 0;
372 Standard_Integer aPolyMode;
373 Standard_ShortReal aPolyFactor, aPolyUnits;
374 theAspect->PolygonOffsets (aPolyMode, aPolyFactor, aPolyUnits);
375 aFaceContext.PolygonOffsetMode = aPolyMode;
376 aFaceContext.PolygonOffsetFactor = (Standard_ShortReal)aPolyFactor;
377 aFaceContext.PolygonOffsetUnits = (Standard_ShortReal)aPolyUnits;
379 aFaceContext.ShaderProgram = theAspect->ShaderProgram();
381 SetAspect (aFaceContext);
384 // =======================================================================
387 // =======================================================================
388 void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
390 theWorkspace->SetAspectFace (this);
393 // =======================================================================
394 // function : Release
396 // =======================================================================
397 void OpenGl_AspectFace::Release (const Handle(OpenGl_Context)& theContext)
399 if (!myResources.Texture.IsNull())
401 if (!theContext.IsNull())
403 if (myResources.TextureId.IsEmpty())
405 theContext->DelayedRelease (myResources.Texture);
409 myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
410 theContext->ReleaseResource (myResources.TextureId);
413 myResources.Texture.Nullify();
415 myResources.TextureId.Clear();
416 myResources.ResetTexture();
418 if (!myResources.ShaderProgram.IsNull() && !theContext.IsNull())
420 theContext->ShaderManager()->Unregister (myResources.ShaderProgram);
422 myResources.ShaderProgramId.Clear();
423 myResources.ResetShader();
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();
479 if (!aContext->IsGlGreaterEqual (2, 0))
482 // release old shader program resources
483 if (!ShaderProgram.IsNull())
485 aContext->ShaderManager()->Unregister (ShaderProgram);
488 ShaderProgramId = theShader.IsNull() ? THE_EMPTY_KEY : theShader->GetId();
490 if (!theShader.IsNull())
492 if (!aContext->GetResource<Handle(OpenGl_ShaderProgram)> (ShaderProgramId, ShaderProgram))
494 ShaderProgram = aContext->ShaderManager()->Create (theShader);
495 if (!ShaderProgramId.IsEmpty())
497 aContext->ShareResource (ShaderProgramId, ShaderProgram);
503 ShaderProgram.Nullify();