2014-01-30 109 views
0

自從我上次與VBO合作以來,我一直在努力讓它再次運行。但是,無論我做什麼,除了使用glClearColor之外,我無法獲取任何內容。OpenGL VBO沒有畫任何東西

這是我的代碼:

初始化GL

... 

vmml::vec3f eye = vmml::vec3f(0.0, 0.0, 0.0); 
vmml::vec3f tar = vmml::vec3f(0.0, 0.0, 1.0); 
vmml::vec3f up = vmml::vec3f(0.0, 1.0, 0.0); 
viewMatrix = lookAt(eye, tar, up); 

創建箱

glGenBuffers(1, &vertexBuffer); 
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW); 
glGenBuffers(1, &uvBuffer); 
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer); 
glBufferData(GL_ARRAY_BUFFER, 48 * sizeof(GLfloat), &uvs[0], GL_STATIC_DRAW); 
glGenBuffers(1, &normalBuffer); 
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); 
glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), &normals[0], GL_STATIC_DRAW); 

[code for adding material and texture to box...] 

PaintGL

[code for shadows...] 

    glViewport(0, 0, width, height); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    shadeManager.bindShader(SHADOWSHADER); 

    vmml::vec4f b1(0.5, 0.0, 0.0, 0.5); 
    vmml::vec4f b2(0.0, 0.5, 0.0, 0.5); 
    vmml::vec4f b3(0.0, 0.0, 0.5, 0.5); 
    vmml::vec4f b4(0.0, 0.0, 0.0, 1.0); 

    vmml::mat4f bias = vmml::mat4f::ZERO; 
    bias.set_column(0, b1); 
    bias.set_column(1, b2); 
    bias.set_column(2, b3); 
    bias.set_column(3, b4); 

    vmml::mat4f depthBiasVP = bias * depthVP; 

    GLuint depthBiasID = glGetUniformLocation(shadowShaderID, "depthBiasVP"); 
    GLuint lightDirID = glGetUniformLocation(shadowShaderID, "lightInvDir"); 
    GLuint shadowMapID = glGetUniformLocation(shadowShaderID, "shadowMap"); 
    GLuint viewID  = glGetUniformLocation(shadowShaderID, "V"); 
    GLuint projectionID = glGetUniformLocation(shadowShaderID, "P"); 

    glUniformMatrix4fv(depthBiasID, 1, GL_FALSE, &depthBiasVP[0][0]); 
    glUniform3fv(lightDirID, 1, &lightPos[0]); 

    glActiveTexture(GL_TEXTURE1); 
    glBindTexture(GL_TEXTURE_2D, depthTextureID); 
    glUniform1i(shadowMapID, 1); 

    [calculate eye, target and up...] 
    viewMatrix = lookAt(eye, target, up); 

    glUniformMatrix4fv(viewID, 1, GL_FALSE, &viewMatrix[0][0]); 
    glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projectionMatrix[0][0]); 

    currentFrustum->setActive(true); 
    currentFrustum->extractFrustum(projectionMatrix, viewMatrix); 
    scenegraph.render(false); 

和渲染盒子

GLuint id = ShaderManager::getInstance().getShader(SHADOWSHADER); 
    if (depthPass) 
     id = ShaderManager::getInstance().getShader(DEPTHSHADER); 

    GLuint mID = glGetUniformLocation(id, "M"); 
    GLuint texID = glGetUniformLocation(id, "tex"); 
    GLuint diffID = glGetUniformLocation(id, "diffMaterial"); 
    GLuint ambiID = glGetUniformLocation(id, "ambiMaterial"); 
    GLuint specID = glGetUniformLocation(id, "specMaterial"); 
    GLuint shinID = glGetUniformLocation(id, "shininess"); 

    glUniformMatrix4fv(mID, 1, GL_FALSE, &mod[0][0]); 

    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, texture); 
    glUniform1i(texID, 0); 

    glUniform3fv(diffID, 1, &values.diffuseMaterial[0]); 
    glUniform3fv(ambiID, 1, &values.ambientMaterial[0]); 
    glUniform3fv(specID, 1, &values.specularMaterial[0]); 
    glUniform1f(shinID, values.shinyMaterial[0]); 

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
    glVertexAttribPointer(
       0, 
       3, 
       GL_FLOAT, 
       GL_FALSE, 
       0, 
       (void*)0 
       ); 
    glEnableVertexAttribArray(0); 

    glBindBuffer(GL_ARRAY_BUFFER, uvBuffer); 
    glVertexAttribPointer(
       1, 
       2, 
       GL_FLOAT, 
       GL_FALSE, 
       0, 
       (void*)0 
       ); 
    glEnableVertexAttribArray(1); 

    glBindBuffer(GL_ARRAY_BUFFER, normalBuffer); 
    glVertexAttribPointer(
       2, 
       3, 
       GL_FLOAT, 
       GL_FALSE, 
       0, 
       (void*)0 
       ); 
    glEnableVertexAttribArray(2); 

    glDrawArrays(GL_QUADS, 0, 6 * 4); 

    glDisableVertexAttribArray(0); 
    glDisableVertexAttribArray(1); 
    glDisableVertexAttribArray(2); 

着色器..

#version 420 core 

