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