2b7e2f68a363eb0695f46714c50b0788eb6379ce
[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_Caps)&    theCaps,
56                               const Handle(OpenGl_Context)& theShareCtx)
57 : myDisplay (theDisplay),
58   myGlContext (new OpenGl_Context (theCaps)),
59   myOwnGContext (theGContext == 0),
60   myWidth ((Standard_Integer )theCWindow.dx),
61   myHeight ((Standard_Integer )theCWindow.dy),
62   myBgColor (THE_DEFAULT_BG_COLOR),
63   myDither (theDisplay->Dither()),
64   myBackDither (theDisplay->BackDither())
65 {
66   myBgColor.rgb[0] = theCWindow.Background.r;
67   myBgColor.rgb[1] = theCWindow.Background.g;
68   myBgColor.rgb[2] = theCWindow.Background.b;
69
70   Cocoa_LocalPool aLocalPool;
71   //NSOpenGLContext* aGContext = (NSOpenGLContext* )theGContext;
72
73   // all GL context within one OpenGl_GraphicDriver should be shared!
74   NSOpenGLContext*     aGLCtxShare = theShareCtx.IsNull() ? NULL : (NSOpenGLContext* )theShareCtx->myGContext;
75   NSOpenGLPixelFormat* aGLFormat   = [[[NSOpenGLPixelFormat alloc] initWithAttributes: THE_DOUBLE_BUFF] autorelease];
76   NSOpenGLContext*     aGLContext  = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
77                                                                 shareContext: aGLCtxShare];
78   if (aGLContext == NULL)
79   {
80     TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
81     Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
82     return;
83   }
84
85   NSView* aView = (NSView* )theCWindow.XWindow;
86   [aGLContext setView: aView];
87
88   myGlContext->Init (aGLContext);
89   myGlContext->Share (theShareCtx);
90   Init();
91 }
92
93 // =======================================================================
94 // function : ~OpenGl_Window
95 // purpose  :
96 // =======================================================================
97 OpenGl_Window::~OpenGl_Window()
98 {
99   NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
100   myGlContext.Nullify();
101
102   [NSOpenGLContext clearCurrentContext];
103   if (myOwnGContext)
104   {
105     [aGLCtx clearDrawable];
106     [aGLCtx release];
107   }
108 }
109
110 // =======================================================================
111 // function : Resize
112 // purpose  : call_subr_resize
113 // =======================================================================
114 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
115 {
116   DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
117   if (aDisp == NULL)
118   {
119     return;
120   }
121
122   // If the size is not changed - do nothing
123   if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
124   {
125     return;
126   }
127
128   myWidth  = (Standard_Integer )theCWindow.dx;
129   myHeight = (Standard_Integer )theCWindow.dy;
130
131   Init();
132 }
133
134 // =======================================================================
135 // function : Init
136 // purpose  :
137 // =======================================================================
138 void OpenGl_Window::Init()
139 {
140   if (!Activate())
141   {
142     return;
143   }
144
145   NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
146   NSRect aBounds = [[aGLCtx view] bounds];
147
148   // we should call this method each time when window is resized
149   [aGLCtx update];
150
151   myWidth  = Standard_Integer(aBounds.size.width);
152   myHeight = Standard_Integer(aBounds.size.height);
153
154   glMatrixMode (GL_MODELVIEW);
155   glViewport (0, 0, myWidth, myHeight);
156
157   glDisable (GL_SCISSOR_TEST);
158   glDrawBuffer (GL_BACK);
159 }
160
161 #endif // __APPLE__