Click here to Skip to main content
13,259,427 members (54,421 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 14-Nov-17 8:00am
Updated 14-Nov-17 8:11am
v2
Comments
GobblesGobbles 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 14 Nov 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100