1 // Created on: 2015-01-16
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2015 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 <OpenGl_BackgroundArray.hxx>
18 #include <Aspect_FillMethod.hxx>
19 #include <NCollection_AlignedAllocator.hxx>
20 #include <OpenGl_Texture.hxx>
21 #include <OpenGl_View.hxx>
22 #include <Graphic3d_TextureParams.hxx>
24 // =======================================================================
25 // method : Constructor
27 // =======================================================================
28 OpenGl_BackgroundArray::OpenGl_BackgroundArray (const Graphic3d_TypeOfBackground theType)
29 : OpenGl_PrimitiveArray (NULL, Graphic3d_TOPA_TRIANGLESTRIPS, NULL, NULL, NULL),
31 myFillMethod (Aspect_FM_NONE),
34 myToUpdate (Standard_False)
36 Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
37 myAttribs = new Graphic3d_Buffer (anAlloc);
39 myDrawMode = GL_TRIANGLE_STRIP;
41 myGradientParams.color1 = OpenGl_Vec4 (0.0f, 0.0f, 0.0f, 1.0f);
42 myGradientParams.color2 = OpenGl_Vec4 (0.0f, 0.0f, 0.0f, 1.0f);
43 myGradientParams.type = Aspect_GFM_NONE;
45 myTrsfPers.Flags = Graphic3d_TMF_2d;
46 if (myType == Graphic3d_TOB_TEXTURE)
48 myTrsfPers.Point.x() = 0.0;
49 myTrsfPers.Point.y() = 0.0;
53 myTrsfPers.Point.x() = -1.0;
54 myTrsfPers.Point.y() = -1.0;
58 // =======================================================================
59 // method : SetTextureParameters
61 // =======================================================================
62 void OpenGl_BackgroundArray::SetTextureParameters (const Aspect_FillMethod theFillMethod)
64 if (myType != Graphic3d_TOB_TEXTURE)
69 myFillMethod = theFillMethod;
73 // =======================================================================
74 // method : SetTextureFillMethod
76 // =======================================================================
77 void OpenGl_BackgroundArray::SetTextureFillMethod (const Aspect_FillMethod theFillMethod)
79 myFillMethod = theFillMethod;
83 // =======================================================================
84 // method : SetGradientParameters
86 // =======================================================================
87 void OpenGl_BackgroundArray::SetGradientParameters (const Quantity_Color& theColor1,
88 const Quantity_Color& theColor2,
89 const Aspect_GradientFillMethod theType)
91 if (myType != Graphic3d_TOB_GRADIENT)
96 Standard_Real anR, aG, aB;
97 theColor1.Values (anR, aG, aB, Quantity_TOC_RGB);
98 myGradientParams.color1 = OpenGl_Vec4 ((float)anR, (float)aG, (float)aB, 0.0f);
100 theColor2.Values (anR, aG, aB, Quantity_TOC_RGB);
101 myGradientParams.color2 = OpenGl_Vec4 ((float)anR, (float)aG, (float)aB, 0.0f);
103 myGradientParams.type = theType;
107 // =======================================================================
108 // method : SetGradientFillMethod
110 // =======================================================================
111 void OpenGl_BackgroundArray::SetGradientFillMethod (const Aspect_GradientFillMethod theType)
113 if (myType != Graphic3d_TOB_GRADIENT)
118 myGradientParams.type = theType;
122 // =======================================================================
123 // method : IsDefined
125 // =======================================================================
126 bool OpenGl_BackgroundArray::IsDefined() const
130 case Graphic3d_TOB_GRADIENT: return myGradientParams.type != Aspect_GFM_NONE;
131 case Graphic3d_TOB_TEXTURE: return myFillMethod != Aspect_FM_NONE;
132 case Graphic3d_TOB_NONE: return Standard_False;
134 return Standard_False;
137 // =======================================================================
138 // method : invalidateData
140 // =======================================================================
141 void OpenGl_BackgroundArray::invalidateData()
143 myToUpdate = Standard_True;
146 // =======================================================================
149 // =======================================================================
150 Standard_Boolean OpenGl_BackgroundArray::init (const Handle(OpenGl_Workspace)& theWorkspace) const
154 case Graphic3d_TOB_GRADIENT:
156 if (!createGradientArray())
158 return Standard_False;
162 case Graphic3d_TOB_TEXTURE:
164 if (!createTextureArray (theWorkspace))
166 return Standard_False;
170 case Graphic3d_TOB_NONE:
173 return Standard_False;
178 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
181 clearMemoryGL (aCtx);
183 buildVBO (aCtx, Standard_True);
184 myIsVboInit = Standard_True;
186 // Data is up-to-date
187 myToUpdate = Standard_False;
188 return Standard_True;
191 // =======================================================================
192 // method : createGradientArray
194 // =======================================================================
195 Standard_Boolean OpenGl_BackgroundArray::createGradientArray() const
197 // Initialize data for primitive array
198 Graphic3d_Attribute aGragientAttribInfo[] =
200 { Graphic3d_TOA_POS, Graphic3d_TOD_VEC2 },
201 { Graphic3d_TOA_COLOR, Graphic3d_TOD_VEC3 }
204 if (!myAttribs->Init (4, aGragientAttribInfo, 2))
206 return Standard_False;
209 OpenGl_Vec2 aVertices[4] =
211 OpenGl_Vec2(float(myViewWidth), 0.0f),
212 OpenGl_Vec2(float(myViewWidth), float(myViewHeight)),
213 OpenGl_Vec2(0.0f, 0.0f),
214 OpenGl_Vec2(0.0f, float(myViewHeight))
217 float* aCorners[4] = {};
218 float aDiagCorner1[3] = {};
219 float aDiagCorner2[3] = {};
221 switch (myGradientParams.type)
225 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
226 aCorners[1] = myGradientParams.color2.xyz().ChangeData();
227 aCorners[2] = myGradientParams.color1.xyz().ChangeData();
228 aCorners[3] = myGradientParams.color1.xyz().ChangeData();
233 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
234 aCorners[1] = myGradientParams.color1.xyz().ChangeData();
235 aCorners[2] = myGradientParams.color2.xyz().ChangeData();
236 aCorners[3] = myGradientParams.color1.xyz().ChangeData();
239 case Aspect_GFM_DIAG1:
241 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
242 aCorners[3] = myGradientParams.color1.xyz().ChangeData();
243 aDiagCorner1[0] = aDiagCorner2[0] = 0.5f * (aCorners[0][0] + aCorners[3][0]);
244 aDiagCorner1[1] = aDiagCorner2[1] = 0.5f * (aCorners[0][1] + aCorners[3][1]);
245 aDiagCorner1[2] = aDiagCorner2[2] = 0.5f * (aCorners[0][2] + aCorners[3][2]);
246 aCorners[1] = aDiagCorner1;
247 aCorners[2] = aDiagCorner2;
250 case Aspect_GFM_DIAG2:
252 aCorners[1] = myGradientParams.color1.xyz().ChangeData();
253 aCorners[2] = myGradientParams.color2.xyz().ChangeData();
254 aDiagCorner1[0] = aDiagCorner2[0] = 0.5f * (aCorners[1][0] + aCorners[2][0]);
255 aDiagCorner1[1] = aDiagCorner2[1] = 0.5f * (aCorners[1][1] + aCorners[2][1]);
256 aDiagCorner1[2] = aDiagCorner2[2] = 0.5f * (aCorners[1][2] + aCorners[2][2]);
257 aCorners[0] = aDiagCorner1;
258 aCorners[3] = aDiagCorner2;
261 case Aspect_GFM_CORNER1:
263 aVertices[0] = OpenGl_Vec2(float(myViewWidth), float(myViewHeight));
264 aVertices[1] = OpenGl_Vec2(0.0f, float(myViewHeight));
265 aVertices[2] = OpenGl_Vec2(float(myViewWidth), 0.0f);
266 aVertices[3] = OpenGl_Vec2(0.0f, 0.0f);
268 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
269 aCorners[1] = myGradientParams.color1.xyz().ChangeData();
270 aCorners[2] = myGradientParams.color2.xyz().ChangeData();
271 aCorners[3] = myGradientParams.color2.xyz().ChangeData();
274 case Aspect_GFM_CORNER2:
276 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
277 aCorners[1] = myGradientParams.color1.xyz().ChangeData();
278 aCorners[2] = myGradientParams.color2.xyz().ChangeData();
279 aCorners[3] = myGradientParams.color2.xyz().ChangeData();
282 case Aspect_GFM_CORNER3:
284 aVertices[0] = OpenGl_Vec2(float(myViewWidth), float(myViewHeight));
285 aVertices[1] = OpenGl_Vec2(0.0f, float(myViewHeight));
286 aVertices[2] = OpenGl_Vec2(float(myViewWidth), 0.0f);
287 aVertices[3] = OpenGl_Vec2(0.0f, 0.0f);
289 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
290 aCorners[1] = myGradientParams.color2.xyz().ChangeData();
291 aCorners[2] = myGradientParams.color1.xyz().ChangeData();
292 aCorners[3] = myGradientParams.color2.xyz().ChangeData();
295 case Aspect_GFM_CORNER4:
297 aCorners[0] = myGradientParams.color2.xyz().ChangeData();
298 aCorners[1] = myGradientParams.color2.xyz().ChangeData();
299 aCorners[2] = myGradientParams.color1.xyz().ChangeData();
300 aCorners[3] = myGradientParams.color2.xyz().ChangeData();
303 case Aspect_GFM_NONE:
309 for (Standard_Integer anIt = 0; anIt < 4; ++anIt)
311 OpenGl_Vec2* aVertData = reinterpret_cast<OpenGl_Vec2* >(myAttribs->changeValue (anIt));
312 *aVertData = aVertices[anIt];
314 OpenGl_Vec3* aColorData = reinterpret_cast<OpenGl_Vec3* >(myAttribs->changeValue (anIt) + myAttribs->AttributeOffset (1));
315 *aColorData = OpenGl_Vec3(aCorners[anIt][0], aCorners[anIt][1], aCorners[anIt][2]);
318 return Standard_True;
321 // =======================================================================
322 // method : createTextureArray
324 // =======================================================================
325 Standard_Boolean OpenGl_BackgroundArray::createTextureArray (const Handle(OpenGl_Workspace)& theWorkspace) const
327 Graphic3d_Attribute aTextureAttribInfo[] =
329 { Graphic3d_TOA_POS, Graphic3d_TOD_VEC2 },
330 { Graphic3d_TOA_UV, Graphic3d_TOD_VEC2 }
333 if (!myAttribs->Init (4, aTextureAttribInfo, 2))
335 return Standard_False;
338 GLfloat aTexRangeX = 1.0f; // texture <s> coordinate
339 GLfloat aTexRangeY = 1.0f; // texture <t> coordinate
341 // Set up for stretching or tiling
342 GLfloat anOffsetX = 0.5f * (float )myViewWidth;
343 GLfloat anOffsetY = 0.5f * (float )myViewHeight;
345 // Setting this coefficient to -1.0f allows to tile textures relatively to the top-left corner of the view
346 // (value 1.0f corresponds to the initial behavior - tiling from the bottom-left corner)
347 GLfloat aCoef = -1.0f;
349 // Get texture parameters
350 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
351 const OpenGl_AspectFace* anAspectFace = theWorkspace->AspectFace();
352 GLfloat aTextureWidth = (GLfloat )anAspectFace->TextureRes (aCtx)->SizeX();
353 GLfloat aTextureHeight = (GLfloat )anAspectFace->TextureRes (aCtx)->SizeY();
355 if (myFillMethod == Aspect_FM_CENTERED)
357 anOffsetX = 0.5f * aTextureWidth;
358 anOffsetY = 0.5f * aTextureHeight;
360 else if (myFillMethod == Aspect_FM_TILED)
362 aTexRangeX = (GLfloat )myViewWidth / aTextureWidth;
363 aTexRangeY = (GLfloat )myViewHeight / aTextureHeight;
366 // NOTE: texture is mapped using GL_REPEAT wrapping mode so integer part
367 // is simply ignored, and negative multiplier is here for convenience only
368 // and does not result e.g. in texture mirroring
371 OpenGl_Vec2* aData = reinterpret_cast<OpenGl_Vec2* >(myAttribs->changeValue (0));
372 aData[0] = OpenGl_Vec2 (anOffsetX, -aCoef * anOffsetY);
373 aData[1] = OpenGl_Vec2 (aTexRangeX, 0.0f);
375 aData = reinterpret_cast<OpenGl_Vec2* >(myAttribs->changeValue (1));
376 aData[0] = OpenGl_Vec2 (anOffsetX, aCoef * anOffsetY);
377 aData[1] = OpenGl_Vec2 (aTexRangeX, aCoef * aTexRangeY);
379 aData = reinterpret_cast<OpenGl_Vec2* >(myAttribs->changeValue (2));
380 aData[0] = OpenGl_Vec2 (-anOffsetX, -aCoef * anOffsetY);
381 aData[1] = OpenGl_Vec2 (0.0f, 0.0f);
383 aData = reinterpret_cast<OpenGl_Vec2* >(myAttribs->changeValue (3));
384 aData[0] = OpenGl_Vec2 (-anOffsetX, aCoef * anOffsetY);
385 aData[1] = OpenGl_Vec2 (0.0f, aCoef * aTexRangeY);
387 return Standard_True;
390 // =======================================================================
391 // method : createTextureArray
393 // =======================================================================
394 void OpenGl_BackgroundArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
396 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
397 Standard_Integer aViewSizeX = aCtx->Viewport()[2];
398 Standard_Integer aViewSizeY = aCtx->Viewport()[3];
399 if (theWorkspace->View()->Camera()->Tile().IsValid())
401 aViewSizeX = theWorkspace->View()->Camera()->Tile().TotalSize.x();
402 aViewSizeY = theWorkspace->View()->Camera()->Tile().TotalSize.y();
405 || myViewWidth != aViewSizeX
406 || myViewHeight != aViewSizeY)
408 myViewWidth = aViewSizeX;
409 myViewHeight = aViewSizeY;
413 OpenGl_Mat4 aProjection = aCtx->ProjectionState.Current();
414 OpenGl_Mat4 aWorldView = aCtx->WorldViewState.Current();
415 myTrsfPers.Apply (theWorkspace->View()->Camera(), aProjection, aWorldView,
416 aCtx->Viewport()[2], aCtx->Viewport()[3]);
418 aCtx->ProjectionState.Push();
419 aCtx->WorldViewState.Push();
420 aCtx->ProjectionState.SetCurrent (aProjection);
421 aCtx->WorldViewState.SetCurrent (aWorldView);
422 aCtx->ApplyProjectionMatrix();
423 aCtx->ApplyModelViewMatrix();
425 OpenGl_PrimitiveArray::Render (theWorkspace);
427 aCtx->ProjectionState.Pop();
428 aCtx->WorldViewState.Pop();
429 aCtx->ApplyProjectionMatrix();