属性着色器中的 mat4 类型 [英] mat4 type in attribute shader

查看:145
本文介绍了属性着色器中的 mat4 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发送到 MAT4 类型的着色器属性?

how to send to a shader attribute with the MAT4 type?

attribute mat4 attr;
...

JS:

var attr=gl.getAttribLocation(_program,"attr");

推荐答案

来自规范第 2.10.4 节

当一个属性变量被声明为 mat4 时,它的矩阵列取自通用属性的 (x, y, z, w) 分量 i通过 i + 3.

When an attribute variable is declared as a mat4, its matrix columns are taken from the (x, y, z, w) components of generic attributes i through i + 3.

所以

JS:

var row0Location = gl.getAttribLocation(_program, "attr");
var row1Location = row0Location + 1;
var row2Location = row0Location + 2;
var row3Location = row0Location + 3; 

至于获取数据,最常见的方法是将所有矩阵放在一个缓冲区中

As for getting the data the most common way would be to put all the matrices in one buffer so

var matrices = new Float32Array(numMatrices * 16);

... // fill out your matrices

gl.bufferData(gl.ARRAY_BUFFER, matrices, gl.STATIC_DRAW);

然后设置属性

var floatsPerRow = 4
var bytesPerRow = floatsPerRow * 4;
var bytesPerMatrix = bytesPerRow * 4;
var row0Offset = bytesPerRow * 0;
var row1Offset = bytesPerRow * 1;
var row2Offset = bytesPerRow * 2;
var row3Offset = bytesPerRow * 3;
gl.enableVertexAttribArray(row0Location);
gl.vertexAttribPointer(row0Location, floatsPerRow, gl.FLOAT, 
                       false, bytesPerMatrix, row0Offset);
gl.enableVertexAttribArray(row1Location);
gl.vertexAttribPointer(row1Location, floatsPerRow, gl.FLOAT, 
                       false, bytesPerMatrix, row1Offset);
gl.enableVertexAttribArray(row2Location);
gl.vertexAttribPointer(row2Location, floatsPerRow, gl.FLOAT, 
                       false, bytesPerMatrix, row2Offset);
gl.enableVertexAttribArray(row3Location);
gl.vertexAttribPointer(row3Location, floatsPerRow, gl.FLOAT, 
                       false, bytesPerMatrix, row3Offset);

需要注意的事情.如果您正在调试并在着色器中注释掉 attr,则 row0Location 将是 -1 并使用一个调用所有 gl.vertexAttrib 函数-1 位置是无操作的,这很好.但是,因为您计算其他位置 row1Locationrow2Locationrow3Location 将是有效的属性位置,就 WebGL 而言,但就WebGL 而言是无效的你的程序有问题.没什么大不了的,只是要记住.

Something to be aware of. If you're debugging and you comment out attr in your shader then row0Location will be -1 and calling all the gl.vertexAttrib functions with a -1 location is a no-op which is good. But, because you compute the other locations row1Location, row2Location and row3Location will be valid attribute locations as far as WebGL is concern but invalid as far as your program is concerned. Not a big deal just something to keep in mind.

这篇关于属性着色器中的 mat4 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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