Created
April 24, 2014 13:01
-
-
Save Orpheon/11253823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void generate_projection_matrix(GLfloat *matrix, double fovx, double aspect, double nearDist, double farDist) | |
| { | |
| // Based on the gluPerspective implementation in java | |
| double deltaDist = farDist - nearDist; | |
| double cot = 1 / tan(fovx*M_PI / (2*aspect*180)); | |
| // Column-major ordering, matrix[0*4 + 1] is right beneath matrix[0*4 + 0] | |
| matrix[0*4 + 0] = cot / aspect; | |
| matrix[0*4 + 1] = 0; | |
| matrix[0*4 + 2] = 0; | |
| matrix[0*4 + 3] = 0; | |
| matrix[1*4 + 0] = 0; | |
| matrix[1*4 + 1] = cot; | |
| matrix[1*4 + 2] = 0; | |
| matrix[1*4 + 3] = 0; | |
| matrix[2*4 + 0] = 0; | |
| matrix[2*4 + 1] = 0; | |
| matrix[2*4 + 2] = -(farDist + nearDist) / deltaDist; | |
| matrix[2*4 + 3] = -1; | |
| matrix[3*4 + 0] = 0; | |
| matrix[3*4 + 1] = 0; | |
| matrix[3*4 + 2] = -2 * nearDist * farDist / deltaDist; | |
| matrix[3*4 + 3] = 0; | |
| } | |
| void generate_view_matrix(GLfloat *matrix, point *cam_pos, point *cam_dir, point *up) | |
| { | |
| printf("\n\nCam dir: (%f|%f|%f), |v| = %f", cam_dir->x, cam_dir->y, cam_dir->z, cam_dir->x*cam_dir->x + cam_dir->y*cam_dir->y + cam_dir->z*cam_dir->z); | |
| fflush(stdout); | |
| // Assumption: cam_dir is normalized | |
| // Side = cam_dir X up | |
| point side; | |
| cross(cam_dir, up, &side); | |
| // Rel up = side X cam_dir | |
| point relative_up; | |
| cross(&side, cam_dir, &relative_up); | |
| // Column-major ordering, matrix[0*4 + 1] is right beneath matrix[0*4 + 0] | |
| matrix[0*4 + 0] = side.x; | |
| matrix[0*4 + 1] = relative_up.x; | |
| matrix[0*4 + 2] = -cam_dir->x; | |
| matrix[0*4 + 3] = 0; | |
| matrix[1*4 + 0] = side.y; | |
| matrix[1*4 + 1] = relative_up.y; | |
| matrix[1*4 + 2] = -cam_dir->y; | |
| matrix[1*4 + 3] = 0; | |
| matrix[2*4 + 0] = side.z; | |
| matrix[2*4 + 1] = relative_up.z; | |
| matrix[2*4 + 2] = -cam_dir->z; | |
| matrix[2*4 + 3] = 0; | |
| matrix[3*4 + 0] = 0; | |
| matrix[3*4 + 1] = 0; | |
| matrix[3*4 + 2] = 0; | |
| matrix[3*4 + 3] = 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment