gpt4 book ai didi

javascript - WebGL:绘制点失败:glDrawArrays: "attempt to access out of range vertices in attribute 1"

转载 作者:行者123 更新时间:2023-12-02 15:52:59 25 4
gpt4 key购买 nike

我正在尝试使用 WebGL 显示点(实际上是点云)。过去两天我经历了this教程,并且仍然有很多基于它的代码。

我正在尝试加载 .ply 文件,然后将每个坐标显示为彩色点,但在尝试绘制它时遇到此错误:

[.WebGLRenderingContext-0x111ff17bc300]GL 错误:GL_INVALID_OPERATION:glDrawArrays:尝试访问属性 1 中超出范围的顶点

我当然做了一些研究,但一般答案是缓冲数组的长度会有所不同,我确信情况并非如此。但这很可能是一个类似的新手错误:

function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");

shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);

if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}

gl.useProgram(shaderProgram);

shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}

var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();

function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}

function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}

function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}

function degToRad(degrees) {
return degrees * Math.PI / 180;
}


var pointcloudVertexPositionBuffer;
var pointcloudColourBuffer;
var pointcloudIndexBuffer;
function handlePoints(pointCoords, pointColours, indices) {
pointcloudVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointCoords), gl.STATIC_DRAW);
pointcloudVertexPositionBuffer.itemSize = 3;
pointcloudVertexPositionBuffer.numItems = pointCoords.length / 3;

pointcloudColourBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointColours), gl.STATIC_DRAW);
pointcloudColourBuffer.itemSize = 3;
pointcloudColourBuffer.numItems = pointColours.length / 3;

pointcloudIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, pointcloudIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
pointcloudIndexBuffer.itemSize = 1;
pointcloudIndexBuffer.numItems = indices.length;
}

var coordList = [];
var colList = [];
var indexList = [];
function loadPLY(){
jQuery.get('some_ascii.ply', function(data) {
var lines = data.split('\n');
var coords = false;

for(var line = 0; line < lines.length; line++){
if (coords) {
var splitString = lines[line].split(" ");
coordList.push(parseFloat(splitString[0]), parseFloat(splitString[1]), parseFloat(splitString[2]));
colList.push(parseFloat(splitString[3]), parseFloat(splitString[4]), parseFloat(splitString[5]));
indexList.push(parseFloat(line));
}
else{
if (lines[line] == "end_header"){
console.log("end header");
coords = true;
}
}
}
handlePoints(coordList, colList, indexList);
});
}

function drawScene(){
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

mat4.identity(mvMatrix);

//mat4.translate(mvMatrix, [0, 0, -6]);

//gl.uniform1i(shaderProgram.samplerUniform, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, pointcloudColourBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, pointcloudVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

setMatrixUniforms();
//gl.drawElements(gl.TRIANGLES, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
//console.log("1", pointcloudIndexBuffer.numItems);
//console.log("2", pointcloudColourBuffer.numItems);
//console.log("3", pointcloudVertexPositionBuffer.numItems);
gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}


function tick() {
requestAnimFrame(tick);
drawScene();
}

function webGLStart(){
var canvas = document.getElementById("canvas");
initGL(canvas);
initShaders();
loadPLY();

gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);

tick();
}

最佳答案

我再次查看了你的代码,发现了一些事情:

如果您正在绘制 gl.POINT,则需要在顶点着色器中包含 gl_PointSize = 8.0;,否则将不会绘制点。

您的颜色数据不正确。负色没有意义。

您的drawArrays调用不正确,应该是gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems);

我禁用了透视,因为如果没有相机,很难判断事物是否正确绘制,但在 View 之外。

您提到的无法读取未定义的属性“itemSize”是因为它试图在解析您的.ply数据之前绘制场景。要修复此问题,请仅在解析完成后才开始绘制。

所以这是我编辑的版本,至少可以绘制(您必须更改 src):

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<input type="file" name="file" id="file">
<script type="text/javascript" src="aaa/glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="aaa/webgl-utils.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;

varying vec4 vColor;

void main(void) {
//gl_FragColor = vColor;
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
</script>

<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

varying vec4 vColor;

void main(void) {
gl_PointSize = 8.0;
//gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = vec4(aVertexPosition, 1.0);
vColor = aVertexColor;
}

</script>


<script type="text/javascript">
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");

shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);

if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}

gl.useProgram(shaderProgram);

shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");

console.log(shaderProgram);
}

var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();

function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}

function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}

function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}

function degToRad(degrees) {
return degrees * Math.PI / 180;
}


var pointcloudVertexPositionBuffer;
var pointcloudColourBuffer;
var pointcloudIndexBuffer;
function handlePoints(pointCoords, pointColours, indices) {
pointcloudVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointCoords), gl.STATIC_DRAW);
pointcloudVertexPositionBuffer.itemSize = 3;
pointcloudVertexPositionBuffer.numItems = pointCoords.length / 3;

pointcloudColourBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointColours), gl.STATIC_DRAW);
pointcloudColourBuffer.itemSize = 3;
pointcloudColourBuffer.numItems = pointColours.length / 3;

pointcloudIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, pointcloudIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
pointcloudIndexBuffer.itemSize = 1;
pointcloudIndexBuffer.numItems = indices.length;
}

var coordList = [];
var colList = [];
var indexList = [];
function loadPLY(){
jQuery.get('aaa/untitled.ply', function(data) {
var lines = data.split('\n');
var coords = false;

for(var line = 0; line < lines.length; line++){
if (coords) {
var splitString = lines[line].split(" ");
if (splitString.length === 6) {
coordList.push(parseFloat(splitString[0]), parseFloat(splitString[1]), parseFloat(splitString[2]));
colList.push(parseFloat(splitString[3]), parseFloat(splitString[4]), parseFloat(splitString[5]));
indexList.push(parseFloat(line));
}
else{
coords = false;
}
}
else{
if (lines[line].indexOf("end_header")===0){
console.log("end header");
coords = true;
}
}
}
handlePoints(coordList, colList, indexList);
tick();
});

}

function drawScene(){
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);

mat4.identity(mvMatrix);

//mat4.translate(mvMatrix, [0, 0, -6]);

//gl.uniform1i(shaderProgram.samplerUniform, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudColourBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, pointcloudColourBuffer.itemSize, gl.FLOAT, false, 0, 0);

gl.bindBuffer(gl.ARRAY_BUFFER, pointcloudVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, pointcloudVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

setMatrixUniforms();
//gl.drawElements(gl.TRIANGLES, pointcloudIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
//console.log("1", pointcloudIndexBuffer.numItems);
//console.log("2", pointcloudColourBuffer.numItems);
//console.log("3", pointcloudVertexPositionBuffer.numItems);
gl.drawArrays(gl.POINTS, 0, pointcloudIndexBuffer.numItems);
}


function tick() {
requestAnimFrame(tick);
drawScene();
}

function webGLStart(){
var canvas = document.getElementById("canvas");
initGL(canvas);
initShaders();
loadPLY();

gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);

}


</script>
</head>

<style type="text/css">
#loadingtext {
position:absolute;
top:250px;
left:150px;
font-size:2em;
color: white;
}
</style>

<body onload="webGLStart();">
<canvas id="canvas" style="border: none;" width="500" height="500"></canvas>
</body>

</html>

enter image description here

关于javascript - WebGL:绘制点失败:glDrawArrays: "attempt to access out of range vertices in attribute 1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31750163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com