layout(location = 0) in vec3 vertexPos; 
layout(location = 1) in vec2 vertexUV; 
layout(location = 2) in vec3 vertexNorm; 

out vec2 UV; 
out vec3 position; 
out vec3 normal; 
out vec3 viewDirection; 
out vec3 lightDirection; 
out vec4 shadow; 

uniform mat4 M; 
uniform mat4 V; 
uniform mat4 P; 
uniform vec3 lightInvDir; 
uniform mat4 depthBiasVP; 

void main() { 
    gl_Position = P * V * M * vec4(vertexPos, 1); 

    UV = vertexUV; 
    position = (M * vec4(vertexPos, 1)).xyz; 
    normal = (V * M * vec4(vertexNorm, 0)).xyz; 
    viewDirection = vec3(0,0,0) - (V * M * vec4(vertexPos, 1)).xyz; 

    lightDirection = (V * vec4(lightInvDir, 0)).xyz; 
    shadow = depthBiasVP * M * vec4(vertexPos, 1); 
} 

#version 420 core 

in vec2 UV; 
in vec3 position; 
in vec3 normal; 
in vec3 viewDirection; 
in vec3 lightDirection; 
in vec4 shadow; 

layout(location = 0) out vec3 color; 

uniform sampler2D tex; 
uniform sampler2D shadowMap; 

uniform vec3 diffLight; 
uniform vec3 ambiLight; 
uniform vec3 specLight; 

uniform vec3 diffMaterial; 
uniform vec3 ambiMaterial; 
uniform vec3 specMaterial; 
uniform float shininess; 

vec2 disk[16] = vec2[] (
    vec2(-0.94201624, -0.39906216), 
    vec2(0.94558609, -0.76890725), 
    vec2(-0.094184101, -0.92938870), 
    vec2(0.34495938, 0.29387760), 
    vec2(-0.91588581, 0.45771432), 
    vec2(-0.81544232, -0.87912464), 
    vec2(-0.38277543, 0.27676845), 
    vec2(0.97484398, 0.75648379), 
    vec2(0.44323325, -0.97511554), 
    vec2(0.53742981, -0.47373420), 
    vec2(-0.26496911, -0.41893023), 
    vec2(0.79197514, 0.19090188), 
    vec2(-0.24188840, 0.99706507), 
    vec2(-0.81409955, 0.91437590), 
    vec2(0.19984126, 0.78641367), 
    vec2(0.14383161, -0.14100790) 
); 

float random(vec3 seed, int i) { 
    vec4 s = vec4(seed, i); 
    float dotProduct = dot(s, vec4(12.9898, 78.233, 45.164, 94.673)); 
    return fract(sin(dotProduct) * 43758.5453); 
} 

void main() { 
    vec3 materialDiffuseColor = diffMaterial * texture2D(tex, UV).rgb; 
    vec3 materialAmbientColor = ambiMaterial * materialDiffuseColor; 
    vec3 materialSpecularColor = specMaterial; 

    vec3 l = normalize(lightDirection); 
    vec3 n = normalize(normal); 
    vec3 v = normalize(viewDirection); 
    vec3 ref = reflect(-l, n); 

    float diff = clamp(dot(n, l), 0,1); 
    float spec = clamp(dot(l, ref), 0,1); 

    float visibility = 1.0; 
    float bias = 0.005*tan(acos(diff)); 
    bias = clamp(bias, 0.0, 0.01); 

    vec4 shadowCoordDivide = shadow/shadow.w; 

    if (texture2D(shadowMap, shadowCoordDivide.xy).z < shadowCoordDivide.z-bias) { 
     //visibility = 0.5; 
     diff = 0; 
     spec = 0; 
    } 

    color = 2 * ambiLight * materialAmbientColor; 

    if (diff != 0) { 
     color += visibility * diffLight * materialDiffuseColor * diff; 

     float iSpec = pow(spec, shininess); 
     color += visibility * specLight * materialSpecularColor * iSpec; 
    } 
} 

我做了肯定的着色器正確加載,並且該模型/視圖/投影矩陣是正確的,仍然一無所獲。任何人都可以指引我走向正確的方向?

EDIT1:添加的代碼段我忘記和刪除了一些不重要的代碼.. EDIT2:脫去代碼到可能出現的錯誤的區域

+0

請不要只轉儲您的所有代碼,將其縮小到發生問題的位置。 – Vallentin

+0

是的,還有更多的代碼,我沒有轉儲..我只發佈相關領域。 – Raccomunk

+0

當你的FBO不再需要時,你是否解除綁定?並在繪製陰影貼圖時再次將其綁定? –

回答

0

剛剛從看你的渲染代碼,它看起來並不像你傳遞查看和透視矩陣到你的頂點着色器。如果您引用尚未通過的制服,則會發生什麼情況取決於您的GPU驅動程序有多棒。有些會崩潰,有些會高興地提供一些默認的回退矩陣,以使着色器運行。這可能是因爲你的着色器看到了這個默認矩陣,並且沒有正確地轉換你的盒子。

如果您在代碼的其他地方正確傳遞矩陣,請忽略此答案。它只是基於你粘貼的代碼。

+0

哦,是的,忘了那一部分。我正在將矩陣傳遞給着色器。 – Raccomunk

+0

是的,我想你是但它從來沒有傷害檢查:) – redsoxfantom