Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-05-03 14:47:12 +0000
committerStijn Buys <ingar@osirion.org>2008-05-03 14:47:12 +0000
commit5d1e18b796d41e035d0d3e828cc6db54ed21a4b6 (patch)
treed4fc3cd9dd34a5064c6b159309ca9d247081e0d8 /src
parent3dedab035c98772fd9607480bddc8ffd9d7dc799 (diff)
camera handling, Cockpit view
Diffstat (limited to 'src')
-rw-r--r--src/client/camera.cc161
-rw-r--r--src/client/camera.h18
-rw-r--r--src/client/input.cc124
-rw-r--r--src/client/view.cc7
-rw-r--r--src/game/ship.cc36
-rw-r--r--src/render/Makefile.am5
-rw-r--r--src/render/draw.cc3
-rw-r--r--src/render/render.cc33
8 files changed, 198 insertions, 189 deletions
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();
diff --git a/src/game/ship.cc b/src/game/ship.cc
index af98513..9a7c059 100644
--- a/src/game/ship.cc
+++ b/src/game/ship.cc
@@ -52,10 +52,13 @@ void Ship::frame(float seconds)
} else if (current_target_pitch > target_pitch) {
current_target_pitch -= direction_change_speed * seconds;
}
- math::clamp(current_target_pitch, -1.0f, 1.0f);
- float pitch_offset = seconds * current_target_pitch;
- if (pitch_offset)
+ if (fabs(current_target_pitch) < 0.005f) {
+ current_target_pitch = 0.0f;
+ } else {
+ math::clamp(current_target_pitch, -1.0f, 1.0f);
+ float pitch_offset = seconds * current_target_pitch;
entity_axis.change_pitch(360.0f * ship_shipmodel->turnspeed() * pitch_offset);
+ }
// update direction
math::clamp(target_direction, -1.0f, 1.0f);
@@ -64,10 +67,13 @@ void Ship::frame(float seconds)
} else if (current_target_direction > target_direction) {
current_target_direction -= direction_change_speed * seconds;
}
- math::clamp(current_target_direction, -1.0f, 1.0f);
- float direction_offset = seconds * current_target_direction;
- if (direction_offset)
+ if (fabs(current_target_direction) < 0.005f) {
+ current_target_direction = 0.0f;
+ } else {
+ math::clamp(current_target_direction, -1.0f, 1.0f);
+ float direction_offset = seconds * current_target_direction;
entity_axis.change_direction(360.0f * ship_shipmodel->turnspeed() * direction_offset);
+ }
// update roll
math::clamp(target_roll, -1.0f, 1.0f);
@@ -76,10 +82,13 @@ void Ship::frame(float seconds)
} else if (current_target_roll > target_roll) {
current_target_roll -= direction_change_speed * seconds;
}
- math::clamp(current_target_roll, -1.0f, 1.0f);
- float roll_offset = seconds * current_target_roll;
- if (roll_offset)
+ if (fabs(current_target_roll) < 0.005f) {
+ current_target_roll = 0.0f;
+ } else {
+ math::clamp(current_target_roll, -1.0f, 1.0f);
+ float roll_offset = seconds * current_target_roll;
entity_axis.change_roll(360.0f * ship_shipmodel->turnspeed() * roll_offset);
+ }
// update speed
if (entity_speed < entity_thrust * ship_shipmodel->maxspeed()) {
@@ -92,9 +101,12 @@ void Ship::frame(float seconds)
if (entity_speed < 0.0f) entity_speed = 0.0f;
}
- entity_location += entity_axis.forward() * entity_speed * seconds;
-
- entity_dirty = true;
+ if (entity_speed != 0.0f) {
+ entity_location += entity_axis.forward() * entity_speed * seconds;
+ entity_dirty = true;
+ } else if ((current_target_pitch != 0.0f) || (current_target_direction != 0.0f) || (current_target_roll != 0.0f)) {
+ entity_dirty = true;
+ }
}
} // namespace game
diff --git a/src/render/Makefile.am b/src/render/Makefile.am
index 75da9c7..5747681 100644
--- a/src/render/Makefile.am
+++ b/src/render/Makefile.am
@@ -3,5 +3,6 @@ METASOURCES = AUTO
noinst_LTLIBRARIES = librender.la
librender_la_LDFLAGS = -avoid-version -no-undefined @GL_LIBS@
librender_la_LIBADD = $(top_builddir)/src/math/libmath.la
-librender_la_SOURCES = draw.cc gl.cc image.cc render.cc text.cc tga.cc
-noinst_HEADERS = draw.h gl.h image.h render.h text.h tga.h
+librender_la_SOURCES = draw.cc gl.cc image.cc render.cc text.cc textures.cc \
+ tga.cc
+noinst_HEADERS = draw.h gl.h image.h render.h text.h textures.h tga.h
diff --git a/src/render/draw.cc b/src/render/draw.cc
index 3fe64a5..18e587a 100644
--- a/src/render/draw.cc
+++ b/src/render/draw.cc
@@ -7,6 +7,7 @@
#include "core/core.h"
#include "core/model.h"
#include "render/render.h"
+#include "render/textures.h"
#include "render/draw.h"
namespace render
@@ -384,7 +385,7 @@ void draw_pass_model_fx() {
void draw_pass_model_lights()
{
//glPointSize(10);
- glBindTexture(GL_TEXTURE_2D, render::textures[3]); // bitmaps/fx/flare01.tga
+ Textures::bind("bitmaps/fx/flare00");
gl::enable(GL_TEXTURE_2D);
gl::begin(gl::Quads);
diff --git a/src/render/render.cc b/src/render/render.cc
index 6755e08..589dce3 100644
--- a/src/render/render.cc
+++ b/src/render/render.cc
@@ -9,9 +9,11 @@
#include <sstream>
#include <iomanip>
-#include "render/render.h"
-#include "render/tga.h"
#include "render/gl.h"
+#include "render/textures.h"
+#include "render/tga.h"
+#include "render/render.h"
+
#include "core/core.h"
#include "filesystem/filesystem.h"
#include "sys/sys.h"
@@ -58,27 +60,7 @@ void init()
con_print << " vendor " << gl::vendor() << std::endl;
con_print << " version " << gl::version() << std::endl;
- con_print << "Loading textures..." << std::endl;
-
- if (!texture( "bitmaps/loader.tga", 0)) {
- con_error << "Essential file bitmaps/loader.tga missing" << std::endl;
- core::application()->shutdown();
- }
-
- if (!texture("bitmaps/conchars.tga", 1)) {
- con_error << "Essential file bitmaps/conchars.tga missing" << std::endl;
- core::application()->shutdown();
- }
-
- if (!texture("bitmaps/crosshair.tga", 2)) {
- con_error << "Essential file bitmaps/crosshair.tga missing" << std::endl;
- core::application()->shutdown();
- }
-
- if (!texture("bitmaps/fx/flare01.tga", 3)) {
- con_error << "Essential file bitmaps/fx/flare01.tga missing" << std::endl;
- core::application()->shutdown();
- }
+ Textures::init();
// size of the vertex array in megabytes
r_arraysize = core::Cvar::get("r_arraysize", 0.0f , core::Cvar::Archive);
@@ -103,7 +85,7 @@ void shutdown()
{
con_print << "Shutting down renderer..." << std::endl;
- glDeleteTextures(2, textures);
+ Textures::shutdown();
// clear entity models, this will force a reload
for (std::map<unsigned int, core::Entity *>::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) {
@@ -112,7 +94,10 @@ void shutdown()
entity->entity_model = 0;
}
+ // clear models
core::Model::clear();
+
+ // clear vertex array
delete vertexarray;
vertexarray = 0;
}