From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../enablevertexattribarray/index.html | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 files/zh-cn/web/api/webglrenderingcontext/enablevertexattribarray/index.html (limited to 'files/zh-cn/web/api/webglrenderingcontext/enablevertexattribarray') diff --git a/files/zh-cn/web/api/webglrenderingcontext/enablevertexattribarray/index.html b/files/zh-cn/web/api/webglrenderingcontext/enablevertexattribarray/index.html new file mode 100644 index 0000000000..493c9efa59 --- /dev/null +++ b/files/zh-cn/web/api/webglrenderingcontext/enablevertexattribarray/index.html @@ -0,0 +1,102 @@ +--- +title: WebGLRenderingContext.enableVertexAttribArray() +slug: Web/API/WebGLRenderingContext/enableVertexAttribArray +translation_of: Web/API/WebGLRenderingContext/enableVertexAttribArray +--- +
{{APIRef("WebGL")}}
+ +

WebGL API中,使用 {{domxref("WebGLRenderingContext")}} 中的enableVertexAttribArray() 方法,可以打开属性数组列表中指定索引处的通用顶点属性数组。

+ +
+

你可以通过以下方法关闭顶点属性数组 {{domxref("WebGLRenderingContext.disableVertexAttribArray", "disableVertexAttribArray()")}}.

+
+ +

在WebGL中,作用于顶点的数据会先储存在attributes。这些数据仅对JavaScript代码和顶点着色器可用。属性由索引号引用到GPU维护的属性列表中。在不同的平台或GPU上,某些顶点属性索引可能具有预定义的值。创建属性时,WebGL层会分配其他属性。

+ +

无论怎样,都需要你使用enableVertexAttribArray()方法,来激活每一个属性以便使用,不被激活的属性是不会被使用的。一旦激活,以下其他方法就可以获取到属性的值了,包括{{domxref("WebGLRenderingContext.vertexAttribPointer", "vertexAttribPointer()")}},{{domxref("WebGLRenderingContext.vertexAttrib", "vertexAttrib*()")}},和 {{domxref("WebGLRenderingContext.getVertexAttrib", "getVertexAttrib()")}}。

+ +

语法

+ +
void gl.enableVertexAttribArray(index);
+
+ +

参数

+ +

index

+ +
+
类型为{{domxref("GLuint")}} 的索引,指向要激活的顶点属性。如果您只知道属性的名称,不知道索引,您可以使用以下方法来获取索引{{domxref("WebGLRenderingContext.getAttribLocation", "getAttribLocation()")}}.
+
+ +

返回值

+ +

undefined.

+ +

错误

+ +

您可以使用{{domxref("WebGLRenderingContext.getError", "getError()")}}方法,来检查使用enableVertexAttribArray()时发生的错误。

+ +
+
WebGLRenderingContext.INVALID_VALUE
+
非法的 index 。一般是 index 大于或等于了顶点属性列表允许的最大值。该值可以通过 WebGLRenderingContext.MAX_VERTEX_ATTRIBS获取。
+
+ +

例子

+ +

该例子是 A basic 2D WebGL animation example 中的一部分,用以说明 enableVertexArray() 是如何激活顶点属性,并将顶点数据传输到shader函数的。

+ +
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
+
+aVertexPosition =
+    gl.getAttribLocation(shaderProgram, "aVertexPosition");
+
+gl.enableVertexAttribArray(aVertexPosition);
+gl.vertexAttribPointer(aVertexPosition, vertexNumComponents,
+      gl.FLOAT, false, 0, 0);
+
+gl.drawArrays(gl.TRIANGLES, 0, vertexCount);
+ +
该段代码来自于 "A basic 2D WebGL animation example." 中的 the function animateScene() 。 通过连接来查看全文,您可以查看产生的动画效果。
+ +

以上代码中,使用了{{domxref("WebGLRenderingContext.bindBuffer", "bindBuffer()")}}来设置将用于绘图的顶点数据的缓存。然后使用{{domxref("WebGLRenderingContext.getAttribLocation", "getAttribLocation()")}}来获取顶点数据在shader函数中的索引。

+ +

我们用 enableVertexAttribArray() 函数来激活 aVertexPosition中记录的索引位置,以便在后面对该顶点属性进行数据绑定。

+ +

使用{{domxref("WebGLRenderingContext.vertexAttribPointer", "vertexAttribPointer()")}} 将缓存数据绑定到shader函数中的顶点属性。于是,我们可以通过{{domxref("WebGLRenderingContext.drawArrays", "drawArrays()")}}函数将顶点一一绘制。

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('WebGL', "#5.14.10", "enableVertexAttribArray")}}{{Spec2('WebGL')}}Initial definition.
{{SpecName('OpenGL ES 2.0', "glEnableVertexAttribArray.xml", "glEnableVertexAttribArray")}}{{Spec2('OpenGL ES 2.0')}}Man page of the OpenGL API.
+ +

Browser compatibility

+ + + +

{{Compat("api.WebGLRenderingContext.enableVertexAttribArray")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf