gpt4 book ai didi

opengl - 为什么 OpenGL + GLSL 着色器不输出任何内容?

转载 作者:行者123 更新时间:2023-12-04 05:50:11 24 4
gpt4 key购买 nike

总之,我的全屏着色器没有输出任何东西,

我正在 GLSL 中编写一个延迟着色器,并且我已经到了在屏幕空间中应用灯光的阶段。我有一个环境 channel ,我基本上将漫反射强度从 GBuffer 写入默认 FBO,这很好用。然后我应用另一个全屏四边形来计算场景中的定向光,这也是来自 GBUffer 的样本,并且应该与默认的 FBO 混合。

我遇到的问题是定向光着色器不输出任何内容。我使用与环境 channel 相同的矩阵和转换,但没有任何变化。

我尝试了以下方法;

  • 在环境 channel 中的每个片段上输出相同的颜色以确保它完全输出。 (作品)
  • 在定向光 channel 中的每个片段上输出相同的颜色。 (还是一无所获)
  • 对 XY 顶点坐标使用屏幕分辨率而不是标准化坐标;
  • 这适用于环境传递,因为我可以看到结果按预期变化。
  • 它对定向光传递没有任何改变,我得到相同的输出。
  • 将环境着色器更改为从法线或位置缓冲区采样,以确保可以从(工作)
  • 采样纹理

    这是应用程序代码;
    glBindFramebuffer(GL_READ_FRAMEBUFFER, m_GeometryBuffer->m_Name);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glDrawBuffers(1,draw_buffers);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_AmbientShader->Begin();
    m_AmbientShader->SetUniformInt1("Diffuse",m_DiffuseTexture->m_TextureUnit);
    // Draw full screen quad
    glBegin(GL_QUADS);
    glMultiTexCoord2i(GL_TEXTURE0,0,0);
    glVertex2i(-1,-1);
    glMultiTexCoord2i(GL_TEXTURE0,1,0);
    glVertex2i(1,-1);
    glMultiTexCoord2i(GL_TEXTURE0,1,1);
    glVertex2i(1,1);
    glMultiTexCoord2i(GL_TEXTURE0,0,1);
    glVertex2i(-1,1);
    glEnd();
    m_AmbientShader->End();

    const DirectionalLight& dl = scene.GetDirectionalLight();

    glDrawBuffers(1, draw_buffers);
    m_DirectionalLightShader->Begin();
    m_DirectionalLightShader->SetUniformInt1("Diffuse",m_DiffuseTexture->m_TextureUnit);
    m_DirectionalLightShader->SetUniformInt1("Position",m_PositionTexture->m_TextureUnit);
    m_DirectionalLightShader->SetUniformInt1("Normal",m_NormalTexture->m_TextureUnit);
    m_DirectionalLightShader->SetUniformFloat3("LightDir",dl.GetDirection());
    m_DirectionalLightShader->SetUniformFloat3("LightAmb",dl.GetAmb());
    m_DirectionalLightShader->SetUniformFloat3("LightDiff",dl.GetDiff());
    m_DirectionalLightShader->SetUniformFloat3("LightSpec",dl.GetSpec());
    m_DirectionalLightShader->SetUniformFloat3("LightAtt",dl.GetAtt());
    m_DirectionalLightShader->SetUniformFloat1("LightIntensity",dl.GetIntensity());
    // Draw full screen quad
    glBegin(GL_QUADS);
    glMultiTexCoord2i(GL_TEXTURE0,0,0);
    glVertex2i(-1,-1);
    glMultiTexCoord2i(GL_TEXTURE0,1,0);
    glVertex2i(1,-1);
    glMultiTexCoord2i(GL_TEXTURE0,1,1);
    glVertex2i(1,1);
    glMultiTexCoord2i(GL_TEXTURE0,0,1);
    glVertex2i(-1,1);
    glEnd();
    m_DirectionalLightShader->End();

    如果我的数学错了,请原谅我,我一直在使用着色器来尝试获得任何类型的输出,但这里是着色器源文件;

    <-- 方向光.vert -->
    varying out vec2 ScreenPos;

    varying out vec3 VSLightDir;

    uniform vec3 LightDir;
    uniform vec3 LightAmb;
    uniform vec3 LightDiff;
    uniform vec3 LightSpec;
    uniform float LightIntensity;

    void main()
    {
    gl_Position = gl_Vertex;
    ScreenPos = gl_MultiTexCoord0.st;
    VSLightDir = normalize((gl_ModelViewMatrix * vec4(LightDir,0.0)).xyz);
    }

    <-- 方向光.frag -->
    // Gbuffer textures
    uniform sampler2D Diffuse;
    uniform sampler2D Position;
    uniform sampler2D Normal;

    in vec2 ScreenPos;
    uniform vec3 LightDir;
    uniform vec3 LightAmb;
    uniform vec3 LightDiff;
    uniform vec3 LightSpec;
    uniform float LightIntensity;

    in vec3 VSLightDir;
    out vec4 OutColour;

    void main()
    {
    // Sample from the GBuffer
    vec4 Diffuse = texture2D(Diffuse, ScreenPos);
    vec4 VSPos = texture2D(Position, ScreenPos);
    vec4 MRT2 = texture2D(Normal, ScreenPos);
    vec3 VSNormal = normalize(MRT2.xyz);
    float Shininess = MRT2.w;

    float LDotN = dot(VSNormal, VSLightDir);

    vec3 View = -normalize(VSPos.xyz);

    // Compute ambient contribution
    vec3 ambient = LightIntensity * LightAmb * Diffuse.xyz;

    // Compute diffuse contribution
    vec3 diffuse = LightIntensity * LightDiff * Diffuse.xyz * max(0.0, LDotN);

    // Compute specular contribution
    vec3 HalfWay = normalize(LightDir + View);
    vec3 specular = (LDotN <= 0.0)
    ? vec3(0.0)
    : LightIntensity * LightSpec * Diffuse.xyz * pow(max(0.0, dot(VSNormal,HalfWay)), Shininess);

    OutColour = vec4(ambient + diffuse + specular, Diffuse.a);
    //OutColour = vec4(0.0,0.0,1.0,1.0); //Debug line
    }

    <-- 环境.vert -->
    varying out vec2 ScreenPos;

    void main()
    {
    gl_Position = gl_Vertex;

    // Used for the position to sample from GBuffer
    ScreenPos = gl_MultiTexCoord0.st;
    }

    <-- 环境.frag -->
    // Gbuffer textures
    uniform sampler2D Diffuse;

    in vec2 ScreenPos;

    out vec4 OutColour;

    void main()
    {
    OutColour = texture2D(Diffuse, ScreenPos);
    //OutColour = vec4(0.0,0.0,1.0,1.0);
    }

    最佳答案

    您是否启用了深度测试?我看到您正在清除深度缓冲区,所以我怀疑可能,但是您正在绘制与第一个纹理完全相同的深度的第二个纹理。与 glDepthFunc默认为 GL_LESS这将导致第二次抽签被完全拒绝。

    否则,您希望如何将两次抽奖结合起来?是否应该涉及一些混合?

    关于opengl - 为什么 OpenGL + GLSL 着色器不输出任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161084/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com