0027851: Wrong result of classification of the point relative to the solid
[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>
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 34namespace
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 62OpenGl_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 75OpenGl_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 85void 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 }
105
106 // update shader program binding
b6472664 107 const TCollection_AsciiString& aShaderKey = myAspect->ShaderProgram().IsNull() ? THE_EMPTY_KEY : myAspect->ShaderProgram()->GetId();
30f0ad28 108 if (aShaderKey.IsEmpty() || myResources.ShaderProgramId != aShaderKey)
109 {
f85399e5 110 myResources.ResetShaderReadiness();
30f0ad28 111 }
2166f0fa
SK
112}
113
bf75be98 114// =======================================================================
115// function : Render
116// purpose :
117// =======================================================================
5e27df78 118void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa 119{
5e27df78 120 theWorkspace->SetAspectFace (this);
2166f0fa
SK
121}
122
bf75be98 123// =======================================================================
124// function : Release
125// purpose :
126// =======================================================================
10b9c7df 127void OpenGl_AspectFace::Release (OpenGl_Context* theContext)
5e27df78 128{
30f0ad28 129 if (!myResources.Texture.IsNull())
bf75be98 130 {
10b9c7df 131 if (theContext)
bf75be98 132 {
30f0ad28 133 if (myResources.TextureId.IsEmpty())
bf75be98 134 {
30f0ad28 135 theContext->DelayedRelease (myResources.Texture);
bf75be98 136 }
137 else
138 {
30f0ad28 139 myResources.Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
05dd08ce 140 theContext->ReleaseResource (myResources.TextureId, Standard_True);
bf75be98 141 }
142 }
30f0ad28 143 myResources.Texture.Nullify();
bf75be98 144 }
30f0ad28 145 myResources.TextureId.Clear();
f85399e5 146 myResources.ResetTextureReadiness();
30f0ad28 147
392ac980 148 if (!myResources.ShaderProgram.IsNull()
10b9c7df 149 && theContext)
30f0ad28 150 {
392ac980 151 theContext->ShaderManager()->Unregister (myResources.ShaderProgramId,
152 myResources.ShaderProgram);
30f0ad28 153 }
154 myResources.ShaderProgramId.Clear();
f85399e5 155 myResources.ResetShaderReadiness();
5e27df78 156}
fd4a6963 157
158// =======================================================================
30f0ad28 159// function : BuildTexture
fd4a6963 160// purpose :
161// =======================================================================
8625ef7e 162void OpenGl_AspectFace::Resources::BuildTexture (const Handle(OpenGl_Context)& theCtx,
30f0ad28 163 const Handle(Graphic3d_TextureMap)& theTexture)
fd4a6963 164{
30f0ad28 165 // release old texture resource
166 if (!Texture.IsNull())
fd4a6963 167 {
30f0ad28 168 if (TextureId.IsEmpty())
169 {
8625ef7e 170 theCtx->DelayedRelease (Texture);
30f0ad28 171 Texture.Nullify();
172 }
173 else
fd4a6963 174 {
30f0ad28 175 Texture.Nullify(); // we need nullify all handles before ReleaseResource() call
8625ef7e 176 theCtx->ReleaseResource (TextureId, Standard_True);
30f0ad28 177 }
178 }
179
180 TextureId = theTexture.IsNull() ? THE_EMPTY_KEY : theTexture->GetId();
181
182 if (!theTexture.IsNull())
183 {
8625ef7e 184 if (TextureId.IsEmpty() || !theCtx->GetResource<Handle(OpenGl_Texture)> (TextureId, Texture))
30f0ad28 185 {
186 Texture = new OpenGl_Texture (theTexture->GetParams());
187 Handle(Image_PixMap) anImage = theTexture->GetImage();
188 if (!anImage.IsNull())
fd4a6963 189 {
8625ef7e 190 Texture->Init (theCtx, *anImage.operator->(), theTexture->Type());
fd4a6963 191 }
30f0ad28 192 if (!TextureId.IsEmpty())
fd4a6963 193 {
8625ef7e 194 theCtx->ShareResource (TextureId, Texture);
fd4a6963 195 }
196 }
30f0ad28 197 }
198}
fd4a6963 199
30f0ad28 200// =======================================================================
201// function : BuildShader
202// purpose :
203// =======================================================================
8625ef7e 204void OpenGl_AspectFace::Resources::BuildShader (const Handle(OpenGl_Context)& theCtx,
30f0ad28 205 const Handle(Graphic3d_ShaderProgram)& theShader)
206{
4e1523ef 207 if (theCtx->core20fwd == NULL)
392ac980 208 {
30f0ad28 209 return;
392ac980 210 }
30f0ad28 211
212 // release old shader program resources
213 if (!ShaderProgram.IsNull())
214 {
8625ef7e 215 theCtx->ShaderManager()->Unregister (ShaderProgramId, ShaderProgram);
f85399e5 216 ShaderProgramId.Clear();
217 ShaderProgram.Nullify();
30f0ad28 218 }
392ac980 219 if (theShader.IsNull())
30f0ad28 220 {
392ac980 221 return;
30f0ad28 222 }
392ac980 223
8625ef7e 224 theCtx->ShaderManager()->Create (theShader, ShaderProgramId, ShaderProgram);
fd4a6963 225}