2014-10-27 45 views
0

我似乎已经打破了我的程序着色器,这里是他们的代码:我着色器停止工作

顶点着色器

#version 330 core 

uniform mat4 camera; 
uniform mat4 model; 

layout(location = 0) in vec3 vert; 
layout(location = 1) in vec3 vertNormal; 

out vec3 fragVert; 
out vec3 fragNormal; 

void main() { 
    // Pass some variables to the fragment shader 
    fragNormal = vertNormal; 
    fragVert = vert; 

    // Apply all matrix transformations to vert 
    gl_Position = camera * model * vec4(vert, 1); 
} 

片段着色器

#version 150 core 

uniform mat4 model; 
uniform vec3 cameraPosition; 

// material settings 
uniform float materialShininess; 
uniform vec3 materialSpecularColor; 
uniform vec3 materialColor; 

uniform struct Light { 
    vec3 position; 
    vec3 intensities; //a.k.a the color of the light 
    float attenuation; 
    float ambientCoefficient; 
} light; 

in vec3 fragNormal; 
in vec3 fragVert; 

out vec4 finalColor; 

void main() { 
    vec3 normal = normalize(transpose(inverse(mat3(model))) * fragNormal); 
    vec3 surfacePos = vec3(model * vec4(fragVert, 1)); 
    vec4 surfaceColor = vec4(materialColor, 1); 
    vec3 surfaceToLight = normalize(light.position - surfacePos); 
    vec3 surfaceToCamera = normalize(cameraPosition - surfacePos); 

    //ambient 
    vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities; 

    //diffuse 
    float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight)); 
    vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities; 

    //specular 
    float specularCoefficient = 0.0; 
    if(diffuseCoefficient > 0.0) 
     specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), materialShininess); 
    vec3 specular = specularCoefficient * materialSpecularColor * light.intensities; 

    //attenuation 
    float distanceToLight = length(light.position - surfacePos); 
    float attenuation = 1.0/(1.0 + light.attenuation * pow(distanceToLight, 2)); 

    //linear color (color before gamma correction) 
    vec3 linearColor = ambient + attenuation*(diffuse + specular); 

    //final color (after gamma correction) 
    vec3 gamma = vec3(1.0/2.2); 
    finalColor = vec4(pow(linearColor, gamma), surfaceColor.a); 
} 

我的资产的我从一个obj文件加载,然后像这样绘制它:

void OpenGLView::run() 
{ 
    initializeAndSetupWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "PhongBunny"); 
    glClearColor(1.0f, 1.0f, 0.0f, 1.0f); 

    loadBunnyAsset(); 

    AssetInstance bunny1; 
    bunny1.asset = bunny; 
    bunny1.position = glm::vec3(2.0f, 2.0f, 2.0f); 
    bunny1.scale = glm::vec3(1.0f, 1.0f, 1.0f); 


    do{ 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     loadUniforms(bunny1); 

     glEnableVertexAttribArray(0); 
     glBindBuffer(GL_ARRAY_BUFFER, bunny.vertexBuffer); 
     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); 

     glEnableVertexAttribArray(1); 
     glBindBuffer(GL_ARRAY_BUFFER, bunny.normalBuffer); 
     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr); 

     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunny.elementBuffer); 

     glDrawElements(GL_TRIANGLES, bunny.elementsSize, GL_UNSIGNED_INT, (void*)0); 

     glDisableVertexAttribArray(0); 
     glDisableVertexAttribArray(1); 

     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } while (!glfwWindowShouldClose(window)); 

    glfwDestroyWindow(window); 
    glfwTerminate(); 
} 

这个被加载制服的功能:导致这种

OpenGLView::OpenGLView() 
{ 
    light.position = glm::vec3(0.0f, 7.0f, 3.0f); 
    light.intensities = glm::vec3(0.3f, 0.3, 0.3f); 
    light.attenuation = 0.3f; 
    light.ambientCoefficient = 0.005f; 

    cameraPosition = glm::vec3(5.0f, 3.0f, 8.0f); 
} 

