[英]Opengl : Render To Cubemap?
我正在嘗試將 3d 場景渲染到立方體貼圖,但立方體貼圖僅渲染天空盒,這是我的 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;
}
渲染場景 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
}
我正在使用 RenderScene 進行主渲染,它可以工作,但是當我在 renderToCubemap 中使用它時,它只顯示天空盒。
有什么幫助或更正嗎?
我已將“shadowTransforms”替換為:
std::vector<glm::mat4> shadowTransforms;
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(glm::lookAt(position, position + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
我還添加了一個深度緩沖區:
unsigned int m_CubemapDepthRBO;
glGenRenderbuffers(1, &m_CubemapDepthRBO);
glBindRenderbuffer(GL_RENDERBUFFER, m_CubemapDepthRBO);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, (int)resolution, (int)resolution);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_CubemapDepthRBO);
現在工作正常,感謝 Nicole Polas 和 Rabbid76 的快速響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.