为什么我不能使用uniform1f 而不是uniform4f 来设置vec4 制服? [英] Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?

查看:92
本文介绍了为什么我不能使用uniform1f 而不是uniform4f 来设置vec4 制服?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过部分2.10.4

<块引用>

所使用的 Uniform* 命令必须与着色器中声明的制服尺寸相匹配.例如,要加载声明为 bvec2 的制服,可以使用 Uniform2i{v} 或 Uniform2f{v}.如果尝试使用不匹配的 Uniform* 命令,则会生成 INVALID_OPERATION 错误.在这个例子中使用 Uniform1iv 会产生一个错误

对于所有其他制服类型,所使用的 Uniform* 命令必须与着色器中声明的制服的大小和类型相匹配.不进行类型转换.例如,要加载声明为 vec4 的制服,必须使用 Uniform4f{v}.要加载 3x3 矩阵,必须使用 UniformMatrix3fv.如果尝试使用不匹配的 Uniform* 命令,则会生成 INVALID_OPERATION 错误.在这个例子中,使用 Uniform4i{v} 会产生一个错误

I learn WebGL step by step via this book. I try to draw three points through using of the buffer (gl.ARRAY_BUFFER) instead of cycle (as I did the same earlier in other samples of the book).

  var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
  if(!u_FragColor){
    console.log('Can\'t to get the "u_FragColor" variable.');
    return -1;
  }

  // gl.uniform1f(u_FragColor, 1.0); // <- this variant doesn't work! Why?
  gl.uniform4f(u_FragColor, 1.0, 0.0, 0.0, 1.0);

It works fine, but I have a question about the gl_FragColor initializing: why I can't replace the

gl.uniform4f(u_FragColor, 1.0, 0.0, 0.0, 1.0);

on the

gl.uniform1f(u_FragColor, 1.0);

? I expected this is the same. But this case I get the error in the console:

解决方案

You have to use the correct type to set a uniform. If your uniform is vec4 then you have to use either gl.uniform4f or gl.uniform4fv.

uniform       valid
type          functions
----------------------------------------------
float         gl.uniform1f    gl.uniform1fv
vec2          gl.uniform2f    gl.uniform2fv
vec3          gl.uniform3f    gl.uniform3fv
vec4          gl.uniform4f    gl.uniform4fv
int           gl.uniform1i    gl.uniform1iv
ivec2         gl.uniform2i    gl.uniform2iv
ivec3         gl.uniform3i    gl.uniform3iv
ivec4         gl.uniform4i    gl.uniform4iv
sampler2D     gl.uniform1i    gl.uniform1iv
samplerCube   gl.uniform1i    gl.uniform1iv

mat2          gl.uniformMatrix2fv
mat3          gl.uniformMatrix3fv
mat4          gl.uniformMatrix4fv

bool          gl.uniform1i gl.uniform1f gl.uniform1iv gl.uniform1fv
bvec2         gl.uniform2i gl.uniform2f gl.uniform2iv gl.uniform2fv
bvec3         gl.uniform3i gl.uniform3f gl.uniform3iv gl.uniform3fv
bvec4         gl.uniform4i gl.uniform4f gl.uniform4iv gl.uniform4fv

From the OpenGL ES 2.0 spec section 2.10.4

The Uniform* command used must match the size of the uniform, as declared in the shader. For example, to load a uniform declared as a bvec2, either Uniform2i{v} or Uniform2f{v} can be used. An INVALID_OPERATION error will be generated if an attempt is made to use a non-matching Uniform* command. In this example using Uniform1iv would generate an error

For all other uniform types the Uniform* command used must match the size and type of the uniform, as declared in the shader. No type conversions are done. For example, to load a uniform declared as a vec4, Uniform4f{v} must be used. To load a 3x3 matrix, UniformMatrix3fv must be used. An INVALID_OPERATION error will be generated if an attempt is made to use a non-matching Uniform* command. In this example, using Uniform4i{v} would generate an error

这篇关于为什么我不能使用uniform1f 而不是uniform4f 来设置vec4 制服?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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