Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 阴影贴图:整个网格处于阴影中,根据深度贴图,应该在哪里没有灯光_C++_Opengl_Glsl_Shadow_Shadow Mapping - Fatal编程技术网

C++ 阴影贴图:整个网格处于阴影中,根据深度贴图,应该在哪里没有灯光

C++ 阴影贴图:整个网格处于阴影中,根据深度贴图,应该在哪里没有灯光,c++,opengl,glsl,shadow,shadow-mapping,C++,Opengl,Glsl,Shadow,Shadow Mapping,第一次尝试使用openGL和glsl着色器语言实现阴影贴图。 我认为第一次渲染纹理的过程是正确的,但是当我比较深度值时,它似乎对所有东西都有阴影 我的透视投影矩阵如下所示: FOV = 90 Aspect = According to the programs window size. (I also tried to put different values here) Near = 2; Far= 10000; 函数初始化帧缓冲区 void OpenGLWin::initDepthMap

第一次尝试使用openGL和glsl着色器语言实现阴影贴图。 我认为第一次渲染纹理的过程是正确的,但是当我比较深度值时,它似乎对所有东西都有阴影

我的透视投影矩阵如下所示:

FOV = 90
Aspect = According to the programs window size. (I also tried to put different values here)
Near = 2;
Far= 10000;
函数初始化帧缓冲区

void OpenGLWin::initDepthMap()
{
    //Framebuffer
    m_glFunctions->glGenFramebuffers(1, &m_frameBuffer);
    m_glFunctions->glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);

    //////////////////////////////////////////////////////////////////////////
    //Texture to render scene to
    m_glFunctions->glGenTextures(1, &m_renderToTexture);
    //Bind created texture to make it current
    m_glFunctions->glBindTexture(GL_TEXTURE_2D, m_renderToTexture);

    //Creates an empty texture of specified size.
    //m_glFunctions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    m_glFunctions->glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
    m_glFunctions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

    m_glFunctions->glDrawBuffer(GL_NONE);
    m_glFunctions->glReadBuffer(GL_NONE);

    // Always check that our framebuffer is ok
    if (m_glFunctions->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        qDebug() << "FrameBuffer not OK";
        return;
    }

    m_glFunctions->glBindFramebuffer(GL_FRAMEBUFFER, 0);   
}
过程1顶点着色器

#version 330 core
//Passthrough vertex shader

uniform mat4 depthMVP;

//Vertex received from the program
layout(location = 0) in vec3 vertexPosition_modelspace;

void main(void)
{
    //Output position of vertex in clip space
    gl_Position = depthMVP * vec4(vertexPosition_modelspace, 1);
}
#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

out vec4 ShadowCoord;

// Values that stay constant for the whole mesh.
uniform mat4 mvp;
uniform mat4 depthBiasMVP;


void main(){

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  mvp * vec4(vertexPosition_modelspace,1);

    ShadowCoord = depthBiasMVP * vec4(vertexPosition_modelspace,1);

}
过程1片段着色器

#version 330 core
//Render to texture

// Ouput data
layout(location = 0) out float depthValue;

void main(void)
{
    depthValue = gl_FragCoord.z;
}
#version 330 core

in vec4 ShadowCoord;

// Ouput data
layout(location = 0) out vec3 color;

// Values that stay constant for the whole mesh.
uniform sampler2D shadowMap;


void main(){

    float visibility=1.0;


    vec3 ProjCoords = ShadowCoord.xyz / ShadowCoord.w;
    vec2 UVCoords;
    UVCoords.x = 0.5 * ProjCoords.x + 0.5;
    UVCoords.y = 0.5 * ProjCoords.y + 0.5;
    float z = 0.5 * ProjCoords.z + 0.5;
    float Depth = texture(shadowMap, UVCoords).z;//or x
    if (Depth < (z + 0.00001)){
        visibility = 0.1;
    }


    color = visibility*vec3(1,0,0);

}
过程2顶点着色器

#version 330 core
//Passthrough vertex shader

uniform mat4 depthMVP;

//Vertex received from the program
layout(location = 0) in vec3 vertexPosition_modelspace;

void main(void)
{
    //Output position of vertex in clip space
    gl_Position = depthMVP * vec4(vertexPosition_modelspace, 1);
}
#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

out vec4 ShadowCoord;

// Values that stay constant for the whole mesh.
uniform mat4 mvp;
uniform mat4 depthBiasMVP;


void main(){

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  mvp * vec4(vertexPosition_modelspace,1);

    ShadowCoord = depthBiasMVP * vec4(vertexPosition_modelspace,1);

}
过程2片段着色器

#version 330 core
//Render to texture

// Ouput data
layout(location = 0) out float depthValue;

void main(void)
{
    depthValue = gl_FragCoord.z;
}
#version 330 core

in vec4 ShadowCoord;

// Ouput data
layout(location = 0) out vec3 color;

// Values that stay constant for the whole mesh.
uniform sampler2D shadowMap;


void main(){

    float visibility=1.0;


    vec3 ProjCoords = ShadowCoord.xyz / ShadowCoord.w;
    vec2 UVCoords;
    UVCoords.x = 0.5 * ProjCoords.x + 0.5;
    UVCoords.y = 0.5 * ProjCoords.y + 0.5;
    float z = 0.5 * ProjCoords.z + 0.5;
    float Depth = texture(shadowMap, UVCoords).z;//or x
    if (Depth < (z + 0.00001)){
        visibility = 0.1;
    }


    color = visibility*vec3(1,0,0);

}
#版本330核心
在vec4阴影坐标中;
//输出数据
布局(位置=0)输出vec3颜色;
//对于整个网格保持不变的值。
均匀采样二维阴影图;
void main(){
浮动能见度=1.0;
vec3 ProjCoords=ShadowCoord.xyz/ShadowCoord.w;
vec2-UVCoords;
UVCoords.x=0.5*ProjCoords.x+0.5;
UVCoords.y=0.5*ProjCoords.y+0.5;
浮动z=0.5*ProjCoords.z+0.5;
浮动深度=纹理(阴影贴图、UVCoords).z;//或x
如果(深度<(z+0.00001)){
能见度=0.1;
}
颜色=可见度*vec3(1,0,0);
}

禁用纹理比较有一个原因。这仅在与
sampler2DShadow
一起使用时有效,并且您显然没有在代码中使用它,因为您的纹理坐标是二维的

这意味着更换以下代码: 换言之:
同样,在非阴影纹理上使用
glu-LINEAR
过滤也是个坏主意。这将平均4个最接近的深度值,并返回单个深度。但这并不是反别名阴影的正确方法;实际上,您希望平均4个深度测试的结果,而不是对4个深度的平均值进行单个测试。

更改为“无”。我在着色器中的“阴影坐标”看起来合适吗?我想当我试图将它转换到深度纹理的空间时,问题就出现了。找到了问题。启用纹理比较是问题之一,以及我如何采样阴影贴图将.z更改为.x。我认为我需要.x,因为深度值只是一个分量,而不是向量<代码>浮动深度=纹理(阴影贴图,UVCoords).x老实说,在核心GL 3.3中,即使深度纹理只有一个组件,它也应该有效地返回以下内容:
(r,r,r,1)
。这意味着
纹理(阴影贴图,UVCoords).x应给出与
纹理(阴影贴图,UVCoords).z
相同的值。在兼容性配置文件中有一个影响此行为的深度纹理模式,但在core中,它应该始终按照我描述的方式进行。