31#import "MyOpenGLView.h"
44#if OO_USE_FBO && OO_TEXTURE_CUBE_MAP
46@interface OOEnvironmentCubeMap (Private)
55@implementation OOEnvironmentCubeMap
57- (id) initWithSideLength:(GLuint)size
65 if ((
self = [super init]))
84 if (_textureName == 0) [
self setUp];
89 OOGL(glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT));
93 OOGL(glViewport(0, 0, _size, _size));
109 OODrawable *sky = [[UNIVERSE nearestEntityMatchingPredicate:HasClassPredicate parameter:[
SkyEntity class] relativeToEntity:nil] drawable];
111 NSArray *planets = [UNIVERSE planets];
114 Vector centers[6] = { { 1, 0, 0 }, { -1, 0, 0 }, { 0, 1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 } };
115 Vector ups[6] = { { 0, -1, 0 }, { 0, -1, 0 }, { 0, 0, 1 }, { 0, 0, -1 }, { 0, -1, 0 }, { 0, -1, 0 } };
117 for (i = 0; i < 6; i++)
120 Vector center = centers[i];
124 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
125 [
self renderOnePassWithSky:sky sun:sun planets:planets];
132 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
141 OOGL(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
143 OOGL(glClear(_planets ? GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT : GL_COLOR_BUFFER_BIT));
145 OOGL(glDepthMask(GL_FALSE));
147 OOGL(glDepthMask(GL_TRUE));
153 NSEnumerator *planetEnum =
nil;
154 OOPlanetEntity *planet =
nil;
155 for (planetEnum = [planets objectEnumerator]; (planet = [planetEnum nextObject]); )
160 [[planet drawable] renderOpaqueParts];
162 [planet drawUnconditionally];
169 [sun drawUnconditionally];
178 if (_textureName != 0)
return;
179 _planets = [UNIVERSE reducedDetail];
181 OOGL(glGenTextures(1, &_textureName));
182 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, _textureName));
186 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
187 OOGL(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
189 OOGL(glGenFramebuffersEXT(6, _fbos));
190 OOGL(glGenRenderbuffersEXT(6, _depthBuffers));
193 for (i = 0; i < 6; i++)
195 GLenum textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
197 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
199 OOGL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, _depthBuffers[i]));
200 OOGL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _size, _size));
201 OOGL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, _depthBuffers[i]));
203 OOGL(glTexImage2D(textarget, 0, GL_RGBA8, _size, _size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
204 OOGL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, textarget, _textureName, 0));
209 OOGL(status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
210 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
212 OOLogERR(
@"environmentCube.fbo.setup.failed",
@"Failed to set up FBO for environment cube map - status is %u.", status);
218 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, 0));
219 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
220 OOGL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0));
229 OOGL(glBindTexture(GL_TEXTURE_CUBE_MAP, _textureName));
235 return NSMakeSize(_size, _size);
242 if (_textureName == 0)
return;
244 OOGL(glDeleteTextures(1, &_textureName));
247 OOGL(glDeleteFramebuffersEXT(6, _fbos));
248 OOGL(glDeleteRenderbuffersEXT(6, _depthBuffers));
258- (struct
OOPixMap) copyPixMapRepresentation
266 for (i = 0; i < 6; i++)
268 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[i]));
269 OOGL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
270 uint8_t *target = pixmap.
pixels;
271 target += pixmap.rowBytes * _size * i;
272 OOGL(glReadPixels(0, 0, _size, _size, GL_RGBA, GL_UNSIGNED_BYTE, target));
275 OOGL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
#define OOLogERR(class, format,...)
#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,...)
const OOPixMap kOONullPixMap
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
BOOL OOIsValidPixMap(OOPixMap pixMap)
BOOL OOCubeMapsAvailable(void)