GLSL顶点着色器`glGetUniformLocation`返回`null`。

huangapple go评论114阅读模式
英文:

glsl vertex shader glGetUniformLocation return null

问题

  1. 大家好,我正在学习 WebGL,但无法获取特殊变量的位置。
  2. 以下是代码
  3. 顶点着色器

uniform vec2 u_translation;
attribute vec2 a_position;
uniform vec2 u_resolution;
void main() {
vec2 position = a_position + u_translation;
vec2 zeroToOne = a_position / u_resolution;
vec2 zeroToTwo = zeroToOne * 2.0;
vec2 clipSpace = zeroToTwo - 1.0;
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
}

  1. 片元着色器

precision mediump float;
uniform vec4 u_color;
void main() {
gl_FragColor = u_color;
}

var translationLocation = gl.getUniformLocation(program, "u_translation");
var positionLocation = gl.getAttribLocation(program, "a_position");
var resolutionLocation = gl.getUniformLocation(program, "u_resolution");
var colorLocation = gl.getUniformLocation(program, "u_color");
// translationLocation 为 null,但其他变量可以获取到

  1. [查看图片描述](https://i.stack.imgur.com/6J6sc.png)
  2. 有人知道发生了什么吗?我认为我在着色器中使用了 'u_translation'
英文:

hello everyone i'm learning webgl and i can't get the location of the special variable
here is the code

vertex-shader

  1. uniform vec2 u_translation;
  2. attribute vec2 a_position;
  3. uniform vec2 u_resolution;
  4. void main() {
  5. vec2 position = a_position + u_translation;
  6. vec2 zeroToOne = a_position / u_resolution;
  7. vec2 zeroToTwo = zeroToOne * 2.0;
  8. vec2 clipSpace = zeroToTwo - 1.0;
  9. gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
  10. }

fragment-shader

  1. precision mediump float;
  2. uniform vec4 u_color;
  3. void main() {
  4. gl_FragColor = u_color;
  5. }
  1. var translationLocation = gl.getUniformLocation(program, "u_translation");
  2. var positionLocation = gl.getAttribLocation(program, "a_position");
  3. var resolutionLocation = gl.getUniformLocation(program, "u_resolution");
  4. var colorLocation = gl.getUniformLocation(program, "u_color");
  5. // translationLocation is null but i can get the others

enter image description here

does any one know what happend? i think i used the 'u_translation' in the shader

答案1

得分: 0

  1. 本地变量 `position` 在着色器中未被使用,因此不需要 `u_translation` 并且被优化掉。因此它不会成为一个活动的程序资源,也不会得到统一位置。可能以下行错误:
  2. >```glsl
  3. >vec2 zeroToOne = a_position / u_resolution;
  4. >```
  5. 但应该是:
  6. ```glsl
  7. vec2 zeroToOne = position / u_resolution;

现在本地变量 position 在着色器程序中被使用,因此 u_translation 成为一个活动的程序资源。

  1. <details>
  2. <summary>英文:</summary>
  3. The local variable `position` is not used anywhere in the shader and thus `u_translation` is not needed and optimized out. So it does not become an active program resource and does not get a uniform location. Likely the following line is wrong
  4. &gt;```glsl
  5. &gt;vec2 zeroToOne = a_position / u_resolution;
  6. &gt;```
  7. but should be
  8. ```glsl
  9. vec2 zeroToOne = position / u_resolution;

Now the local variable position is used in the shader program and thus u_translation becomes an active program resource.

huangapple
  • 本文由 发表于 2023年2月27日 16:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定