1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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 #ifndef _OpenGl_Workspace_Header
17 #define _OpenGl_Workspace_Header
22 #include <Handle_OpenGl_Workspace.hxx>
23 #include <OpenGl_Window.hxx>
25 #include <TColStd_Array2OfReal.hxx>
26 #include <Quantity_Color.hxx>
27 #include <Graphic3d_CView.hxx>
28 #include <Graphic3d_TypeOfComposition.hxx>
29 #include <Graphic3d_TypeOfTexture.hxx>
30 #include <Graphic3d_PtrFrameBuffer.hxx>
31 #include <Graphic3d_BufferType.hxx>
32 #include <Handle_Graphic3d_TextureParams.hxx>
34 #include <Aspect_CLayer2d.hxx>
35 #include <Aspect_Handle.hxx>
36 #include <Aspect_PrintAlgo.hxx>
38 #include <InterfaceGraphic_Graphic3d.hxx>
39 #include <InterfaceGraphic_Visual3d.hxx>
41 #include <NCollection_Sequence.hxx>
43 #include <OpenGl_AspectFace.hxx>
44 #include <OpenGl_Display.hxx>
45 #include <OpenGl_FrameBuffer.hxx>
46 #include <OpenGl_Matrix.hxx>
47 #include <OpenGl_NamedStatus.hxx>
48 #include <OpenGl_PrinterContext.hxx>
49 #include <OpenGl_SceneGeometry.hxx>
50 #include <OpenGl_TextParam.hxx>
51 #include <OpenGl_RenderFilter.hxx>
52 #include <OpenGl_Vec.hxx>
54 #include <Handle_OpenGl_View.hxx>
55 #include <Handle_OpenGl_Texture.hxx>
57 #include <OpenGl_ShaderObject.hxx>
58 #include <OpenGl_ShaderProgram.hxx>
59 #include <OpenGl_TextureBufferArb.hxx>
61 class OpenGl_AspectLine;
62 class OpenGl_AspectMarker;
63 class OpenGl_AspectText;
64 class OpenGl_FrameBuffer;
65 class OpenGl_Structure;
69 //! OpenGL material definition
70 struct OpenGl_Material
73 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
74 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
75 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
76 OpenGl_Vec4 Emission; //!< material emission
77 OpenGl_Vec4 Params; //!< extra packed parameters
79 Standard_ShortReal Shine() const { return Params.x(); }
80 Standard_ShortReal& ChangeShine() { return Params.x(); }
82 Standard_ShortReal Transparency() const { return Params.y(); }
83 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
85 //! Initialize material
86 void Init (const OPENGL_SURF_PROP& theProps);
88 //! Returns packed (serialized) representation of material properties
89 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
90 static Standard_Integer NbOfVec4() { return 5; }
94 //! Represents window with GL context.
95 //! Provides methods to render primitives and maintain GL state.
96 class OpenGl_Workspace : public OpenGl_Window
100 //! Main constructor - prepare GL context for specified window.
101 OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
102 const CALL_DEF_WINDOW& theCWindow,
103 Aspect_RenderingContext theGContext,
104 const Handle(OpenGl_Caps)& theCaps,
105 const Handle(OpenGl_Context)& theShareCtx);
108 virtual ~OpenGl_Workspace();
110 void SetActiveView (const Handle(OpenGl_View)& theView) { myView = theView; }
111 const Handle(OpenGl_View)& ActiveView () const { return myView; }
113 //! Redraw the window.
114 void Redraw (const Graphic3d_CView& theCView,
115 const Aspect_CLayer2d& theCUnderLayer,
116 const Aspect_CLayer2d& theCOverLayer);
118 Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
119 void RedrawImmediate (const Graphic3d_CView& theCView,
120 const Aspect_CLayer2d& theCUnderLayer,
121 const Aspect_CLayer2d& theCOverLayer,
122 const Standard_Boolean theToForce = Standard_False);
124 void Invalidate (const Graphic3d_CView& /*theCView*/)
126 myBackBufferRestored = Standard_False;
129 //! Special method to perform printing.
130 //! System-specific and currently only Win platform implemented.
131 Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext,
132 const Graphic3d_CView& theCView,
133 const Aspect_CLayer2d& theCUnderLayer,
134 const Aspect_CLayer2d& theCOverLayer,
135 const Aspect_Handle theHPrintDC,
136 const Standard_Boolean theToShowBackground,
137 const Standard_CString theFileName,
138 const Aspect_PrintAlgo thePrintAlgorithm,
139 const Standard_Real theScaleFactor);
141 const Handle(OpenGl_PrinterContext)& PrinterContext() const
143 return myPrintContext;
146 void DisplayCallback (const Graphic3d_CView& theCView, int theReason);
148 Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
149 void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
150 Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr,
151 Image_PixMap& theImage,
152 const Graphic3d_BufferType& theBufferType);
154 void UseTransparency (const Standard_Boolean theFlag);
155 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
156 Standard_Boolean& UseDepthTest() { return myUseDepthTest; }
157 Standard_Boolean& UseGLLight() { return myUseGLLight; }
159 //// RELATED TO STATUS ////
161 Standard_Integer NamedStatus;
163 const TEL_COLOUR* HighlightColor;
165 const OpenGl_Matrix* SetViewMatrix (const OpenGl_Matrix* );
166 const OpenGl_Matrix* SetStructureMatrix (const OpenGl_Matrix*, bool aRevert = false);
168 //! Updates current model-view matrix
169 //! replacing it with StructureMatrixT*ViewMatrix from the workspace.
170 const void UpdateModelViewMatrix();
172 const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
173 const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
174 const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
175 const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
177 void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
179 //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
180 Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply);
181 Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply);
182 Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
183 Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply);
184 inline const OpenGl_TextParam* AspectTextParams() const
186 return TextParam_applied;
189 //! Clear the applied aspect state.
190 void ResetAppliedAspect();
192 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
193 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
194 const Handle(Graphic3d_TextureParams)& theParams = NULL);
196 //! Set filter for restricting rendering of particular elements.
197 //! Filter can be applied for rendering passes used by recursive
198 //! rendering algorithms for rendering elements of groups.
199 //! @param theFilter [in] the filter instance.
200 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
202 myRenderFilter = theFilter;
205 //! Get rendering filter.
206 //! @return filter instance.
207 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
209 return myRenderFilter;
212 //! @return applied view matrix.
213 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
215 //! @return applied model structure matrix.
216 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
220 //! Copy content of Back buffer to the Front buffer
221 void copyBackToFront();
223 virtual Standard_Boolean Activate();
225 void redraw1 (const Graphic3d_CView& theCView,
226 const Aspect_CLayer2d& theCUnderLayer,
227 const Aspect_CLayer2d& theCOverLayer,
228 const int theToSwap);
230 void updateMaterial (const int theFlag);
232 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
233 const Handle(Graphic3d_TextureParams)& theParams);
237 //! Result of OpenGL shaders initialization.
238 enum RaytraceInitStatus
245 //! Describes update mode (state).
248 OpenGl_GUM_CHECK, //!< check if geometry update is necessary
249 OpenGl_GUM_PREPARE, //!< collect unchanged objects
250 OpenGl_GUM_UPDATE //!< update raytracing data, rebuild changed objects
253 //! Defines frequently used shader variables.
254 enum ShaderVariableIndex
271 OpenGl_RT_uLightAmbnt,
272 OpenGl_RT_uLightCount,
274 OpenGl_RT_uShadEnabled,
275 OpenGl_RT_uReflEnabled,
277 OpenGl_RT_uInputTexture,
283 OpenGl_RT_uEnvironmentEnable,
285 OpenGl_RT_NbVariables // special field
288 //! Defines texture samplers.
289 enum ShaderSamplerNames
291 OpenGl_RT_SceneNodeInfoTexture = 0,
292 OpenGl_RT_SceneMinPointTexture = 1,
293 OpenGl_RT_SceneMaxPointTexture = 2,
295 OpenGl_RT_ObjectNodeInfoTexture = 3,
296 OpenGl_RT_ObjectMinPointTexture = 4,
297 OpenGl_RT_ObjectMaxPointTexture = 5,
299 OpenGl_RT_GeometryVertexTexture = 6,
300 OpenGl_RT_GeometryNormalTexture = 7,
301 OpenGl_RT_GeometryTriangTexture = 8,
303 OpenGl_RT_EnvironmentMapTexture = 9,
305 OpenGl_RT_RaytraceMaterialTexture = 10,
306 OpenGl_RT_RaytraceLightSrcTexture = 11,
308 OpenGl_RT_FSAAInputTexture = 12,
310 OpenGl_RT_SceneTransformTexture = 13
313 //! Tool class for management of shader sources.
318 //! Creates new uninitialized shader source.
324 //! Creates new shader source from specified file.
325 ShaderSource (const TCollection_AsciiString& theFileName)
327 Load (&theFileName, 1);
332 //! Returns prefix to insert before the source.
333 const TCollection_AsciiString& Prefix() const
338 //! Sets prefix to insert before the source.
339 void SetPrefix (const TCollection_AsciiString& thePrefix)
341 myPrefix = thePrefix;
344 //! Returns shader source combined with prefix.
345 TCollection_AsciiString Source() const;
347 //! Loads shader source from specified files.
348 void Load (const TCollection_AsciiString* theFileNames, const Standard_Integer theCount);
352 TCollection_AsciiString mySource; //!< Source string of the shader object
353 TCollection_AsciiString myPrefix; //!< Prefix to insert before the source
357 //! Default size of traversal stack.
358 static const Standard_Integer THE_DEFAULT_STACK_SIZE = 24;
360 protected: //! @name methods related to ray-tracing
362 //! Updates 3D scene geometry for ray-tracing.
363 Standard_Boolean UpdateRaytraceGeometry (GeomUpdateMode theMode);
365 //! Checks to see if the structure is modified.
366 Standard_Boolean CheckRaytraceStructure (const OpenGl_Structure* theStructure);
368 //! Updates 3D scene light sources for ray-tracing.
369 Standard_Boolean UpdateRaytraceLightSources (const GLdouble theInvModelView[16]);
371 //! Updates environment map for ray-tracing.
372 Standard_Boolean UpdateRaytraceEnvironmentMap();
374 //! Adds OpenGL structure to ray-traced scene geometry.
375 Standard_Boolean AddRaytraceStructure (const OpenGl_Structure* theStructure,
376 const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements);
378 //! Adds OpenGL primitive array to ray-traced scene geometry.
379 OpenGl_TriangleSet* AddRaytracePrimitiveArray (
380 const OpenGl_PrimitiveArray* theArray, int theMatID, const Standard_ShortReal* theTrans);
382 //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
383 Standard_Boolean AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
384 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
386 //! Adds OpenGL triangle array to ray-traced scene geometry.
387 Standard_Boolean AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
388 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
390 //! Adds OpenGL triangle fan array to ray-traced scene geometry.
391 Standard_Boolean AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
392 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
394 //! Adds OpenGL triangle strip array to ray-traced scene geometry.
395 Standard_Boolean AddRaytraceTriangleStripArray (OpenGl_TriangleSet* theSet,
396 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
398 //! Adds OpenGL quadrangle array to ray-traced scene geometry.
399 Standard_Boolean AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
400 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
402 //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
403 Standard_Boolean AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
404 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
406 //! Adds OpenGL polygon array to ray-traced scene geometry.
407 Standard_Boolean AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
408 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
410 //! Loads and compiles shader object from specified source.
411 Handle(OpenGl_ShaderObject) LoadShader (const ShaderSource& theSource, GLenum theType);
413 //! Performs safe exit when shaders initialization fails.
414 Standard_Boolean SafeFailBack (const TCollection_ExtendedString& theMessage);
416 //! Initializes OpenGL/GLSL shader programs.
417 Standard_Boolean InitRaytraceResources();
419 //! Releases OpenGL/GLSL shader programs.
420 void ReleaseRaytraceResources();
422 //! Uploads ray-trace data to the GPU.
423 Standard_Boolean UploadRaytraceData();
425 //! Resizes OpenGL frame buffers.
426 Standard_Boolean ResizeRaytraceBuffers (const Standard_Integer theSizeX,
427 const Standard_Integer theSizeY);
429 //! Generates viewing rays for corners of screen quad.
430 void UpdateCamera (const NCollection_Mat4<GLdouble>& theOrientation,
431 const NCollection_Mat4<GLdouble>& theViewMapping,
432 OpenGl_Vec3 theOrigins[4],
433 OpenGl_Vec3 theDirects[4]);
435 //! Runs ray-tracing shader programs.
436 Standard_Boolean RunRaytraceShaders (const Graphic3d_CView& theCView,
437 const Standard_Integer theSizeX,
438 const Standard_Integer theSizeY,
439 const OpenGl_Vec3 theOrigins[4],
440 const OpenGl_Vec3 theDirects[4],
441 OpenGl_FrameBuffer* theFrameBuffer);
443 //! Redraws the window using OpenGL/GLSL ray-tracing.
444 Standard_Boolean Raytrace (const Graphic3d_CView& theCView,
445 const Standard_Integer theSizeX,
446 const Standard_Integer theSizeY,
447 const Standard_Boolean theToSwap,
448 OpenGl_FrameBuffer* theFrameBuffer);
450 protected: //! @name fields related to ray-tracing
452 //! Result of shaders initialization.
453 RaytraceInitStatus myComputeInitStatus;
455 //! Is geometry data valid?
456 Standard_Boolean myIsRaytraceDataValid;
458 //! 3D scene geometry data for ray-tracing.
459 OpenGl_RaytraceGeometry myRaytraceGeometry;
461 //! Radius of bounding sphere of the scene.
462 Standard_ShortReal myRaytraceSceneRadius;
463 //! Scene epsilon to prevent self-intersections.
464 Standard_ShortReal myRaytraceSceneEpsilon;
466 //! Actual size of traversal stack in shader program.
467 Standard_Integer myTraversalStackSize;
469 //! OpenGL/GLSL source of ray-tracing fragment shader.
470 ShaderSource myRaytraceShaderSource;
471 //! OpenGL/GLSL source of adaptive-AA fragment shader.
472 ShaderSource myPostFSAAShaderSource;
474 //! OpenGL/GLSL ray-tracing fragment shader.
475 Handle(OpenGl_ShaderObject) myRaytraceShader;
476 //! OpenGL/GLSL adaptive-AA fragment shader.
477 Handle(OpenGl_ShaderObject) myPostFSAAShader;
479 //! OpenGL/GLSL ray-tracing shader program.
480 Handle(OpenGl_ShaderProgram) myRaytraceProgram;
481 //! OpenGL/GLSL adaptive-AA shader program.
482 Handle(OpenGl_ShaderProgram) myPostFSAAProgram;
484 //! Texture buffer of data records of high-level BVH nodes.
485 Handle(OpenGl_TextureBufferArb) mySceneNodeInfoTexture;
486 //! Texture buffer of minimum points of high-level BVH nodes.
487 Handle(OpenGl_TextureBufferArb) mySceneMinPointTexture;
488 //! Texture buffer of maximum points of high-level BVH nodes.
489 Handle(OpenGl_TextureBufferArb) mySceneMaxPointTexture;
490 //! Texture buffer of transformations of high-level BVH nodes.
491 Handle(OpenGl_TextureBufferArb) mySceneTransformTexture;
493 //! Texture buffer of data records of bottom-level BVH nodes.
494 Handle(OpenGl_TextureBufferArb) myObjectNodeInfoTexture;
495 //! Texture buffer of minimum points of bottom-level BVH nodes.
496 Handle(OpenGl_TextureBufferArb) myObjectMinPointTexture;
497 //! Texture buffer of maximum points of bottom-level BVH nodes.
498 Handle(OpenGl_TextureBufferArb) myObjectMaxPointTexture;
500 //! Texture buffer of vertex coords.
501 Handle(OpenGl_TextureBufferArb) myGeometryVertexTexture;
502 //! Texture buffer of vertex normals.
503 Handle(OpenGl_TextureBufferArb) myGeometryNormalTexture;
504 //! Texture buffer of triangle indices.
505 Handle(OpenGl_TextureBufferArb) myGeometryTriangTexture;
507 //! Texture buffer of material properties.
508 Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture;
509 //! Texture buffer of light source properties.
510 Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
512 //! Vertex buffer (VBO) for drawing dummy quad.
513 OpenGl_VertexBuffer myRaytraceScreenQuad;
515 //! Framebuffer (FBO) to perform adaptive FSAA.
516 Handle(OpenGl_FrameBuffer) myRaytraceFBO1;
517 //! Framebuffer (FBO) to perform adaptive FSAA.
518 Handle(OpenGl_FrameBuffer) myRaytraceFBO2;
520 //! State of OpenGL view.
521 Standard_Size myViewModificationStatus;
522 //! State of OpenGL layer list.
523 Standard_Size myLayersModificationStatus;
525 //! State of OpenGL structures reflected to ray-tracing.
526 std::map<const OpenGl_Structure*, Standard_Size> myStructureStates;
528 //! PrimitiveArray to TriangleSet map for scene partial update.
529 std::map<const OpenGl_PrimitiveArray*, OpenGl_TriangleSet*> myArrayToTrianglesMap;
531 //! Cached locations of frequently used uniform variables.
532 Standard_Integer myUniformLocations[2][OpenGl_RT_NbVariables];
534 protected: //! @name protected fields
536 Handle(OpenGl_PrinterContext) myPrintContext;
537 Handle(OpenGl_View) myView; // WSViews - now just one view is supported
538 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
539 Standard_Boolean myBackBufferRestored;
540 Standard_Boolean myIsImmediateDrawn; //!< flag indicates that immediate mode buffer contains some data
541 Standard_Boolean myUseTransparency;
542 Standard_Boolean myUseZBuffer;
543 Standard_Boolean myUseDepthTest;
544 Standard_Boolean myUseGLLight;
546 protected: //! @name fields related to status
548 Handle(OpenGl_RenderFilter) myRenderFilter;
549 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
550 const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
551 const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
552 const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
553 const OpenGl_AspectText *AspectText_set, *AspectText_applied;
555 const OpenGl_TextParam *TextParam_set, *TextParam_applied;
557 const OpenGl_Matrix* ViewMatrix_applied;
558 const OpenGl_Matrix* StructureMatrix_applied;
560 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
561 OpenGl_Material myMatBack; //!< current back material state
562 OpenGl_Material myMatTmp; //!< temporary variable
563 TelCullMode myCullingMode; //!< back face culling mode, applied from face aspect
565 //! Model matrix with applied structure transformations
566 OpenGl_Matrix myModelViewMatrix;
568 const TEL_POFFSET_PARAM* PolygonOffset_applied;
570 OpenGl_AspectFace myAspectFaceHl; // Hiddenline aspect
572 public: //! @name type definition
574 DEFINE_STANDARD_RTTI(OpenGl_Workspace)
575 DEFINE_STANDARD_ALLOC
579 #endif // _OpenGl_Workspace_Header