From 5d1e18b796d41e035d0d3e828cc6db54ed21a4b6 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 3 May 2008 14:47:12 +0000 Subject: camera handling, Cockpit view --- src/client/camera.cc | 161 ++++++++++++++++++++++++++------------------------- src/client/camera.h | 18 ++---- src/client/input.cc | 124 +++++++++++++++++++++------------------ src/client/view.cc | 7 ++- 4 files changed, 160 insertions(+), 150 deletions(-) (limited to 'src/client') diff --git a/src/client/camera.cc b/src/client/camera.cc index c2bbc83..8144781 100644 --- a/src/client/camera.cc +++ b/src/client/camera.cc @@ -10,6 +10,7 @@ #include "client/client.h" #include "client/camera.h" #include "render/render.h" +#include "sys/sys.h" using math::degrees360f; using math::degrees180f; @@ -27,51 +28,43 @@ math::Vector3f target; math::Vector3f eye; math::Axis axis; -// target yaw, angle in XZ plane, positive is looking left -float yaw_target; -// target pitch, angle in XZ plane, positive is looking left -float pitch_target; -// distance from the camera to the target -float distance; + // current camera mode Mode mode; // private variables -// current yaw, angle in XZ plane, positive is looking left +// current and target yaw angle in XZ plane, positive is looking left float yaw_current; -// current pitch, angle in XY, positive is looking up -float pitch_current; +float yaw_target; +// movement direction in free mode +float target_direction; -// current direction rotation speed -float current_direction_speed; -// current pitcj rotation speed -float current_pitch_speed; +// current and target pitch angle in XY, positive is looking up +float pitch_current; +float pitch_target; +// movement direction in free mode +float target_pitch; -// default pitch in mode::Overview -float pitch_overview; +float distance; // default pitch in mode::Track const float pitch_track = -15.0f; - -// default rotate offset increase/decrease -float rotate_offset_inc; - -// default translate offset increase/decrease -const float translate_offset_inc = 0.1; +const float pitch_overview = -75.0f; void set_mode(Mode newmode); void init() { - rotate_offset_inc = 5.0f; - yaw_current = 0; yaw_target = 0; pitch_current = pitch_track * 2; pitch_target = pitch_track; + target_pitch = 0.0f; + target_direction = 0.0f; + distance = 0.4f; set_mode(Track); @@ -84,12 +77,13 @@ void shutdown() void set_mode(Mode newmode) { - current_direction_speed = 0; - current_pitch_speed = 0; yaw_target = 0; yaw_current = yaw_target; pitch_target = pitch_track; - pitch_current = pitch_target; + pitch_current = pitch_target; + + target_direction = 0.0f; + target_pitch = 0.0f; distance = 0.4f; switch(newmode) { @@ -101,6 +95,12 @@ void set_mode(Mode newmode) { case Free: // switch camera to Free mode mode = Free; + pitch_target = 2.0 * pitch_track; + pitch_current = pitch_target; + break; + + case Cockpit: + mode = Cockpit; break; case Overview: @@ -125,11 +125,21 @@ void next_mode() case Free: // switch camera to Track mode set_mode(Track); + con_print << "camera mode: track" << std::endl; break; + case Track: + // switch camera to Cockpit mode + set_mode(Cockpit); + con_print << "camera mode: cockpit" << std::endl; + break; + + case Cockpit: // switch camera to Free mode set_mode(Free); + con_print << "camera mode: free" << std::endl; break; + default: break; } @@ -149,7 +159,7 @@ void draw(float seconds) target.clear(); axis.clear(); - pitch_current = -75.0f; + pitch_current = pitch_overview; axis.change_pitch(pitch_current); distance = 20.0f; @@ -160,38 +170,46 @@ void draw(float seconds) set_mode(Track); target.assign(core::localcontrol()->location()); - if (core::localcontrol()->model()) - target += core::localcontrol()->model()->maxbbox().z * core::localcontrol()->axis().up(); if (mode == Track) { + if (core::localcontrol()->model()) + target += core::localcontrol()->model()->maxbbox().x * core::localcontrol()->axis().forward(); + // make the camera swing while turning - yaw_target = -25.0f * core::localcontrol()->target_direction; - pitch_target = pitch_track - 25 * core::localcontrol()->target_pitch; - /* - d = -math::dotproduct(axis.forward(), core::localcontrol()->axis().forward()) + 1; - axis.change_direction(360.0f * d * seconds); - - //d = -math::dotproduct(axis.left(), core::localcontrol()->axis().left()) + 1; - // axis.change_pitch(360.0f * d * seconds); - */ - } + target_direction = core::localcontrol()->target_direction; + pitch_target = core::localcontrol()->target_pitch; + + yaw_target = - 45 * target_direction; + pitch_target = pitch_track -45 * target_pitch; + + } else if (mode == Free) { + + yaw_target = yaw_current - 90 * target_direction; + pitch_target = pitch_current - 90 * target_pitch; + } else if (mode == Cockpit) { + if (core::localcontrol()->model()) + target += core::localcontrol()->model()->maxbbox().x * core::localcontrol()->axis().forward(); + } + axis.assign(core::localcontrol()->axis()); - // adjust direction - d = degrees180f(yaw_current - yaw_target); - yaw_current = degrees360f( yaw_current - d * seconds); - axis.change_direction(yaw_current); + if (mode != Cockpit) { + // adjust direction + d = degrees180f(yaw_current - yaw_target); + yaw_current = degrees360f( yaw_current - 2* d * seconds); + axis.change_direction(yaw_current); - // adjust pitch target - d = degrees180f(pitch_current - pitch_target); - pitch_current = degrees360f(pitch_current - d *seconds); - axis.change_pitch(pitch_current); + // adjust pitch target + d = degrees180f(pitch_current - pitch_target); + pitch_current = degrees360f(pitch_current - 2* d *seconds); + axis.change_pitch(pitch_current); - if (core::localcontrol()->model()) - distance = 1+core::localcontrol()->model()->radius(); - else - distance = 1.5f; + if (core::localcontrol()->model()) + distance = 1+core::localcontrol()->model()->radius(); + else + distance = 1.5f; + } } // map world coordinates to opengl coordinates @@ -207,44 +225,29 @@ void draw(float seconds) // match the camera with the current target gl::translate(-1.0f * target); - // apply camera offset - gl::translate((1.0f+distance) * axis.forward()); - - // calculate eye position - eye = target - ((1.0f+distance) * axis.forward()); -} + if (mode != Cockpit) { + // apply camera offset + gl::translate((1.0f+distance) * axis.forward()); -void key_right() -{ - if (mode == Free) { - yaw_target = degrees360f( yaw_target + rotate_offset_inc); - } -} - -void key_left() -{ - if (mode == Free) { - yaw_target = degrees360f( yaw_target - rotate_offset_inc); + // calculate eye position + eye = target - ((1.0f+distance) * axis.forward()); + } else { + eye.assign(target); } } -void key_down() +void set_direction(float direction) { - if (mode == Free) { - pitch_target = pitch_target + rotate_offset_inc; - if (pitch_target > 90.0f) pitch_target = 90.0f; - } + target_direction = direction; + math::clamp(target_direction, -1.0f, 1.0f); } -void key_up() +void set_pitch(float pitch) { - if (mode == Free) { - pitch_target = pitch_target - rotate_offset_inc; - if (pitch_target < -90.0f) pitch_target = -90.0f; - } + target_pitch = pitch; + math::clamp(target_pitch, -1.0f, 1.0f); } - void reset() { set_mode(mode); diff --git a/src/client/camera.h b/src/client/camera.h index 751ff02..649bdc9 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -15,7 +15,7 @@ namespace client { namespace camera { /// enum indicating the camera mode - enum Mode {Free, Track, Overview}; + enum Mode {Free, Track, Cockpit, Overview}; /// initialize the camera void init(); @@ -25,18 +25,12 @@ namespace camera /// draw the OpenGL camera transformation void draw(float elapsed); + + /// set target direction + void set_direction(float direction); - /// rotate the camera left - void key_left(); - - /// rotate the camera right - void key_right(); - - /// rotate the camera up - void key_up(); - - /// rotate the camera down - void key_down(); + /// set target pitch + void set_pitch(float pitch); /// switch to next camera mode void next_mode(); diff --git a/src/client/input.cc b/src/client/input.cc index 533ea64..17ca724 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -25,7 +25,7 @@ namespace input { // local controls -float local_turn = 0.0f; +float local_direction = 0.0f; float local_pitch = 0.0f; float local_thrust = 0.0f; float local_roll = 0.0f; @@ -37,6 +37,9 @@ unsigned int last_control = 0; int mouse_x = 0; int mouse_y = 0; +float mouse_pitch; +float mouse_direction; + // true if the mouse is in the deadzone bool mouse_deadzone = false; @@ -63,12 +66,24 @@ void shutdown() void keyreleased(const SDL_keysym &keysym) { switch (keysym.sym) { + case SDLK_LEFT: + camera::set_direction(0.0f); + break; + case SDLK_RIGHT: + camera::set_direction(0.0f); + break; + case SDLK_UP: + camera::set_pitch(0.0f); + break; + case SDLK_DOWN: + camera::set_pitch(0.0f); + break; case SDLK_SPACE: camera::next_mode(); local_roll = 0; local_pitch = 0; - local_turn = 0; + local_direction = 0; break; case SDLK_KP8: // turn down @@ -80,11 +95,11 @@ void keyreleased(const SDL_keysym &keysym) break; case SDLK_KP4: // turn left - local_turn = 0.0f; + local_direction = 0.0f; break; case SDLK_KP6: // turn right - local_turn = 0.0f; + local_direction = 0.0f; break; case SDLK_KP_DIVIDE: // roll left @@ -107,16 +122,16 @@ void keypressed(const SDL_keysym &keysym) { switch (keysym.sym) { case SDLK_LEFT: - camera::key_left(); + camera::set_direction(1.0f); break; case SDLK_RIGHT: - camera::key_right(); + camera::set_direction(-1.0f); break; case SDLK_UP: - camera::key_up(); + camera::set_pitch(1.0f); break; case SDLK_DOWN: - camera::key_down(); + camera::set_pitch(-1.0f); break; case SDLK_KP_PLUS: @@ -136,15 +151,15 @@ void keypressed(const SDL_keysym &keysym) break; case SDLK_KP4: // left - local_turn = 1.0f; + local_direction = 1.0f; break; case SDLK_KP6: // right - local_turn = -1.0f; + local_direction = -1.0f; break; case SDLK_KP5: // level - local_turn = 0; + local_direction = 0; local_pitch = 0; local_roll = 0; break; @@ -183,7 +198,7 @@ void mousebuttonpressed(const SDL_MouseButtonEvent &button) void frame(float seconds) { if (core::localcontrol() && (last_control != core::localcontrol()->id())) { - local_turn = 0.0f; + local_direction = 0.0f; local_pitch = 0.0f; local_roll = 0.0f; local_thrust = core::localcontrol()->thrust(); @@ -249,59 +264,56 @@ void frame(float seconds) } if (!console::visible() && core::application()->connected() && core::localcontrol()) { - - if (local_thrust > 1.0f) - local_thrust = 1.0f; - else if (local_thrust < -2.0 * thruster_offset) - local_thrust = -2.0 * thruster_offset; - - if (camera::mode == camera::Track) { - if (cl_mousecontrol->value()) { - // mouse control when camera is in tracking mode - int deadzone_size = 24; - mouse_deadzone = true; - - // direction - int l = mouse_x - (video::width >> 1); - if (abs(l) < ( deadzone_size >> 1 )) { - // dead zone - local_turn = 0; - } else { - l = (mouse_x - deadzone_size) - ((video::width - deadzone_size) >> 1); - local_turn = float (-l) / (float) ((video::width - deadzone_size) >> 1); - mouse_deadzone = false; - } - - // pitch - int h = mouse_y - (video::height >> 1); - - if (abs(h) < ( deadzone_size >> 1 )) { - // dead zone - local_pitch = 0; - } else { - h = (mouse_y - deadzone_size) - ((video::height - deadzone_size) >> 1); - local_pitch = float (-h) / (float) ((video::height - deadzone_size) >> 1); - mouse_deadzone = false; - } + if (cl_mousecontrol->value()) { + // the mouse will not react if it is in the deadzone + int deadzone_size = 24; + mouse_deadzone = true; + + // direction + int l = mouse_x - (video::width >> 1); + if (abs(l) < ( deadzone_size >> 1 )) { + // dead zone + mouse_direction = 0; } else { - if (local_turn > 1.0f) - local_turn = 1.0f; - else if (local_turn < -1.0f) - local_turn = -1.0f; + l = (mouse_x - deadzone_size) - ((video::width - deadzone_size) >> 1); + mouse_direction = float (-l) / (float) ((video::width - deadzone_size) >> 1); + mouse_deadzone = false; + } - if (local_pitch > 1.0f) - local_pitch = 1.0f; - else if (local_pitch < -1.0f) - local_pitch = -1.0f; + // pitch + int h = mouse_y - (video::height >> 1); + if (abs(h) < ( deadzone_size >> 1 )) { + // dead zone + mouse_pitch = 0; + } else { + h = (mouse_y - deadzone_size) - ((video::height - deadzone_size) >> 1); + mouse_pitch = float (-h) / (float) ((video::height - deadzone_size) >> 1); mouse_deadzone = false; - } + } + + if ((camera::mode == camera::Track) || (camera::mode == camera::Cockpit)) { + local_direction = mouse_direction; + local_pitch = mouse_pitch; + } else if (camera::mode == camera::Free) { + camera::set_direction(-mouse_direction); + camera::set_pitch(-mouse_pitch); + } + + } else { + + math::clamp(local_direction, -1.0f, 1.0f); + math::clamp(local_pitch, -1.0f, 1.0f); + } + + math::clamp(local_thrust, 0.0f, 1.0f); + math::clamp(local_roll, -1.0f, 1.0f); core::localcontrol()->set_thrust(local_thrust); - core::localcontrol()->set_direction(local_turn); + core::localcontrol()->set_direction(local_direction); core::localcontrol()->set_pitch(local_pitch / video::aspect ); core::localcontrol()->set_roll(local_roll); } diff --git a/src/client/view.cc b/src/client/view.cc index 6f5ceca..aaf8210 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -19,6 +19,7 @@ #include "client/video.h" #include "render/draw.h" #include "render/render.h" +#include "render/textures.h" #include "core/core.h" #include "math/mathlib.h" #include "sys/sys.h" @@ -100,7 +101,7 @@ void draw_loader() { using namespace render; - glBindTexture(GL_TEXTURE_2D, render::textures[0]); // bitmaps/loader.tga + render::Textures::bind("bitmaps/loader"); gl::color(1.0f, 1.0f, 1.0f, 1.0f); gl::begin(gl::Quads); @@ -206,7 +207,7 @@ void draw_cursor() using namespace render; - glBindTexture(GL_TEXTURE_2D, render::textures[2]); // bitmaps/crosshairs.tga + render::Textures::bind("bitmaps/crosshair"); math::Color color; if (draw_crosshaircolor && draw_crosshaircolor->value()) { @@ -288,7 +289,7 @@ void frame(float seconds) } // draw text elements - glBindTexture(GL_TEXTURE_2D, render::textures[1]); // bitmaps/conchars.tga + render::Textures::bind("bitmaps/conchars"); console::draw(); chat::draw(); -- cgit v1.2.3