英文:
OpenGL problem: I can only get white when I trying to draw my depth texture
问题
我尝试在一个正方形上绘制深度纹理,但只有白色,如下所示 白色正方形和小屋。此外,我还尝试生成点光源的阴影立方体贴图,但仍然只是白色。
以下是生成阴影贴图的代码:
unsigned int genParaShadowTex(unsigned int width, unsigned int height, glm::vec3 viewPos, glm::vec3 lightDir, std::vector<const char*>& shaderCodeQpsd, std::vector<model*>& models) {
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
unsigned int shadowFBO;
glGenFramebuffers(1, &shadowFBO);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureID, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
glm::mat4 shadowProj = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 30.0f);
glm::mat4 shadowTransform = shadowProj * glm::lookAt(viewPos, glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
unsigned int shaderProgram = renderInit(shaderCodeQpsd);
glUseProgram(shaderProgram);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glClear(GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < models.size(); ++i) models[i]->draw();
glDeleteProgram(shaderProgram);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return textureID;
}
renderInit
函数和 GLSL 代码如下:
unsigned int renderInit(std::vector<const char*>& shaderCodeQ){
unsigned int shaderProgram;
unsigned int vertexShader, fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &shaderCodeQ[0], NULL);
glCompileShader(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &shaderCodeQ[1], NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
if (shaderCodeQ.size() > 2) {
unsigned int geometryShader;
geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometryShader, 1, &shaderCodeQ[2], NULL);
glCompileShader(geometryShader);
glAttachShader(shaderProgram, geometryShader);
}
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
return shaderProgram;
}
我使用以下着色器生成阴影贴图:
const char* pshadowVertexShader = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos, 1.0f);\n"
"}\0";
const char* pshadowFragShader = "#version 330 core\n"
"void main()\n"
"{\n"
"}\0";
然后,我使用以下着色器来显示我的阴影贴图:
float testwall[] = {
-1.0f, 1.0f, 5.0f,
-1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, 1.0f, 5.0f,
-1.0f, 1.0f, 5.0f
}; // 我在这个正方形上绘制阴影贴图
const char* pVertexShadowTest = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec2 TexCoord;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
" TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n"
"}\0";
const char* pFragShadowTest = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
" FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n"
"}\n\0";
英文:
I tried to draw a depth texture on a square but there is only white like this
white square with a cottage
besides, I also tried to generate a point light shadow cubemap, but it is still only white.
here is the shadow map generation code
unsigned int genParaShadowTex(unsigned int width, unsigned int height, glm::vec3 viewPos, glm::vec3 lightDir, std::vector<const char*> shaderCodeQpsd, std::vector<model*> models) {
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
unsigned int shadowFBO;
glGenFramebuffers(1, &shadowFBO);
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureID, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
glm::mat4 shadowProj = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 30.0f);
glm::mat4 shadowTransform = shadowProj * glm::lookAt(viewPos, glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
unsigned int shaderProgram = renderInit(shaderCodeQpsd);
glUseProgram(shaderProgram);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glClear(GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < models.size(); ++i) models[i]->draw();
glDeleteProgram(shaderProgram);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return textureID;
}
the function renderInit() and GLSL code:
unsigned int renderInit(std::vector<const char*> shaderCodeQ){
unsigned int shaderProgram;
unsigned int vertexShader, fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &shaderCodeQ[0], NULL);
glCompileShader(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &shaderCodeQ[1], NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
if (shaderCodeQ.size() > 2) {
unsigned int geometryShader;
geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometryShader, 1, &shaderCodeQ[2], NULL);
glCompileShader(geometryShader);
glAttachShader(shaderProgram, geometryShader);
}
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
return shaderProgram;
}
I generate shadow map by this shader
const char* pshadowVertexShader = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos, 1.0f);\n"
"}const char* pshadowVertexShader = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos, 1.0f);\n"
"}\0";
const char* pshadowFragShader = "#version 330 core\n"
"void main()\n"
"{\n"
"}\0";
";
const char* pshadowFragShader = "#version 330 core\n"
"void main()\n"
"{\n"
"}const char* pshadowVertexShader = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos, 1.0f);\n"
"}\0";
const char* pshadowFragShader = "#version 330 core\n"
"void main()\n"
"{\n"
"}\0";
";
then I show my shadow map by the shader below
float testwall[] = {
-1.0f, 1.0f, 5.0f,
-1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, 1.0f, 5.0f,
-1.0f, 1.0f, 5.0f
};// I draw the shadow map on this square
const char* pVertexShadowTest = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec2 TexCoord;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
" TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n"
"}float testwall[] = {
-1.0f, 1.0f, 5.0f,
-1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, 1.0f, 5.0f,
-1.0f, 1.0f, 5.0f
};// I draw the shadow map on this square
const char* pVertexShadowTest = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec2 TexCoord;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
" TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n"
"}\0";
const char* pFragShadowTest = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
" FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n"
"}\n\0";
";
const char* pFragShadowTest = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
" FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n"
"}\nfloat testwall[] = {
-1.0f, 1.0f, 5.0f,
-1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, -1.0f, 5.0f,
1.0f, 1.0f, 5.0f,
-1.0f, 1.0f, 5.0f
};// I draw the shadow map on this square
const char* pVertexShadowTest = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec2 TexCoord;\n"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
" gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
" TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n"
"}\0";
const char* pFragShadowTest = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
" FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n"
"}\n\0";
";
答案1
得分: 0
我忘记绑定变换矩阵,现在它的工作方式如下[一个小屋的影子!]
在渲染任何场景之前,请注意每个状态。
英文:
I forget to bind the transform matrix, now it works like that a cottage shadow!
be careful about the every state before you are going to render any scene.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论