0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[occt.git] / src / OpenGl / OpenGl_AspectFace.cxx
CommitLineData
b311480e 1// Created on: 2011-07-13
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
30f0ad28 16#include <Aspect_PolygonOffsetMode.hxx>
17#include <NCollection_Vec3.hxx>
18
2166f0fa 19#include <OpenGl_AspectFace.hxx>
30f0ad28 20#include <OpenGl_Context.hxx>
21#include <OpenGl_ShaderManager.hxx>
22#include <OpenGl_ShaderProgram.hxx>
bf75be98 23#include <OpenGl_Texture.hxx>
24#include <OpenGl_Workspace.hxx>
2166f0fa 25
30f0ad28 26#include <Graphic3d_ShaderProgram.hxx>
bf75be98 27#include <Graphic3d_TextureMap.hxx>
4269bd1b 28#include <Graphic3d_TypeOfReflection.hxx>
29#include <Graphic3d_MaterialAspect.hxx>
30
bf75be98 31namespace
2166f0fa 32{
bf75be98 33 static OPENGL_SURF_PROP THE_DEFAULT_MATERIAL =
34 {
35 0.2F, 0.8F, 0.1F, 0.0F, // amb, diff, spec, emsv
36 1.0F, 10.0F, 0.0F, // trans, shine, env_reflexion
37 0, // isphysic
38 (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), // color_mask
39 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // ambient color
40 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // diffuse color
41 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // specular color
42 {{ 1.0F, 1.0F, 1.0F, 1.0F }}, // emissive color
43 {{ 1.0F, 1.0F, 1.0F, 1.0F }} // material color
44 };
45
46 static TEL_POFFSET_PARAM THE_DEFAULT_POFFSET = { Aspect_POM_Fill, 1.0F, 0.0F };
fd4a6963 47 static const TCollection_AsciiString THE_EMPTY_KEY;
bf75be98 48};
2166f0fa 49
bf75be98 50// =======================================================================
51// function : convertMaterial
52// purpose :
53// =======================================================================
54void OpenGl_AspectFace::convertMaterial (const CALL_DEF_MATERIAL& theMat,
55 OPENGL_SURF_PROP& theSurf)
2166f0fa 56{
bf75be98 57 theSurf.amb = theMat.IsAmbient ? theMat.Ambient : 0.0f;
58 theSurf.diff = theMat.IsDiffuse ? theMat.Diffuse : 0.0f;
59 theSurf.spec = theMat.IsSpecular ? theMat.Specular : 0.0f;
60 theSurf.emsv = theMat.IsEmission ? theMat.Emission : 0.0f;
2166f0fa 61
bf75be98 62 theSurf.isphysic = theMat.IsPhysic ? 1 : 0; // type of material
2166f0fa 63
bf75be98 64 // color of material
65 theSurf.color_mask = 0;
66 if (theMat.IsAmbient)
67 {
68 theSurf.color_mask |= OPENGL_AMBIENT_MASK;
69 }
70 if (theMat.IsDiffuse)
71 {
72 theSurf.color_mask |= OPENGL_DIFFUSE_MASK;
73 }
74 if (theMat.IsSpecular)
75 {
76 theSurf.color_mask |= OPENGL_SPECULAR_MASK;
77 }
78 if (theMat.IsEmission)
79 {
80 theSurf.color_mask |= OPENGL_EMISSIVE_MASK;
81 }
2166f0fa 82
bf75be98 83 // ambient color
84 theSurf.ambcol.rgb[0] = theMat.ColorAmb.r;
85 theSurf.ambcol.rgb[1] = theMat.ColorAmb.g;
86 theSurf.ambcol.rgb[2] = theMat.ColorAmb.b;
87 theSurf.ambcol.rgb[3] = 1.0f;
88
89 // diffuse color
90 theSurf.difcol.rgb[0] = theMat.ColorDif.r;
91 theSurf.difcol.rgb[1] = theMat.ColorDif.g;
92 theSurf.difcol.rgb[2] = theMat.ColorDif.b;
93 theSurf.difcol.rgb[3] = 1.0f;
94
95 // specular color
96 theSurf.speccol.rgb[0] = theMat.ColorSpec.r;
97 theSurf.speccol.rgb[1] = theMat.ColorSpec.g;
98 theSurf.speccol.rgb[2] = theMat.ColorSpec.b;
99 theSurf.speccol.rgb[3] = 1.0f;
100
101 // emission color
102 theSurf.emscol.rgb[0] = theMat.ColorEms.r;
103 theSurf.emscol.rgb[1] = theMat.ColorEms.g;
104 theSurf.emscol.rgb[2] = theMat.ColorEms.b;
105 theSurf.emscol.rgb[3] = 1.0f;
106
107 theSurf.shine = 128.0f * float(theMat.Shininess);
108 theSurf.env_reflexion = theMat.EnvReflexion;
109
110 // trans = 0. => opaque
111 // trans = 1. => transparent
112 // in OpenGl it is opposite.
113 theSurf.trans = 1.0f - theMat.Transparency;
114}
2166f0fa 115
bf75be98 116// =======================================================================
117// function : OpenGl_AspectFace
118// purpose :
119// =======================================================================
120OpenGl_AspectFace::OpenGl_AspectFace()
fd4a6963 121: myInteriorStyle (Aspect_IS_SOLID),
122 myEdge (TOff),
123 myHatch (TEL_HS_SOLID),
124 myDistinguishingMode (TOff),
125 myCullingMode (TelCullNone),
126 myIntFront (THE_DEFAULT_MATERIAL),
127 myIntBack (THE_DEFAULT_MATERIAL),
128 myPolygonOffset (THE_DEFAULT_POFFSET),
30f0ad28 129 myDoTextureMap (false)
fd4a6963 130{}
2166f0fa 131
bf75be98 132// =======================================================================
fd4a6963 133// function : SetAspect
bf75be98 134// purpose :
135// =======================================================================
fd4a6963 136void OpenGl_AspectFace::SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect)
bf75be98 137{
fd4a6963 138 myInteriorStyle = (Aspect_InteriorStyle )theAspect.Style;
139 myEdge = theAspect.Edge ? TOn : TOff;
2166f0fa
SK
140
141 //TelInteriorStyleIndex
bf75be98 142 switch (theAspect.Hatch)
2166f0fa 143 {
bf75be98 144 case 0: /* Aspect_HS_HORIZONTAL */
fd4a6963 145 myHatch = TEL_HS_HORIZONTAL;
2166f0fa 146 break;
bf75be98 147 case 1: /* Aspect_HS_HORIZONTAL_WIDE */
fd4a6963 148 myHatch = TEL_HS_HORIZONTAL_SPARSE;
2166f0fa 149 break;
bf75be98 150 case 2: /* Aspect_HS_VERTICAL */
fd4a6963 151 myHatch = TEL_HS_VERTICAL;
2166f0fa 152 break;
bf75be98 153 case 3: /* Aspect_HS_VERTICAL_WIDE */
fd4a6963 154 myHatch = TEL_HS_VERTICAL_SPARSE;
2166f0fa 155 break;
bf75be98 156 case 4: /* Aspect_HS_DIAGONAL_45 */
fd4a6963 157 myHatch = TEL_HS_DIAG_45;
2166f0fa 158 break;
bf75be98 159 case 5: /* Aspect_HS_DIAGONAL_45_WIDE */
fd4a6963 160 myHatch = TEL_HS_DIAG_45_SPARSE;
2166f0fa 161 break;
bf75be98 162 case 6: /* Aspect_HS_DIAGONAL_135 */
fd4a6963 163 myHatch = TEL_HS_DIAG_135;
2166f0fa 164 break;
bf75be98 165 case 7: /* Aspect_HS_DIAGONAL_135_WIDE */
fd4a6963 166 myHatch = TEL_HS_DIAG_135_SPARSE;
2166f0fa 167 break;
bf75be98 168 case 8: /* Aspect_HS_GRID */
fd4a6963 169 myHatch = TEL_HS_GRID;
2166f0fa 170 break;
bf75be98 171 case 9: /* Aspect_HS_GRID_WIDE */
fd4a6963 172 myHatch = TEL_HS_GRID_SPARSE;
2166f0fa 173 break;
bf75be98 174 case 10: /* Aspect_HS_GRID_DIAGONAL */
fd4a6963 175 myHatch = TEL_HS_CROSS;
2166f0fa 176 break;
bf75be98 177 case 11: /* Aspect_HS_GRID_DIAGONAL_WIDE */
fd4a6963 178 myHatch = TEL_HS_CROSS_SPARSE;
2166f0fa 179 break;
bf75be98 180 default:
fd4a6963 181 myHatch = 0;
2166f0fa
SK
182 break;
183 }
184
fd4a6963 185 myDistinguishingMode = theAspect.Distinguish ? TOn : TOff;
186 myCullingMode = theAspect.BackFace ? TelCullBack : TelCullNone;
2166f0fa 187
fd4a6963 188 convertMaterial (theAspect.Front, myIntFront);
189 convertMaterial (theAspect.Back, myIntBack);
2166f0fa
SK
190
191 //TelInteriorColour
fd4a6963 192 myIntFront.matcol.rgb[0] = (float )theAspect.IntColor.r;
193 myIntFront.matcol.rgb[1] = (float )theAspect.IntColor.g;
194 myIntFront.matcol.rgb[2] = (float )theAspect.IntColor.b;
195 myIntFront.matcol.rgb[3] = 1.0f;
2166f0fa
SK
196
197 //TelBackInteriorColour
fd4a6963 198 myIntBack.matcol.rgb[0] = (float )theAspect.BackIntColor.r;
199 myIntBack.matcol.rgb[1] = (float )theAspect.BackIntColor.g;
200 myIntBack.matcol.rgb[2] = (float )theAspect.BackIntColor.b;
201 myIntBack.matcol.rgb[3] = 1.0f;
bf75be98 202
2166f0fa 203 //TelPolygonOffset
fd4a6963 204 myPolygonOffset.mode = (Aspect_PolygonOffsetMode )theAspect.PolygonOffsetMode;
205 myPolygonOffset.factor = theAspect.PolygonOffsetFactor;
206 myPolygonOffset.units = theAspect.PolygonOffsetUnits;
207
208 CALL_DEF_CONTEXTLINE anEdgeAspect;
209 anEdgeAspect.Color.r = (float )theAspect.EdgeColor.r;
210 anEdgeAspect.Color.g = (float )theAspect.EdgeColor.g;
211 anEdgeAspect.Color.b = (float )theAspect.EdgeColor.b;
212 anEdgeAspect.LineType = (Aspect_TypeOfLine )theAspect.LineType;
213 anEdgeAspect.Width = (float )theAspect.Width;
214 myAspectEdge.SetAspect (anEdgeAspect);
30f0ad28 215
216 myDoTextureMap = (theAspect.Texture.doTextureMap != 0);
217
218 // update texture binding
219 myTexture = theAspect.Texture.TextureMap;
220
221 const TCollection_AsciiString& aTextureKey = myTexture.IsNull() ? THE_EMPTY_KEY : myTexture->GetId();
30f0ad28 222 if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey)
223 {
f85399e5 224 myResources.ResetTextureReadiness();
30f0ad28 225 }
226
227 // update shader program binding
228 myShaderProgram = theAspect.ShaderProgram;
229
230 const TCollection_AsciiString& aShaderKey = myShaderProgram.IsNull() ? THE_EMPTY_KEY : myShaderProgram->GetId();
30f0ad28 231 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
232 {
f85399e5 233 myResources.ResetShaderReadiness();
30f0ad28 234 }
2166f0fa
SK
235}
236
bf75be98 237// =======================================================================
fd4a6963 238// function : SetAspect
4269bd1b 239// purpose :
240// =======================================================================
fd4a6963 241void OpenGl_AspectFace::SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect)
4269bd1b 242{
fd4a6963 243 CALL_DEF_CONTEXTFILLAREA aFaceContext;
4269bd1b 244 Standard_Real aWidth;
245 Quantity_Color aBackIntColor;
246 Quantity_Color aEdgeColor;
247 Aspect_TypeOfLine aLType;
248 Quantity_Color aIntColor;
249 Aspect_InteriorStyle aIntStyle;
250 NCollection_Vec3<Standard_Real> aColor;
251
252 theAspect->Values (aIntStyle, aIntColor, aBackIntColor, aEdgeColor, aLType, aWidth);
253 aIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
254
fd4a6963 255 aFaceContext.Style = int (aIntStyle);
256 aFaceContext.IntColor.r = float (aColor.r());
257 aFaceContext.IntColor.g = float (aColor.g());
258 aFaceContext.IntColor.b = float (aColor.b());
4269bd1b 259
260 if (theAspect->Distinguish())
261 {
262 aBackIntColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
263 }
264
fd4a6963 265 aFaceContext.BackIntColor.r = float (aColor.r());
266 aFaceContext.BackIntColor.g = float (aColor.g());
267 aFaceContext.BackIntColor.b = float (aColor.b());
4269bd1b 268
fd4a6963 269 aFaceContext.Edge = theAspect->Edge () ? 1:0;
4269bd1b 270 aEdgeColor.Values (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
271
fd4a6963 272 aFaceContext.EdgeColor.r = float (aColor.r());
273 aFaceContext.EdgeColor.g = float (aColor.g());
274 aFaceContext.EdgeColor.b = float (aColor.b());
275 aFaceContext.LineType = int (aLType);
276 aFaceContext.Width = float (aWidth);
277 aFaceContext.Hatch = int (theAspect->HatchStyle ());
4269bd1b 278
fd4a6963 279 aFaceContext.Distinguish = theAspect->Distinguish () ? 1:0;
280 aFaceContext.BackFace = theAspect->BackFace () ? 1:0;
4269bd1b 281
fd4a6963 282 aFaceContext.Back.Shininess = float ((theAspect->BackMaterial ()).Shininess ());
283 aFaceContext.Back.Ambient = float ((theAspect->BackMaterial ()).Ambient ());
284 aFaceContext.Back.Diffuse = float ((theAspect->BackMaterial ()).Diffuse ());
285 aFaceContext.Back.Specular = float ((theAspect->BackMaterial ()).Specular ());
286 aFaceContext.Back.Transparency = float ((theAspect->BackMaterial ()).Transparency ());
287 aFaceContext.Back.Emission = float ((theAspect->BackMaterial ()).Emissive ());
4269bd1b 288
289 // Reflection mode
fd4a6963 290 aFaceContext.Back.IsAmbient = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0 );
291 aFaceContext.Back.IsDiffuse = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0 );
292 aFaceContext.Back.IsSpecular = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0 );
293 aFaceContext.Back.IsEmission = ((theAspect->BackMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0 );
4269bd1b 294
295 // Material type
296 const Graphic3d_MaterialAspect aBackMat = theAspect->BackMaterial ();
297 Standard_Boolean isBackPhys = aBackMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
fd4a6963 298 aFaceContext.Back.IsPhysic = (isBackPhys ? 1 : 0 );
4269bd1b 299
300 // Specular Color
fd4a6963 301 aFaceContext.Back.ColorSpec.r = float (((theAspect->BackMaterial ()).SpecularColor ()).Red ());
302 aFaceContext.Back.ColorSpec.g = float (((theAspect->BackMaterial ()).SpecularColor ()).Green ());
303 aFaceContext.Back.ColorSpec.b = float (((theAspect->BackMaterial ()).SpecularColor ()).Blue ());
4269bd1b 304
305 // Ambient color
fd4a6963 306 aFaceContext.Back.ColorAmb.r = float (((theAspect->BackMaterial ()).AmbientColor ()).Red ());
307 aFaceContext.Back.ColorAmb.g = float (((theAspect->BackMaterial ()).AmbientColor ()).Green ());
308 aFaceContext.Back.ColorAmb.b = float (((theAspect->BackMaterial ()).AmbientColor ()).Blue ());
4269bd1b 309
310 // Diffuse color
fd4a6963 311 aFaceContext.Back.ColorDif.r = float (((theAspect->BackMaterial ()).DiffuseColor ()).Red ());
312 aFaceContext.Back.ColorDif.g = float (((theAspect->BackMaterial ()).DiffuseColor ()).Green ());
313 aFaceContext.Back.ColorDif.b = float (((theAspect->BackMaterial ()).DiffuseColor ()).Blue ());
4269bd1b 314
315 // Emissive color
fd4a6963 316 aFaceContext.Back.ColorEms.r = float (((theAspect->BackMaterial ()).EmissiveColor ()).Red ());
317 aFaceContext.Back.ColorEms.g = float (((theAspect->BackMaterial ()).EmissiveColor ()).Green ());
318 aFaceContext.Back.ColorEms.b = float (((theAspect->BackMaterial ()).EmissiveColor ()).Blue ());
4269bd1b 319
fd4a6963 320 aFaceContext.Back.EnvReflexion = float ((theAspect->BackMaterial ()).EnvReflexion());
4269bd1b 321
fd4a6963 322 aFaceContext.Front.Shininess = float ((theAspect->FrontMaterial ()).Shininess ());
323 aFaceContext.Front.Ambient = float ((theAspect->FrontMaterial ()).Ambient ());
324 aFaceContext.Front.Diffuse = float ((theAspect->FrontMaterial ()).Diffuse ());
325 aFaceContext.Front.Specular = float ((theAspect->FrontMaterial ()).Specular ());
326 aFaceContext.Front.Transparency = float ((theAspect->FrontMaterial ()).Transparency ());
327 aFaceContext.Front.Emission = float ((theAspect->FrontMaterial ()).Emissive ());
4269bd1b 328
329 // Reflection mode
fd4a6963 330 aFaceContext.Front.IsAmbient = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_AMBIENT) ? 1 : 0);
331 aFaceContext.Front.IsDiffuse = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_DIFFUSE) ? 1 : 0);
332 aFaceContext.Front.IsSpecular = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
333 aFaceContext.Front.IsEmission = ((theAspect->FrontMaterial ()).ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
4269bd1b 334
fd4a6963 335 // Material type
4269bd1b 336 const Graphic3d_MaterialAspect aFrontMat = theAspect->FrontMaterial ();
337 Standard_Boolean isFrontPhys = aFrontMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
fd4a6963 338 aFaceContext.Front.IsPhysic = (isFrontPhys ? 1 : 0 );
4269bd1b 339
340 // Specular Color
fd4a6963 341 aFaceContext.Front.ColorSpec.r = float (((theAspect->FrontMaterial ()).SpecularColor ()).Red ());
342 aFaceContext.Front.ColorSpec.g = float (((theAspect->FrontMaterial ()).SpecularColor ()).Green ());
343 aFaceContext.Front.ColorSpec.b = float (((theAspect->FrontMaterial ()).SpecularColor ()).Blue ());
4269bd1b 344
345 // Ambient color
fd4a6963 346 aFaceContext.Front.ColorAmb.r = float (((theAspect->FrontMaterial ()).AmbientColor ()).Red ());
347 aFaceContext.Front.ColorAmb.g = float (((theAspect->FrontMaterial ()).AmbientColor ()).Green ());
348 aFaceContext.Front.ColorAmb.b = float (((theAspect->FrontMaterial ()).AmbientColor ()).Blue ());
4269bd1b 349
350 // Diffuse color
fd4a6963 351 aFaceContext.Front.ColorDif.r = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Red ());
352 aFaceContext.Front.ColorDif.g = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Green ());
353 aFaceContext.Front.ColorDif.b = float (((theAspect->FrontMaterial ()).DiffuseColor ()).Blue ());
4269bd1b 354
355 // Emissive color
fd4a6963 356 aFaceContext.Front.ColorEms.r = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Red ());
357 aFaceContext.Front.ColorEms.g = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Green ());
358 aFaceContext.Front.ColorEms.b = float (((theAspect->FrontMaterial ()).EmissiveColor ()).Blue ());
4269bd1b 359
fd4a6963 360 aFaceContext.Front.EnvReflexion = float ((theAspect->FrontMaterial ()).EnvReflexion());
361 aFaceContext.IsDef = 1;
362 aFaceContext.Texture.TextureMap = theAspect->TextureMap();
363 aFaceContext.Texture.doTextureMap = theAspect->TextureMapState() ? 1 : 0;
4269bd1b 364
365 Standard_Integer aPolyMode;
366 Standard_ShortReal aPolyFactor, aPolyUnits;
367 theAspect->PolygonOffsets (aPolyMode, aPolyFactor, aPolyUnits);
fd4a6963 368 aFaceContext.PolygonOffsetMode = aPolyMode;
369 aFaceContext.PolygonOffsetFactor = (Standard_ShortReal)aPolyFactor;
370 aFaceContext.PolygonOffsetUnits = (Standard_ShortReal)aPolyUnits;
4269bd1b 371
30f0ad28 372 aFaceContext.ShaderProgram = theAspect->ShaderProgram();
373
fd4a6963 374 SetAspect (aFaceContext);
4269bd1b 375}
376
377// =======================================================================
bf75be98 378// function : Render
379// purpose :
380// =======================================================================
5e27df78 381void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa 382{
5e27df78 383 theWorkspace->SetAspectFace (this);
2166f0fa
SK
384}
385
bf75be98 386// =======================================================================
387// function : Release
388// purpose :
389// =======================================================================
5e27df78 390void OpenGl_AspectFace::Release (const Handle(OpenGl_Context)& theContext)
391{
30f0ad28 392 if (!myResources.Texture.IsNull())
bf75be98 393 {
394 if (!theContext.IsNull())
395 {
30f0ad28 396 if (myResources.TextureId.IsEmpty())
bf75be98 397 {
30f0ad28 398 theContext->DelayedRelease (myResources.Texture);
bf75be98 399 }
400 else
401 {
30f0ad28 402 myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
05dd08ce 403 theContext->ReleaseResource (myResources.TextureId, Standard_True);
bf75be98 404 }
405 }
30f0ad28 406 myResources.Texture.Nullify();
bf75be98 407 }
30f0ad28 408 myResources.TextureId.Clear();
f85399e5 409 myResources.ResetTextureReadiness();
30f0ad28 410
392ac980 411 if (!myResources.ShaderProgram.IsNull()
412 && !theContext.IsNull())
30f0ad28 413 {
392ac980 414 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
415 myResources.ShaderProgram);
30f0ad28 416 }
417 myResources.ShaderProgramId.Clear();
f85399e5 418 myResources.ResetShaderReadiness();
5e27df78 419}
fd4a6963 420
421// =======================================================================
30f0ad28 422// function : BuildTexture
fd4a6963 423// purpose :
424// =======================================================================
30f0ad28 425void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Workspace)& theWS,
426 const Handle(Graphic3d_TextureMap)& theTexture)
fd4a6963 427{
30f0ad28 428 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
fd4a6963 429
30f0ad28 430 // release old texture resource
431 if (!Texture.IsNull())
fd4a6963 432 {
30f0ad28 433 if (TextureId.IsEmpty())
434 {
435 aContext->DelayedRelease (Texture);
436 Texture.Nullify();
437 }
438 else
fd4a6963 439 {
30f0ad28 440 Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
95eef64d 441 aContext->ReleaseResource (TextureId, Standard_True);
30f0ad28 442 }
443 }
444
445 TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId();
446
447 if (!theTexture.IsNull())
448 {
449 if (TextureId.IsEmpty() || !aContext->GetResource<Handle(OpenGl_Texture)> (TextureId, Texture))
450 {
451 Texture = new OpenGl_Texture (theTexture->GetParams());
452 Handle(Image_PixMap) anImage = theTexture->GetImage();
453 if (!anImage.IsNull())
fd4a6963 454 {
30f0ad28 455 Texture->Init (aContext, *anImage.operator->(), theTexture->Type());
fd4a6963 456 }
30f0ad28 457 if (!TextureId.IsEmpty())
fd4a6963 458 {
30f0ad28 459 aContext->ShareResource (TextureId, Texture);
fd4a6963 460 }
461 }
30f0ad28 462 }
463}
fd4a6963 464
30f0ad28 465// =======================================================================
466// function : BuildShader
467// purpose :
468// =======================================================================
392ac980 469void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Workspace)& theWS,
30f0ad28 470 const Handle(Graphic3d_ShaderProgram)& theShader)
471{
472 const Handle(OpenGl_Context)& aContext = theWS->GetGlContext();
30f0ad28 473 if (!aContext->IsGlGreaterEqual (2, 0))
392ac980 474 {
30f0ad28 475 return;
392ac980 476 }
30f0ad28 477
478 // release old shader program resources
479 if (!ShaderProgram.IsNull())
480 {
392ac980 481 aContext->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
f85399e5 482 ShaderProgramId.Clear();
483 ShaderProgram.Nullify();
30f0ad28 484 }
392ac980 485 if (theShader.IsNull())
30f0ad28 486 {
392ac980 487 return;
30f0ad28 488 }
392ac980 489
490 aContext->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
fd4a6963 491}