OpenGL反射着色器仅显示灰色 [英] OpenGL Reflection shader showing only grey

查看:314
本文介绍了OpenGL反射着色器仅显示灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very new to OpenGL and I've been working with setting up sky boxes, and finally fixed it thanks to some help here but now the reflection shader I've tried to set up by editing some I've found (so a sphere will have a basic reflection effect based on the cube map of my sky box) will not show any color but grey as shown in the image. http://i.imgur.com/Th56Phg.png

我没办法弄清楚,这是我的着色器代码:

I'm having no luck figuring out, here is my shader code:

顶点着色器

#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();

推荐答案

此操作顺序是错误的:

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

shader.Bind();

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

Uniforms are per program state in the GL, and glUniform*() calls always affect the uniforms of the program currently in use. You seem to try to set this uniform before the program is bound, so it will fail, and the uniform in that program will still stay at the default value of 0.

这篇关于OpenGL反射着色器仅显示灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