Commit | Line | Data |
---|---|---|
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> |
c04c30b3 | 28 | #include <Graphic3d_TextureParams.hxx> |
4269bd1b | 29 | #include <Graphic3d_TypeOfReflection.hxx> |
30 | #include <Graphic3d_MaterialAspect.hxx> | |
31 | ||
c04c30b3 | 32 | #include <Image_PixMap.hxx> |
33 | ||
bf75be98 | 34 | namespace |
2166f0fa | 35 | { |
b6472664 | 36 | //! Initialize default material in this way for backward compatibility. |
37 | inline Graphic3d_MaterialAspect initDefaultMaterial() | |
bf75be98 | 38 | { |
b6472664 | 39 | Graphic3d_MaterialAspect aMat; |
40 | aMat.SetMaterialType (Graphic3d_MATERIAL_ASPECT); | |
41 | aMat.SetAmbient (0.2); | |
42 | aMat.SetDiffuse (0.8); | |
43 | aMat.SetSpecular (0.1); | |
44 | aMat.SetEmissive (0.0); | |
45 | aMat.SetAmbientColor (Quantity_NOC_WHITE); | |
46 | aMat.SetDiffuseColor (Quantity_NOC_WHITE); | |
47 | aMat.SetEmissiveColor(Quantity_NOC_WHITE); | |
48 | aMat.SetSpecularColor(Quantity_NOC_WHITE); | |
49 | aMat.SetShininess (10.0 / 128.0); | |
50 | aMat.SetRefractionIndex (1.0); | |
51 | return aMat; | |
52 | } | |
bf75be98 | 53 | |
b6472664 | 54 | static const TCollection_AsciiString THE_EMPTY_KEY; |
55 | static const Graphic3d_MaterialAspect THE_DEFAULT_MATERIAL = initDefaultMaterial(); | |
a3f6f591 | 56 | } |
2166f0fa | 57 | |
bf75be98 | 58 | // ======================================================================= |
b6472664 | 59 | // function : OpenGl_AspectFace |
bf75be98 | 60 | // purpose : |
61 | // ======================================================================= | |
b6472664 | 62 | OpenGl_AspectFace::OpenGl_AspectFace() |
63 | : myAspect (new Graphic3d_AspectFillArea3d (Aspect_IS_SOLID, Quantity_NOC_WHITE, | |
64 | Quantity_NOC_WHITE, Aspect_TOL_SOLID, 1.0, | |
65 | THE_DEFAULT_MATERIAL, THE_DEFAULT_MATERIAL)), | |
66 | myIsNoLighting (false) | |
2166f0fa | 67 | { |
b6472664 | 68 | myAspect->SetHatchStyle (Aspect_HS_SOLID); |
bf75be98 | 69 | } |
2166f0fa | 70 | |
bf75be98 | 71 | // ======================================================================= |
72 | // function : OpenGl_AspectFace | |
73 | // purpose : | |
74 | // ======================================================================= | |
b6472664 | 75 | OpenGl_AspectFace::OpenGl_AspectFace (const Handle(Graphic3d_AspectFillArea3d)& theAspect) |
76 | : myIsNoLighting (false) | |
77 | { | |
78 | SetAspect (theAspect); | |
79 | } | |
2166f0fa | 80 | |
bf75be98 | 81 | // ======================================================================= |
fd4a6963 | 82 | // function : SetAspect |
bf75be98 | 83 | // purpose : |
84 | // ======================================================================= | |
b6472664 | 85 | void OpenGl_AspectFace::SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect) |
bf75be98 | 86 | { |
b6472664 | 87 | myAspect = theAspect; |
2166f0fa | 88 | |
b6472664 | 89 | const Graphic3d_MaterialAspect& aMat = theAspect->FrontMaterial(); |
90 | myIsNoLighting = !aMat.ReflectionMode (Graphic3d_TOR_AMBIENT) | |
91 | && !aMat.ReflectionMode (Graphic3d_TOR_DIFFUSE) | |
92 | && !aMat.ReflectionMode (Graphic3d_TOR_SPECULAR) | |
93 | && !aMat.ReflectionMode (Graphic3d_TOR_EMISSION); | |
2166f0fa | 94 | |
b6472664 | 95 | myAspectEdge.Aspect()->SetColor (theAspect->EdgeColor()); |
96 | myAspectEdge.Aspect()->SetType (theAspect->EdgeLineType()); | |
97 | myAspectEdge.Aspect()->SetWidth (theAspect->EdgeWidth()); | |
30f0ad28 | 98 | |
99 | // update texture binding | |
b6472664 | 100 | const TCollection_AsciiString& aTextureKey = myAspect->TextureMap().IsNull() ? THE_EMPTY_KEY : myAspect->TextureMap()->GetId(); |
30f0ad28 | 101 | if (aTextureKey.IsEmpty() || myResources.TextureId != aTextureKey) |
102 | { | |
f85399e5 | 103 | myResources.ResetTextureReadiness(); |
30f0ad28 | 104 | } |
d2edda76 | 105 | else if (!myResources.Texture.IsNull() |
106 | && !myAspect->TextureMap().IsNull() | |
107 | && myResources.Texture->Revision() != myAspect->TextureMap()->Revision()) | |
108 | { | |
109 | myResources.ResetTextureReadiness(); | |
110 | } | |
30f0ad28 | 111 | |
112 | // update shader program binding | |
b6472664 | 113 | const TCollection_AsciiString& aShaderKey = myAspect->ShaderProgram().IsNull() ? THE_EMPTY_KEY : myAspect->ShaderProgram()->GetId(); |
30f0ad28 | 114 | if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey) |
115 | { | |
f85399e5 | 116 | myResources.ResetShaderReadiness(); |
30f0ad28 | 117 | } |
2166f0fa SK |
118 | } |
119 | ||
bf75be98 | 120 | // ======================================================================= |
121 | // function : Render | |
122 | // purpose : | |
123 | // ======================================================================= | |
5e27df78 | 124 | void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) const |
2166f0fa | 125 | { |
5e27df78 | 126 | theWorkspace->SetAspectFace (this); |
2166f0fa SK |
127 | } |
128 | ||
bf75be98 | 129 | // ======================================================================= |
130 | // function : Release | |
131 | // purpose : | |
132 | // ======================================================================= | |
10b9c7df | 133 | void OpenGl_AspectFace::Release (OpenGl_Context* theContext) |
5e27df78 | 134 | { |
30f0ad28 | 135 | if (!myResources.Texture.IsNull()) |
bf75be98 | 136 | { |
10b9c7df | 137 | if (theContext) |
bf75be98 | 138 | { |
30f0ad28 | 139 | if (myResources.TextureId.IsEmpty()) |
bf75be98 | 140 | { |
30f0ad28 | 141 | theContext->DelayedRelease (myResources.Texture); |
bf75be98 | 142 | } |
143 | else | |
144 | { | |
30f0ad28 | 145 | myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call |
05dd08ce | 146 | theContext->ReleaseResource (myResources.TextureId, Standard_True); |
bf75be98 | 147 | } |
148 | } | |
30f0ad28 | 149 | myResources.Texture.Nullify(); |
bf75be98 | 150 | } |
30f0ad28 | 151 | myResources.TextureId.Clear(); |
f85399e5 | 152 | myResources.ResetTextureReadiness(); |
30f0ad28 | 153 | |
392ac980 | 154 | if (!myResources.ShaderProgram.IsNull() |
10b9c7df | 155 | && theContext) |
30f0ad28 | 156 | { |
392ac980 | 157 | theContext->ShaderManager()->Unregister (myResources.ShaderProgramId, |
158 | myResources.ShaderProgram); | |
30f0ad28 | 159 | } |
160 | myResources.ShaderProgramId.Clear(); | |
f85399e5 | 161 | myResources.ResetShaderReadiness(); |
5e27df78 | 162 | } |
fd4a6963 | 163 | |
164 | // ======================================================================= | |
30f0ad28 | 165 | // function : BuildTexture |
fd4a6963 | 166 | // purpose : |
167 | // ======================================================================= | |
8625ef7e | 168 | void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Context)& theCtx, |
30f0ad28 | 169 | const Handle(Graphic3d_TextureMap)& theTexture) |
fd4a6963 | 170 | { |
30f0ad28 | 171 | // release old texture resource |
172 | if (!Texture.IsNull()) | |
fd4a6963 | 173 | { |
d2edda76 | 174 | if (!theTexture.IsNull() |
175 | && theTexture->GetId() == TextureId | |
176 | && theTexture->Revision() != Texture->Revision()) | |
177 | { | |
178 | Handle(Image_PixMap) anImage = theTexture->GetImage(); | |
179 | if (!anImage.IsNull()) | |
180 | { | |
181 | Texture->Init (theCtx, *anImage.operator->(), theTexture->Type()); | |
182 | Texture->SetRevision (Texture->Revision()); | |
183 | return; | |
184 | } | |
185 | } | |
186 | ||
30f0ad28 | 187 | if (TextureId.IsEmpty()) |
188 | { | |
8625ef7e | 189 | theCtx->DelayedRelease (Texture); |
30f0ad28 | 190 | Texture.Nullify(); |
191 | } | |
192 | else | |
fd4a6963 | 193 | { |
30f0ad28 | 194 | Texture.Nullify(); // we need nullify all handles before ReleaseResource() call |
8625ef7e | 195 | theCtx->ReleaseResource (TextureId, Standard_True); |
30f0ad28 | 196 | } |
197 | } | |
198 | ||
199 | TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId(); | |
200 | ||
201 | if (!theTexture.IsNull()) | |
202 | { | |
8625ef7e | 203 | if (TextureId.IsEmpty() || !theCtx->GetResource<Handle(OpenGl_Texture)> (TextureId, Texture)) |
30f0ad28 | 204 | { |
205 | Texture = new OpenGl_Texture (theTexture->GetParams()); | |
206 | Handle(Image_PixMap) anImage = theTexture->GetImage(); | |
207 | if (!anImage.IsNull()) | |
fd4a6963 | 208 | { |
8625ef7e | 209 | Texture->Init (theCtx, *anImage.operator->(), theTexture->Type()); |
d2edda76 | 210 | Texture->SetRevision (Texture->Revision()); |
fd4a6963 | 211 | } |
30f0ad28 | 212 | if (!TextureId.IsEmpty()) |
fd4a6963 | 213 | { |
8625ef7e | 214 | theCtx->ShareResource (TextureId, Texture); |
fd4a6963 | 215 | } |
216 | } | |
30f0ad28 | 217 | } |
218 | } | |
fd4a6963 | 219 | |
30f0ad28 | 220 | // ======================================================================= |
221 | // function : BuildShader | |
222 | // purpose : | |
223 | // ======================================================================= | |
8625ef7e | 224 | void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Context)& theCtx, |
30f0ad28 | 225 | const Handle(Graphic3d_ShaderProgram)& theShader) |
226 | { | |
4e1523ef | 227 | if (theCtx->core20fwd == NULL) |
392ac980 | 228 | { |
30f0ad28 | 229 | return; |
392ac980 | 230 | } |
30f0ad28 | 231 | |
232 | // release old shader program resources | |
233 | if (!ShaderProgram.IsNull()) | |
234 | { | |
8625ef7e | 235 | theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram); |
f85399e5 | 236 | ShaderProgramId.Clear(); |
237 | ShaderProgram.Nullify(); | |
30f0ad28 | 238 | } |
392ac980 | 239 | if (theShader.IsNull()) |
30f0ad28 | 240 | { |
392ac980 | 241 | return; |
30f0ad28 | 242 | } |
392ac980 | 243 | |
8625ef7e | 244 | theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram); |
fd4a6963 | 245 | } |