javascript - Javascript 数组在某些情况下不适用于 WebGL
问题描述
我正在为 WebGL 开发 OBJ 文件阅读器,但在渲染读取的三角形时遇到问题。当我尝试绘制三角形时,出现此错误:[Error] WebGL: INVALID_OPERATION: drawArrays: attempt to access out of bounds arrays
这是我的源代码:
function ObjModel(filename, shader, gl) {
var readVertices = {}, readUVs = {}, readNormals = {};
var verticesIdx = 0, uvIndices = 0, normalIndices = 0;
var tmpVertices = {}, tmpUVs = {}, tmpNormals = {};
var tmpVerticesIdx = 0;
this.components = {};
loadFileAsText(filename, (result) => {
if(lines[i][0] == "v" && lines[i][1] == " ") {
var floats = lines[i].split(" ");
readVertices[verticesIdx + 0] = parseFloat(floats[1]);
readVertices[verticesIdx + 1] = parseFloat(floats[2]);
readVertices[verticesIdx + 2] = parseFloat(floats[3]);
verticesIdx += 3;
}
if(lines[i][0] == "f") {
var points = lines[i].split(" ");
var point1 = points[1].split("/");
var point2 = points[2].split("/");
var point3 = points[3].split("/");
tmpVertices[tmpVerticesIdx + 0] = readVertices[parseInt(point1[0]) - 1];
tmpVertices[tmpVerticesIdx + 1] = readVertices[parseInt(point2[0]) - 1];
tmpVertices[tmpVerticesIdx + 2] = readVertices[parseInt(point3[0]) - 1];
tmpUVs[tmpVerticesIdx + 0] = readUVs[parseInt(point1[1]) - 1];
tmpUVs[tmpVerticesIdx + 1] = readUVs[parseInt(point2[1]) - 1];
tmpUVs[tmpVerticesIdx + 2] = readUVs[parseInt(point3[1]) - 1];
tmpNormals[tmpVerticesIdx + 0] = readNormals[parseInt(point1[2]) - 1];
tmpNormals[tmpVerticesIdx + 1] = readNormals[parseInt(point2[2]) - 1];
tmpNormals[tmpVerticesIdx + 2] = readNormals[parseInt(point3[2]) - 1];
tmpVerticesIdx += 3;
}
}
this.components[0] = new ObjComponent(tmpVertices, tmpUVs, tmpNormals);
this.cluster = new TriangleCluster(this.components[0].vertices, this.components[0].uvs, this.components[0].normals, 1, shader, gl);
});
}
有趣的部分是最后两行。这ObjComponent
是这个简单的类:
function ObjComponent(vertices, uvs, normals) {
this.vertices = vertices;
this.uvs = uvs;
this.normals = normals;
}
它只包含顶点、uns 和法线的数组。TriangleCluster
是绘制三角形的类。使用此代码,我从上面收到错误消息,但是我自己将数组设置为某些值是可行的,因此数组以某种方式不起作用。但我可以访问tmpVertices
. 我在render函数里面打印了数组内容TriangleCluster
,数据还是可以的
这是我的TriangleCluster
课:
function TriangleCluster(vertices, uvs, normals, triangleNumber, shader, gl) {
shader.use();
this.vertices = vertices;
this.uvs = uvs;
this.normals = normals;
var verticesBuffer = new ArrayBufferFloat(this.vertices, gl);
var verticesAttribLocation = new VertexAttribPointerFloat(shader.getProgram(), "vertex", 3, 3, 0, gl);
var uvsBuffer = new ArrayBufferFloat(this.uvs, gl);
var uvsAttribLocation = new VertexAttribPointerFloat(shader.getProgram(), "uv", 2, 2, 0, gl);
var normalsBuffer = new ArrayBufferFloat(this.normals, gl);
var normalsAttribLocation = new VertexAttribPointerFloat(shader.getProgram(), "normal", 3, 3, 0, gl);
this.render = function() {
verticesBuffer.bind();
verticesAttribLocation.bind();
uvsBuffer.bind();
uvsAttribLocation.bind();
normalsBuffer.bind();
normalsAttribLocation.bind();
this.texture.activate(gl.TEXTURE0);
gl.drawArrays(gl.TRIANGLES, 0, triangleNumber * 3);
}
}
这种行为可能来自我的ArrayBuffer
班级,我在那里创建了一个new Float32Array
不适用于parseFloat
?
那么问题出在哪里,在代码中?
解决方案
问题是,出于某种原因,JS 不喜欢数组转换。
推荐阅读
- javascript - useState() 钩子不更新更改值
- pentaho - 在 Pentaho 中将列转换为行
- python - 使用浮点数安全地播种 Python RNG?
- spring - 在春季启动 TypeMismatchException.getPropertyName() 总是返回 null
- apache-kafka - Siddhi 中的 Kafka 源初始化问题
- hadoop - Hive 按组计算中位数和平均值
- oracle - PL/SQL 过程中布尔输出变量的默认值是多少?
- haskell - 使用 monad 转换器和延续来为过程提前返回做一个最小的解释器
- node.js - 将有效的时间类型 usnig 时刻从 React setState 传递给 postgres
- sql - 在 SELECT 语句中执行 SELECT 语句?