2014-11-23 193 views
0

我嘗試在第一遍渲染幾何圖形,並在第二遍渲染時將其從緩衝區渲染到四邊形。
直接渲染幾何圖形到屏幕上工作,但只要我嘗試渲染到具有多遍的四邊形,屏幕就會保持黑色(透明色爲灰色)。
我認爲我的着色器可能是問題所在。如果需要,請索取更多代碼。
OpenGL延遲渲染不起作用

頂點着色器:

#version 440 

layout(location = 0) in vec3 vertexPosition; 
layout(location = 1) in vec2 vertexUV; 
layout(location = 2) in vec3 vertexNormal; 

centroid out vec2 UV; 
out vec4 position; 
out vec3 normal; 

uniform mat4 uniMatModel; 
uniform mat4 uniMatView; 
uniform mat4 uniMatProjection; 
uniform mat4 uniMatModelView; 
uniform mat4 uniMatModelViewProjection; 
uniform mat3 uniMatNormal; 

void main() 
{ 
    UV = vertexUV; 
    position = uniMatModelView * vec4(vertexPosition, 1.0); 
    normal = normalize(uniMatNormal * vertexNormal); 
    gl_Position = uniMatModelViewProjection * vec4(vertexPosition, 1.0); 
} 



片段着色器:

#version 440 

centroid in vec2 UV; 
in vec4 position; 
in vec3 normal; 

layout (location = 0) out vec4 fragColor; 
layout (location = 1) out vec3 positionData; 
layout (location = 2) out vec3 normalData; 
layout (location = 3) out vec3 colorData; 

subroutine void renderPassType(); 
subroutine uniform renderPassType renderPass; 

uniform sampler2D uniSamTexture; 
uniform sampler2D uniSamAlpha; 

layout(binding = 0) uniform sampler2D positionTex; 
layout(binding = 1) uniform sampler2D normalTex; 
layout(binding = 2) uniform sampler2D colorTex; 

subroutine (renderPassType) 
void renderWorld() 
{ 
    if (texture2D(uniSamAlpha, UV).rgb[0] == 1.0) // Alphamaps have to be inverted! 
    { 
     discard; 
     colorData = vec3(0.0); 
    } 
    else 
    { 
     colorData = texture2D(uniSamTexture, UV).rgb; 
    } 

    positionData = vec3(position.x, position.y, position.z); 
    normalData = normal; 
} 

subroutine (renderPassType) 
void renderLight() 
{ 
    fragColor = vec4(texture(colorTex, UV).rgb, 1.0); 
} 

void main() 
{ 
    renderPass(); 
} 



編輯:

void C0::Cocaine::makeFBO(GLuint &fbo) 
{ 
    GLuint depthBuf; 
    Texture 
     posTex(this), 
     normTex(this), 
     colorTex(this) 
    ; 

    // Create and bind the FBO 
    glGenFramebuffers(1, &fbo); 
    glBindFramebuffer(GL_FRAMEBUFFER, fbo); 

    // The depth buffer 
    glGenRenderbuffers(1, &depthBuf); 
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuf); 
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, static_cast<GLsizei>(resolution.x), static_cast<GLsizei>(resolution.y)); 

    // Create the textures for position, normal and color 
    posTex.createGBuf(GL_TEXTURE0, GL_RGB32F, static_cast<int>(resolution.x), static_cast<int>(resolution.y)); 
    normTex.createGBuf(GL_TEXTURE1, GL_RGB32F, static_cast<int>(resolution.x), static_cast<int>(resolution.y)); 
    colorTex.createGBuf(GL_TEXTURE2, GL_RGB8, static_cast<int>(resolution.x), static_cast<int>(resolution.y)); 

    // Attach the textures to the framebuffer 
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuf); 
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, posTex.getTextureID(), 0); 
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normTex.getTextureID(), 0); 
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorTex.getTextureID(), 0); 

    GLenum drawBuffers[] = { GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 }; 
    glDrawBuffers(4, drawBuffers); 

    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
} 


void C0::Texture::createGBuf(GLenum texUnit, GLenum format, int width, int height) 
{ 
    this->width = width; 
    this->height = height; 
    glActiveTexture(texUnit); 
    glGenTextures(1, &textureID); 
    glBindTexture(GL_TEXTURE_2D, textureID); 
    glTexStorage2D(GL_TEXTURE_2D, 1, format, width, height); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
} 



編輯: render方法:

void C0::Mesh::render() 
{ 
    if (shader != nullptr) 
    { 
     shader->use(); 
     shader->setModelMatrix(modelMatrix); 
    } 

    glBindVertexArray(VAO); 

    if (!is2D) 
    { 
     for (int i = 0x1; i <= 0xB; i++) 
     { 
      if (texture[i] != nullptr) 
      { 
       glBindMultiTextureEXT(GL_TEXTURE0 + i, GL_TEXTURE_2D, texture[i]->getTextureID()); 
      } 
     } 
    } 

    glDrawArrays(GL_TRIANGLES, 0, indexCount); 
    glBindVertexArray(0); 
} 



更多代碼:

pass1->setRenderCallback([&]() { 

    sp->useSubRoutine(srp1); 
    cube->render(); 

    return true; 
}); 

pass2->setRenderCallback([&]() { 

    sp->useSubRoutine(srp2); 
    sp->resetMatrices(); // it should be a 2D quad, so I reset the MVP uniforms to 1.0f 
    sp->lockMVP(true); // workaround (still playing around with everything) 
    dq->render(); // render display quad 
    sp->lockMVP(false); 

    return true; 
}); 

e->setOnRender([&]() { 

    pass1->render(); 
    pass2->render(); 

    /* handle input etc... */ 
}); 



通行證渲染方法:

bool Pass::render() 
{ 
    glBindFramebuffer(GL_FRAMEBUFFER, fbo); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    if (depthTest) 
    { 
     glEnable(GL_DEPTH_TEST); 
    } 
    else 
    { 
     glDisable(GL_DEPTH_TEST); 
    } 

    if (onRender != nullptr) 
    { 
     if (!onRender()) 
     { 
      return false; 
     } 
    } 

    if (fbo != 0) 
    { 
     glFinish(); 
    } 

    return true; 
} 
+1

請**不要編輯您的問題以解決答案中指出的問題。它使答案無效。 – 2014-11-23 15:25:37

+0

您是否檢查過單獨的圖像是否呈現正確? – 2014-11-23 16:10:58

+0

@ReetoKoradi:這不是答案...... – 2014-11-23 19:07:16

回答

2

你們用着色器版本字符串:

#version 440 

配置文件的默認值是core。從GLSL 4.40規格:

配置文件參數只能與版本150或更高版本一起使用。如果未提供配置文件參數且版本爲150或更高版本,則默認爲核心

您在片段着色器中調用的texture2D()函數僅在兼容性配置文件中可用。在覈心配置文件,您需要使用重載texture()

if (texture(uniSamAlpha, UV).rgb[0] == 1.0) // Alphamaps have to be inverted! 
... 
    colorData = texture(uniSamTexture, UV).rgb; 

如果你有你的着色器的問題,它總是一個好主意,檢查編譯成功。您可以使用以下方式撥打電話:

glGetShaderiv(shaderId, GL_COMPILE_STATUS, ...); 
glGetShaderInfoLog(shaderId, ...); 
+0

着色器編譯和鏈接成功,沒有任何錯誤或警告。 Ť exture2D()函數到texture()並沒有解決問題。 – 2014-11-23 13:44:06

+1

它可能不會在所有平臺上編譯。有些GLSL編譯器會忽略很多錯誤,但有些則不會。 – 2014-11-23 14:10:21