OpenGL反射着色器仅显示灰色

时间:2016-01-24 00:37:04

标签: c++ opengl shader fragment-shader vertex-shader

我是OpenGL的新手,我一直在设置天空盒,最后通过这里的一些帮助修复了它,但现在我试图设置的反射着色器编辑我发现的一些(因此球体将具有基于我的天空盒的立方体贴图的基本反射效果)将不会显示任何颜色而是灰色,如图所示。 http://i.imgur.com/Th56Phg.png

我没有运气搞清楚,这是我的着色器代码:

顶点着色器

#version 330 core

attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;


out vec3 Normal;
out vec3 Position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform mat4 transform;

void main()
{
    gl_Position = transform * vec4(position, 1.0);
    Normal = mat3(transpose(inverse(model))) * normal;
    Position = vec3(model * vec4(position, 1.0f));
}  

片段着色器

#version 330 core
in vec3 Normal;
in vec3 Position;
out vec4 color;

uniform vec3 cameraPos;
uniform samplerCube skybox;

void main()
{             
    vec3 I = normalize(Position - cameraPos);
    vec3 R = reflect(I, normalize(Normal));
    color = texture(skybox, R);
}

最后这是我的用法:

glm::mat4 model;
        glm::mat4 view = camera.GetViewProjection();
        glm::mat4 projection = glm::perspective(70.0f, (float)1600 / (float)1200, 0.1f, 1000.0f);
        glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "model"), 1, GL_FALSE, glm::value_ptr(model));
        glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(refShader.getProg(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        glUniform3f(glGetUniformLocation(refShader.getProg(), "cameraPos"), camera.getPos().x, camera.getPos().y, camera.getPos().z);

        glActiveTexture(GL_TEXTURE3); 
        glUniform1i(glGetUniformLocation(refShader.getProg(), "skybox"), 3);

        shader.Bind();
        texture.Bind(0);
        shader.Update(transformCube, camera);
        cubeMesh.Draw();

        glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
        refShader.Bind();
        refShader.Update(transform, camera);
        sphereMesh.Draw();

1 个答案:

答案 0 :(得分:1)

这种操作的顺序是错误的:

glActiveTexture(GL_TEXTURE3); 
glUniform1i(glGetUniformLocation(refShader.getProg(), "skybox"), 3);

shader.Bind();

制服在GL中按程序状态glUniform*()调用始终会影响当前正在使用的程序的制服。在程序绑定之前,您似乎尝试设置此统一,因此它将失败,并且该程序中的统一仍将保持默认值0。