有一段时间我有bunny1的位置设置为0,0,0:

void OpenGLView::loadUniforms(AssetInstance assetInstance) 
{ 
    Asset* asset = &assetInstance.asset; 

    glUseProgram(asset->shaderProgramID); 
    glm::mat4 Projection = glm::perspective(45.0f, 4.0f/3.0f, 0.1f, 1000.0f); 
    glm::mat4 camera = Projection * getViewMatrix(); 
    glm::mat4 model = translate(assetInstance.position) * scale(assetInstance.position); 

    GLuint cameraID = glGetUniformLocation(asset->shaderProgramID, "camera"); 
    GLuint modelID = glGetUniformLocation(asset->shaderProgramID, "model"); 
    GLuint cameraPositionID = glGetUniformLocation(asset->shaderProgramID, "cameraPosition"); 
    GLuint lightPositionID = glGetUniformLocation(asset->shaderProgramID, "light.position"); 
    GLuint lightIntensitiesID = glGetUniformLocation(asset->shaderProgramID, "light.intensities"); 
    GLuint lightAttenuationID = glGetUniformLocation(asset->shaderProgramID, "light.attenuation"); 
    GLuint lightAmbientCoefficientID = glGetUniformLocation(asset->shaderProgramID, "light.ambientCoefficient"); 
    GLuint materialColorID = glGetUniformLocation(asset->shaderProgramID, "materialColor"); 
    GLuint materialShininessID = glGetUniformLocation(asset->shaderProgramID, "materialShininess"); 
    GLuint materialSpecularColorID = glGetUniformLocation(asset->shaderProgramID, "materialSpecularColor"); 

    glUniformMatrix4fv(cameraID, 1, GL_FALSE, &camera[0][0]); 
    glUniformMatrix4fv(modelID, 1, GL_FALSE, &model[0][0]); 
    glUniform3fv(cameraPositionID, 1, &cameraPosition[0]); 
    glUniform3fv(lightPositionID, 1, &light.position[0]); 
    glUniform3fv(lightIntensitiesID, 1, &light.intensities[0]); 
    glUniform1f(lightAttenuationID, light.attenuation); 
    glUniform1f(lightAmbientCoefficientID, light.ambientCoefficient); 
    glUniform3fv(materialColorID, 1, &assetInstance.materialColor[0]); 
    glUniform1f(materialShininessID, assetInstance.materialShininess); 
    glUniform3fv(materialSpecularColorID, 1, &assetInstance.materialSpecularColor[0]); 
} 

这里正在做一些设置根本不能被画出来,我想不出为什么是这样?然后,当我将它改为1,1,1时,它开始绘制,但现在我的key_callback函数(旋转并缩放兔子)停止工作。另外,这里是我的转换和调整功能:

glm::mat4 OpenGLView::translate(glm::vec3 position) 
{ 
    return glm::translate(glm::mat4(), position); 
} 

glm::mat4 OpenGLView::scale(glm::vec3 size) 
{ 
    return glm::scale(glm::mat4(), size); 
} 

,我想不通为什么改变bunny1.position似乎缩放兔子,而不是翻译的地位?

回答

1

为什么你的兔子的规模变化而变化,当bunny1.position的原因是因为你通过bunny1.position扩展您的兔子:

 glm::mat4 model = translate(assetInstance.position) * scale(assetInstance.position); 

这也可能是为什么当设置兔子在前看不见它的位置的原因( 0,0,0)因为您然后将其缩放到0.

+0

...这是尴尬。谢谢! – LarrySellers 2014-10-27 21:07:40

+0

如果这解决了您的问题,请接受答案,以便其他人不费吹灰之力。 :) – BDL 2014-10-27 21:10:20

+0

谢谢。这解释了为什么我的兔子不显示,但我仍然有阴影和旋转不工作的问题... – LarrySellers 2014-10-27 21:15:48