Oolite
Loading...
Searching...
No Matches
OOEnvironmentCubeMap.m
Go to the documentation of this file.
1/*
2
3OOEnvironmentCubeMap.m
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
29#import "MyOpenGLView.h"
30#import "OOMacroOpenGL.h"
31#import "OOPixMap.h"
32#import "OOTexture.h"
33#import "OOTextureInternal.h"
34
35#import "OODrawable.h"
37#import "OOPlanetEntity.h"
38#import "OOSunEntity.h"
39#import "PlayerEntity.h"
40#import "SkyEntity.h"
41#import "Universe.h"
42
43#if OO_USE_FBO && OO_TEXTURE_CUBE_MAP
44
45@interface OOEnvironmentCubeMap (Private)
46
47- (void)setUp;
48
49- (void)renderOnePassWithSky:(OODrawable*)sky sun:(OOSunEntity*)sun planets:(NSArray*)planets;
50
51@end
52
53@implementation OOEnvironmentCubeMap
54
55- (id)initWithSideLength:(GLuint)size
56{
57 if (![[OOOpenGLExtensionManager sharedManager] fboSupported] || !OOCubeMapsAvailable()) {
58 [self release];
59 return nil;
60 }
61
62 if ((self = [super init])) {
63 _size = size;
64 }
65
66 return self;
67}
68
69- (void)dealloc
70{
71 [self forceRebind];
72
73 [super dealloc];
74}
75
76- (void)render
77{
78 if (_textureName == 0)
79 [self setUp];
80
82
83 // Save stuff.
84 OOGL(glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT));
87
88 OOGL(glViewport(0, 0, _size, _size));
89 OOGLScaleProjection(make_vector(-1.0, 1.0, 1.0)); // flip left and right
90
91 /* TODO: once confirmed working (and rendering everything in the right
92 orientation), replace with glLoadMatrix and the following:
93 1 0 0 0
94 0 1 0 0
95 0 0 -1 -2
96 0 0 -1 0
97
98 ...and appropriate rotations thereof.
99 */
100
102 OOGLPerspective(90.0, 1.0, 1.0, MAX_CLEAR_DEPTH);
103
104 OODrawable* sky = [[UNIVERSE nearestEntityMatchingPredicate:HasClassPredicate parameter:[SkyEntity class] relativeToEntity:nil] drawable];
105 OOSunEntity* sun = [UNIVERSE sun];
106 NSArray* planets = [UNIVERSE planets];
107
108 unsigned i;
109 Vector centers[6] = { { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } };
110 Vector ups[6] = { { 0, -1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 }, { 0, -1, 0 }, { 0, -1, 0 } };
111
112 for (i = 0; i < 6; i++) {
114 Vector center = centers[i];
115 Vector up = ups[i];
116 OOGLLookAt(kZeroVector, center, up);
117
118 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
119 [self renderOnePassWithSky:sky sun:sun planets:planets];
120
122 }
123
126 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
127 OOGL(glPopAttrib());
128}
129
130- (void)renderOnePassWithSky:(OODrawable*)sky sun:(OOSunEntity*)sun planets:(NSArray*)planets
131{
133
134 OOGL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
135 OOGL(glClearDepth(MAX_CLEAR_DEPTH));
136 OOGL(glClear(_planets ? GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT : GL_COLOR_BUFFER_BIT));
137
138 OOGL(glDepthMask(GL_FALSE));
139 [sky renderOpaqueParts];
140 OOGL(glDepthMask(GL_TRUE));
141
145
146 NSEnumerator* planetEnum = nil;
147 OOPlanetEntity* planet = nil;
148 for (planetEnum = [planets objectEnumerator]; (planet = [planetEnum nextObject]);) {
150 OOGLTranslateModelView(planet position]);
151#if NEW_PLANETS
152 [[planet drawable] renderOpaqueParts];
153#else
154 [planet drawUnconditionally];
155#endif
157 }
158
160 OOGLTranslateModelView([sun position]);
161 [sun drawUnconditionally];
164}
165
166- (void)setUp
167{
169
170 if (_textureName != 0)
171 return;
172 _planets = [UNIVERSE reducedDetail];
173
174 OOGL(glGenTextures(1, &_textureName));
175 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, _textureName));
176 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
177 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
178 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE));
179 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
180 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
181
182 OOGL(glGenFramebuffersEXT(6, _fbos));
183 OOGL(glGenRenderbuffersEXT(6, _depthBuffers));
184
185 unsigned i;
186 for (i = 0; i < 6; i++) {
187 GLenum textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
188
189 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
190
191 OOGL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depthBuffers[i]));
192 OOGL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _size, _size));
193 OOGL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, _depthBuffers[i]));
194
195 OOGL(glTexImage2D(textarget, 0, GL_RGBA8, _size, _size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
196 OOGL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, textarget, _textureName, 0));
197 }
198
199#ifndef NDEBUG
200 GLenum status;
201 OOGL(status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
202 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
203 OOLogERR(@"environmentCube.fbo.setup.failed", @"Failed to set up FBO for environment cube map - status is %u.", status);
204 DESTROY(self);
205 }
206
207 OOCheckOpenGLErrors(@"after setting up environment cube map FBO");
208#endif
209 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, 0));
210 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
211 OOGL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0));
212}
213
214// OOTexture stuff.
215
216- (void)apply
217{
219 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, _textureName));
220}
221
222- (NSSize)dimensions
223{
224 return NSMakeSize(_size, _size);
225}
226
227- (void)forceRebind
228{
230 if (_textureName == 0)
231 return;
232
233 OOGL(glDeleteTextures(1, &_textureName));
234 _textureName = 0;
235
236 OOGL(glDeleteFramebuffersEXT(6, _fbos));
237 OOGL(glDeleteRenderbuffersEXT(6, _depthBuffers));
238}
239
240- (BOOL)isCubeMap
241{
242 return YES;
243}
244
245- (struct OOPixMap)copyPixMapRepresentation
246{
247 OOPixMap pixmap = OOAllocatePixMap(_size, _size * 6, 4, 0, 0);
248 if (!OOIsValidPixMap(pixmap))
249 return kOONullPixMap;
250
252
253 unsigned i;
254 for (i = 0; i < 6; i++) {
255 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
256 OOGL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
257 uint8_t* target = pixmap.pixels;
258 target += pixmap.rowBytes * _size * i;
259 OOGL(glReadPixels(0, 0, _size, _size, GL_RGBA, GL_UNSIGNED_BYTE, target));
260 }
261
262 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
263 return pixmap;
264}
265
266@end
267
268#endif
#define MAX_CLEAR_DEPTH
#define DESTROY(x)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OO_ENTER_OPENGL()
void OOGLLookAt(Vector eye, Vector center, Vector up)
void OOGLScaleProjection(Vector scale)
void OOGLPushModelView(void)
void OOGLPushProjection(void)
void OOGLResetModelView(void)
void OOGLTranslateModelView(Vector vector)
void OOGLPerspective(double fovy, double aspect, double zNear, double zFar)
OOMatrix OOGLPopModelView(void)
void OOGLResetProjection(void)
OOMatrix OOGLPopProjection(void)
BOOL OOCheckOpenGLErrors(NSString *format,...)
Definition OOOpenGL.m:38
#define OOGL(statement)
Definition OOOpenGL.h:262
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
struct OOPixMap OOPixMap
return nil
#define GL_CLAMP_TO_EDGE
BOOL OOCubeMapsAvailable(void)
Definition OOTexture.m:643
OOINLINE Vector vector_flip(Vector v) INLINE_CONST_FUNC
Definition OOVector.h:227
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
struct Vector Vector
const Vector kZeroVector
Definition OOVector.m:27
#define PLAYER
void renderOpaqueParts()
Definition OODrawable.m:33
voidpf void uLong size
Definition ioapi.h:126
void * pixels
Definition OOPixMap.h:44