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 <OpenGl_Window.hxx>
24 #include <TColStd_Array2OfReal.hxx>
25 #include <Quantity_Color.hxx>
26 #include <Graphic3d_CView.hxx>
27 #include <Graphic3d_TypeOfComposition.hxx>
28 #include <Graphic3d_TypeOfTexture.hxx>
29 #include <Graphic3d_PtrFrameBuffer.hxx>
30 #include <Graphic3d_BufferType.hxx>
32 #include <Aspect_CLayer2d.hxx>
33 #include <Aspect_Handle.hxx>
34 #include <Aspect_PrintAlgo.hxx>
35 #include <Aspect_PolygonOffsetMode.hxx>
37 #include <InterfaceGraphic_Graphic3d.hxx>
38 #include <InterfaceGraphic_Visual3d.hxx>
40 #include <NCollection_Sequence.hxx>
42 #include <OpenGl_AspectFace.hxx>
43 #include <OpenGl_FrameBuffer.hxx>
44 #include <OpenGl_Matrix.hxx>
45 #include <OpenGl_NamedStatus.hxx>
46 #include <OpenGl_PrinterContext.hxx>
47 #include <OpenGl_SceneGeometry.hxx>
48 #include <OpenGl_TextParam.hxx>
49 #include <OpenGl_RenderFilter.hxx>
50 #include <OpenGl_Vec.hxx>
51 #include <OpenGl_LineAttributes.hxx>
52 #include <OpenGl_CappingAlgo.hxx>
55 #include <OpenGl_ShaderObject.hxx>
56 #include <OpenGl_ShaderProgram.hxx>
57 #include <OpenGl_TextureBufferArb.hxx>
59 class OpenGl_AspectLine;
60 class OpenGl_AspectMarker;
61 class OpenGl_AspectText;
62 class OpenGl_FrameBuffer;
63 class OpenGl_Structure;
64 class OpenGl_TriangleSet;
68 //! OpenGL material definition
69 struct OpenGl_Material
72 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
73 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
74 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
75 OpenGl_Vec4 Emission; //!< material emission
76 OpenGl_Vec4 Params; //!< extra packed parameters
78 Standard_ShortReal Shine() const { return Params.x(); }
79 Standard_ShortReal& ChangeShine() { return Params.x(); }
81 Standard_ShortReal Transparency() const { return Params.y(); }
82 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
84 //! Initialize material
85 void Init (const OPENGL_SURF_PROP& theProps);
87 //! Returns packed (serialized) representation of material properties
88 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
89 static Standard_Integer NbOfVec4() { return 5; }
93 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
95 //! Graphical ray-tracing filter.
96 //! Filters out all raytracable structures.
97 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
101 //! Default constructor.
102 OpenGl_RaytraceFilter() {}
104 //! Returns the previously set filter.
105 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
107 return myPrevRenderFilter;
110 //! Remembers the previously set filter.
111 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
113 myPrevRenderFilter = theFilter;
116 //! Checks whether the element can be rendered or not.
117 //! @param theElement [in] the element to check.
118 //! @return True if element can be rendered.
119 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement);
123 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
127 DEFINE_STANDARD_RTTI(OpenGl_RaytraceFilter, OpenGl_RenderFilter)
130 //! Represents window with GL context.
131 //! Provides methods to render primitives and maintain GL state.
132 class OpenGl_Workspace : public OpenGl_Window
136 //! Main constructor - prepare GL context for specified window.
137 OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
138 const CALL_DEF_WINDOW& theCWindow,
139 Aspect_RenderingContext theGContext,
140 const Handle(OpenGl_Caps)& theCaps,
141 const Handle(OpenGl_Context)& theShareCtx);
144 virtual ~OpenGl_Workspace();
146 void SetActiveView (const Handle(OpenGl_View)& theView,
147 const Standard_Integer theViewId)
150 myViewId = theViewId;
153 const Handle(OpenGl_View)& ActiveView() const { return myView; }
155 Standard_Integer ActiveViewId() const { return myViewId; }
157 //! Redraw the window.
158 void Redraw (const Graphic3d_CView& theCView,
159 const Aspect_CLayer2d& theCUnderLayer,
160 const Aspect_CLayer2d& theCOverLayer);
162 Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
163 void RedrawImmediate (const Graphic3d_CView& theCView,
164 const Aspect_CLayer2d& theCUnderLayer,
165 const Aspect_CLayer2d& theCOverLayer);
167 void Invalidate (const Graphic3d_CView& /*theCView*/)
169 myBackBufferRestored = Standard_False;
172 //! Special method to perform printing.
173 //! System-specific and currently only Win platform implemented.
174 Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext,
175 const Graphic3d_CView& theCView,
176 const Aspect_CLayer2d& theCUnderLayer,
177 const Aspect_CLayer2d& theCOverLayer,
178 const Aspect_Handle theHPrintDC,
179 const Standard_Boolean theToShowBackground,
180 const Standard_CString theFileName,
181 const Aspect_PrintAlgo thePrintAlgorithm,
182 const Standard_Real theScaleFactor);
184 const Handle(OpenGl_PrinterContext)& PrinterContext() const
186 return myPrintContext;
189 void DisplayCallback (const Graphic3d_CView& theCView, int theReason);
191 Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
192 void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
193 Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr,
194 Image_PixMap& theImage,
195 const Graphic3d_BufferType& theBufferType);
197 //! Setup Z-buffer usage flag (without affecting GL state!).
198 //! Returns previously set flag.
199 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
201 const Standard_Boolean wasUsed = myUseZBuffer;
202 myUseZBuffer = theToUse;
206 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
207 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
208 Standard_Boolean& UseGLLight() { return myUseGLLight; }
210 Standard_Integer AntiAliasingMode() const { return myAntiAliasingMode; }
212 //// RELATED TO STATUS ////
214 Standard_Integer NamedStatus;
216 const TEL_COLOUR* HighlightColor;
218 const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
219 const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
220 const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
221 const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
223 void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
225 //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
226 Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply);
227 Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply);
228 Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
229 Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply);
230 inline const OpenGl_TextParam* AspectTextParams() const
232 return TextParam_applied;
235 //! Clear the applied aspect state.
236 void ResetAppliedAspect();
238 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
239 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
240 const Handle(Graphic3d_TextureParams)& theParams = NULL);
241 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
243 //! Set filter for restricting rendering of particular elements.
244 //! Filter can be applied for rendering passes used by recursive
245 //! rendering algorithms for rendering elements of groups.
246 //! @param theFilter [in] the filter instance.
247 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
249 myRenderFilter = theFilter;
252 //! Get rendering filter.
253 //! @return filter instance.
254 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
256 return myRenderFilter;
259 //! @return applied view matrix.
260 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
262 //! @return applied model structure matrix.
263 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
265 //! Sets and applies current polygon offset.
266 void SetPolygonOffset (int theMode, Standard_ShortReal theFactor, Standard_ShortReal theUnits);
268 //! Returns currently applied polygon offset params.
269 const TEL_POFFSET_PARAM& AppliedPolygonOffset() { return PolygonOffset_applied; }
271 //! @return true if clipping algorithm enabled
272 inline Standard_Boolean IsCullingEnabled() const { return myIsCullingEnabled; }
274 //! Returns capping algorithm rendering filter.
275 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
277 return myDefaultCappingAlgoFilter;
280 //! Returns face aspect for none culling mode.
281 const OpenGl_AspectFace& NoneCulling() const
283 return myNoneCulling;
286 //! Returns face aspect for front face culling mode.
287 const OpenGl_AspectFace& FrontCulling() const
289 return myFrontCulling;
294 //! Copy content of Back buffer to the Front buffer
295 void copyBackToFront();
297 //! Blit image from/to specified buffers.
298 bool blitBuffers (OpenGl_FrameBuffer* theReadFbo,
299 OpenGl_FrameBuffer* theDrawFbo);
301 virtual Standard_Boolean Activate();
303 void redraw1 (const Graphic3d_CView& theCView,
304 const Aspect_CLayer2d& theCUnderLayer,
305 const Aspect_CLayer2d& theCOverLayer,
306 OpenGl_FrameBuffer* theReadDrawFbo,
307 const Graphic3d_Camera::Projection theProjection);
309 //! Setup default FBO.
310 void bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo = NULL);
312 //! Blend together views pair into stereo image.
313 void drawStereoPair (const Graphic3d_CView& theCView);
315 //! Blit snapshot containing main scene (myMainSceneFbos or BackBuffer)
316 //! into presentation buffer (myMainSceneFbos -> offscreen FBO or myMainSceneFbos -> BackBuffer or BackBuffer -> FrontBuffer),
317 //! and redraw immediate structures on top.
319 //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
320 //! the first step (blitting) will be skipped.
322 //! @return false if immediate structures has been rendered directly into FrontBuffer and Buffer Swap should not be called.
323 bool redrawImmediate (const Graphic3d_CView& theCView,
324 const Aspect_CLayer2d& theCUnderLayer,
325 const Aspect_CLayer2d& theCOverLayer,
326 OpenGl_FrameBuffer* theReadFbo,
327 const Graphic3d_Camera::Projection theProjection,
328 OpenGl_FrameBuffer* theDrawFbo,
329 const Standard_Boolean theIsPartialUpdate = Standard_False);
331 void updateMaterial (const int theFlag);
333 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
334 const Handle(Graphic3d_TextureParams)& theParams);
336 protected: //! @name protected fields
338 //! Two framebuffers (left and right views) store cached main presentation
339 //! of the view (without presentation of immediate layers).
340 Handle(OpenGl_FrameBuffer) myMainSceneFbos[2];
341 //! Additional buffers for immediate layer in stereo mode.
342 Handle(OpenGl_FrameBuffer) myImmediateSceneFbos[2];
343 //! Special flag which is invalidated when myMainSceneFbos can not be blitted for some reason (e.g. driver bugs).
344 Standard_Boolean myHasFboBlit;
346 //! Vertices for full-screen quad rendering.
347 OpenGl_VertexBuffer myFullScreenQuad;
349 Handle(OpenGl_PrinterContext) myPrintContext;
350 Handle(OpenGl_View) myView;
351 Handle(OpenGl_LineAttributes) myLineAttribs;
352 Standard_Integer myViewId;
353 Standard_Integer myAntiAliasingMode;
354 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
355 Standard_Boolean myBackBufferRestored;
356 Standard_Boolean myIsImmediateDrawn; //!< flag indicates that immediate mode buffer contains some data
357 Standard_Boolean myUseZBuffer;
358 Standard_Boolean myUseDepthWrite;
359 Standard_Boolean myUseGLLight;
360 Standard_Boolean myIsCullingEnabled; //!< frustum culling flag
362 unsigned int myFrameCounter; //!< redraw counter, for debugging
364 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
365 OpenGl_AspectFace myNoneCulling;
366 OpenGl_AspectFace myFrontCulling;
368 protected: //! @name fields related to status
370 Handle(OpenGl_RenderFilter) myRenderFilter;
371 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
372 const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
373 const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
374 const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
375 const OpenGl_AspectText *AspectText_set, *AspectText_applied;
377 const OpenGl_TextParam *TextParam_set, *TextParam_applied;
379 const OpenGl_Matrix* ViewMatrix_applied;
380 const OpenGl_Matrix* StructureMatrix_applied;
382 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
383 OpenGl_Material myMatBack; //!< current back material state
384 OpenGl_Material myMatTmp; //!< temporary variable
385 TelCullMode myCullingMode; //!< back face culling mode, applied from face aspect
387 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
389 TEL_POFFSET_PARAM PolygonOffset_applied; //!< Currently applied polygon offset.
391 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
393 public: //! @name type definition
395 DEFINE_STANDARD_RTTI(OpenGl_Workspace, OpenGl_Window)
396 DEFINE_STANDARD_ALLOC
400 #endif // _OpenGl_Workspace_Header