Sign in to follow this  
wlw_wl

perspective transformation failing

Recommended Posts

Hello, I've implemented glPerspective and glLookAt, however I can't get the former to work. Some explanation first: I'm not using stacks, instead I just have two global matrices, ViewMatrix (serves as projection stack) and WorldMatrix (as model view stack). Both initialize with identity, and then i.e. calling LookAt multiplies ViewMatrix by appropriate matrix. So the pipeline looks like this: take vertex P, multiply it by WorldMAtrix, then by ViewMatrix. This should give screen x and y coordinates to draw the pixel. While wLookAt seems to be working fine (I tested it with some false 3d to 2d projection), the wPerspective won't. Here's the code (I don't know how to make those code snippets...): void wLookAt(double eyex, double eyey, double eyez, double centerx, double centery, double centerz, Vec3 up) { Vec3 forward(0.0, 0.0, 0.0), side(0.0, 0.0, 0.0), _up(up); Mat4x4 m; // temp matrix m.identity(); forward.x = centerx - eyex; forward.y = centery - eyey; forward.z = centerz - eyez; normalizeSelf(forward); // just normalizes a vector side = cross(forward, _up); normalizeSelf(side); _up = cross(side, forward); m.m[0][0] = side.x; m.m[1][0] = side.y; m.m[2][0] = side.z; m.m[0][1] = -_up.x; // "-" because objects got drawn upside-down m.m[1][1] = -_up.y; m.m[2][1] = -_up.z; m.m[0][2] = -forward.x; m.m[1][2] = -forward.y; m.m[2][2] = -forward.z; //ViewMatrix = m * ViewMatrix; ViewMatrix *= m; ViewMatrix.translate(-eyex, -eyey, -eyez); } and the other one: void wPerspective(double fovy, double aspect, double znear, double zfar) { double fovy_rad = fovy * 0.017453292519943; // degs to radians double yScale = -1/tan(fovy_rad/2.0); // -cot(fovy_rad/2), yet again "-" because of up-down double xScale = yScale / aspect; Mat4x4 m; // temp matrix m.zero(); m.m[0][0] = xScale; m.m[1][1] = yScale; m.m[2][2] = zfar/(znear-zfar); m.m[3][2] = znear*zfar/(znear-zfar); m.m[2][3] = -1; ViewMatrix = m * ViewMatrix; //ViewMatrix *= m; } and after calling in drawin loop WorldMatrix.loadIdentity(); ViewMatrix.loadIdentity(); Vec3 up(0.0, 1.0, 0.0); wLookAt(0.0, 0.0, -3.0, 0.0, 0.0, 0.0, up); // drawing area is 800x600 wPerspective(45.0, (double)(4.0/3.0), 0.0, 100.0); and Flushing*, I get some weird coordinates which cause app to terminate - negative coordinates in a very first frame, which shouldn't happen as theo nly one object in the scene is set up at the origin and 1 unit big. *) maybe I did this wrong? for each vertex flushing does this: //(d is some vertex in 3d space with Lx, Ly, Lz (L for it's local) coords Vec3 A; A = Vec3(d->Lx, d->Ly, d->Lz) * WorldMatrix; A *= ViewMatrix; //and (try to) draw pixel at (A.x, A.y); - FAIL Please, if you have a clue but need more of the code, say it. I didn't want to paste all at once because it would be unreadable...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this