0023243: Adapt OpenGL viewer for using in Cocoa applications on Mac OS X
[occt.git] / src / OpenGl / OpenGl_Workspace_4.cxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #include <OpenGl_GlCore11.hxx>
21
22 #if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
23   #include <OpenGl_AVIWriter.hxx>
24 #endif
25
26 #include <OpenGl_FrameBuffer.hxx>
27 #include <InterfaceGraphic_Graphic3d.hxx>
28 #include <InterfaceGraphic_Visual3d.hxx>
29
30 #include <OpenGl_Workspace.hxx>
31
32 //call_togl_redraw
33 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
34                                const Aspect_CLayer2d& theCUnderLayer,
35                                const Aspect_CLayer2d& theCOverLayer)
36 {
37   if (!Activate())
38     return;
39
40   // release pending GL resources
41   Handle(OpenGl_Context) aGlCtx = GetGlContext();
42   aGlCtx->ReleaseDelayed();
43
44   // cache render mode state
45   GLint aRendMode = GL_RENDER;
46   glGetIntegerv (GL_RENDER_MODE, &aRendMode);
47   aGlCtx->SetFeedback (aRendMode == GL_FEEDBACK);
48
49   Tint toSwap = (aRendMode == GL_RENDER); // swap buffers
50   GLint aViewPortBack[4];
51   OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
52   if (aFrameBuffer != NULL)
53   {
54     glGetIntegerv (GL_VIEWPORT, aViewPortBack);
55     aFrameBuffer->SetupViewport();
56     aFrameBuffer->BindBuffer (aGlCtx);
57     toSwap = 0; // no need to swap buffers
58   }
59
60   Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
61   RedrawImmediatMode();
62
63   if (aFrameBuffer != NULL)
64   {
65     aFrameBuffer->UnbindBuffer (aGlCtx);
66     // move back original viewport
67     glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
68   }
69
70 #if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
71   if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
72   {
73     GLint params[4];
74     glGetIntegerv (GL_VIEWPORT, params);
75     int nWidth  = params[2] & ~0x7;
76     int nHeight = params[3] & ~0x7;
77
78     const int nBitsPerPixel = 24;
79     GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
80
81     glPixelStorei (GL_PACK_ALIGNMENT, 1);
82     glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
83     OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
84     delete[] aDumpData;
85   }
86 #endif
87
88   // reset render mode state
89   aGlCtx->SetFeedback (Standard_False);
90 }