1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | #include "OgreOSXWindow.h" |
---|
31 | #include "OgreOSXCGLContext.h" |
---|
32 | #include "OgreRoot.h" |
---|
33 | #include "OgreGLRenderSystem.h" |
---|
34 | #include "OgreImageCodec.h" |
---|
35 | #include "OgreException.h" |
---|
36 | #include "OgreLogManager.h" |
---|
37 | #include "OgreStringConverter.h" |
---|
38 | |
---|
39 | #include <OpenGL/gl.h> |
---|
40 | #define GL_EXT_texture_env_combine 1 |
---|
41 | #include <OpenGL/glext.h> |
---|
42 | #include <OpenGL/glu.h> |
---|
43 | |
---|
44 | namespace Ogre |
---|
45 | { |
---|
46 | |
---|
47 | //-------------------------------------------------------------------------------------------------// |
---|
48 | OSXWindow::OSXWindow() : mCGLContext(NULL) |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 | //-------------------------------------------------------------------------------------------------// |
---|
53 | OSXWindow::~OSXWindow() |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | //-------------------------------------------------------------------------------------------------// |
---|
58 | void OSXWindow::writeContentsToFile(const String& filename) |
---|
59 | { |
---|
60 | ImageCodec::ImageData* imgData = new ImageCodec::ImageData; |
---|
61 | imgData->width = mWidth; |
---|
62 | imgData->height = mHeight; |
---|
63 | imgData->format = PF_BYTE_RGB; |
---|
64 | |
---|
65 | // Allocate buffer |
---|
66 | uchar* pBuffer = new uchar[mWidth * mHeight * 3]; |
---|
67 | |
---|
68 | // Read pixels |
---|
69 | // I love GL: it does all the locking & colour conversion for us |
---|
70 | glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); |
---|
71 | |
---|
72 | // Wrap buffer in a memory stream |
---|
73 | DataStreamPtr stream(new MemoryDataStream(pBuffer, mWidth * mHeight * 3, false)); |
---|
74 | |
---|
75 | // Need to flip the read data over in Y though |
---|
76 | Image img; |
---|
77 | img.loadRawData(stream, mWidth, mHeight, PF_BYTE_RGB ); |
---|
78 | img.flipAroundX(); |
---|
79 | |
---|
80 | MemoryDataStreamPtr streamFlipped(new MemoryDataStream(img.getData(), stream->size(), false)); |
---|
81 | |
---|
82 | // Get codec |
---|
83 | size_t pos = filename.find_last_of("."); |
---|
84 | String extension; |
---|
85 | if( pos == String::npos ) |
---|
86 | OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Unable to determine image type for '" |
---|
87 | + filename + "' - invalid extension.", "OSXCarbonWindow::writeContentsToFile" ); |
---|
88 | |
---|
89 | while( pos != filename.length() - 1 ) |
---|
90 | extension += filename[++pos]; |
---|
91 | |
---|
92 | // Get the codec |
---|
93 | Codec * pCodec = Codec::getCodec(extension); |
---|
94 | |
---|
95 | // Write out |
---|
96 | Codec::CodecDataPtr codecDataPtr(imgData); |
---|
97 | pCodec->codeToFile(streamFlipped, filename, codecDataPtr); |
---|
98 | |
---|
99 | delete [] pBuffer; |
---|
100 | } |
---|
101 | |
---|
102 | //-------------------------------------------------------------------------------------------------// |
---|
103 | void OSXWindow::createCGLFullscreen(unsigned int width, unsigned int height, unsigned int depth, unsigned int fsaa, CGLContextObj sharedContext) |
---|
104 | { |
---|
105 | // Find the best match to what was requested |
---|
106 | boolean_t exactMatch; |
---|
107 | CFDictionaryRef displayMode = CGDisplayBestModeForParameters(kCGDirectMainDisplay, depth, width, height, &exactMatch); |
---|
108 | |
---|
109 | if(!exactMatch) |
---|
110 | { |
---|
111 | // TODO: Report the size difference |
---|
112 | // That mode is not available, using the closest match |
---|
113 | String request = StringConverter::toString(width) + String(" x ") + StringConverter::toString(height) + String(" @ ") + |
---|
114 | StringConverter::toString(depth) + "bpp. "; |
---|
115 | |
---|
116 | String recieved = StringConverter::toString(width) + String(" x ") + StringConverter::toString(height) + String(" @ ") + |
---|
117 | StringConverter::toString(depth) + "bpp. "; |
---|
118 | |
---|
119 | LogManager::getSingleton().logMessage(String("RenderSystem Warning: You requested a fullscreen mode of ") + request + |
---|
120 | String(" This mode is not available and you will recieve the closest match.")); |
---|
121 | } |
---|
122 | |
---|
123 | // Do the fancy display fading |
---|
124 | CGDisplayFadeReservationToken reservationToken; |
---|
125 | CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, |
---|
126 | &reservationToken); |
---|
127 | CGDisplayFade(reservationToken, |
---|
128 | 0.5, |
---|
129 | kCGDisplayBlendNormal, |
---|
130 | kCGDisplayBlendSolidColor, |
---|
131 | 0.0, 0.0, 0.0, |
---|
132 | true); |
---|
133 | |
---|
134 | // Grab the main display and save it for later. |
---|
135 | // You could render to any display, but picking what display |
---|
136 | // to render to could be interesting. |
---|
137 | CGDisplayCapture(kCGDirectMainDisplay); |
---|
138 | |
---|
139 | // Switch to the correct resolution |
---|
140 | CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode); |
---|
141 | |
---|
142 | // Get a pixel format that best matches what we are looking for |
---|
143 | CGLPixelFormatAttribute attribs[] = { |
---|
144 | kCGLPFADoubleBuffer, |
---|
145 | kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8, |
---|
146 | kCGLPFADepthSize, (CGLPixelFormatAttribute)depth, |
---|
147 | kCGLPFAStencilSize, (CGLPixelFormatAttribute)8, |
---|
148 | kCGLPFASampleBuffers, (CGLPixelFormatAttribute)0, |
---|
149 | kCGLPFASamples, (CGLPixelFormatAttribute)0, |
---|
150 | kCGLPFAFullScreen, |
---|
151 | kCGLPFASingleRenderer, |
---|
152 | kCGLPFAAccelerated, |
---|
153 | kCGLPFADisplayMask, (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), |
---|
154 | (CGLPixelFormatAttribute)0 |
---|
155 | }; |
---|
156 | |
---|
157 | // Set up FSAA if it was requested |
---|
158 | if(fsaa > 1) |
---|
159 | { |
---|
160 | // turn on kCGLPFASampleBuffers |
---|
161 | attribs[8] = (CGLPixelFormatAttribute)1; |
---|
162 | // set the samples for kCGLPFASamples |
---|
163 | attribs[10] = (CGLPixelFormatAttribute)fsaa; |
---|
164 | } |
---|
165 | |
---|
166 | // Storage for our pixel format |
---|
167 | CGLPixelFormatObj pixelFormatObj; |
---|
168 | long numPixelFormats = 0; |
---|
169 | |
---|
170 | // Get a pixelFormatObj from our atributes |
---|
171 | CGLError err; |
---|
172 | err = CGLChoosePixelFormat(attribs, &pixelFormatObj, &numPixelFormats); |
---|
173 | if(err != 0) |
---|
174 | { |
---|
175 | CGReleaseAllDisplays(); |
---|
176 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("CGL Error: " + String(CGLErrorString(err))), "OSXCarbonWindow::create"); |
---|
177 | } |
---|
178 | |
---|
179 | // Create the CGLcontext from our pixel format, share it with the sharedContext passed in |
---|
180 | err = CGLCreateContext(pixelFormatObj, sharedContext, &mCGLContext); |
---|
181 | if(err != 0) |
---|
182 | { |
---|
183 | CGReleaseAllDisplays(); |
---|
184 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("CGL Error: " + String(CGLErrorString(err))), "OSXCarbonWindow::create"); |
---|
185 | } |
---|
186 | |
---|
187 | // Once we have the context we can destroy the pixel format |
---|
188 | CGLDestroyPixelFormat(pixelFormatObj); |
---|
189 | |
---|
190 | // Set the context to drawable |
---|
191 | CGLSetFullScreen(mCGLContext); |
---|
192 | |
---|
193 | // Set the context as current |
---|
194 | CGLSetCurrentContext(mCGLContext); |
---|
195 | |
---|
196 | // This synchronizes CGL with the vertical retrace |
---|
197 | // Apple docs suggest that OpenGL blocks rendering calls when waiting for |
---|
198 | // a vertical retrace anyhow. |
---|
199 | long swapInterval = 1; |
---|
200 | CGLSetParameter(mCGLContext, kCGLCPSwapInterval, &swapInterval); |
---|
201 | |
---|
202 | // Give a copy of our context to the rendersystem |
---|
203 | mContext = new OSXCGLContext(mCGLContext); |
---|
204 | |
---|
205 | // Let everyone know we are fullscreen now |
---|
206 | mIsFullScreen = true; |
---|
207 | |
---|
208 | CGDisplayFade(reservationToken, |
---|
209 | 2.0, |
---|
210 | kCGDisplayBlendSolidColor, |
---|
211 | kCGDisplayBlendNormal, |
---|
212 | 0.0, 0.0, 0.0, |
---|
213 | false); |
---|
214 | CGReleaseDisplayFadeReservation(reservationToken); |
---|
215 | } |
---|
216 | |
---|
217 | //-------------------------------------------------------------------------------------------------// |
---|
218 | void OSXWindow::destroyCGLFullscreen(void) |
---|
219 | { |
---|
220 | CGReleaseAllDisplays(); |
---|
221 | } |
---|
222 | |
---|
223 | //-------------------------------------------------------------------------------------------------// |
---|
224 | void OSXWindow::swapCGLBuffers(void) |
---|
225 | { |
---|
226 | CGLFlushDrawable(mCGLContext); |
---|
227 | CGLContextObj curCtx = CGLGetCurrentContext(); |
---|
228 | if(curCtx != mCGLContext) |
---|
229 | { |
---|
230 | CGLSetCurrentContext(mCGLContext); |
---|
231 | CGLSetFullScreen(mCGLContext); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | } |
---|