在webgl中实现透视矩阵

时间:2014-11-02 18:53:59

标签: javascript webgl

我有这个小代码,沿着y轴在世界原点(0,0,0)处旋转一个三角形。

<!DOCTYPE HTML>
<html>

<canvas id = "can" width="400" height="400">

</canvas>


<script>
var webgl_canvas = document.getElementById('can');
var gl = webgl_canvas.getContext('experimental-webgl');
var width = gl.width;
var vertices = [-1,-1,0,1,-1,0,0,1,0];
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(vertices), gl.STATIC_DRAW); 

vertexBuffer.itemSize = 3;
vertexBuffer.numItems = parseInt(vertices.length/vertexBuffer.itemSize);

var tx = 0, ty = 0, tz = 0;

var degrees = 0.1;

function rotY (degrees) {
    m = identityMatrix;
    var c = Math.cos(degrees);
    var s = Math.sin(degrees);
    var mv0=m[0], mv4=m[4], mv8=m[8];
    m[0]=c*m[0]+s*m[2];
    m[4]=c*m[4]+s*m[6];
    m[8]=c*m[8]+s*m[10];

    m[2]=c*m[2]-s*mv0;
    m[6]=c*m[6]-s*mv4;
    m[10]=c*m[10]-s*mv8;
}

var identityMatrix = [1,0,0,0,
                      0,1,0,0,
                      0,0,1,0,
                      0,0,0,1];

var translationMatrix = [1,0,0,tx,
                         0,1,0,ty,
                         0,0,1,tz,
                         0,0,0,1];



function degToRadians(deg){
    return (deg*MATH.PI/180);
}

function translation(x) {
    m = identityMatrix;
    return m[12] += x;
}

var vertexShader_source = 'attribute vec3 a_position;' + 'uniform mat4 u_move;' + 'void main() { gl_Position = u_move * vec4 (a_position,1); }';
var fragmentShader_source = 'precision mediump float;' + 'void main() { gl_FragColor = vec4 (0.9,0,0.1,1); }';



//Compile shaders
var buildShader = function (shaderSource, typeOfShader) {
var shader = gl.createShader(typeOfShader);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    alert (gl.getShaderInfoLog(shader));
}
return shader;
}

var compiledVertexShader = buildShader (vertexShader_source, gl.VERTEX_SHADER);
var compiledFragmentShader = buildShader (fragmentShader_source, gl.FRAGMENT_SHADER);

//setup GLSL program
program = gl.createProgram();
gl.attachShader(program,compiledVertexShader);
gl.attachShader(program,compiledFragmentShader);
gl.linkProgram(program);



var positionLocation = gl.getAttribLocation(program,"a_position");
gl.enableVertexAttribArray(positionLocation);
gl.useProgram(program);




var translate = gl.getUniformLocation (program, "u_move");



//Draw
var start_time =0;
var animate=function(time) {
var dt= time-start_time;
var matrix = rotY(degrees);
gl.uniformMatrix4fv(translate,false,new Float32Array(identityMatrix));
gl.vertexAttribPointer(positionLocation, vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);


//console.log(dt);

start_time=time;
gl.drawArrays (gl.TRIANGLES, 0, vertexBuffer.numItems);
window.requestAnimationFrame(animate);
}
animate(0);
</script>

</html>

现在,我已经添加了旋转功能

function rotY (degrees) {
    m = identityMatrix;
    console.log(identityMatrix);
    var c = Math.cos(degrees);
    var s = Math.sin(degrees);
    var mv0=m[0], mv4=m[4], mv8=m[8];
    m[0]=c*m[0]+s*m[2];
    m[4]=c*m[4]+s*m[6];
    m[8]=c*m[8]+s*m[10];

    m[2]=c*m[2]-s*mv0;
    m[6]=c*m[6]-s*mv4;
    m[10]=c*m[10]-s*mv8;
}

将其与制服

相关联
var perspective_matrix = gl.getUniformLocation (program, "u_perspective");

并启用它

gl.uniformMatrix4fv(perspective_matrix,false, PERSPMATRIX);

三角形现已消失。检查控制台我的矩阵值返回undefined所以我怀疑我有什么不对劲,虽然我没有看到确切的位置。 完整的代码如下

<!DOCTYPE HTML>
<html>

<canvas id = "can" width="400" height="400">

</canvas>


<script>
var webgl_canvas = document.getElementById('can');
var gl = webgl_canvas.getContext('experimental-webgl');
webgl_canvas.width=window.innerWidth;
webgl_canvas.height=window.innerHeight;
var width = gl.width;
var vertices = [-1,-1,0,1,-1,0,0,1,0];
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(vertices), gl.STATIC_DRAW); 

vertexBuffer.itemSize = 3;
vertexBuffer.numItems = parseInt(vertices.length/vertexBuffer.itemSize);

var tx = 0, ty = 0, tz = 0;

