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