1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Aspect_PolygonOffsetMode.hxx>
17 #include <NCollection_Vec3.hxx>
19 #include <OpenGl_AspectFace.hxx>
20 #include <OpenGl_Context.hxx>
21 #include <OpenGl_ShaderManager.hxx>
22 #include <OpenGl_ShaderProgram.hxx>
23 #include <OpenGl_Texture.hxx>
24 #include <OpenGl_Workspace.hxx>
26 #include <Graphic3d_ShaderProgram.hxx>
27 #include <Graphic3d_TextureMap.hxx>
28 #include <Graphic3d_TextureParams.hxx>
29 #include <Graphic3d_TypeOfReflection.hxx>
30 #include <Graphic3d_MaterialAspect.hxx>
32 #include <Image_PixMap.hxx>
36 static OPENGL_SURF_PROP THE_DEFAULT_MATERIAL =
38 0.2F, 0.8F, 0.1F, 0.0F, // amb, diff, spec, emsv
39 1.0F, 10.0F, 1.0F, 0.0F, // trans, shine, index, env_reflexion
41 (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), // color_mask
42 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // ambient color
43 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // diffuse color
44 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // specular color
45 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // emissive color
46 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // material color
50 static const TCollection_AsciiString THE_EMPTY_KEY;
53 // =======================================================================
54 // function : convertMaterial
56 // =======================================================================
57 void OpenGl_AspectFace::convertMaterial (const CALL_DEF_MATERIAL& theMat,
58 OPENGL_SURF_PROP& theSurf)
60 theSurf.amb = theMat.IsAmbient ? theMat.Ambient : 0.0f;
61 theSurf.diff = theMat.IsDiffuse ? theMat.Diffuse : 0.0f;
62 theSurf.spec = theMat.IsSpecular ? theMat.Specular : 0.0f;
63 theSurf.emsv = theMat.IsEmission ? theMat.Emission : 0.0f;
65 theSurf.isphysic = theMat.IsPhysic ? 1 : 0; // type of material
68 theSurf.color_mask = 0;
71 theSurf.color_mask |= OPENGL_AMBIENT_MASK;
75 theSurf.color_mask |= OPENGL_DIFFUSE_MASK;
77 if (theMat.IsSpecular)
79 theSurf.color_mask |= OPENGL_SPECULAR_MASK;
81 if (theMat.IsEmission)
83 theSurf.color_mask |= OPENGL_EMISSIVE_MASK;
87 theSurf.ambcol.rgb[0] = theMat.ColorAmb.r;
88 theSurf.ambcol.rgb[1] = theMat.ColorAmb.g;
89 theSurf.ambcol.rgb[2] = theMat.ColorAmb.b;
90 theSurf.ambcol.rgb[3] = 1.0f;
93 theSurf.difcol.rgb[0] = theMat.ColorDif.r;
94 theSurf.difcol.rgb[1] = theMat.ColorDif.g;
95 theSurf.difcol.rgb[2] = theMat.ColorDif.b;
96 theSurf.difcol.rgb[3] = 1.0f;
99 theSurf.speccol.rgb[0] = theMat.ColorSpec.r;
100 theSurf.speccol.rgb[1] = theMat.ColorSpec.g;
101 theSurf.speccol.rgb[2] = theMat.ColorSpec.b;
102 theSurf.speccol.rgb[3] = 1.0f;
105 theSurf.emscol.rgb[0] = theMat.ColorEms.r;
106 theSurf.emscol.rgb[1] = theMat.ColorEms.g;
107 theSurf.emscol.rgb[2] = theMat.ColorEms.b;
108 theSurf.emscol.rgb[3] = 1.0f;
110 theSurf.shine = 128.0f * float(theMat.Shininess);
111 theSurf.env_reflexion = theMat.EnvReflexion;
113 // trans = 0. => opaque
114 // trans = 1. => transparent
115 // in OpenGl it is opposite.
116 theSurf.trans = 1.0f - theMat.Transparency;
117 theSurf.index = theMat.RefractionIndex;
119 // material BSDF (for physically-based rendering)
120 theSurf.BSDF = theMat.BSDF;
123 // =======================================================================
124 // function : OpenGl_AspectFace
126 // =======================================================================
127 OpenGl_AspectFace::OpenGl_AspectFace()
128 : myInteriorStyle (Aspect_IS_SOLID),
130 myHatch (TEL_HS_SOLID),
131 myDistinguishingMode (TOff),
132 myCullingMode (TelCullNone),
133 myIntFront (THE_DEFAULT_MATERIAL),
134 myIntBack (THE_DEFAULT_MATERIAL),
135 myPolygonOffset (THE_DEFAULT_POFFSET),
136 myDoTextureMap (false)
139 // =======================================================================
140 // function : SetAspect
142 // =======================================================================
143 void OpenGl_AspectFace::SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect)
145 myInteriorStyle = (Aspect_InteriorStyle )theAspect.Style;
146 myEdge = theAspect.Edge ? TOn : TOff;
148 //TelInteriorStyleIndex
149 switch (theAspect.Hatch)
151 case 0: /* Aspect_HS_HORIZONTAL */
152 myHatch = TEL_HS_HORIZONTAL;
154 case 1: /* Aspect_HS_HORIZONTAL_WIDE */
155 myHatch = TEL_HS_HORIZONTAL_SPARSE;
157 case 2: /* Aspect_HS_VERTICAL */
158 myHatch = TEL_HS_VERTICAL;
160 case 3: /* Aspect_HS_VERTICAL_WIDE */
161 myHatch = TEL_HS_VERTICAL_SPARSE;
163 case 4: /* Aspect_HS_DIAGONAL_45 */
164 myHatch = TEL_HS_DIAG_45;
166 case 5: /* Aspect_HS_DIAGONAL_45_WIDE */
167 myHatch = TEL_HS_DIAG_45_SPARSE;
169 case 6: /* Aspect_HS_DIAGONAL_135 */
170 myHatch = TEL_HS_DIAG_135;
172 case 7: /* Aspect_HS_DIAGONAL_135_WIDE */
173 myHatch = TEL_HS_DIAG_135_SPARSE;
175 case 8: /* Aspect_HS_GRID */
176 myHatch = TEL_HS_GRID;
178 case 9: /* Aspect_HS_GRID_WIDE */
179 myHatch = TEL_HS_GRID_SPARSE;
181 case 10: /* Aspect_HS_GRID_DIAGONAL */
182 myHatch = TEL_HS_CROSS;
184 case 11: /* Aspect_HS_GRID_DIAGONAL_WIDE */
185 myHatch = TEL_HS_CROSS_SPARSE;
192 myDistinguishingMode = theAspect.Distinguish ? TOn : TOff;
193 myCullingMode = theAspect.BackFace ? TelCullBack : TelCullNone;
195 convertMaterial (theAspect.Front, myIntFront);
196 convertMaterial (theAspect.Back, myIntBack);
199 myIntFront.matcol.rgb[0] = (float )theAspect.IntColor.r;
200 myIntFront.matcol.rgb[1] = (float )theAspect.IntColor.g;
201 myIntFront.matcol.rgb[2] = (float )theAspect.IntColor.b;
202 myIntFront.matcol.rgb[3] = 1.0f;
204 //TelBackInteriorColour
205 myIntBack.matcol.rgb[0] = (float )theAspect.BackIntColor.r;
206 myIntBack.matcol.rgb[1] = (float )theAspect.BackIntColor.g;
207 myIntBack.matcol.rgb[2] = (float )theAspect.BackIntColor.b;
208 myIntBack.matcol.rgb[3] = 1.0f;
211 myPolygonOffset.mode = (Aspect_PolygonOffsetMode )theAspect.PolygonOffsetMode;
212 myPolygonOffset.factor = theAspect.PolygonOffsetFactor;
213 myPolygonOffset.units = theAspect.PolygonOffsetUnits;
215 CALL_DEF_CONTEXTLINE anEdgeAspect;
216 anEdgeAspect.Color.r = (float )theAspect.EdgeColor.r;
217 anEdgeAspect.Color.g = (float )theAspect.EdgeColor.g;
218 anEdgeAspect.Color.b = (float )theAspect.EdgeColor.b;
219 anEdgeAspect.LineType = (Aspect_TypeOfLine )theAspect.LineType;
220 anEdgeAspect.Width = (float )theAspect.Width;
221 myAspectEdge.SetAspect (anEdgeAspect);
223 myDoTextureMap = (theAspect.Texture.doTextureMap != 0);
225 // update texture binding
226 myTexture = theAspect.Texture.TextureMap;
228 const TCollection_AsciiString& aTextureKey = myTexture.IsNull() ? THE_EMPTY_KEY : myTexture->GetId();
229 if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey)
231 myResources.ResetTextureReadiness();
234 // update shader program binding
235 myShaderProgram = theAspect.ShaderProgram;
237 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
238 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
240 myResources.ResetShaderReadiness();
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 (OpenGl_Context* theContext)
399 if (!myResources.Texture.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, Standard_True);
413 myResources.Texture.Nullify();
415 myResources.TextureId.Clear();
416 myResources.ResetTextureReadiness();
418 if (!myResources.ShaderProgram.IsNull()
421 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
422 myResources.ShaderProgram);
424 myResources.ShaderProgramId.Clear();
425 myResources.ResetShaderReadiness();
428 // =======================================================================
429 // function : BuildTexture
431 // =======================================================================
432 void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Context)& theCtx,
433 const Handle(Graphic3d_TextureMap)& theTexture)
435 // release old texture resource
436 if (!Texture.IsNull())
438 if (TextureId.IsEmpty())
440 theCtx->DelayedRelease (Texture);
445 Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
446 theCtx->ReleaseResource (TextureId, Standard_True);
450 TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId();
452 if (!theTexture.IsNull())
454 if (TextureId.IsEmpty() || !theCtx->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 (theCtx, *anImage.operator->(), theTexture->Type());
462 if (!TextureId.IsEmpty())
464 theCtx->ShareResource (TextureId, Texture);
470 // =======================================================================
471 // function : BuildShader
473 // =======================================================================
474 void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Context)& theCtx,
475 const Handle(Graphic3d_ShaderProgram)& theShader)
477 if (theCtx->core20fwd == NULL)
482 // release old shader program resources
483 if (!ShaderProgram.IsNull())
485 theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
486 ShaderProgramId.Clear();
487 ShaderProgram.Nullify();
489 if (theShader.IsNull())
494 theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);