I've attempted implementing a dynamic cube map for object reflections. I'm nearly successful except for some artifacts that I'm noticing on the object in the reflection.
These are the major functions I use to setup and render to the cube map.
bool setup(GLsizei width = 256, GLsizei height = 256)
{
// The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
if (!fbo)
glGenFramebuffers(1, &fbo);
if (!cubeMap)
glGenTextures(1, &cubeMap);
if (!depthrenderbuffer)
glGenRenderbuffers(1, &depthrenderbuffer); // The depth buffer
for (int i = 0; i < 6; ++i)
{
if (facesFbo[i])
break;
glGenFramebuffers(1, &facesFbo[i]);
//NSLog(@"Generated facesFbo[%u]", i);
}
GLint mainFrameBuffer; // save the previous framebuffer, most probably the main one
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mainFrameBuffer);
//setup the texture
glActiveTexture(GL_TEXTURE5); // for mixing textures and cubemaps
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
//glEnable(GL_TEXTURE_CUBE_MAP);
// allocate
if (!allocatedTextures)
{
for (int i = 0; i < 6; ++i)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
allocatedTextures = true;
}
//bind it
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, cubeMap, 0);
// set the texture rendering targets
//for (int i = GL_TEXTURE_CUBE_MAP_POSITIVE_X, len = i + 6; i < len; ++i)
if (!attachTextures)
{
for (int i = 0; i < 6; ++i)
{
glBindFramebuffer(GL_FRAMEBUFFER, facesFbo[i]);
//depth
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubeMap, 0);
}
attachTextures = true;
}
//bring it back
glActiveTexture(GL_TEXTURE0);
// Set the list of draw buffers.
glDrawBuffer(GL_COLOR_ATTACHMENT0); // "1" is the size of DrawBuffers
// restore
glBindFramebuffer(GL_FRAMEBUFFER, mainFrameBuffer);
// Always check that our framebuffer is ok
return (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE);
}
void beginDrawFace(GLenum textarget, GLsizei fxWidth = 256, GLsizei fxHeight = 256)
{
// save the previous frame buffer
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mainFrameBuffer);
// and the viewport resolution
glGetIntegerv(GL_VIEWPORT, &vp[0]);
// just in case, turn off cube mapping
glDisable(GL_TEXTURE_CUBE_MAP);
// bind it cuz we doin stuff to it
glViewport(0, 0, fxWidth, fxHeight);
//glBindFramebuffer(GL_FRAMEBUFFER, fbo);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + textarget, cubeMap, 0);
glBindFramebuffer(GL_FRAMEBUFFER, facesFbo[textarget]);
// clear stuff out
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f); //used to be 2
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void endDrawFace()
{
// restore
glViewport(vp[0], vp[1], vp[2], vp[3]);
glBindFramebuffer(GL_FRAMEBUFFER, mainFrameBuffer);
}
Has anyone seen this kind of problem? I suspect that it has to do with the depth buffer as I recently attached one to fight artifacts that were much worse.
I managed the make those artifacts go away by shortening the distance between the far and near plane.
Use "glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT )" for the FIRST face render..
Then use only "glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); " for all THE OTHER face renders..
(your clears may differ slightly, but you'll make it work using this logic)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.