0025703: Visualization - Decrease number of samplers used in ray-tracing mode
[occt.git] / src / OpenGl / OpenGl_Window_1.mm
1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
17
18 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
19
20 #import <Cocoa/Cocoa.h>
21
22 #include <InterfaceGraphic.hxx>
23
24 #include <OpenGl_Window.hxx>
25
26 #include <OpenGl_Context.hxx>
27 #include <Aspect_GraphicDeviceDefinitionError.hxx>
28 #include <Cocoa_LocalPool.hxx>
29 #include <TCollection_AsciiString.hxx>
30
31 #include <OpenGL/CGLRenderers.h>
32
33 namespace
34 {
35   static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
36 };
37
38 // =======================================================================
39 // function : OpenGl_Window
40 // purpose  :
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)
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
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
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)
79   {
80     anAttribs[0] = aDummyAttrib;
81     aGLFormat    = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
82     aGLContext   = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
83                                               shareContext: aGLCtxShare];
84   }
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 {
123   // If the size is not changed - do nothing
124   if (myWidth  == (Standard_Integer )theCWindow.dx
125    && myHeight == (Standard_Integer )theCWindow.dy)
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__