我曾尝试为对象反射实现动态立方体贴图。我几乎成功了,除了我在反射中的对象上注意到的一些人工制品。
这些是我用来设置和渲染立方体贴图的主要函数。
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);
}
有没有人见过这种问题?我怀疑这与深度缓冲区有关,因为我最近安装了一个缓冲区来对抗更糟糕的文物。
2条答案
按热度按时间r7s23pms1#
我设法通过缩短远平面和近平面之间的距离来使这些人工制品消失。
1yjd4xko2#
使用“glClear(GL_COLOR_BUFFER_BIT|GL_Depth_BUFFER_BIT|GL_STEMWEL_BUFFER_BIT)”进行第一次面渲染。
然后,对于所有其他面渲染,仅使用“glClear(GL_Depth_Buffer_Bit|GL_Stensel_Buffer_Bit);”。
(您的清算可能略有不同,但您将使用以下逻辑使其发挥作用)