Pregunta

mat4.lookAt() below is giving me unexpected results. Please see the images associated with the 3 test examples. The first example is "correct", and the others seem clipped, but I cannot figure why. The perspectives and distances from the camera seem correct on all, but except for the first, I am not getting the whole view I want. Can you suggest what I need to do adjust this?

function draw() { 

    gl.clearColor(bgcolor[0],bgcolor[1],bgcolor[2],1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    if (document.getElementById("persproj").checked) {
    mat4.perspective(projection, Math.PI/4, 1, 4, 8);
    }
    else {
    mat4.ortho(projection,-2.5, 2.5, -2.5, 2.5, 4, 8);
    }
    gl.uniformMatrix4fv(uProjection, false, projection );

    mat4.lookAt(modelview, [0,0,6], [0,0,0], [0,1,0]); // This is the key line
    mat4.rotateX(modelview, modelview, rotateX);
    mat4.rotateY(modelview, modelview, rotateY);
    gl.uniformMatrix4fv(uModelview, false, modelview );

    mat3.normalFromMat4(normalMatrix, modelview);
    gl.uniformMatrix3fv(uNormalMatrix, false, normalMatrix);

    gl.uniform1i( uLit, 0 );  // The lines representing the coordinate axes are not lit.

    gl.lineWidth(4);
    drawPrimitive( gl.LINES, [1,0,0,1], [ -2,0,0, 2,0,0] );
    drawPrimitive( gl.LINES, [0,1,0,1], [ 0,-2,0, 0,2,0] );
    drawPrimitive( gl.LINES, [0,0,1,1], [ 0,0,-2, 0,0,2] );
    gl.lineWidth(1);
    if (leftColors.length>0){
    drawTurtles(linecolors,moves,leftColors,rightColors,backColors,bottoms,lefts,rights,backs,bottomNs,leftNs,rightNs,backNs);
    }
}

mat4.lookAt(modelview, [0,0,6], [0,0,0], [0,1,0]) mat4.lookAt(modelview, [0,0,6], [0,0,0], [0,1,0])

mat4.lookAt(modelview, [0,0,8], [0,0,0], [0,1,0]) mat4.lookAt(modelview, [0,0,8], [0,0,0], [0,1,0])

mat4.lookAt(modelview, [0,0,4], [0,0,0], [0,1,0]) mat4.lookAt(modelview, [0,0,4], [0,0,0], [0,1,0])

¿Fue útil?

Solución

The problem comes from the clipping distance set by mat4.Ortho:

enter image description here

mat4.lookAt(modelview, [0,0,8], [0,0,0], [0,1,0])

This will set your eye position such as the far plane will pass thru (0,0,0), limiting your viewable content to the view plane on (0,0,0).

mat4.lookAt(modelview, [0,0,4], [0,0,0], [0,1,0])

This will set your eye pos at the limit of the near plane, giving unpredictable results when exactly equal.

So the solution is to adjust your near and far clipping in mat4.Ortho:

mat4.ortho(projection,-2.5, 2.5, -2.5, 2.5, 4 - x, 8 + x);

where x could be a minimal displacement.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top