Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
DisplaySurface.prototype.projectionMatrix = function(eye, znear, zfar){
    var mat = new Mat4();
    mat.loadIdentity();
    // ...  

    var br = new Vec3.add(this.u, this.origin), //pb
        tl = new Vec3.add(this.v, this.origin); //pc

    var bre = new Vec3.subtract(br, eye),
        tle = new Vec3.subtract(tl, eye);

    var u = this.u.normalize(),
        v = this.v.normalize(),
        n = this.n.normalize(),
        eyedist = Vec3.dot(bre, n);

    var l = Vec3.dot(tle, u)*znear/eyedist,
        r = Vec3.dot(bre, u)*znear/eyedist,
        b = Vec3.dot(bre, v)*znear/eyedist,
        t = Vec3.dot(tle, v)*znear/eyedist;

    console.log(r-l);

    mat.frustum(l, r, b, t, znear, zfar);
    console.log(mat);
    return mat;
};

Mat4.prototype.frustum = function(left, right, bottom, top, near, far){
    var a = this._;
    var n2 = 2.0*near,
        diffX = right - left,
        diffY = top - bottom,
        diffZ = far - near;
    a[0] = n2/diffX;
    a[1] = 0;
    a[2] = 0;
    a[3] = 0;

    a[4] = 0;
    a[5] = n2/diffY;
    a[6] = 0;
    a[7] = 0;

    a[8] = (right + left) / diffX;
    a[9] = (top + bottom) / diffY;
    a[10] = -(far + near) / diffZ;
    a[11] = -1.0;

    a[12] = 0.0;
    a[13] = 0.0;
    a[14] = -(n2 * far) / diffZ;
    a[15] = 0.0;
};


What I have tried:

So... this is supposed to return a projection matrix. Everything runs smoothly on the firts frame, then I start getting weird results. I never worked with javascript, and I have been trying for a while to figure it out - the only clue I got is that (l-r) seems to be producing the strange results, though (l) by itself is fine.

@Edit. Yeah, t and r are the problem. Though why? :(
Posted
Updated 14-Nov-17 7:11am
v2
Comments
ZurdoDev 14-Nov-17 16:16pm    
Just debug it and see what their values are.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900