1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2012 OPEN CASCADE SAS
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.
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.
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.
20 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
22 #import <Cocoa/Cocoa.h>
24 #include <InterfaceGraphic.hxx>
26 #include <OpenGl_Window.hxx>
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>
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,
48 // =======================================================================
49 // function : OpenGl_Window
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())
65 myBgColor.rgb[0] = theCWindow.Background.r;
66 myBgColor.rgb[1] = theCWindow.Background.g;
67 myBgColor.rgb[2] = theCWindow.Background.b;
69 Cocoa_LocalPool aLocalPool;
70 //NSOpenGLContext* aGContext = (NSOpenGLContext* )theGContext;
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)
79 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
80 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
84 NSView* aView = (NSView* )theCWindow.XWindow;
85 [aGLContext setView: aView];
87 myGlContext->Init (aGLContext);
88 myGlContext->Share (theShareCtx);
92 // =======================================================================
93 // function : ~OpenGl_Window
95 // =======================================================================
96 OpenGl_Window::~OpenGl_Window()
98 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
99 myGlContext.Nullify();
101 [NSOpenGLContext clearCurrentContext];
104 [aGLCtx clearDrawable];
109 // =======================================================================
111 // purpose : call_subr_resize
112 // =======================================================================
113 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
115 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
121 // If the size is not changed - do nothing
122 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
127 myWidth = (Standard_Integer )theCWindow.dx;
128 myHeight = (Standard_Integer )theCWindow.dy;
133 // =======================================================================
136 // =======================================================================
137 void OpenGl_Window::Init()
144 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
145 NSRect aBounds = [[aGLCtx view] bounds];
147 // we should call this method each time when window is resized
150 myWidth = Standard_Integer(aBounds.size.width);
151 myHeight = Standard_Integer(aBounds.size.height);
153 glMatrixMode (GL_MODELVIEW);
154 glViewport (0, 0, myWidth, myHeight);
156 glDisable (GL_SCISSOR_TEST);
157 glDrawBuffer (GL_BACK);