1 // Created on: 2014-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 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
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.
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 #import <Cocoa/Cocoa.h>
20 #include <InterfaceGraphic.hxx>
22 #include <OpenGl_Window.hxx>
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>
30 #include <OpenGL/CGLRenderers.h>
34 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
37 // =======================================================================
38 // function : OpenGl_Window
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())
55 myBgColor.rgb[0] = theCWindow.Background.r;
56 myBgColor.rgb[1] = theCWindow.Background.g;
57 myBgColor.rgb[2] = theCWindow.Background.b;
59 Cocoa_LocalPool aLocalPool;
60 //NSOpenGLContext* aGContext = (NSOpenGLContext* )theGContext;
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,
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)
82 anAttribs[0] = aDummyAttrib;
83 aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
84 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
85 shareContext: aGLCtxShare];
87 if (aGLContext == NULL)
89 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
90 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
94 NSView* aView = (NSView* )theCWindow.XWindow;
95 [aGLContext setView: aView];
97 myGlContext->Init (aGLContext);
98 myGlContext->Share (theShareCtx);
102 // =======================================================================
103 // function : ~OpenGl_Window
105 // =======================================================================
106 OpenGl_Window::~OpenGl_Window()
108 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
109 myGlContext.Nullify();
111 [NSOpenGLContext clearCurrentContext];
114 [aGLCtx clearDrawable];
119 // =======================================================================
121 // purpose : call_subr_resize
122 // =======================================================================
123 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
125 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
131 // If the size is not changed - do nothing
132 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
137 myWidth = (Standard_Integer )theCWindow.dx;
138 myHeight = (Standard_Integer )theCWindow.dy;
143 // =======================================================================
146 // =======================================================================
147 void OpenGl_Window::Init()
154 NSOpenGLContext* aGLCtx = (NSOpenGLContext* )myGlContext->myGContext;
155 NSRect aBounds = [[aGLCtx view] bounds];
157 // we should call this method each time when window is resized
160 myWidth = Standard_Integer(aBounds.size.width);
161 myHeight = Standard_Integer(aBounds.size.height);
163 glMatrixMode (GL_MODELVIEW);
164 glViewport (0, 0, myWidth, myHeight);
166 glDisable (GL_SCISSOR_TEST);
167 glDrawBuffer (GL_BACK);