Created
March 22, 2014 13:37
-
-
Save Orpheon/9707255 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_view_matrix(GLfloat *matrix, point *cam_pos, point *cam_dir) | |
| { | |
| // http://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml | |
| // f = cam_dir, up = (0|1|0) | |
| // s = normalize(f X up) | |
| // u = s X f | |
| double x = cam_dir->x, y = cam_dir->y, z = cam_dir->z; | |
| double l = sqrt(z*z + x*x); | |
| double s_x = -z/l, s_z = x/l; | |
| double u_x = -s_z*y, u_y = s_z*x - s_x*z, u_z = s_x*y; | |
| // 4x4 matrix | |
| // 4th element is the translation | |
| // s[0] s[1] s[2] x | |
| // u[0] u[1] u[2] y | |
| // -f[0] -f[1] -f[2] z | |
| // 0 0 0 1 | |
| matrix[0] = s_x; | |
| matrix[1] = u_x; | |
| matrix[2] = -x; | |
| matrix[3] = 0; | |
| matrix[4] = 0; | |
| matrix[5] = u_y; | |
| matrix[6] = -y; | |
| matrix[7] = 0; | |
| matrix[8] = s_z; | |
| matrix[9] = u_z; | |
| matrix[10] = -z; | |
| matrix[11] = 0; | |
| matrix[12] = cam_pos->x; | |
| matrix[13] = cam_pos->y; | |
| matrix[14] = cam_pos->z; | |
| matrix[15] = 1; | |
| } | |
| ... | |
| void turn_view(GLFWwindow* window, point *cam_dir) | |
| { | |
| double mouse_x, mouse_y; | |
| glfwGetCursorPos(window, &mouse_x, &mouse_y); | |
| // Get the vector from the center of the screen to current mouse position and normalize it relative to screen size | |
| mouse_x = (mouse_x - WINDOW_WIDTH/2.0) * HORIZONTAL_TURNSPEED / WINDOW_WIDTH; | |
| mouse_y = (mouse_y - WINDOW_HEIGHT/2.0) * VERTICAL_TURNSPEED / WINDOW_HEIGHT; | |
| // Rotate the view vector in accordance | |
| double tmp; | |
| tmp = cam_dir->x; | |
| cam_dir->x += mouse_x * -cam_dir->z; | |
| cam_dir->y = sin(mouse_y + asin(cam_dir->y)); | |
| cam_dir->z += mouse_x * tmp; | |
| normalize(cam_dir); | |
| } | |
| ... | |
| layout(location = 0) in vec4 vertex_position; | |
| uniform mat4 projection_matrix; | |
| uniform mat4 view_matrix; | |
| void main() | |
| { | |
| gl_Position = view_matrix * projection_matrix * vertex_position; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment