Hi,
I am trying to render a scene to a cube map but the faces are not fitting together. I think there is something wrong with the perspective:
I define a framebuffer and render the scene six times with different viewing directions (one renderpass for each face of the cube map). Then I place the camera at the origin and render a unit cube with the cube map.
The creation of the cube map:
TextureCubeMap::TextureCubeMap(const unsigned int size) : _size(size), _boundSlot(-1){
glGenTextures(1, &_texID);
glBindTexture(GL_TEXTURE_CUBE_MAP, _texID);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0);
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);
for (int loop = 0; loop < 6; ++loop)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + loop, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
}
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
int TextureCubeMap::bind(){
...
glEnable(GL_TEXTURE0 + _boundSlot);
glActiveTexture(GL_TEXTURE0 + _boundSlot);
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, _texID);
...
return _boundSlot;
}
void TextureCubeMap::unbind(){
...
glActiveTexture(GL_TEXTURE0 + _boundSlot);
glBindTexture(GL_TEXTURE_2D, 0);
...
}
And this is the part where I render the scene to the cube map sixe times:
void RenderToCubeMap::render(SceneContext& scene_context){
glBindFramebuffer(GL_FRAMEBUFFER, _fboID);
const GLenum drawBuffer = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, &drawBuffer);
glViewport(0, 0, (GLsizei)_cubeMap->getSize(), (GLsizei)_cubeMap->getSize());
SceneContext cubeMapContext = scene_context; //Contains all relevant matrices like projection-, view- and modelmatrix
cubeMapContext._P = glm::perspective(90.f, 1.f, 1.f, 1000.f);
_cubeMap->bind();
for (unsigned int iFace = 0; iFace < 6; iFace++){
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + iFace, _cubeMap->getTexID(), 0);
IRenderPass::startRenderPass(); //Enable depth test, backface culling, clear depth & draw buffer
glm::vec3 viewDir, up;
switch (GL_TEXTURE_CUBE_MAP_POSITIVE_X + iFace){
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
viewDir = glm::vec3(1.0f, 0.0f, 0.0f);
up = glm::vec3(0.f, -1.f, 0.f);
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
viewDir = glm::vec3(-1.0f, 0.0f, 0.0f);
up = glm::vec3(0.f, -1.f, 0.f);
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
viewDir = glm::vec3(0.0f, 1.0f, 0.0f);
up = glm::vec3(0.f, 0.f, 1.f);
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
viewDir = glm::vec3(0.0f, -1.0f, 0.0f);
up = glm::vec3(0.f, 0.f, -1.f);
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
viewDir = glm::vec3(0.0f, 0.0f, 1.0f);
up = glm::vec3(0.f, -1.f, 0.f);
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
viewDir = glm::vec3(0.0f, 0.0f, -1.0f);
up = glm::vec3(0.f, -1.f, 0.f);
break;
}
auto viewMatrix = glm::lookAt(glm::vec3(scene_context._camPos), glm::vec3(viewDir + scene_context._camPos), up);
cubeMapContext._V = viewMatrix;
cubeMapContext._MV = viewMatrix * cubeMapContext._M;
cubeMapContext._MVP = cubeMapContext._P * cubeMapContext._MV;
_sceneGraph->renderSubtree(cubeMapContext);
}
_cubeMap->unbind();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
IRenderPass::endRenderPass(); //Disable depth test, backface culling
}
What am I doing wrong? Is there something wrong with the projection matrix? I am really thankful for any input.
Best regards,
Stan