使用立方体贴图时,我的着色器(而不是程序)的结果不一致。

出于测试目的,我编写了一个测试程序,该程序仅创建一个深度立方体贴图纹理,并在其所有侧面写入“ 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;
}


片段着色器:

#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,无论我将其用作方向矢量是什么都应该是不可能的。

我想念什么?

最佳答案

您始终颠倒了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

10-07 21:29