dce08ac5d97089b6b4d903cc1ee1c487d473f10e
[occt.git] / src / OpenGl / OpenGl_Window_1.mm
1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 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 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
21
22 #import <Cocoa/Cocoa.h>
23
24 #include <InterfaceGraphic.hxx>
25
26 #include <OpenGl_Window.hxx>
27
28 #include <OpenGl_Context.hxx>
29 #include <OpenGl_Display.hxx>
30 #include <Aspect_GraphicDeviceDefinitionError.hxx>
31 #include <Cocoa_LocalPool.hxx>
32 #include <TCollection_AsciiString.hxx>
33
34 namespace
35 {
36   static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
37   static const NSOpenGLPixelFormatAttribute THE_DOUBLE_BUFF[] = {
38     //NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute )32,
39     NSOpenGLPFADepthSize,   (NSOpenGLPixelFormatAttribute )24,
40     NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute )8,
41     NSOpenGLPFADoubleBuffer,
42     NSOpenGLPFAAccelerated,
43     0
44   };
45
46 };
47
48 // =======================================================================
49 // function : OpenGl_Window
50 // purpose  :
51 // =======================================================================
52 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
53                               const CALL_DEF_WINDOW&        theCWindow,
54                               Aspect_RenderingContext       theGContext,
55                               const Handle(OpenGl_Context)& theShareCtx)
56 : myDisplay (theDisplay),
57   myGlContext (new OpenGl_Context()),
58   myOwnGContext (theGContext == 0),
59   myWidth ((Standard_Integer )theCWindow.dx),
60   myHeight ((Standard_Integer )theCWindow.dy),
61   myBgColor (THE_DEFAULT_BG_COLOR),
62   myDither (theDisplay->Dither()),
63   myBackDither (theDisplay->BackDither())
64 {
65   myBgColor.rgb[0] = theCWindow.Background.r;
66   myBgColor.rgb[1] = theCWindow.Background.g;
67   myBgColor.rgb[2] = theCWindow.Background.b;
68
69   Cocoa_LocalPool aLocalPool;
70   //NSOpenGLContext* aGContext = (NSOpenGLContext* )theGContext;
71
72   // all GL context within one OpenGl_GraphicDriver should be shared!
73   NSOpenGLContext*     aGLCtxShare = theShareCtx.IsNull() ? NULL : (NSOpenGLContext* )theShareCtx->myGContext;
74   NSOpenGLPixelFormat* aGLFormat   = [[[NSOpenGLPixelFormat alloc] initWithAttributes: THE_DOUBLE_BUFF] autorelease];
75   NSOpenGLContext*     aGLContext  = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
76                                                                 shareContext: aGLCtxShare];
77   if (aGLContext == NULL)
78   {
79     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
80     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
81     return;
82   }
83
84   NSView* aView = (NSView* )theCWindow.XWindow;
85   [aGLContext setView: aView];
86
87   myGlContext->Init (aGLContext);
88   myGlContext->Share (theShareCtx);
89   Init();
90 }
91
92 // =======================================================================
93 // function : ~OpenGl_Window
94 // purpose  :
95 // =======================================================================
96 OpenGl_Window::~OpenGl_Window()
97 {
98   NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
99   myGlContext.Nullify();
100
101   [NSOpenGLContext clearCurrentContext];
102   if (myOwnGContext)
103   {
104     [aGLCtx clearDrawable];
105     [aGLCtx release];
106   }
107 }
108
109 // =======================================================================
110 // function : Resize
111 // purpose  : call_subr_resize
112 // =======================================================================
113 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
114 {
115   DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
116   if (aDisp == NULL)
117   {
118     return;
119   }
120
121   // If the size is not changed - do nothing
122   if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
123   {
124     return;
125   }
126
127   myWidth  = (Standard_Integer )theCWindow.dx;
128   myHeight = (Standard_Integer )theCWindow.dy;
129
130   Init();
131 }
132
133 // =======================================================================
134 // function : Init
135 // purpose  :
136 // =======================================================================
137 void OpenGl_Window::Init()
138 {
139   if (!Activate())
140   {
141     return;
142   }
143
144   NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
145   NSRect aBounds = [[aGLCtx view] bounds];
146
147   // we should call this method each time when window is resized
148   [aGLCtx update];
149
150   myWidth  = Standard_Integer(aBounds.size.width);
151   myHeight = Standard_Integer(aBounds.size.height);
152
153   glMatrixMode (GL_MODELVIEW);
154   glViewport (0, 0, myWidth, myHeight);
155
156   glDisable (GL_SCISSOR_TEST);
157   glDrawBuffer (GL_BACK);
158 }
159
160 #endif // __APPLE__