Tuesday, February 16, 2016

opengl es - culling lines on the back of a globe


I am drawing a simple outline of the world using GL_LINE_STRIPs (in WebGL).


I want to not draw those lines that are on the other side of the spinning globe.


Using Nicol Bolas approach below I am trying to determine if a point is going away from the camera or not:


scene.map = {
vbo: gl.createBuffer(),

draw: function(pMatrix,mvMatrix,camera,t) {
gl.useProgram(this.program);
gl.uniformMatrix4fv(this.program.pMatrix,false,pMatrix);
var spin = mat4_rotation(t,[0,1,0]);
mvMatrix = mat4_multiply(mvMatrix,spin);
gl.uniformMatrix4fv(this.program.mvMatrix,false,mvMatrix);
var nMatrix = mat4_inverse(mat4_transpose(mvMatrix));
gl.uniformMatrix4fv(this.program.nMatrix,false,nMatrix);
gl.uniform3fv(this.program.camera,camera);
gl.uniform4fv(this.program.colour,[0,1,0,1]);

gl.enableVertexAttribArray(this.program.vertex);
gl.bindBuffer(gl.ARRAY_BUFFER,this.vbo);
gl.vertexAttribPointer(this.program.vertex,3,gl.FLOAT,false,3*4,0);
var ofs = 0, parts = this.data.ofs;
for(var part=0; part var start = ofs;
ofs += parts[part];
gl.drawArrays(gl.LINE_STRIP,start,ofs-start);
}
gl.disableVertexAttribArray(this.program.vertex);

gl.bindBuffer(gl.ARRAY_BUFFER,null);
gl.useProgram(null);
},
program: createProgram("precision mediump float;\n"+
"attribute vec3 vertex;\n"+
"uniform mat4 pMatrix, mvMatrix, nMatrix;\n"+
"varying vec3 n;\n"+
"void main() {\n"+
" gl_Position = pMatrix * mvMatrix * vec4(vertex,1.0);\n"+
" n = (nMatrix * vec4(vertex,1.0)).xyz;\n"+

"}\n",
"precision mediump float;\n"+
"uniform vec4 colour;\n"+
"uniform vec3 camera;\n"+
"varying vec3 n;\n"+
"void main() {\n"+
" if(dot(camera,n) < 0.0) discard;\n"+
" gl_FragColor = colour;\n"+
"}\n",
["pMatrix","mvMatrix","colour","camera","nMatrix"],

["vertex"]),
data: getFile("json","data/world.json"),
};
var pts = [], deg2rad = Math.PI/180;
for(var i=0; i var lng = scene.map.data.pts[i] * deg2rad,
lat = scene.map.data.pts[i+1] * deg2rad;
pts.push(-Math.cos(lat)*Math.cos(lng),Math.sin(lat),Math.cos(lat)*Math.sin(lng));
}
pts.push(0,0,0); // centre of sphere, for sprite z-culling trick

gl.bindBuffer(gl.ARRAY_BUFFER,scene.map.vbo);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(pts),gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER,null);

and to draw it:


var pMatrix = createPerspective(60,canvas.width/canvas.height,0.1,4),
eye = [-2,0,0],
mvMatrix = createLookAt(eye,[0,0,0],[0,1,0]);
scene.map.draw(pMatrix,mvMatrix,eye,t);


However, this is only drawing the left hemisphere rather than the front:


enter image description here


How can I correctly compute the normal of a point, and the position of the camera?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...