2016-10-17 86 views
0

你好,我正在嘗試在openGL中使用C++實現陰影。 我創建了一個FrameBuffer和DepthTexture。每一幀I m rendering my entities to the FrameBuffer. For now I m只是像其他任何GUI一樣在屏幕上顯示紋理,但紋理完全是白色,並且在移動相機時不會改變。我希望你能幫我找到問題。從FrameBuffer渲染DepthTexture

我的代碼:



    //Creating FrameBuffer + Texture 
    glGenFramebuffers(1, &depthMapFBO); 
    glGenTextures(1, &depthMap); 
    glBindTexture(GL_TEXTURE_2D, depthMap); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); 
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0); 
    glDrawBuffer(GL_NONE); 
    glReadBuffer(GL_NONE); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 

在渲染:



    glm::mat4 lightSpaceMatrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, Camera::ZNEAR, Camera::ZFAR) * glm::lookAt(m_position, m_position + m_forward, m_up); 
    m_shader.Bind(); 
    m_shader.loadMat4("lightSpaceMatrix", lightSpaceMatrix); 

    //Bind the FrameBuffer 
    glViewport(0, 0, m_width, m_height); 
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); 
    glClear(GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_DEPTH_TEST); 

    //Render all Entitys 
    for (auto entity : m_entitys) { 
     m_shader.loadMat4("model", entity->getTransform()->getModel()); 
     entity->getTexturedModel()->getMesh()->Draw(); 
    } 

    //Unbind the FrameBuffer 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    glViewport(0, 0, Setting::width, Setting::height); 
    m_shader.unbind(); 

我的頂點着色器:



    #version 430 
    in layout(location=0) vec3 position; 
    uniform mat4 lightSpaceMatrix; 
    uniform mat4 model; 

    void main() 
    { 
     gl_Position = lightSpaceMatrix * model * vec4(position, 1.0f); 
    } 

我的片段着色器:



    #version 430 
    void main() 
    { 
     gl_FragColor = vec4(1); 
    } 

+2

您希望用'gl_FragColor = vec4(1);'顯示什麼顏色? – mlkn

+0

因爲我沒有顏色附件,所以我認爲片段着色器中發生了什麼並不重要,所以我只是將gl_FragColor設置爲vec4(1) – Philipp98

+0

您是否試圖通過該投影矩陣正常渲染場景?也許沒有任何東西會被寫入,並且你只是在深度緩衝區中清除全部白色。另外,你有沒有正確的glDepthFunc? – mlkn

回答

0

使用我的正常投影矩陣,而不是正確的矩陣它工作得很好。所以我嘗試了一下,當我創建了正交矩陣時,近平面和遠平面被設置爲0。我修好了,現在正在工作。感謝您的幫助。

相關問題