如何将模型矩阵包含到VBO中? [英] How to include model matrix to a VBO?

查看:141
本文介绍了如何将模型矩阵包含到VBO中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送一个缓冲区列表(到GPU/顶点着色器),其中包含有关顶点位置,世界位置,颜色,比例和旋转的信息.

I want to send a buffer list (to the GPU/vertex shader) which contains information about vertex position, world position, color, scale, and rotation.

如果我的每个3D对象在矩阵中都具有与转换相关的信息,我如何通过VBO将这个矩阵数组(除了其他顶点数据之外)传递给GPU?

If each of my 3D objects have transformation related information in a matrix, how can i pass this array of matrices (in addition to the other vertex data) to the GPU via the VBO(s) ?

已更新 请原谅任何错别字:

Updated Please excuse any typos:

// bind & set vertices.
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAtribPointer(a_Position, 3, gl.FLOAT, false, stride, 0);

// bind & set vertex normals.
gl.bindBuffer(gl.ARRAY_BUFFER,, vertexNormalsBuffer);
gl.vertexAttribPointer(a_Normal, 3, gl.FLOAT, false, stride, 0);

// becaue i cant pass in a model matrix via VBO, im tryng to pass in my world coordinates.
gl.bindBuffer(gl.ARRAY_BUFFER, worldPositionBuffer);

// not sure why i need to do this, but most tutorials i've read says to do this.
gl.bindBuffer(gl.ARRAY_BUFFER, null);

// bind & draw index buffer.
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
gl.drawElements(gl.TRIANGLES, vertexIndexCount, gl.UNSIGNED_SHORT, 0);

请注意,这些缓冲区(vertexBuffervertexNormalsBufferworldPostiionBuffervertexIndexBuffer)是我场景中所有各个3D对象的串联(我通过属性/制服一一渲染) -天真的方法,更简单,更容易掌握,但对于1000个对象却非常慢.

Note that these buffers (vertexBuffer, vertexNormalsBuffer, worldPostiionBuffer, vertexIndexBuffer) are a concatenation of all the respective 3D objects in my scene (which i was rendering one-by-one via attributes/uniforms - a naive approach which is much simpler and easier to grasp, yet horribly slow for 1000's objects).

推荐答案

对于渲染框架时需要经常更改的任何值,将它们作为attribute而不是uniform.这还具有以下优点:您可以选择将值存储在VBO中.请注意,不需要 将属性存储在VBO中,也可以使用glVertexAttrib[1234]f()glVertexAttrib[1234]fv()指定它们.

For any values that you need to change frequently while rendering a frame, it can be more efficient to pass them into the shader as an attribute instead of a uniform. This also has the advantage that you can store the values in a VBO if you choose. Note that it's not required to store attributes in VBOs, they can also be specified with glVertexAttrib[1234]f() or glVertexAttrib[1234]fv().

这适用于变换矩阵,就像传递到着色器中的任何其他值一样.如果更改非常频繁,则可能应将其设置为属性.在这种情况下,唯一的细微变化是我们正在处理矩阵,并且属性必须是向量.但这很容易克服.通常作为mat4传入的内容可以由类型为vec4的3个值表示,其中这3个向量是矩阵的列向量.表示一个完全通用的4x4矩阵当然是4个向量,但是转换矩阵中的第4列不用于任何常见的转换类型(投影矩阵除外).

This applies to the transformation matrix like any other value passed into the shader. If it changes very frequently, you should probably make it an attribute. The only slight wrinkle in this case is that we're dealing with a matrix, and attributes have to be vectors. But that's easy to overcome. What is normally passed in as a mat4 can be represented by 3 values of type vec4, where these 3 vectors are the column vectors of the matrix. It would of course be 4 vectors to represent a fully generic 4x4 matrix, but the 4th column in a transformation matrix is not used for any common transformation types (except for projection matrices).

如果希望将转换包含在VBO中,则可以设置3个以上的属性,就像您对位置和颜色所做的一样.您存储在VBO中的属性的值是相应转换矩阵的列向量.

If you want the transformations to be in the VBO, you set up 3 more attributes, the same way you already did for your positions and colors. The values of the attributes you store in the VBO are the column vectors of the corresponding transformation matrix.

然后在顶点着色器中,通过计算变换属性向量与输入位置的点积来应用变换.代码看起来像这样:

Then in the vertex shader, you apply the transformation by calculating the dot product of the transformation attribute vectors with your input position. The code could look like this:

attribute vec4 InPosition;
attribute vec4 XTransform;
attribute vec4 YTransform;
attribute vec4 ZTransform;
main() {
    vec3 eyePosition = vec3(
        dot(XTransform, InPosition),
        dot(YTransform, InPosition),
        dot(ZTransform, InPosition));
    ...
}

还有其他方法可以在完整的OpenGL中解决此问题,例如使用统一缓冲区对象.但是对于WebGL和OpenGL ES 2.0,我认为这是最好的解决方案.

There are other approaches to solve this problem in full OpenGL, like using Uniform Buffer Objects. But for WebGL and OpenGL ES 2.0, I think this is the best solution.

这篇关于如何将模型矩阵包含到VBO中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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