0031082: Visualization - crash on display if there are no lights in the view
[occt.git] / src / OpenGl / OpenGl_Aspects.cxx
1 // Copyright (c) 2019 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <OpenGl_Aspects.hxx>
15 #include <OpenGl_Context.hxx>
16 #include <OpenGl_Workspace.hxx>
17
18 #include <Graphic3d_TypeOfReflection.hxx>
19 #include <Graphic3d_MaterialAspect.hxx>
20
21 namespace
22 {
23   //! Initialize default material in this way for backward compatibility.
24   inline Graphic3d_MaterialAspect initDefaultMaterial()
25   {
26     Graphic3d_MaterialAspect aMat;
27     aMat.SetMaterialType (Graphic3d_MATERIAL_ASPECT);
28     aMat.SetAmbientColor (Quantity_Color (Graphic3d_Vec3 (0.2f)));
29     aMat.SetDiffuseColor (Quantity_Color (Graphic3d_Vec3 (0.8f)));
30     aMat.SetEmissiveColor(Quantity_Color (Graphic3d_Vec3 (0.1f)));
31     aMat.SetSpecularColor(Quantity_NOC_BLACK);
32     aMat.SetShininess (10.0f / 128.0f);
33     aMat.SetRefractionIndex (1.0f);
34     return aMat;
35   }
36
37   static const Graphic3d_MaterialAspect THE_DEFAULT_MATERIAL = initDefaultMaterial();
38 }
39
40 // =======================================================================
41 // function : OpenGl_Aspects
42 // purpose  :
43 // =======================================================================
44 OpenGl_Aspects::OpenGl_Aspects()
45 : myAspect (new Graphic3d_Aspects()),
46   myShadingModel (Graphic3d_TOSM_UNLIT)
47 {
48   myAspect->SetInteriorStyle (Aspect_IS_SOLID);
49   myAspect->SetInteriorColor (Quantity_NOC_WHITE);
50   myAspect->SetEdgeColor (Quantity_NOC_WHITE);
51   myAspect->SetFrontMaterial (THE_DEFAULT_MATERIAL);
52   myAspect->SetBackMaterial (THE_DEFAULT_MATERIAL);
53   myAspect->SetShadingModel (myShadingModel);
54   myAspect->SetHatchStyle (Handle(Graphic3d_HatchStyle)());
55 }
56
57 // =======================================================================
58 // function : OpenGl_Aspects
59 // purpose  :
60 // =======================================================================
61 OpenGl_Aspects::OpenGl_Aspects (const Handle(Graphic3d_Aspects)& theAspect)
62 : myShadingModel (Graphic3d_TOSM_DEFAULT)
63 {
64   SetAspect (theAspect);
65 }
66
67 // =======================================================================
68 // function : SetAspect
69 // purpose  :
70 // =======================================================================
71 void OpenGl_Aspects::SetAspect (const Handle(Graphic3d_Aspects)& theAspect)
72 {
73   myAspect = theAspect;
74
75   const Graphic3d_MaterialAspect& aMat = theAspect->FrontMaterial();
76   myShadingModel = theAspect->ShadingModel() != Graphic3d_TOSM_UNLIT
77                 && (aMat.ReflectionMode (Graphic3d_TOR_AMBIENT)
78                  || aMat.ReflectionMode (Graphic3d_TOR_DIFFUSE)
79                  || aMat.ReflectionMode (Graphic3d_TOR_SPECULAR)
80                  || aMat.ReflectionMode (Graphic3d_TOR_EMISSION))
81                  ? theAspect->ShadingModel()
82                  : Graphic3d_TOSM_UNLIT;
83
84   // invalidate resources
85   myResTextureSet.UpdateRediness (myAspect);
86   myResSprite.UpdateRediness (myAspect);
87   myResProgram.UpdateRediness (myAspect);
88   if (!myResSprite.IsReady())
89   {
90     myResTextureSet.Invalidate();
91   }
92 }
93
94 // =======================================================================
95 // function : Render
96 // purpose  :
97 // =======================================================================
98 void OpenGl_Aspects::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
99 {
100   theWorkspace->SetAspects (this);
101 }
102
103 // =======================================================================
104 // function : Release
105 // purpose  :
106 // =======================================================================
107 void OpenGl_Aspects::Release (OpenGl_Context* theContext)
108 {
109   myResTextureSet.Release (theContext);
110   myResSprite.Release (theContext);
111   myResProgram.Release (theContext);
112 }
113
114 // =======================================================================
115 // function : DumpJson
116 // purpose  :
117 // =======================================================================
118 void OpenGl_Aspects::DumpJson (Standard_OStream& theOStream, const Standard_Integer theDepth) const
119 {
120   OCCT_DUMP_CLASS_BEGIN (theOStream, OpenGl_Aspects);
121
122   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myAspect.get());
123   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myShadingModel);
124 }