WebGLRenderingContext.vertexAttrib[1234]f[v]() - Web APIs 编辑

The WebGLRenderingContext.vertexAttrib[1234]f[v]() methods of the WebGL API specify constant values for generic vertex attributes.

Syntax

void gl.vertexAttrib1f(index, v0);
void gl.vertexAttrib2f(index, v0, v1);
void gl.vertexAttrib3f(index, v0, v1, v2);
void gl.vertexAttrib4f(index, v0, v1, v2, v3);

void gl.vertexAttrib1fv(index, value);
void gl.vertexAttrib2fv(index, value);
void gl.vertexAttrib3fv(index, value);
void gl.vertexAttrib4fv(index, value);

Parameters

index
A GLuint specifying the position of the vertex attribute to be modified.
v0, v1, v2, v3
A floating point Number for the vertex attribute value.
value

A Float32Array for floating point vector vertex attribute values.

Return value

None.

Description

While vertex attributes are usually used to specify values which are different for each vertex (using vertexAttribPointer), it can be useful to specify a constant value. For example, if you have a shader which has a color vertex attribute, but you want to draw everything in a single color, you can use vertexAttrib to achieve that without creating a buffer filled with only one value or having to create a separate shader which uses a uniform for the color.

This value will be used if a bound array buffer has not been enabled with enableVertexAttribArray.

Attributes may be matrices, in which case columns of the matrix must be loaded into successive vertex attribute slots.

The values set with vertexAttribPointer are context-global, i.e. they aren't part of the shader state (like generix vertex attribute indexes to shader variable bindings) and aren't part of the vertex array object state (like enabled vertex attribute arrays). The only way to change the values is by calling this function again.

Examples

const a_foobar = gl.getAttribLocation(shaderProgram, 'foobar');
//either set each component individually:
gl.vertexAttrib3f(a_foobar, 10.0, 5.0, 2.0);
//or provide a Float32Array:
const floatArray = new Float32Array([10.0, 5.0, 2.0]);
gl.vertexAttrib3fv(a_foobar, floatArray);
// we want to load the following 3x3 matrix into attribute named "matrix3x3"
// 0 1 2
// 3 4 5
// 6 7 8
const matrix3x3Location = gl.getAttribLocation(shaderProgram, 'matrix3x3');
gl.vertexAttrib3f(matrix3x3Location,     0, 3, 6);
gl.vertexAttrib3f(matrix3x3Location + 1, 1, 4, 7);
gl.vertexAttrib3f(matrix3x3Location + 2, 2, 5, 8);

Specifications

SpecificationStatusComment
WebGL 1.0
The definition of 'vertexAttrib' in that specification.
RecommendationInitial definition.
OpenGL ES 2.0
The definition of 'glVertexAttrib' in that specification.
StandardMan page of the OpenGL API.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:59 次

字数:5741

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文