I am trying to render 3d scene to a cubemap but the cubemap only renders the skybox, here is my RenderToCubemap function:
GLuint RenderToCubemap(glm::vec3 position, float resolution, float nearPlane, float farPlane)
{
unsigned int CM_FBO;
unsigned int textureID;
glGenFramebuffers(1, &CM_FBO);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
for (unsigned int i = 0; i < 6; i++)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, resolution, resolution, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_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);
// attach depth texture as FBO's depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, CM_FBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureID, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), (float)resolution / (float)resolution, nearPlane, farPlane);
std::vector<glm::mat4> shadowTransforms;
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
// Render scene to cubemap
// --------------------------------
glViewport(0, 0, (int)resolution, (int)resolution);
glBindFramebuffer(GL_FRAMEBUFFER, CM_FBO);
Camera capture_cam;
capture_cam.temp_cam = true;
capture_cam.projectionMatrix = shadowProj;
capture_cam.transform.Position = position;
for (size_t i = 0; i < 6; i++)
{
capture_cam.viewMatrix = shadowTransforms[i];
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, textureID, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
RenderScene(capture_cam);
}
return textureID;
}
RenderScene function :
void RenderScene(Camera& cam)
{
glm::mat4 proj = cam.GetProjectionMatrix();
glm::mat4 view = cam.GetViewMatrix();
glm::vec3 viewPos = cam.transform.Position;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Shader* shader = &materials.main_shader;
shader ->use();
shader ->SetMat4("projection", proj);
shader ->SetMat4("view", view);
shader ->SetVec3("CamPos", viewPos);
// Render all meshs
// ----------------------------------------------------
for (size_t i = 0; i < Meshs.size(); i++)
{
if (Meshs[i].Material == nullptr)
continue;
Meshs[i].Material->setShaderProperties(shader);
shader->SetMat4("model", Meshs[i].Transform);
Meshs[i]->Draw();
}
// render skybox
// ----------------------------------------------------
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
Shader* bg = &materials.background;
bg->use();
bg->SetMat4("view", view);
bg->SetMat4("projection", proj);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, envCubemap);
renderCube();
glDepthFunc(GL_LESS); // set depth function back to default
}
I am using RenderScene for main rendering and it works, but when i use it in renderToCubemap it shows only the skybox.
Any help or correction?
Aucun commentaire:
Enregistrer un commentaire