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