void camera_handle_rotation(camera* cam) { vec3f vertical_axis = vec3f_init(0.0f, 1.0f, 0.0f); // Rotate the view vector by the horizontal angle around the vertical axis vec3f view = vec3f_init(1.0f, 0.0f, 0.0f); view = vec3f_rotate_around_axis(view, vertical_axis, cam->angle_x); view = vec3f_normalize(view); // Rotate the view vector by the vertical angle around the horizontal axis vec3f horizontal_axis = vec3f_cross(vertical_axis, view); horizontal_axis = vec3f_normalize(horizontal_axis); view = vec3f_rotate_around_axis(view, horizontal_axis, cam->angle_y); cam->target = vec3f_normalize(view); cam->up = vec3f_cross(cam->target, horizontal_axis); cam->up = vec3f_normalize(cam->up); }
void do_movement() { GLfloat camera_speed; camera_speed = 5.0f * delta_time; if(keys[GLFW_KEY_W]) { vec3f_muls(temp_vec3f, camera_front, camera_speed); vec3f_add(camera_pos, camera_pos, temp_vec3f); } if(keys[GLFW_KEY_S]) { vec3f_muls(temp_vec3f, camera_front, camera_speed); vec3f_sub(camera_pos, camera_pos, temp_vec3f); } if(keys[GLFW_KEY_A]) { vec3f_cross(temp_vec3f, camera_front, camera_up); vec3f_normalize(temp_vec3f, temp_vec3f); vec3f_muls(temp_vec3f, temp_vec3f, camera_speed); vec3f_sub(camera_pos, camera_pos, temp_vec3f); } if(keys[GLFW_KEY_D]) { vec3f_cross(temp_vec3f, camera_front, camera_up); vec3f_normalize(temp_vec3f, temp_vec3f); vec3f_muls(temp_vec3f, temp_vec3f, camera_speed); vec3f_add(camera_pos, camera_pos, temp_vec3f); } }
void camera_handle_movement(camera* cam, Uint32 delta_time) { //float y = cam->pos.v[1]; float dt = delta_time / 1000.0f; if (cam->moving_forward) { cam->pos = vec3f_add(cam->pos, vec3f_mult(cam->target, cam->step * dt)); } if (cam->moving_back) { cam->pos = vec3f_subtract(cam->pos, vec3f_mult(cam->target, cam->step * dt)); } if (cam->moving_left) { vec3f left = vec3f_normalize(vec3f_cross(cam->target, cam->up)); cam->pos = vec3f_add(cam->pos, vec3f_mult(left, cam->step * dt)); } if (cam->moving_right) { vec3f right = vec3f_normalize(vec3f_cross(cam->up, cam->target)); cam->pos = vec3f_add(cam->pos, vec3f_mult(right, cam->step * dt)); } //cam->pos.v[1] = y; }
void mouse_callback(GLFWwindow *window, double xpos, double ypos) { GLfloat x_offset, y_offset, sensitivity, x, y, z; UNUSED(window); xpos = (float)xpos; ypos = (float)ypos; if(first_mouse) { last_x = xpos; last_y = ypos; first_mouse = false; } x_offset = xpos - last_x; y_offset = last_y - ypos; last_x = xpos; last_y = ypos; sensitivity = 0.001f; x_offset *= sensitivity; y_offset *= sensitivity; yaw += x_offset; pitch += y_offset; if(pitch > M_PI/2) { pitch = M_PI/2; } if(pitch < -M_PI/2) { pitch = -M_PI/2; } x = cosf(yaw) * cosf(pitch); y = sinf(pitch); z = sinf(yaw) * cosf(pitch); vec3f_set(camera_front, x, y, z); vec3f_normalize(camera_front, camera_front); }
/** This function should be called whenever a mouse cursor motion * occurs and is automatically called by * mousemove_glfwCursorPosCallback() and mousemove_glutMotionFunc() * * @param x The x coordinate of the mouse cursor. * @param y The y coordinate of the mouse cursor. * @return 1 if the scene needs to be redawn, 0 otherwise. */ int mousemove_movement(int x, int y) { if(cur_button == -1) return 0; /* Distance cursor has moved since last press. */ int dx = x-last_x; int dy = y-last_y; /* Vectors to store our orthonormal basis. */ float f[3], r[3], u[3]; // Calculate a new vector pointing from the camera to the // look at point and normalize it. vec3f_sub_new(f,cam_lookat_down,cam_position_down); vec3f_normalize(f); // Get our up vector vec3f_copy(u,cam_up); // Get a right vector based on the up and lookat vector. vec3f_cross_new(r, f, u); // If right vector was short, then look vector was pointing in // nearly the same direction as the up vector. if(vec3f_normSq(r) < EPSILON) { //printf("mousemove: whoops, pointed camera at up vector."); // move the up vector slightly and try again: u[0] += 0.05; vec3f_cross_new(r,f,u); } vec3f_normalize(r); // recalculate the up vector from the right vector and up vector // to ensure we have a an orthonormal basis. vec3f_cross_new(u, r, f); vec3f_normalize(u); switch(cur_button) { case 0: // left mouse button - translate left/right or up/down /* Translate the camera along the right and up vectors * appropriately depending on the type of mouse movement. */ for(int i=0; i<3; i++) { float offset = r[i]*dx*settings_trans_scale + -u[i]*dy*settings_trans_scale; cam_position[i] = cam_position_down[i] - offset; cam_lookat[i] = cam_lookat_down[i] - offset; } break; case 1: // middle mouse button - translate forward and back mousemove_translate_inout(dy, f); break; case 2: // right mouse button - rotate // If the mouse is moved left/right, rotate the facing // vector around the up vector. mousemove_private_rotate_point(dx*settings_rot_scale, u, f); // If the mouse is moved up/down, rotate the facing vector // around the right vector. mousemove_private_rotate_point(dy*settings_rot_scale, r, f); vec3f_normalize(f); // Add new facing vector to the position that the camera // was at when the button was first pressed to get a new // lookat point. vec3f_add_new(cam_lookat, cam_position_down, f); break; } return 1; }
void camera_init(camera* cam) { vec3f h_target = vec3f_normalize(vec3f_init(cam->target.v[0], 0.0f, cam->target.v[2])); }