0024588: Fix some ICC warning fixes
[occt.git] / src / OpenGl / OpenGl_Workspace.hxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
1981cb22 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa
SK
16#ifndef _OpenGl_Workspace_Header
17#define _OpenGl_Workspace_Header
18
e276548b 19#ifdef HAVE_OPENCL
20 #include <map>
21 #include <set>
22
23 #include <OpenGl_Cl.hxx>
24#endif
25
2166f0fa
SK
26#include <Handle_OpenGl_Workspace.hxx>
27#include <OpenGl_Window.hxx>
28
29#include <TColStd_Array2OfReal.hxx>
30#include <Quantity_Color.hxx>
bf75be98 31#include <Graphic3d_CView.hxx>
2166f0fa 32#include <Graphic3d_TypeOfComposition.hxx>
bf75be98 33#include <Graphic3d_TypeOfTexture.hxx>
34#include <Graphic3d_PtrFrameBuffer.hxx>
35#include <Graphic3d_BufferType.hxx>
36#include <Handle_Graphic3d_TextureParams.hxx>
2166f0fa 37
2166f0fa
SK
38#include <Aspect_CLayer2d.hxx>
39#include <Aspect_Handle.hxx>
40#include <Aspect_PrintAlgo.hxx>
2166f0fa
SK
41
42#include <InterfaceGraphic_Graphic3d.hxx>
43#include <InterfaceGraphic_Visual3d.hxx>
44
1981cb22 45#include <NCollection_Sequence.hxx>
46
2166f0fa
SK
47#include <OpenGl_tsm.hxx>
48
bf75be98 49#include <OpenGl_AspectFace.hxx>
2166f0fa
SK
50#include <OpenGl_Display.hxx>
51#include <OpenGl_Matrix.hxx>
52#include <OpenGl_NamedStatus.hxx>
a174a3c5 53#include <OpenGl_PrinterContext.hxx>
e276548b 54#ifdef HAVE_OPENCL
55 #include <OpenGl_SceneGeometry.hxx>
56#endif
2166f0fa 57#include <OpenGl_TextParam.hxx>
4269bd1b 58#include <OpenGl_RenderFilter.hxx>
0adbd30f 59#include <OpenGl_Vec.hxx>
2166f0fa
SK
60
61#include <Handle_OpenGl_View.hxx>
bf75be98 62#include <Handle_OpenGl_Texture.hxx>
2166f0fa
SK
63
64class OpenGl_AspectLine;
2166f0fa
SK
65class OpenGl_AspectMarker;
66class OpenGl_AspectText;
67class OpenGl_FrameBuffer;
68class OpenGl_Structure;
4269bd1b 69class OpenGl_Element;
692613e5 70class Image_PixMap;
2166f0fa 71
0adbd30f 72//! OpenGL material definition
73struct OpenGl_Material
74{
75
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
81
82 Standard_ShortReal Shine() const { return Params.x(); }
83 Standard_ShortReal& ChangeShine() { return Params.x(); }
84
85 Standard_ShortReal Transparency() const { return Params.y(); }
86 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
87
88 //! Initialize material
89 void Init (const OPENGL_SURF_PROP& theProps);
90
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; }
94
95};
96
e276548b 97//! Represents window with GL context.
98//! Provides methods to render primitives and maintain GL state.
2166f0fa
SK
99class OpenGl_Workspace : public OpenGl_Window
100{
101public:
102
103 //! Main constructor - prepare GL context for specified window.
104 OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
105 const CALL_DEF_WINDOW& theCWindow,
5e27df78 106 Aspect_RenderingContext theGContext,
58655684 107 const Handle(OpenGl_Caps)& theCaps,
5e27df78 108 const Handle(OpenGl_Context)& theShareCtx);
2166f0fa
SK
109
110 //! Destructor
111 virtual ~OpenGl_Workspace();
112
113 void SetActiveView (const Handle(OpenGl_View)& theView) { myView = theView; }
114 const Handle(OpenGl_View)& ActiveView () const { return myView; }
115
116 //! Redraw the window.
117 void Redraw (const Graphic3d_CView& theCView,
118 const Aspect_CLayer2d& theCUnderLayer,
119 const Aspect_CLayer2d& theCOverLayer);
120
298f9ad7 121 //! Deprecated. Simply calls Redraw().
2166f0fa
SK
122 void Update (const Graphic3d_CView& theCView,
123 const Aspect_CLayer2d& theCUnderLayer,
124 const Aspect_CLayer2d& theCOverLayer)
125 {
298f9ad7 126 Redraw (theCView, theCUnderLayer, theCOverLayer);
2166f0fa
SK
127 }
128
129 //! Special method to perform printing.
130 //! System-specific and currently only Win platform implemented.
a174a3c5 131 Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext,
132 const Graphic3d_CView& theCView,
bf75be98 133 const Aspect_CLayer2d& theCUnderLayer,
2166f0fa
SK
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);
140
a174a3c5 141 const Handle(OpenGl_PrinterContext)& PrinterContext() const
142 {
143 return myPrintContext;
144 }
145
2166f0fa
SK
146 void DisplayCallback (const Graphic3d_CView& theCView, int theReason);
147
1981cb22 148 Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
2166f0fa
SK
149 Standard_Boolean BeginAddMode();
150 void EndAddMode();
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();
2166f0fa 158 void DrawStructure (const OpenGl_Structure* theStructure);
2166f0fa
SK
159
160 Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
161 void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
692613e5 162 Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr,
163 Image_PixMap& theImage,
164 const Graphic3d_BufferType& theBufferType);
2166f0fa 165
2166f0fa
SK
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; }
170
171 //// RELATED TO STATUS ////
172
173 Standard_Integer NamedStatus;
174
2166f0fa
SK
175 const TEL_COLOUR* HighlightColor;
176
177 const OpenGl_Matrix* SetViewMatrix (const OpenGl_Matrix* );
30f0ad28 178 const OpenGl_Matrix* SetStructureMatrix (const OpenGl_Matrix*, bool aRevert = false);
2166f0fa 179
0f8c0fb8 180 //! Updates current model-view matrix
181 //! replacing it with StructureMatrixT*ViewMatrix from the workspace.
182 const void UpdateModelViewMatrix();
183
2166f0fa
SK
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);
188
189 void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
190
3946774d 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);
a174a3c5 196 inline const OpenGl_TextParam* AspectTextParams() const
197 {
198 return TextParam_applied;
199 }
2166f0fa 200
34a44cbd 201 //! Clear the applied aspect state.
202 void ResetAppliedAspect();
203
bf75be98 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);
207
4269bd1b 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)
213 {
214 myRenderFilter = theFilter;
215 }
216
217 //! Get rendering filter.
218 //! @return filter instance.
219 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
220 {
221 return myRenderFilter;
222 }
223
224 //! @return applied view matrix.
225 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
226
227 //! @return applied model structure matrix.
228 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
229
2166f0fa
SK
230protected:
231
1981cb22 232 void CopyBuffers (const Standard_Boolean theFrontToBack);
2166f0fa
SK
233
234 virtual Standard_Boolean Activate();
235
236 // TEMPORARY!!!
bf75be98 237 void Redraw1 (const Graphic3d_CView& theCView,
238 const Aspect_CLayer2d& theCUnderLayer,
2166f0fa
SK
239 const Aspect_CLayer2d& theCOverLayer,
240 const int theToSwap);
241
0adbd30f 242 void updateMaterial (const int theFlag);
2166f0fa 243
bf75be98 244 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
245 const Handle(Graphic3d_TextureParams)& theParams);
2166f0fa 246
e276548b 247#ifdef HAVE_OPENCL
248
249public:
250
251 //! Returns information about OpenCL device used for computations.
252 Standard_Boolean GetOpenClDeviceInfo (
253 NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>& theInfo) const;
254
255protected:
256
257 //! Describes result of OpenCL initializing.
258 enum OpenClInitStatus
259 {
260 OpenGl_CLIS_NONE,
261 OpenGl_CLIS_INIT,
262 OpenGl_CLIS_FAIL
263 };
264
265protected: //! @name methods related to ray-tracing
266
267 //! Updates 3D scene geometry for ray-tracing.
268 Standard_Boolean UpdateRaytraceGeometry (Standard_Boolean theCheck);
269
270 //! Checks to see if the structure is modified.
271 Standard_Boolean CheckRaytraceStructure (const OpenGl_Structure* theStructure);
272
273 //! Updates 3D scene light sources for ray-tracing.
274 Standard_Boolean UpdateRaytraceLightSources (const GLdouble theInvModelView[16]);
275
276 //! Updates environment map for ray-tracing.
277 Standard_Boolean UpdateRaytraceEnvironmentMap();
265d4508 278
e276548b 279 //! Adds OpenGL structure to ray-traced scene geometry.
265d4508 280 Standard_Boolean AddRaytraceStructure (const OpenGl_Structure* theStructure,
281 const Standard_ShortReal* theTransform, std::set<const OpenGl_Structure*>& theElements);
e276548b 282
283 //! Adds OpenGL primitive array to ray-traced scene geometry.
265d4508 284 OpenGl_TriangleSet* AddRaytracePrimitiveArray (
285 const CALL_DEF_PARRAY* theArray, int theMatID, const Standard_ShortReal* theTrans);
e276548b 286
287 //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
265d4508 288 Standard_Boolean AddRaytraceVertexIndices (OpenGl_TriangleSet* theSet,
289 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 290
291 //! Adds OpenGL triangle array to ray-traced scene geometry.
265d4508 292 Standard_Boolean AddRaytraceTriangleArray (OpenGl_TriangleSet* theSet,
293 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 294
295 //! Adds OpenGL triangle fan array to ray-traced scene geometry.
265d4508 296 Standard_Boolean AddRaytraceTriangleFanArray (OpenGl_TriangleSet* theSet,
297 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 298
265d4508 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);
e276548b 302
303 //! Adds OpenGL quadrangle array to ray-traced scene geometry.
265d4508 304 Standard_Boolean AddRaytraceQuadrangleArray (OpenGl_TriangleSet* theSet,
305 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 306
307 //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
265d4508 308 Standard_Boolean AddRaytraceQuadrangleStripArray (OpenGl_TriangleSet* theSet,
309 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 310
311 //! Adds OpenGL polygon array to ray-traced scene geometry.
265d4508 312 Standard_Boolean AddRaytracePolygonArray (OpenGl_TriangleSet* theSet,
313 const CALL_DEF_PARRAY* theArray, Standard_Integer theOffset, Standard_Integer theCount, Standard_Integer theMatID);
e276548b 314
315 //! Initializes OpenCL resources.
316 Standard_Boolean InitOpenCL();
317
318 //! Releases OpenCL resources.
319 void ReleaseOpenCL();
320
321 //! Resizes OpenCL output image.
322 Standard_Boolean ResizeRaytraceOutputBuffer (const cl_int theSizeX, const cl_int theSizeY);
323
324 //! Writes scene geometry to OpenCl device.
325 Standard_Boolean WriteRaytraceSceneToDevice();
326
327 //! Runs OpenCL ray-tracing kernels.
265d4508 328 Standard_Boolean RunRaytraceOpenCLKernelsOld (const Graphic3d_CView& theCView,
e276548b 329 const GLfloat theOrigins[16],
330 const GLfloat theDirects[16],
331 const int theSizeX,
332 const int theSizeY);
333
265d4508 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);
340
e276548b 341 //! Redraws the window using OpenCL ray tracing.
342 Standard_Boolean Raytrace (const Graphic3d_CView& theCView,
68333c8f 343 const int theSizeX, const int theSizeY, const Tint theToSwap);
e276548b 344
345protected: //! @name fields related to ray-tracing
346
347 //! Result of OpenCL initialization.
348 OpenClInitStatus myComputeInitStatus;
349 //! Is ATI/AMD OpenCL platform used?
350 Standard_Boolean myIsAmdComputePlatform;
351
352 //! Is geometry data valid?
353 Standard_Boolean myIsRaytraceDataValid;
354 //! Is geometry data musty be updated?
355 Standard_Boolean myToUpdateRaytraceData;
265d4508 356
e276548b 357 //! 3D scene geometry data for ray-tracing.
265d4508 358 OpenGl_RaytraceGeometry myRaytraceGeometry;
e276548b 359
360 //! Radius of bounding sphere of the scene.
265d4508 361 Standard_ShortReal myRaytraceSceneRadius;
e276548b 362 //! Scene epsilon to prevent self-intersections.
265d4508 363 Standard_ShortReal myRaytraceSceneEpsilon;
e276548b 364
365 //! OpenCL context.
366 cl_context myComputeContext;
367 //! OpenCL command queue.
265d4508 368 cl_command_queue myComputeQueue;
e276548b 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;
375
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.
68333c8f 381 cl_mem myRaytraceOutputImageAA;
e276548b 382
68333c8f 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;
e276548b 387
e276548b 388 //! OpenCL buffer of material properties.
389 cl_mem myRaytraceMaterialBuffer;
e276548b 390 //! OpenCL buffer of light source properties.
391 cl_mem myRaytraceLightSourceBuffer;
392
265d4508 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;
399
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;
406
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;
413
e276548b 414 //! State of OpenGL view.
415 Standard_Size myViewModificationStatus;
e276548b 416 //! State of OpenGL layer list.
417 Standard_Size myLayersModificationStatus;
418
265d4508 419 //! State of OpenGL structures reflected to ray-tracing.
e276548b 420 std::map<const OpenGl_Structure*, Standard_Size> myStructureStates;
421
422#endif // HAVE_OPENCL
423
bf75be98 424protected: //! @name protected fields
2166f0fa 425
a174a3c5 426 Handle(OpenGl_PrinterContext) myPrintContext;
bf75be98 427 Handle(OpenGl_View) myView; // WSViews - now just one view is supported
bf75be98 428 Standard_Boolean myIsTransientOpen; // transientOpen
1981cb22 429 Standard_Boolean myRetainMode;
430 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
431
432 NCollection_Sequence<const OpenGl_Structure*> myTransientList;
2166f0fa 433
bf75be98 434 Standard_Boolean myUseTransparency;
435 Standard_Boolean myUseZBuffer;
436 Standard_Boolean myUseDepthTest;
437 Standard_Boolean myUseGLLight;
438 Standard_Boolean myBackBufferRestored;
2166f0fa 439
bf75be98 440protected: //! @name fields related to status
441
4269bd1b 442 Handle(OpenGl_RenderFilter) myRenderFilter;
bf75be98 443 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
2166f0fa
SK
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;
448
449 const OpenGl_TextParam *TextParam_set, *TextParam_applied;
450
451 const OpenGl_Matrix* ViewMatrix_applied;
452 const OpenGl_Matrix* StructureMatrix_applied;
453
3b1817a9 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
0adbd30f 458
0f8c0fb8 459 //! Model matrix with applied structure transformations
460 OpenGl_Matrix myModelViewMatrix;
461
2166f0fa
SK
462 const TEL_POFFSET_PARAM* PolygonOffset_applied;
463
bf75be98 464 OpenGl_AspectFace myAspectFaceHl; // Hiddenline aspect
465
466public: //! @name type definition
2166f0fa 467
bf75be98 468 DEFINE_STANDARD_RTTI(OpenGl_Workspace)
1c35b92f 469 DEFINE_STANDARD_ALLOC
2166f0fa
SK
470
471};
472
bf75be98 473#endif // _OpenGl_Workspace_Header