GLSL - 从阴影立方体贴图中检索值时结果不正确

时间:2014-07-22 16:41:45

标签: c++ opengl glsl shadow

使用立方体贴图时,我的着色器与我的程序相比,结果不一致。

出于测试目的,我编写了一个测试程序,它只是创建一个深度立方体贴图纹理并将“1”写入其中:

unsigned int frameBuffer;
glGenFramebuffers(1,&frameBuffer);

unsigned int texture;
glGenTextures(1,&texture);

glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
glBindTexture(GL_TEXTURE_CUBE_MAP,texture);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_DEPTH_TEXTURE_MODE,GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL);
glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_COMPARE_MODE,GL_COMPARE_R_TO_TEXTURE);

unsigned int width = 512;
unsigned int height = 512;
for(unsigned int i=0;i<6;i++)
{
    glTexImage2D(
        GL_TEXTURE_CUBE_MAP_POSITIVE_X +i,
        0,
        GL_DEPTH_COMPONENT16,
        width,height,
        0,GL_DEPTH_COMPONENT,
        GL_FLOAT,
        0
    );
    glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_CUBE_MAP_POSITIVE_X +i,texture,0);
}
unsigned int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
    return;
float *data = new float[width *height];
for(unsigned long long i=0;i<(width *height);i++)
    data[i] = 1.f;
for(unsigned int i=0;i<6;i++)
{
    glTexSubImage2D(
        GL_TEXTURE_CUBE_MAP_POSITIVE_X +i,
        0,
        0,
        0,
        width,height,
        GL_DEPTH_COMPONENT,
        GL_FLOAT,
        &data[0]
    );
}
delete[] data;

// Check to see if data has been written correctly
data = new float[width *height];
for(unsigned int i=0;i<6;i++)
{
    glFramebufferTexture2D(GL_READ_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_CUBE_MAP_POSITIVE_X +i,texture,0);
    glReadPixels(0,0,width,height,GL_DEPTH_COMPONENT,GL_FLOAT,&data[0]);
    for(unsigned long long j=0;j<(width *height);j++)
    {
        if(data[j] != 1.f)
            return;
    }
}
delete[] data;
// Check end    

渲染:

float screenVerts[18] = {
    -1.f,-1.f,0.f,
    1.f,-1.f,0.f,
    -1.f,1.f,0.f,
    -1.f,1.f,0.f,
    1.f,-1.f,0.f,
    1.f,1.f,0.f
};
unsigned int vertexBuffer;
glGenBuffers(1,&vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(float) *18,&screenVerts[0],GL_STATIC_DRAW);

glUseProgram(shader);
glBindTexture(GL_TEXTURE_CUBE_MAP,texture);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
    glDrawArrays(GL_TRIANGLES,0,6);
glDisableVertexAttribArray(0);

顶点着色器:

#version 330 core

layout(location = 0) in vec3 vertPos;

out vec2 UV;

void main()
{
    gl_Position =  vec4(vertPos,1);
    UV = (vertPos.xy +vec2(1,1)) /2.0;
}

Fragment Shader:

#version 330 core

in vec2 UV;

out vec3 color;

uniform samplerCubeShadow testShadow;

void main()
{
    color.r = texture(testShadow,vec4(0,0,1,1)); 
    // Just grab the value from a random direction and put it out as red color
}

(我已经从另一种语言移植了C ++代码,所以如果你在那里发现了一些语法错误,不要介意那些,它们不在实际代码中)

glGetError()不会返回任何错误。

ReadPixels证明写入过程有效,但结果是黑屏。这意味着着色器内部的纹理调用返回0,无论我使用什么作为方向向量,这都应该是不可能的。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您一直有glBind*()来电被忽略的论据。它们都将目标作为第一个参数,并将对象id(也称为名称)作为第二个参数。而不是:

glBindFramebuffer(frameBuffer,GL_FRAMEBUFFER);
glBindTexture(texture,GL_TEXTURE_CUBE_MAP);
glBindBuffer(vertexBuffer,GL_ARRAY_BUFFER);

应该是这样的:

glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

代码中有一些这样的实例,所以请确保全部捕获它们。

除此之外,如果这是完整的代码,您就不会渲染到屏幕上。你有一个没有颜色附件限制的FBO,所以输出无处可去。如果要渲染到屏幕,则需要取消绑定FBO:

glBindFramebuffer(GL_FRAMEBUFFER, 0);

您还要保留片段输出的gb组件未定义,因为您只写r