OpenGL问题:当我尝试绘制我的深度纹理时,我只能得到白色。

huangapple go评论65阅读模式
英文:

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&lt;const char*&gt; shaderCodeQpsd, std::vector&lt;model*&gt; models) {
	unsigned int textureID;
	glGenTextures(1, &amp;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, &amp;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 &lt;&lt; &quot;Framebuffer not complete!&quot; &lt;&lt; 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 &lt; models.size(); ++i) models[i]-&gt;draw();
	glDeleteProgram(shaderProgram);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	return textureID;
}

the function renderInit() and GLSL code:

unsigned int renderInit(std::vector&lt;const char*&gt; shaderCodeQ){
	unsigned int shaderProgram;
	unsigned int vertexShader, fragmentShader;
	vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &amp;shaderCodeQ[0], NULL);
	glCompileShader(vertexShader);
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &amp;shaderCodeQ[1], NULL);
	glCompileShader(fragmentShader);
	shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	if (shaderCodeQ.size() &gt; 2) {
		unsigned int geometryShader;
		geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
		glShaderSource(geometryShader, 1, &amp;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 = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos, 1.0f);\n&quot;
&quot;}
const char* pshadowVertexShader = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos, 1.0f);\n&quot;
&quot;}\0&quot;;
const char* pshadowFragShader = &quot;#version 330 core\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;}\0&quot;;
&quot;; const char* pshadowFragShader = &quot;#version 330 core\n&quot; &quot;void main()\n&quot; &quot;{\n&quot; &quot;}
const char* pshadowVertexShader = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos, 1.0f);\n&quot;
&quot;}\0&quot;;
const char* pshadowFragShader = &quot;#version 330 core\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;}\0&quot;;
&quot;;

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 = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec2 TexCoord;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n&quot;
&quot;}
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 = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec2 TexCoord;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n&quot;
&quot;}\0&quot;;
const char* pFragShadowTest = &quot;#version 330 core\n&quot;
&quot;out vec4 FragColor;\n&quot;
&quot;in vec2 TexCoord;\n&quot;
&quot;uniform sampler2D ourTexture;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n&quot;
&quot;}\n\0&quot;;
&quot;; const char* pFragShadowTest = &quot;#version 330 core\n&quot; &quot;out vec4 FragColor;\n&quot; &quot;in vec2 TexCoord;\n&quot; &quot;uniform sampler2D ourTexture;\n&quot; &quot;void main()\n&quot; &quot;{\n&quot; &quot; FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n&quot; &quot;}\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 = &quot;#version 330 core\n&quot;
&quot;layout (location = 0) in vec3 aPos;\n&quot;
&quot;out vec2 TexCoord;\n&quot;
&quot;uniform mat4 transform;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   gl_Position = transform * vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n&quot;
&quot;	TexCoord = vec2(aPos.x/2+0.5, aPos.y/2+0.5);\n&quot;
&quot;}\0&quot;;
const char* pFragShadowTest = &quot;#version 330 core\n&quot;
&quot;out vec4 FragColor;\n&quot;
&quot;in vec2 TexCoord;\n&quot;
&quot;uniform sampler2D ourTexture;\n&quot;
&quot;void main()\n&quot;
&quot;{\n&quot;
&quot;   FragColor = vec4(vec3(texture(ourTexture, TexCoord).r),1.0f);\n&quot;
&quot;}\n\0&quot;;
&quot;;

答案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.

huangapple
  • 本文由 发表于 2023年2月24日 15:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定