1 // Created on: 2013-08-15
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-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 <NCollection_AlignedAllocator.hxx>
17 #include <OpenGl_CappingPlaneResource.hxx>
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_Vec.hxx>
20 #include <Precision.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_CappingPlaneResource,OpenGl_Resource)
26 //! 12 plane vertices, interleaved:
27 //! - 4 floats, position
28 //! - 4 floats, normal
29 //! - 4 floats, UV texture coordinates
30 static const GLfloat THE_CAPPING_PLN_VERTS[12 * (4 + 4 + 4)] =
32 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
33 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
34 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,
36 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
37 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,
38 -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
40 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
41 -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
42 0.0f, 0.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
44 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
45 0.0f, 0.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
46 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f
49 static const OpenGl_Matrix OpenGl_IdentityMatrix =
52 { { 1.0f, 0.0f, 0.0f, 0.0f },
53 { 0.0f, 1.0f, 0.0f, 0.0f },
54 { 0.0f, 0.0f, 1.0f, 0.0f },
55 { 0.0f, 0.0f, 0.0f, 1.0f } }
61 // =======================================================================
62 // function : OpenGl_CappingPlaneResource
64 // =======================================================================
65 OpenGl_CappingPlaneResource::OpenGl_CappingPlaneResource (const Handle(Graphic3d_ClipPlane)& thePlane)
66 : myPrimitives (NULL),
67 myOrientation (OpenGl_IdentityMatrix),
69 myPlaneRoot (thePlane),
70 myEquationMod ((unsigned int )-1),
71 myAspectMod ((unsigned int )-1)
73 // Fill primitive array
74 Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
75 Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (anAlloc);
76 Graphic3d_Attribute anAttribInfo[] =
78 { Graphic3d_TOA_POS, Graphic3d_TOD_VEC4 },
79 { Graphic3d_TOA_NORM, Graphic3d_TOD_VEC4 },
80 { Graphic3d_TOA_UV, Graphic3d_TOD_VEC4 }
82 if (anAttribs->Init (12, anAttribInfo, 3))
84 memcpy (anAttribs->ChangeData(), THE_CAPPING_PLN_VERTS, sizeof(THE_CAPPING_PLN_VERTS));
85 myPrimitives.InitBuffers (NULL, Graphic3d_TOPA_TRIANGLES, NULL, anAttribs, NULL);
89 // =======================================================================
90 // function : OpenGl_CappingPlaneResource
92 // =======================================================================
93 OpenGl_CappingPlaneResource::~OpenGl_CappingPlaneResource()
98 // =======================================================================
101 // =======================================================================
102 void OpenGl_CappingPlaneResource::Update (const Handle(OpenGl_Context)& ,
103 const Handle(Graphic3d_AspectFillArea3d)& theObjAspect)
106 updateAspect (theObjAspect);
109 // =======================================================================
110 // function : Release
112 // =======================================================================
113 void OpenGl_CappingPlaneResource::Release (OpenGl_Context* theContext)
115 OpenGl_Element::Destroy (theContext, myAspect);
116 myPrimitives.Release (theContext);
117 myEquationMod = (unsigned int )-1;
118 myAspectMod = (unsigned int )-1;
121 // =======================================================================
122 // function : updateAspect
124 // =======================================================================
125 void OpenGl_CappingPlaneResource::updateAspect (const Handle(Graphic3d_AspectFillArea3d)& theObjAspect)
127 if (myAspect == NULL)
129 myAspect = new OpenGl_AspectFace();
130 myAspectMod = myPlaneRoot->MCountAspect() - 1; // mark out of sync
133 if (theObjAspect.IsNull())
135 if (myAspectMod != myPlaneRoot->MCountAspect())
137 myAspect->SetAspect (myPlaneRoot->CappingAspect());
138 myAspectMod = myPlaneRoot->MCountAspect();
143 if (myFillAreaAspect.IsNull())
145 myFillAreaAspect = new Graphic3d_AspectFillArea3d();
147 if (myAspectMod != myPlaneRoot->MCountAspect())
149 *myFillAreaAspect = *myPlaneRoot->CappingAspect();
152 if (myPlaneRoot->ToUseObjectMaterial())
154 // only front material currently supported by capping rendering
155 myFillAreaAspect->SetFrontMaterial (theObjAspect->FrontMaterial());
156 myFillAreaAspect->SetInteriorColor (theObjAspect->InteriorColor());
158 if (myPlaneRoot->ToUseObjectTexture())
160 myFillAreaAspect->SetTextureSet (theObjAspect->TextureSet());
161 if (theObjAspect->ToMapTexture())
163 myFillAreaAspect->SetTextureMapOn();
167 myFillAreaAspect->SetTextureMapOff();
170 if (myPlaneRoot->ToUseObjectShader())
172 myFillAreaAspect->SetShaderProgram (theObjAspect->ShaderProgram());
175 myAspect->SetAspect (myFillAreaAspect);
178 // =======================================================================
179 // function : updateTransform
181 // =======================================================================
182 void OpenGl_CappingPlaneResource::updateTransform()
184 const Graphic3d_ClipPlane::Equation& anEquation = myPlaneRoot->GetEquation();
185 if (myEquationMod == myPlaneRoot->MCountEquation())
187 return; // nothing to update
190 // re-evaluate infinite plane transformation matrix
191 Standard_ShortReal N[3] =
192 { (Standard_ShortReal)anEquation[0],
193 (Standard_ShortReal)anEquation[1],
194 (Standard_ShortReal)anEquation[2] };
196 Standard_ShortReal T[3] =
197 { (Standard_ShortReal)(anEquation[0] * -anEquation[3]),
198 (Standard_ShortReal)(anEquation[1] * -anEquation[3]),
199 (Standard_ShortReal)(anEquation[2] * -anEquation[3]) };
201 Standard_ShortReal L[3] = { 0.0f, 0.0f, 0.0f };
202 Standard_ShortReal F[3] = { 0.0f, 0.0f, 0.0f };
204 // project plane normal onto OX to find left vector
205 Standard_ShortReal aProjLen =
206 sqrt ( (Standard_ShortReal)(anEquation[0] * anEquation[0])
207 + (Standard_ShortReal)(anEquation[2] * anEquation[2]));
208 if (aProjLen < ShortRealSmall())
214 L[0] = N[2] / aProjLen;
215 L[2] = -N[0] / aProjLen;
219 F[0] = (-L[1])*N[2] - (-L[2])*N[1];
220 F[1] = (-L[2])*N[0] - (-L[0])*N[2];
221 F[2] = (-L[0])*N[1] - (-L[1])*N[0];
223 myOrientation.mat[0][0] = L[0];
224 myOrientation.mat[0][1] = L[1];
225 myOrientation.mat[0][2] = L[2];
226 myOrientation.mat[0][3] = 0.0f;
228 myOrientation.mat[1][0] = N[0];
229 myOrientation.mat[1][1] = N[1];
230 myOrientation.mat[1][2] = N[2];
231 myOrientation.mat[1][3] = 0.0f;
233 myOrientation.mat[2][0] = F[0];
234 myOrientation.mat[2][1] = F[1];
235 myOrientation.mat[2][2] = F[2];
236 myOrientation.mat[2][3] = 0.0f;
238 myOrientation.mat[3][0] = T[0];
239 myOrientation.mat[3][1] = T[1];
240 myOrientation.mat[3][2] = T[2];
241 myOrientation.mat[3][3] = 1.0f;
243 myEquationMod = myPlaneRoot->MCountEquation();