/** * Called every time the cursor moves. It is used to calculate the Camera's direction * in window - the window holding the cursor * in xpos - the xposition of the cursor on the screen * in ypos - the yposition of the curose on the screen */ static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) { GLfloat quat[] = {0.0f,0.0f,0.0f,0.0f}; //calculate pitch static double previous_ypos = ypos; double position_y_difference = ypos - previous_ypos; previous_ypos = ypos; //calculate yaw static double previous_xpos = xpos; double position_x_difference = xpos - previous_xpos; previous_xpos = xpos; //reduce signal camera.yaw += position_x_difference *camera.signal_amplifier; camera.pitch += position_y_difference *camera.signal_amplifier; //calculate rotation sequence create_versor(quat, camera.pitch, 1.0f, 0.0f, 0.0f); quat_to_mat4(camera.Rpitch.m, quat); create_versor(quat, camera.yaw, 0.0f, 1.0f, 0.0f); quat_to_mat4(camera.Ryaw.m,quat); // mult_quat_quat(camera.resultQuat, camera.quatYaw, camera.quatPitch); // mult_quat_quat(camera.resultQuat, camera.quatYaw, camera.quatPitch); // quat_to_mat4(camera.R.m, camera.resultQuat); }
static void calculateCursorRotations(Cursor *cursor){ GLfloat quat[] = {0.0f,0.0f,0.0f,0.0f}; create_versor(quat, cursor->pitch, 1.0f, 0.0f, 0.0f); quat_to_mat4(cursor->Rpitch.m, quat); create_versor(quat, cursor->yaw, 0.0f, 1.0f, 0.0f); quat_to_mat4(cursor->Ryaw.m, quat); create_versor(quat, cursor->roll, 0.0f, 0.0f, 1.0f); quat_to_mat4(cursor->Rroll.m, quat); }
GLCamera::GLCamera(float aspect) { near = 0.1f; far = 100.0f; fovy = 67.0f; //aspect = (float)g_gl_width / (float)g_gl_height; this->aspect = aspect; proj_mat = perspective(fovy, this->aspect, near, far); cam_speed = 5.0f; cam_heading_speed = 100.0f; cam_heading = 0.0f; cam_pos = vector3(0.0f, 0.0f, 5.0f); T = identity_mat4().translate( vector3(-cam_pos.v[0], -cam_pos.v[1], -cam_pos.v[2]) ); create_versor(quaternion, -cam_heading, 0.0f, 1.0f, 0.0f); quat_to_mat4(R.m, quaternion); // combine the inverse rotation and transformation to make a view matrix view_mat = R * T; // keep track of some useful vectors that can be used for keyboard movement fwd = vector4(0.0f, 0.0f, -1.0f, 0.0f); rgt = vector4(1.0f, 0.0f, 0.0f, 0.0f); up = vector4(0.0f, 1.0f, 0.0f, 0.0f); cam_yaw = 0.0f; // y-rotation in degrees cam_pitch = 0.0f; cam_roll = 0.0; }
Camera::Camera(int view_mat_location, int proj_mat_location) { this->view_mat_location = view_mat_location; this->proj_mat_location = proj_mat_location; cam_pos = vec3 (0.0f, 0.0f, 5.0f); near = 0.1f; // clipping plane far = 100.0f; // clipping plane fovy = 67.0f; // 67 degrees // aspect ratio aspect = (float)g_gl_width / (float)g_gl_height; proj_mat = perspective (fovy, aspect, near, far); cam_speed = 5.0f; // 1 unit per second cam_heading_speed = 100.0f; // 30 degrees per second cam_heading = 0.0f; // y-rotation in degrees T = translate ( identity_mat4 (), vec3 (-cam_pos.v[0], -cam_pos.v[1], -cam_pos.v[2]) ); create_versor (quaternion, -cam_heading, 0.0f, 1.0f, 0.0f); // convert the quaternion to a rotation matrix (just an array of 16 floats) quat_to_mat4 (R.m, quaternion); // combine the inverse rotation and transformation to make a view matrix view_mat = R * T; fwd = FORWARD; rgt = RIGHT; up = UP; set_view(); set_proj(); reset_control(); }
void skyUpdate(Skybox *sky){ //calculate new sky ngle GLfloat quat[] = {0.0f,0.0f,0.0f,0.0f}; sky->angle += 0.002f; if (sky->angle> 359) sky->angle= 0; create_versor(quat, sky->angle, 0.0f, 1.0f, 0.0f); quat_to_mat4(sky->modelMatrix.m, quat); }
void Camera::roll_right(double elapsed_seconds) { cam_roll += cam_heading_speed * elapsed_seconds; cam_moved = true; float q_roll[16]; create_versor (q_roll, cam_roll, fwd.v[0], fwd.v[1], fwd.v[2]); mult_quat_quat (quaternion, q_roll, quaternion); // recalc axes to suit new orientation recalc_axes(); }
void Camera::yaw_right(double elapsed_seconds) { cam_yaw -= cam_heading_speed * elapsed_seconds; cam_moved = true; float q_yaw[16]; create_versor (q_yaw, cam_yaw, up.v[0], up.v[1], up.v[2]); mult_quat_quat (quaternion, q_yaw, quaternion); // recalc axes to suit new orientation recalc_axes(); }
void Camera::pitch_up(double elapsed_seconds) { cam_pitch += cam_heading_speed * elapsed_seconds; cam_moved = true; float q_pitch[16]; create_versor ( q_pitch, cam_pitch, rgt.v[0], rgt.v[1], rgt.v[2] ); mult_quat_quat (quaternion, q_pitch, quaternion); // recalc axes to suit new orientation recalc_axes(); }
void GLCamera::movePitchDown(double elapsed_seconds) { cam_pitch -= cam_heading_speed * elapsed_seconds; float q_pitch[16]; create_versor(q_pitch, cam_pitch, rgt.v[0], rgt.v[1], rgt.v[2]); mult_quat_quat(quaternion, q_pitch, quaternion); // recalc axes to suit new orientation quat_to_mat4(R.m, quaternion); fwd = R * vector4(0.0, 0.0, -1.0, 0.0); rgt = R * vector4(1.0, 0.0, 0.0, 0.0); up = R * vector4(0.0, 1.0, 0.0, 0.0); }
void Camera::yaw_left(double elapsed_seconds) { cam_yaw += cam_heading_speed * elapsed_seconds; cam_moved = true; // create a quaternion representing change in heading (the yaw) float q_yaw[16]; create_versor (q_yaw, cam_yaw, up.v[0], up.v[1], up.v[2]); // add yaw rotation to the camera's current orientation mult_quat_quat (quaternion, q_yaw, quaternion); // recalc axes to suit new orientation recalc_axes(); }
void GLCamera::rollRight(double elapsed_seconds) { cam_roll += cam_heading_speed * elapsed_seconds; float q_roll[16]; create_versor(q_roll, cam_roll, fwd.v[0], fwd.v[1], fwd.v[2]); mult_quat_quat(quaternion, q_roll, quaternion); // recalc axes to suit new orientation quat_to_mat4(R.m, quaternion); fwd = R * vector4(0.0, 0.0, -1.0, 0.0); rgt = R * vector4(1.0, 0.0, 0.0, 0.0); up = R * vector4(0.0, 1.0, 0.0, 0.0); }
void GLCamera::moveYawLeft(double elapsed_seconds) { cam_yaw += getHeadingSpeed() * elapsed_seconds; // create a quaternion representing change in heading (the yaw) float q_yaw[16]; create_versor(q_yaw, cam_yaw, up.v[0], up.v[1], up.v[2]); // add yaw rotation to the camera's current orientation mult_quat_quat(quaternion, q_yaw, quaternion); // recalc axes to suit new orientation quat_to_mat4(R.m, quaternion); fwd = R * vector4(0.0, 0.0, -1.0, 0.0); rgt = R * vector4(1.0, 0.0, 0.0, 0.0); up = R * vector4(0.0, 1.0, 0.0, 0.0); }
void GLCamera::setPosition(vector3 pos) { this->cam_pos = pos; T = identity_mat4().translate(vector3(-cam_pos.v[0], -cam_pos.v[1], -cam_pos.v[2])); T = identity_mat4().translate( vector3(-cam_pos.v[0], -cam_pos.v[1], -cam_pos.v[2]) ); create_versor(quaternion, -cam_heading, 0.0f, 1.0f, 0.0f); quat_to_mat4(R.m, quaternion); // combine the inverse rotation and transformation to make a view matrix view_mat = R * T; // keep track of some useful vectors that can be used for keyboard movement fwd = vector4(0.0f, 0.0f, -1.0f, 0.0f); rgt = vector4(1.0f, 0.0f, 0.0f, 0.0f); up = vector4(0.0f, 1.0f, 0.0f, 0.0f); cam_yaw = 0.0f; // y-rotation in degrees cam_pitch = 0.0f; cam_roll = 0.0; }