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
8 // under the terms of the GNU Lesser General Public 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
23 #include <OpenGl_Cl.hxx>
26 #include <Handle_OpenGl_Workspace.hxx>
27 #include <OpenGl_Window.hxx>
29 #include <TColStd_Array2OfReal.hxx>
30 #include <Quantity_Color.hxx>
31 #include <Graphic3d_CView.hxx>
32 #include <Graphic3d_TypeOfComposition.hxx>
33 #include <Graphic3d_TypeOfTexture.hxx>
34 #include <Graphic3d_PtrFrameBuffer.hxx>
35 #include <Graphic3d_BufferType.hxx>
36 #include <Handle_Graphic3d_TextureParams.hxx>
38 #include <Aspect_CLayer2d.hxx>
39 #include <Aspect_Handle.hxx>
40 #include <Aspect_PrintAlgo.hxx>
42 #include <InterfaceGraphic_Graphic3d.hxx>
43 #include <InterfaceGraphic_Visual3d.hxx>
45 #include <NCollection_Sequence.hxx>
47 #include <OpenGl_tsm.hxx>
49 #include <OpenGl_AspectFace.hxx>
50 #include <OpenGl_Display.hxx>
51 #include <OpenGl_Matrix.hxx>
52 #include <OpenGl_NamedStatus.hxx>
53 #include <OpenGl_PrinterContext.hxx>
55 #include <OpenGl_SceneGeometry.hxx>
57 #include <OpenGl_TextParam.hxx>
58 #include <OpenGl_RenderFilter.hxx>
59 #include <OpenGl_Vec.hxx>
61 #include <Handle_OpenGl_View.hxx>
62 #include <Handle_OpenGl_Texture.hxx>
64 class OpenGl_AspectLine;
65 class OpenGl_AspectMarker;
66 class OpenGl_AspectText;
67 class OpenGl_FrameBuffer;
68 class OpenGl_Structure;
72 //! OpenGL material definition
73 struct OpenGl_Material
76 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
77 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
78 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
79 OpenGl_Vec4 Emission; //!< material emission
80 OpenGl_Vec4 Params; //!< extra packed parameters
82 Standard_ShortReal Shine() const { return Params.x(); }
83 Standard_ShortReal& ChangeShine() { return Params.x(); }
85 Standard_ShortReal Transparency() const { return Params.y(); }
86 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
88 //! Initialize material
89 void Init (const OPENGL_SURF_PROP& theProps);
91 //! Returns packed (serialized) representation of material properties
92 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
93 static Standard_Integer NbOfVec4() { return 5; }
97 //! Represents window with GL context.
98 //! Provides methods to render primitives and maintain GL state.
99 class OpenGl_Workspace : public OpenGl_Window
103 //! Main constructor - prepare GL context for specified window.
104 OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
105 const CALL_DEF_WINDOW& theCWindow,
106 Aspect_RenderingContext theGContext,
107 const Handle(OpenGl_Caps)& theCaps,
108 const Handle(OpenGl_Context)& theShareCtx);
111 virtual ~OpenGl_Workspace();
113 void SetActiveView (const Handle(OpenGl_View)& theView) { myView = theView; }
114 const Handle(OpenGl_View)& ActiveView () const { return myView; }
116 //! Redraw the window.
117 void Redraw (const Graphic3d_CView& theCView,
118 const Aspect_CLayer2d& theCUnderLayer,
119 const Aspect_CLayer2d& theCOverLayer);
121 //! Deprecated. Simply calls Redraw().
122 void Update (const Graphic3d_CView& theCView,
123 const Aspect_CLayer2d& theCUnderLayer,
124 const Aspect_CLayer2d& theCOverLayer)
126 Redraw (theCView, theCUnderLayer, theCOverLayer);
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 Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
149 Standard_Boolean BeginAddMode();
151 void ClearImmediatMode (const Graphic3d_CView& theCView,
152 const Standard_Boolean theToFlush);
153 void RedrawImmediatMode();
154 Standard_Boolean BeginImmediatMode (const Graphic3d_CView& theCView,
155 const Standard_Boolean theUseDepthTest,
156 const Standard_Boolean theRetainMode);
157 void EndImmediatMode();
158 void DrawStructure (const OpenGl_Structure* theStructure);
160 Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
161 void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
162 Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr,
163 Image_PixMap& theImage,
164 const Graphic3d_BufferType& theBufferType);
166 void UseTransparency (const Standard_Boolean theFlag);
167 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
168 Standard_Boolean& UseDepthTest() { return myUseDepthTest; }
169 Standard_Boolean& UseGLLight() { return myUseGLLight; }
171 //// RELATED TO STATUS ////
173 Standard_Integer NamedStatus;
175 const TEL_COLOUR* HighlightColor;
177 const OpenGl_Matrix* SetViewMatrix (const OpenGl_Matrix* );
178 const OpenGl_Matrix* SetStructureMatrix (const OpenGl_Matrix*, bool aRevert = false);
180 //! Updates current model-view matrix
181 //! replacing it with StructureMatrixT*ViewMatrix from the workspace.
182 const void UpdateModelViewMatrix();
184 const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
185 const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
186 const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
187 const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
189 void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
191 //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
192 Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply);
193 Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply);
194 Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
195 Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply);
196 inline const OpenGl_TextParam* AspectTextParams() const
198 return TextParam_applied;
201 //! Clear the applied aspect state.
202 void ResetAppliedAspect();
204 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
205 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
206 const Handle(Graphic3d_TextureParams)& theParams = NULL);
208 //! Set filter for restricting rendering of particular elements.
209 //! Filter can be applied for rendering passes used by recursive
210 //! rendering algorithms for rendering elements of groups.
211 //! @param theFilter [in] the filter instance.
212 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
214 myRenderFilter = theFilter;
217 //! Get rendering filter.
218 //! @return filter instance.
219 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
221 return myRenderFilter;
224 //! @return applied view matrix.
225 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
227 //! @return applied model structure matrix.
228 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
232 void CopyBuffers (const Standard_Boolean theFrontToBack);
234 virtual Standard_Boolean Activate();
237 void Redraw1 (const Graphic3d_CView& theCView,
238 const Aspect_CLayer2d& theCUnderLayer,
239 const Aspect_CLayer2d& theCOverLayer,
240 const int theToSwap);
242 void updateMaterial (const int theFlag);
244 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
245 const Handle(Graphic3d_TextureParams)& theParams);
251 //! Returns information about OpenCL device used for computations.
252 Standard_Boolean GetOpenClDeviceInfo (
253 NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>& theInfo) const;
257 //! Describes result of OpenCL initializing.
258 enum OpenClInitStatus
265 protected: //! @name methods related to ray-tracing
267 //! Updates 3D scene geometry for ray-tracing.
268 Standard_Boolean UpdateRaytraceGeometry (Standard_Boolean theCheck);
270 //! Checks to see if the structure is modified.
271 Standard_Boolean CheckRaytraceStructure (const OpenGl_Structure* theStructure);
273 //! Updates 3D scene light sources for ray-tracing.
274 Standard_Boolean UpdateRaytraceLightSources (const GLdouble theInvModelView[16]);
276 //! Updates environment map for ray-tracing.
277 Standard_Boolean UpdateRaytraceEnvironmentMap();
279 //! Adds OpenGL structure to ray-traced scene geometry.
280 Standard_Boolean AddRaytraceStructure (const OpenGl_Structure* theStructure,
281 const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements);
283 //! Adds OpenGL primitive array to ray-traced scene geometry.
284 OpenGl_TriangleSet* AddRaytracePrimitiveArray (
285 const CALL_DEF_PARRAY* theArray, int theMatID, const Standard_ShortReal* theTrans);
287 //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
288 Standard_Boolean AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
289 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
291 //! Adds OpenGL triangle array to ray-traced scene geometry.
292 Standard_Boolean AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
293 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
295 //! Adds OpenGL triangle fan array to ray-traced scene geometry.
296 Standard_Boolean AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
297 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
299 //! Adds OpenGL triangle strip array to ray-traced scene geometry.
300 Standard_Boolean AddRaytraceTriangleStripArray (OpenGl_TriangleSet* theSet,
301 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
303 //! Adds OpenGL quadrangle array to ray-traced scene geometry.
304 Standard_Boolean AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
305 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
307 //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
308 Standard_Boolean AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
309 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
311 //! Adds OpenGL polygon array to ray-traced scene geometry.
312 Standard_Boolean AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
313 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
315 //! Initializes OpenCL resources.
316 Standard_Boolean InitOpenCL();
318 //! Releases OpenCL resources.
319 void ReleaseOpenCL();
321 //! Resizes OpenCL output image.
322 Standard_Boolean ResizeRaytraceOutputBuffer (const cl_int theSizeX, const cl_int theSizeY);
324 //! Writes scene geometry to OpenCl device.
325 Standard_Boolean WriteRaytraceSceneToDevice();
327 //! Runs OpenCL ray-tracing kernels.
328 Standard_Boolean RunRaytraceOpenCLKernelsOld (const Graphic3d_CView& theCView,
329 const GLfloat theOrigins[16],
330 const GLfloat theDirects[16],
334 //! Launches OpenCL ray-tracing kernels.
335 Standard_Boolean RunRaytraceOpenCLKernels (const Graphic3d_CView& theCView,
336 const Standard_ShortReal theOrigins[16],
337 const Standard_ShortReal theDirects[16],
338 const Standard_Integer theSizeX,
339 const Standard_Integer theSizeY);
341 //! Redraws the window using OpenCL ray tracing.
342 Standard_Boolean Raytrace (const Graphic3d_CView& theCView,
343 const int theSizeX, const int theSizeY, const Tint theToSwap);
345 protected: //! @name fields related to ray-tracing
347 //! Result of OpenCL initialization.
348 OpenClInitStatus myComputeInitStatus;
349 //! Is ATI/AMD OpenCL platform used?
350 Standard_Boolean myIsAmdComputePlatform;
352 //! Is geometry data valid?
353 Standard_Boolean myIsRaytraceDataValid;
354 //! Is geometry data musty be updated?
355 Standard_Boolean myToUpdateRaytraceData;
357 //! 3D scene geometry data for ray-tracing.
358 OpenGl_RaytraceGeometry myRaytraceGeometry;
360 //! Radius of bounding sphere of the scene.
361 Standard_ShortReal myRaytraceSceneRadius;
362 //! Scene epsilon to prevent self-intersections.
363 Standard_ShortReal myRaytraceSceneEpsilon;
366 cl_context myComputeContext;
367 //! OpenCL command queue.
368 cl_command_queue myComputeQueue;
369 //! OpenCL computing program.
370 cl_program myRaytraceProgram;
371 //! OpenCL ray-tracing render kernel.
372 cl_kernel myRaytraceRenderKernel;
373 //! OpenCL adaptive anti-aliasing kernel.
374 cl_kernel myRaytraceSmoothKernel;
376 //! OpenCL image to store environment map.
377 cl_mem myRaytraceEnvironment;
378 //! OpenCL image to store rendering result.
379 cl_mem myRaytraceOutputImage;
380 //! OpenCL image to store anti-aliasing result.
381 cl_mem myRaytraceOutputImageAA;
383 //! OpenGL texture to store rendering result.
384 Handle(OpenGl_Texture) myRaytraceOutputTexture;
385 //! OpenGL texture to store anti-aliasing result.
386 Handle(OpenGl_Texture) myRaytraceOutputTextureAA;
388 //! OpenCL buffer of material properties.
389 cl_mem myRaytraceMaterialBuffer;
390 //! OpenCL buffer of light source properties.
391 cl_mem myRaytraceLightSourceBuffer;
393 //! OpenCL buffer of vertex coords.
394 cl_mem myGeometryVertexBuffer;
395 //! OpenCL buffer of vertex normals.
396 cl_mem myGeometryNormalBuffer;
397 //! OpenCL buffer of triangle indices.
398 cl_mem myGeometryTriangBuffer;
400 //! OpenCL buffer of data records of high-level BVH nodes.
401 cl_mem mySceneNodeInfoBuffer;
402 //! OpenCL buffer of minimum points of high-level BVH nodes.
403 cl_mem mySceneMinPointBuffer;
404 //! OpenCL buffer of maximum points of high-level BVH nodes.
405 cl_mem mySceneMaxPointBuffer;
407 //! OpenCL buffer of data records of bottom-level BVH nodes.
408 cl_mem myObjectNodeInfoBuffer;
409 //! OpenCL buffer of minimum points of bottom-level BVH nodes.
410 cl_mem myObjectMinPointBuffer;
411 //! OpenCL buffer of maximum points of bottom-level BVH nodes.
412 cl_mem myObjectMaxPointBuffer;
414 //! State of OpenGL view.
415 Standard_Size myViewModificationStatus;
416 //! State of OpenGL layer list.
417 Standard_Size myLayersModificationStatus;
419 //! State of OpenGL structures reflected to ray-tracing.
420 std::map<const OpenGl_Structure*, Standard_Size> myStructureStates;
422 #endif // HAVE_OPENCL
424 protected: //! @name protected fields
426 Handle(OpenGl_PrinterContext) myPrintContext;
427 Handle(OpenGl_View) myView; // WSViews - now just one view is supported
428 Standard_Boolean myIsTransientOpen; // transientOpen
429 Standard_Boolean myRetainMode;
430 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
432 NCollection_Sequence<const OpenGl_Structure*> myTransientList;
434 Standard_Boolean myUseTransparency;
435 Standard_Boolean myUseZBuffer;
436 Standard_Boolean myUseDepthTest;
437 Standard_Boolean myUseGLLight;
438 Standard_Boolean myBackBufferRestored;
440 protected: //! @name fields related to status
442 Handle(OpenGl_RenderFilter) myRenderFilter;
443 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
444 const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
445 const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
446 const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
447 const OpenGl_AspectText *AspectText_set, *AspectText_applied;
449 const OpenGl_TextParam *TextParam_set, *TextParam_applied;
451 const OpenGl_Matrix* ViewMatrix_applied;
452 const OpenGl_Matrix* StructureMatrix_applied;
454 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
455 OpenGl_Material myMatBack; //!< current back material state
456 OpenGl_Material myMatTmp; //!< temporary variable
457 TelCullMode myCullingMode; //!< back face culling mode, applied from face aspect
459 //! Model matrix with applied structure transformations
460 OpenGl_Matrix myModelViewMatrix;
462 const TEL_POFFSET_PARAM* PolygonOffset_applied;
464 OpenGl_AspectFace myAspectFaceHl; // Hiddenline aspect
466 public: //! @name type definition
468 DEFINE_STANDARD_RTTI(OpenGl_Workspace)
469 DEFINE_STANDARD_ALLOC
473 #endif // _OpenGl_Workspace_Header