var degrees = 0.1;

var identityMatrix = [1,0,0,0,
                      0,1,0,0,
                      0,0,1,0,
                      0,0,0,1];

function perspective(angle,aspect,zMin,zMax){
    var tan = Math.tan(degToRadians(0.5*angle)),
    A =-(zMax+zMin)/(zMax-zMin),
    B = (-2*zMax*zMin)/(zMax-zMin);

    return [ .5/tan,0,0,0,
             0, .5*aspect/tan, 0, 0,
             0,0,A,-1,
             0,0,B,0
           ];    
}

function rotX (degrees) {
    m = identityMatrix;
    var c = Math.cos(degrees);
    var s = Math.sin(degrees);
    var mv1=m[1], mv5=m[5], mv9=m[9];
    m[1]=m[1]*c-m[2]*s;
    m[5]=m[5]*c-m[6]*s;
    m[9]=m[9]*c-m[10]*s;

    m[2]=m[2]*c+mv1*s;
    m[6]=m[6]*c+mv5*s;
    m[10]=m[10]*c+mv9*s;
}

function rotY (degrees) {
    m = identityMatrix;
    console.log(identityMatrix);
    var c = Math.cos(degrees);
    var s = Math.sin(degrees);
    var mv0=m[0], mv4=m[4], mv8=m[8];
    m[0]=c*m[0]+s*m[2];
    m[4]=c*m[4]+s*m[6];
    m[8]=c*m[8]+s*m[10];

    m[2]=c*m[2]-s*mv0;
    m[6]=c*m[6]-s*mv4;
    m[10]=c*m[10]-s*mv8;
}


var translationMatrix = [1,0,0,tx,
                         0,1,0,ty,
                         0,0,1,tz,
                         0,0,0,1];



function degToRadians(deg){
    return (deg*Math.PI/180);
}

function translation(x) {
    m = identityMatrix;
    return m[12] += x;
}

var vertexShader_source = 'attribute vec3 a_position;' + 'uniform mat4 u_move;' + 'uniform mat4 u_perspective;' + 'void main() { gl_Position = u_perspective * u_move * vec4 (a_position,1); }';
var fragmentShader_source = 'precision mediump float;' + 'void main() { gl_FragColor = vec4 (0.9,0,0.1,1); }';



//Compile shaders
var buildShader = function (shaderSource, typeOfShader) {
var shader = gl.createShader(typeOfShader);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    alert (gl.getShaderInfoLog(shader));
}
return shader;
}

var compiledVertexShader = buildShader (vertexShader_source, gl.VERTEX_SHADER);
var compiledFragmentShader = buildShader (fragmentShader_source, gl.FRAGMENT_SHADER);

//setup GLSL program
program = gl.createProgram();
gl.attachShader(program,compiledVertexShader);
gl.attachShader(program,compiledFragmentShader);
gl.linkProgram(program);

//link javascript variables with shaders uniforms
var perspective_matrix = gl.getUniformLocation (program, "u_perspective");
var translate = gl.getUniformLocation (program, "u_move");

var positionLocation = gl.getAttribLocation(program,"a_position");
gl.enableVertexAttribArray(positionLocation);
gl.useProgram(program);


var PERSPMATRIX = perspective(40,webgl_canvas.width/webgl_canvas.height,1,100);



//Draw
var start_time =0;
var animate=function(time) {



var dt= time-start_time;
var matrix = rotX(degrees);
console.log(matrix);
gl.uniformMatrix4fv(perspective_matrix,false, PERSPMATRIX);
gl.uniformMatrix4fv(translate,false,identityMatrix);
gl.vertexAttribPointer(positionLocation, vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
start_time=time;
gl.drawArrays (gl.TRIANGLES, 0, vertexBuffer.numItems);
window.requestAnimationFrame(animate);
}
animate(0);
</script>

</html>

1 个答案:

答案 0 :(得分:1)

这只会指出特定问题。

1)使用var identityMatrix = [ ... ]创建一个数组。每次使用var m = identityMatrix;引用它时,都不会创建新的数组,但始终只有指向原始数据。现在,当您更改var m中的项目时,您更改了原始identityMatrix中的相同项目,并且更改会显示在您在整个代码中指向它的所有位置。

使identityMatrix成为一个返回数组的函数:

var identityMatrix = function() {return [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];}

每次你想要以一个新的identityMatrix开始:

var m = identityMatrix(); // now you get new matrices not affecting each other

var verticesvar translationMatrix可能会发生同样的情况,我会给您留意。

2)你的函数translation()不返回矩阵,只返回一个数字(矩阵的第13项)。必须是:

function translation(x) {
    var m = identityMatrix; // don't miss the var keyword
    m[12] += x; // change value at index 12
    return m; // return the whole matrix
}

3)两个rotate - 函数都不返回任何内容。里面应该有return m;