1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
18 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
20 #import <Cocoa/Cocoa.h>
22 #include <InterfaceGraphic.hxx>
24 #include <OpenGl_Window.hxx>
26 #include <OpenGl_Context.hxx>
27 #include <Aspect_GraphicDeviceDefinitionError.hxx>
28 #include <Cocoa_LocalPool.hxx>
29 #include <TCollection_AsciiString.hxx>
31 #include <OpenGL/CGLRenderers.h>
35 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
38 // =======================================================================
39 // function : OpenGl_Window
41 // =======================================================================
42 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
43 const CALL_DEF_WINDOW& theCWindow,
44 Aspect_RenderingContext theGContext,
45 const Handle(OpenGl_Caps)& theCaps,
46 const Handle(OpenGl_Context)& theShareCtx)
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)
53 myBgColor.rgb[0] = theCWindow.Background.r;
54 myBgColor.rgb[1] = theCWindow.Background.g;
55 myBgColor.rgb[2] = theCWindow.Background.b;
57 Cocoa_LocalPool aLocalPool;
58 //NSOpenGLContext* aGContext = (NSOpenGLContext* )theGContext;
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,
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: anAttribs] autorelease];
75 NSOpenGLContext* aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
76 shareContext: aGLCtxShare];
77 if (aGLContext == NULL
78 && theCaps->contextStereo)
80 anAttribs[0] = aDummyAttrib;
81 aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
82 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
83 shareContext: aGLCtxShare];
85 if (aGLContext == NULL)
87 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
88 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
92 NSView* aView = (NSView* )theCWindow.XWindow;
93 [aGLContext setView: aView];
95 myGlContext->Init (aGLContext);
96 myGlContext->Share (theShareCtx);
100 // =======================================================================
101 // function : ~OpenGl_Window
103 // =======================================================================
104 OpenGl_Window::~OpenGl_Window()
106 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
107 myGlContext.Nullify();
109 [NSOpenGLContext clearCurrentContext];
112 [aGLCtx clearDrawable];
117 // =======================================================================
119 // purpose : call_subr_resize
120 // =======================================================================
121 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
123 // If the size is not changed - do nothing
124 if (myWidth == (Standard_Integer )theCWindow.dx
125 && myHeight == (Standard_Integer )theCWindow.dy)
130 myWidth = (Standard_Integer )theCWindow.dx;
131 myHeight = (Standard_Integer )theCWindow.dy;
136 // =======================================================================
139 // =======================================================================
140 void OpenGl_Window::Init()
147 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
148 NSRect aBounds = [[aGLCtx view] bounds];
150 // we should call this method each time when window is resized
153 myWidth = Standard_Integer(aBounds.size.width);
154 myHeight = Standard_Integer(aBounds.size.height);
156 glMatrixMode (GL_MODELVIEW);
157 glViewport (0, 0, myWidth, myHeight);
159 glDisable (GL_SCISSOR_TEST);
160 glDrawBuffer (GL_BACK);