GLSL:无法获取制服位置 [英] GLSL: Can't get uniforms location

查看:88
本文介绍了GLSL:无法获取制服位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我无法获得一些制服的位置,而其他人的位置却没有问题.

My problem is fact that I can't get some uniforms location, when location of others I get without problems.

例如,我在VS制服中拥有"MVP"和"Model",而我的"MVP"位置没有问题,但是我没有对称使用的"Model"位置. 同样,我无法从FS的Light结构中获取字段的位置.

For example I have in VS uniforms called "MVP" and "Model" and I have "MVP" location without problems, but I don't have "Model" location which I use symmetrically. In the same manner I can't get location of fields from Light structure in FS.

其代码将数据传递到着色器中:

Its code passing data into shader:

bool Shader::SetShaderParameters(mat4 MVPMatrix,mat4 Model,GLint textureUnit)
{
  GLuint matrixLocation = glGetUniformLocation(programID,"MVP");
  if(matrixLocation==-1)
    return false;
  glUniformMatrix4fv(matrixLocation,1,GL_FALSE,&MVPMatrix[0][0]);
  GLuint modelLocation = glGetUniformLocation(programID,"Model");
  if(modelLocation==-1)
    return false;
  glUniformMatrix4fv(modelLocation,1,GL_FALSE,&Model[0][0]);
  return true;
}

bool Shader::SetShaderLights(vec3 lightColor,vec3 ambientIntensity,vec3 direction,vec3 diffuseIntensity)
{
    GLuint lightColorLocation = glGetUniformLocation(programID,"light.lightColor");
    if(lightColorLocation==-1)
        return false;
    glUniform3fv(lightColorLocation,1,&lightColor[0]);
    GLuint ambientIntensityLocation = glGetUniformLocation(programID,"light.ambientIntensity");
    if(ambientIntensityLocation==-1)
        return false;
    glUniform3fv(ambientIntensityLocation,1,&ambientIntensity[0]);
    GLuint directionLocation = glGetUniformLocation(programID,"light.direction");
    if(directionLocation==-1)
        return false;
    glUniform3fv(directionLocation,1,&direction[0]);
    GLuint diffuseIntensityLocation = glGetUniformLocation(programID,"light.diffuseIntensity");
    if(diffuseIntensityLocation==-1)
        return false;
    glUniform3fv(diffuseIntensityLocation,1,&diffuseIntensity[0]);
    return true;
}

着色器代码:

VS:

#version 440 core

layout(location=0) in vec3 vertexPosition;
layout(location=1) in vec2 vertexUV;
layout(location=2) in vec3 normal;

out vec2 uv;
out vec3 norm;

uniform mat4 Model;
uniform mat4 MVP;

void main()
{
  gl_Position = MVP*vec4(vertexPosition,1.0);
  uv = vertexUV;
  norm = (Model*vec4(normal,0.0)).xyz;
}

FS:

#version 440 core

struct Light
{
  vec3 lightColor;
  vec3 ambientIntensity;
  vec3 direction;
  vec3 diffuseIntensity;
};

in vec2 uv;
in vec3 norm;

out vec3 color;

uniform sampler2D texSamp;
uniform Light light;

void main()
{
  color = texture(texSamp,uv).rgb*light.ambientIntensity;
}

获取制服位置时,各种行为可能会出现什么问题?也许我使用了不好的着色器版本,或者类似于着色器中的数据优化.我正在调试解决方案,并且我很确定着色器程序ID是正确的.

What can be problem in various behaviours while getting location of uniforms? Maybe I use bad version of shaders or it's somelike optimisations of data in shaders. I was debugging my solution and I'm pretty sure that shader program IDs are correct.

: 当我尝试在FS中使用norm时,没有任何变化,在无法获取位置的glGetUniformLocation()之后,我得到了零错误代码.

: When I try to use norm in FS nothing changes, and after glGetUniformLocation() which can't get location I got zero error code.

推荐答案

允许编译器优化所有未使用的制服,这就是为什么会出现此错误的原因.

The compiler is allowed to optimize away any unused uniforms, which is why you get this error.

您可以放心地忽略它.如果找不到该制服,则glGetUniformLocation返回-1.根据OpenGL 4.3规范的第7.6.1节:

You can safely ignore this. If the uniform isn't found, glGetUniformLocation returns -1. From the OpenGL 4.3 specs, section 7.6.1:

如果location的值为-1,则Uniform *命令将静默运行 忽略传入的数据,当前统一值将不会 改变了.

If the value of location is -1, the Uniform* commands will silently ignore the data passed in, and the current uniform values will not be changed.

这篇关于GLSL:无法获取制服位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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