简体   繁体   English

glsl顶点着色器glGetUniformLocation失败

[英]glsl vertex shader glGetUniformLocation fails

I want to set a uniform Vector in my Vertex Shader. 我想在我的顶点着色器中设置一个统一的Vector。

int loc = glGetUniformLocation(shader, "LightPos");
if (loc != -1)
{
    //do Stuff
}

The problem is that loc is -1 all the time. 问题是loc一直都是-1。 I tried it with a variable from the Fragment Shader, that actually worked. 我使用了片段着色器中的变量进行了尝试,该变量确实有效。 The Vertex Shader: 顶点着色器:

uniform vec3 LightPos;
varying vec2 UVCoord;
varying float LightIntensity;

void main()
{           
    UVCoord = gl_MultiTexCoord0.st;
    gl_Position = ftransform();
    vec3 Normal = normalize(gl_NormalMatrix * gl_Normal);
    LightIntensity = max(dot(normalize(vec3(0, -10, 0)), Normal), 0.0);
}

The Fragment Shader: 片段着色器:

uniform sampler2D tex1;
varying vec2 UVCoord;
varying float LightIntensity;

void main()
{
    vec3 Color = vec3(texture2D(tex1, UVCoord));
    gl_FragColor = vec4(Color * LightIntensity, 1.0);
}

Does anybody have an idea what I am doing wrong? 有人知道我在做什么错吗?

Unfortunately, you misunderstood how glGetUniformLocation (...) and uniform location assignment in general works. 不幸的是,您误解了glGetUniformLocation (...)和统一位置分配在一般情况下的工作方式。

Locations are only assigned after your shaders are compiled and linked. 仅在编译和链接了着色器之后才分配位置。 This is a two-phase operation that effectively identifies only the used inputs and outputs between all stages of a GLSL program (vertex, fragment, geometry, tessellation). 这是一个两阶段操作,可有效地仅识别GLSL程序的所有阶段(顶点,片段,几何,细分)之间使用的输入和输出。 Because LightPos is not used in your vertex shader (or anywhere else for that matter) it is not assigned a location when your program is linked. 由于LightPos未在您的顶点着色器(或其他任何地方)中使用,因此在链接程序时未为其分配位置。 It simply ceases to exist. 它根本不存在。

This is where the term active uniform comes from. 这是主动制服一词的来历。 And glGetUniformLocation (...) only returns the location of active uniforms. glGetUniformLocation (...)仅返回活动制服的位置。


Name 名称

glGetUniformLocation — Returns the location of a uniform variable glGetUniformLocation —返回统一变量的位置

[...] [...]

Description 描述

glGetUniformLocation returns an integer that represents the location of a specific uniform variable within a program object. glGetUniformLocation返回一个整数,该整数表示程序对象中特定统一变量的位置。 name must be a null terminated string that contains no white space. 名称必须是不包含空格的以空终止的字符串。 name must be an active uniform variable name in program that is not a structure, an array of structures, or a subcomponent of a vector or a matrix. 名称必须是程序中的活动统一变量名称,该名称不是结构,结构数组或向量或矩阵的子组件。 This function returns -1 if name does not correspond to an active uniform variable in program, if name starts with the reserved prefix "gl_", or if name is associated with an atomic counter or a named uniform block. 如果名称不对应于程序中的活动统一变量,或者名称以保留前缀“ gl_”开头,或者名称与原子计数器或命名统一块关联,则此函数返回-1。

you don't actually use LightPos in the shader, so the optimiser didn't allocate any registers for it 您实际上并没有在着色器中使用 LightPos,因此优化程序没有为其分配任何寄存器

the compiler is free to optimize uniforms and attribute out if they are not used 编译器可以自由地优化制服并在不使用制服的情况下进行归因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM