I am trying to display points (a pointcloud actually) using WebGL. The past 2 days I spent going through this tutorial and still have a lot of my code based on it.
I am attempting to load a .ply file and then show each coordinate as a coloured point, but get stuck at this error when trying to draw it:
[.WebGLRenderingContext-0x111ff17bc300]GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1
I have of course done some research, but the general answer was that the length of the buffered arrays would be different, which I have made sure is not the case. It's mostly likely a similar noobish mistake though:
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();
}
Aucun commentaire:
Enregistrer un commentaire