From 3866f2b33313d891347f454537843befd295b78f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 20 Oct 2007 10:02:51 +0000 Subject: Initial import. --- src/Makefile.am | 6 ++ src/client/Makefile.am | 12 +++ src/client/camera.cc | 107 ++++++++++++++++++++++++ src/client/camera.h | 44 ++++++++++ src/client/hud.cc | 0 src/client/hud.h | 0 src/client/input.cc | 86 +++++++++++++++++++ src/client/input.h | 24 ++++++ src/client/main.cc | 65 ++++++++++++++ src/client/shipdrawer.cc | 108 ++++++++++++++++++++++++ src/client/shipdrawer.h | 26 ++++++ src/client/stardrawer | 0 src/client/stardrawer.cc | 22 +++++ src/client/stardrawer.h | 26 ++++++ src/client/video.cc | 109 ++++++++++++++++++++++++ src/client/video.h | 30 +++++++ src/client/view.cc | 153 +++++++++++++++++++++++++++++++++ src/client/view.h | 24 ++++++ src/common/Makefile.am | 8 ++ src/common/color.cc | 84 +++++++++++++++++++ src/common/color.h | 50 +++++++++++ src/common/functions.cc | 48 +++++++++++ src/common/functions.h | 42 ++++++++++ src/common/osirion.h | 11 +++ src/common/vector3f.cc | 151 +++++++++++++++++++++++++++++++++ src/common/vector3f.h | 138 ++++++++++++++++++++++++++++++ src/game/Makefile.am | 9 ++ src/game/game.cc | 31 +++++++ src/game/game.h | 33 ++++++++ src/game/player.h | 20 +++++ src/game/sector.h | 26 ++++++ src/game/ship.cc | 85 +++++++++++++++++++ src/game/ship.h | 54 ++++++++++++ src/game/star.cc | 17 ++++ src/game/star.h | 29 +++++++ src/game/world.h | 24 ++++++ src/gl/Makefile.am | 10 +++ src/gl/box.cc | 96 +++++++++++++++++++++ src/gl/box.h | 41 +++++++++ src/gl/osiriongl.cc | 214 +++++++++++++++++++++++++++++++++++++++++++++++ src/gl/osiriongl.h | 158 ++++++++++++++++++++++++++++++++++ src/gl/sphere.cc | 89 ++++++++++++++++++++ src/gl/sphere.h | 50 +++++++++++ 43 files changed, 2360 insertions(+) create mode 100644 src/Makefile.am create mode 100644 src/client/Makefile.am create mode 100644 src/client/camera.cc create mode 100644 src/client/camera.h create mode 100644 src/client/hud.cc create mode 100644 src/client/hud.h create mode 100644 src/client/input.cc create mode 100644 src/client/input.h create mode 100644 src/client/main.cc create mode 100644 src/client/shipdrawer.cc create mode 100644 src/client/shipdrawer.h create mode 100644 src/client/stardrawer create mode 100644 src/client/stardrawer.cc create mode 100644 src/client/stardrawer.h create mode 100644 src/client/video.cc create mode 100644 src/client/video.h create mode 100644 src/client/view.cc create mode 100644 src/client/view.h create mode 100644 src/common/Makefile.am create mode 100644 src/common/color.cc create mode 100644 src/common/color.h create mode 100644 src/common/functions.cc create mode 100644 src/common/functions.h create mode 100644 src/common/osirion.h create mode 100644 src/common/vector3f.cc create mode 100644 src/common/vector3f.h create mode 100644 src/game/Makefile.am create mode 100644 src/game/game.cc create mode 100644 src/game/game.h create mode 100644 src/game/player.h create mode 100644 src/game/sector.h create mode 100644 src/game/ship.cc create mode 100644 src/game/ship.h create mode 100644 src/game/star.cc create mode 100644 src/game/star.h create mode 100644 src/game/world.h create mode 100644 src/gl/Makefile.am create mode 100644 src/gl/box.cc create mode 100644 src/gl/box.h create mode 100644 src/gl/osiriongl.cc create mode 100644 src/gl/osiriongl.h create mode 100644 src/gl/sphere.cc create mode 100644 src/gl/sphere.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..b30324a --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,6 @@ + +# set the include path found by configure +AM_CPPFLAGS = $(LIBSDL_CFLAGS) $(all_includes) + +# the library search path. +SUBDIRS = common game gl client diff --git a/src/client/Makefile.am b/src/client/Makefile.am new file mode 100644 index 0000000..f234f49 --- /dev/null +++ b/src/client/Makefile.am @@ -0,0 +1,12 @@ + +METASOURCES = AUTO +bin_PROGRAMS = osirion +osirion_LDADD = $(top_builddir)/src/common/libcommon.la \ + $(top_builddir)/src/game/libgame.la $(top_builddir)/src/gl/libosiriongl.la -lSDL +osirion_SOURCES = camera.cc camera.h input.cc input.h main.cc video.cc video.h \ + view.cc view.h shipdrawer.cc shipdrawer.h stardrawer.cc stardrawer.h + + + +INCLUDES = -I$(top_srcdir)/src +osirion_LDFLAGS = -L/usr/X11/lib64 diff --git a/src/client/camera.cc b/src/client/camera.cc new file mode 100644 index 0000000..ca60d18 --- /dev/null +++ b/src/client/camera.cc @@ -0,0 +1,107 @@ +/* camera.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "common/functions.h" +#include "common/osirion.h" + +#include "game/game.h" +#include "gl/osiriongl.h" + +namespace camera +{ +enum Mode {Free, Track}; +Mode mode = Track; + +const float track_pitch = 15.0f; // default tracking pitch +const float offset_inc = 5.0f; // default offset increment + +float yaw = 90; // current yaw, angle in XZ plane +float yaw_offset = 0; // target offset, relative to target yaw + +float pitch = -90; // current pitch, angle in XY +float pitch_offset = track_pitch; // target offset, relative to target pitch + +float distance = 2.0f; // distance from the eye to the target + + +void draw(float elapsed) +{ + // adjust yaw target + float yaw_target = game::ship.yaw - yaw_offset; + float d = degreesf(yaw - yaw_target); + yaw = degreesf( yaw - d * elapsed); + + // adjust pitch target + float pitch_target = 0.0f - pitch_offset; + d= degreesf(pitch - pitch_target); + pitch = degreesf( pitch - d *elapsed); + + /* + gl::translate(distance, 0.0, 0.0); + gl::rotate(-pitch,0, 0, 1.0 ); + gl::rotate(-yaw, 0, 1.0f, 0); + */ + switch (mode) { + case Free: + gl::translate(1+distance * GAMESCALE, 0.0f, 0.0f); + gl::rotate(-pitch, 0, 0, 1.0f); + gl::rotate(-yaw, 0, 1.0f, 0); + break; + case Track: + gl::translate(1+distance* GAMESCALE, -0.5f* GAMESCALE, 0.0f); + gl::rotate(-pitch, 0, 0, 1.0f); + gl::rotate(-yaw, 0, 1.0f, 0); + break; + default: + break; + } +} + +void rotate_right() +{ + if (mode == Free ) { + yaw_offset = degreesf( yaw_offset - offset_inc); + } +} + +void rotate_left() +{ + if (mode == Free ) { + yaw_offset = degreesf( yaw_offset + offset_inc); + } +} + +void rotate_up() +{ + if (mode == Free ) { + pitch_offset = degreesf( pitch_offset + offset_inc); + } +} + +void rotate_down() +{ + if (mode == Free ) { + pitch_offset = degreesf( pitch_offset - offset_inc); + } +} + +void nextmode() { + switch(mode) { + case Free: + mode = Track; + yaw_offset = 0; + yaw = game::ship.yaw; + pitch_offset = track_pitch; + break; + case Track: + mode = Free; + yaw_offset = 0; + break; + default: + break; + } +} + +} //namespace camera diff --git a/src/client/camera.h b/src/client/camera.h new file mode 100644 index 0000000..63c7aa5 --- /dev/null +++ b/src/client/camera.h @@ -0,0 +1,44 @@ +/* camera.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_CAMERA_H__ +#define __INCLUDED_CAMERA_H__ + +#include "common/vector3f.h" + +/// camera functions +/** The functions in this namespace performs the transformations +for the camera eye location. The camera always looks at (0,0,0) +*/ +namespace camera +{ + /// draw the OpenGL camera transformation + void draw(float elapsed); + + /// rotate the camera left + void rotate_left(); + /// rotate the camera right + void rotate_right(); + /// rotate the camera up + void rotate_up(); + /// rotate the camera down + void rotate_down(); + + /// switch to next camera mode + void nextmode(); + + /// camera target + /** The location the camera is looking at */ + extern Vector3f target; + + /// horizontal viewing angle x/z plane + extern float horiz_angle; + /// vertical viewing angle z/y plane + extern float vert_angle; + /// distance from the camera to the target + /** The distance in game units from the eye of the camera to the target */ + extern float distance; +}; + +#endif // __INCLUDED_CAMERA_H__ diff --git a/src/client/hud.cc b/src/client/hud.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/client/hud.h b/src/client/hud.h new file mode 100644 index 0000000..e69de29 diff --git a/src/client/input.cc b/src/client/input.cc new file mode 100644 index 0000000..8af7414 --- /dev/null +++ b/src/client/input.cc @@ -0,0 +1,86 @@ +/* input.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +//project headers +#include "common/functions.h" +#include "game/game.h" + +#include "view.h" +#include "camera.h" + +namespace input +{ + +void init() +{ + SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); +} + +void shutdown() +{ +} + +void handle_keydown(SDL_keysym* keysym) +{ + switch( keysym->sym ) { + case SDLK_ESCAPE: + game::shutdown(); + break; + case SDLK_SPACE: + camera::nextmode(); + break; + case SDLK_LEFT: + camera::rotate_left(); + break; + case SDLK_RIGHT: + camera::rotate_right(); + break; + case SDLK_UP: + camera::rotate_up(); + break; + case SDLK_DOWN: + camera::rotate_down(); + break; + case SDLK_KP_PLUS: + game::ship.thrust_increase(); + break; + case SDLK_KP_MINUS: + game::ship.thrust_decrease(); + break; + case SDLK_KP4: + game::ship.turn_left(); + break; + case SDLK_KP6: + game::ship.turn_right(); + break; + default: + break; + } + +} + +void process() +{ + SDL_Event event; + + while( SDL_PollEvent( &event ) ) { + switch( event.type ) { +// case SDL_MOUSEBUTTONUP: + case SDL_KEYDOWN: + handle_keydown( &event.key.keysym ); + break; + case SDL_QUIT: + game::shutdown(); + break; + } + + } + +} + +} // namespace input diff --git a/src/client/input.h b/src/client/input.h new file mode 100644 index 0000000..f1e94d9 --- /dev/null +++ b/src/client/input.h @@ -0,0 +1,24 @@ +/* input.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_INPUT_H__ +#define __INCLUDED_INPUT_H__ + + +namespace input +{ + /// initialize the input subsystem + void init(); + /// shutdown the input subsystem + void shutdown(); + + /// exit the application + void quit(int exit_code); + + /// process input events + void process(); +}; + +#endif // __INCLUDED_INPUT_H__ diff --git a/src/client/main.cc b/src/client/main.cc new file mode 100644 index 0000000..66e0fa3 --- /dev/null +++ b/src/client/main.cc @@ -0,0 +1,65 @@ +/* main.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +// C++ headers +#include + +// project headers +#include "common/osirion.h" +#include "game/game.h" + +#include "input.h" +#include "video.h" + +void quit(int exit_code) +{ + SDL_Quit(); + exit(exit_code); +} + +int main( int argc, char *argv[] ) +{ + std::cout << "The Osirion project " << OSIRION_VERSION << std::endl; + + // Initialize the video subsystem + video::init(); + if (!video::initialized) { + quit(1); + } + + // initialize input + input::init(); + + // initialize game + game::init(); + + Uint32 startup = SDL_GetTicks(); + while(game::initialized) { + Uint32 chrono = SDL_GetTicks(); + + // overflow protection ~49 days + if (chrono < startup) { + startup = chrono; + } + + // update the game chronometers + float elapsed = (float) ( chrono - startup) / 1000.0f; + game::update(elapsed); + + // update the video chronometers and draw + video::draw(elapsed); + startup = chrono; + + // process input + input::process(); + } + + video::shutdown(); + + quit(0); +} diff --git a/src/client/shipdrawer.cc b/src/client/shipdrawer.cc new file mode 100644 index 0000000..d889178 --- /dev/null +++ b/src/client/shipdrawer.cc @@ -0,0 +1,108 @@ +/* shipdrawer.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include + +// project headers +#include "gl/osiriongl.h" +#include "gl/box.h" + +#include "shipdrawer.h" + +Vector3f v0(1.0f, -1.0f, -1.0f); +Vector3f v1(1.0f, 1.0f, -1.0f); +Vector3f v2(1.0f, 1.0f, 1.0f); +Vector3f v3(1.0f, -1.0f, 1.0f); + +Vector3f v4(-1.0f, -1.0f, -1.0f); +Vector3f v5(-1.0f, 1.0f, -1.0f); +Vector3f v6(-1.0f, 1.0f, 1.0f); +Vector3f v7(-1.0f, -1.0f, 1.0f); + +ShipDrawer::ShipDrawer(Ship *s) +{ + angle = 0; + ship = s; +} + +ShipDrawer::~ShipDrawer() +{ +} + +void ShipDrawer::draw(float elapsed) +{ + using namespace gl; + + gl::push(); + + rotate(ship->yaw, 0.0f, 1.0f, 0.0f ); + + + Vector3f tl(0.25, 0.125, 0.125); + Vector3f br(-0.25, -0.125, -0.125); + + Box box(tl, br); + box.draw(); + + tl = Vector3f(0, 0.07, 0.25); + br = Vector3f(-0.5, -0.07, 0.125); + Box engine1(tl, br); + engine1.topcolor = Color(0.7, 0.7, 0.7); + engine1.bottomcolor = engine1.topcolor * 0.5; + engine1.draw(); + + tl = Vector3f(0, 0.07, -0.125); + br = Vector3f(-0.5, -0.07, -0.25); + Box engine2(tl, br); + engine2.topcolor = engine1.topcolor; + engine2.bottomcolor = engine1.bottomcolor; + engine2.draw(); + + tl = Vector3f(0.4, 0.07, 0.07); + br = Vector3f(0.25, -0.07, -0.07); + Box cockpit(tl, br); + cockpit.topcolor = engine1.topcolor; + cockpit.bottomcolor = engine1.bottomcolor; + cockpit.draw(); + + if(ship->thrust > 0 ) { + color(1.0f,0 ,0 ); + begin(Lines); + vertex(-0.5f, 0, 0.185); + vertex(-0.5f-0.25f*ship->thrust, 0, 0.185); + + vertex(-0.5f, 0, -0.185f); + vertex(-0.5f-0.25f*ship->thrust, 0, -0.185f); + end(); + } + + // shield rotation + rotate(angle, 0.0f, 1.0f, 0.0f ); + angle += 180.0f * elapsed; + if( angle > 360.0f ) { + angle -= 360.0f; + } + + // draw the shield + color(Color(0.0f, 1.0f ,0.0f , 0.5f)); + + begin(LineStrip); + vertex(v0); + vertex(v1); + vertex(v2); + vertex(v3); + vertex(v0); + end(); + + begin(LineStrip); + vertex(v4); + vertex(v5); + vertex(v6); + vertex(v7); + vertex(v4); + end(); + + gl::pop(); +} diff --git a/src/client/shipdrawer.h b/src/client/shipdrawer.h new file mode 100644 index 0000000..8860585 --- /dev/null +++ b/src/client/shipdrawer.h @@ -0,0 +1,26 @@ +/* shipdrawer.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_SHIPDRAWER_H__ +#define __INCLUDED_SHIPDRAWER_H__ + +// project headers +#include "game/ship.h" + +class ShipDrawer +{ +public: + ShipDrawer(Ship *s); + ~ShipDrawer(); + + /// update the model state + void draw(float elapsed); + +private: + Ship *ship; + float angle; +}; + +#endif // __INCLUDED_SHIPDRAWER_H__ diff --git a/src/client/stardrawer b/src/client/stardrawer new file mode 100644 index 0000000..e69de29 diff --git a/src/client/stardrawer.cc b/src/client/stardrawer.cc new file mode 100644 index 0000000..6bdf34c --- /dev/null +++ b/src/client/stardrawer.cc @@ -0,0 +1,22 @@ +/* stardrawer.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "gl/osiriongl.h" +#include "stardrawer.h" + +StarDrawer::StarDrawer(Star *s) { + star = s; + sphere.radius = s->radius; + +} +StarDrawer::~StarDrawer() { + star = 0; +} + +void StarDrawer::draw(float elapsed) +{ + gl::color(star->color); + sphere.draw(); +} diff --git a/src/client/stardrawer.h b/src/client/stardrawer.h new file mode 100644 index 0000000..7656414 --- /dev/null +++ b/src/client/stardrawer.h @@ -0,0 +1,26 @@ +/* stardrawer.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_STARDRAWER_H__ +#define __INCLUDED_STARDRAWER_H__ + +#include "gl/sphere.h" +#include "game/star.h" + +/// Class to draw stars +class StarDrawer +{ +public: + StarDrawer(Star *s); + ~StarDrawer(); + + void draw(float elapsed); + +private: + Star *star; + gl::Sphere sphere; +}; + +#endif // __INCLUDED_STARDRAWER_H__ diff --git a/src/client/video.cc b/src/client/video.cc new file mode 100644 index 0000000..eb4297e --- /dev/null +++ b/src/client/video.cc @@ -0,0 +1,109 @@ +/* video.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// SDL headers +#include + +// C++ headers +#include + +// project headers +#include "gl/osiriongl.h" +#include "view.h" + + +namespace video +{ +int width = 0; +int height = 0; +bool initialized = false; +float ratio = 1; + +void reset() +{ + ratio = (float) width / (float) height; + + // Our shading model--Gouraud (smooth). + gl::shademodel(GL_SMOOTH); + + // Culling + gl::cullface( GL_BACK ); + gl::frontface(GL_CCW ); + gl::enable( GL_CULL_FACE ); + + gl::depthmask(GL_TRUE); // Depth buffer writing + gl::enable(GL_DEPTH_TEST); + + // Set the clear color + gl::clearcolor( 0, 0, 0, 0 ); + + // Setup our viewport. + gl::viewport(0, 0, width, height ); + + view::reset(); +} + +void init() +{ + if (initialized) { + return; + } + + int bpp = 0; + int flags = 0; + + if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { + std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl; + return; + } + + const SDL_VideoInfo* sdl_videoinfo = SDL_GetVideoInfo(); + if( !sdl_videoinfo) { + std::cerr << "SDL_GetVideoInfo() failed: " << SDL_GetError() << std::endl; + return; + } + + width = 800; + height = 600; + bpp = sdl_videoinfo->vfmt->BitsPerPixel; + + SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); + SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + + flags = SDL_OPENGL | SDL_FULLSCREEN; + + if(!SDL_SetVideoMode(width, height, bpp, flags )) { + std::cerr << "SDL_SetVideoMode() failed: " << SDL_GetError() << std::endl; + return; + } + + gl::init(); + + initialized = true; + view::init(); + + reset(); + return; +} + +void draw(float elapsed) +{ + view::draw(elapsed); +} + +void shutdown() +{ + view::shutdown(); + gl::shutdown(); + + initialized = false; + width = 0; + height = 0; +} + +} // namespace video diff --git a/src/client/video.h b/src/client/video.h new file mode 100644 index 0000000..3b126f1 --- /dev/null +++ b/src/client/video.h @@ -0,0 +1,30 @@ +/* video.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_VIDEO_H__ +#define __INCLUDED_VIDEO_H__ + + +namespace video +{ + /// initialize the video subsystem + void init(); + /// shutdown the video subsystem + void shutdown(); + /// Update the screen state and redraw + void draw(float elapsed); + /// reset and clear the viewport + void reset(); + + /// Width of the SDL window in pixels + extern int width; + /// Height of the SDL window in pixels + extern int height; + /// True if the video subsystem is initialized + extern bool initialized; + /// width/height ratio + extern float ratio; +}; + +#endif // __INCLUDED_VIDEO_H__ diff --git a/src/client/view.cc b/src/client/view.cc new file mode 100644 index 0000000..3641261 --- /dev/null +++ b/src/client/view.cc @@ -0,0 +1,153 @@ +/* view.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +// C++ headers +#include + +// SDL headers +#include + +// project headers +#include "common/functions.h" +#include "common/osirion.h" + +#include "game/game.h" +#include "gl/osiriongl.h" + +#include "video.h" +#include "camera.h" + +#include "shipdrawer.h" +#include "stardrawer.h" + +namespace view +{ + +ShipDrawer *shipdrawer = 0; +StarDrawer *stardrawer = 0; + +Ship *target =0; // the view's target + +void init() { + // draw scene + if (!shipdrawer) { + stardrawer = new StarDrawer(&game::star); + shipdrawer = new ShipDrawer(&game::ship); + target = &game::ship; + } + +} + +void shutdown() +{ + delete stardrawer; + stardrawer = 0; + delete shipdrawer; + shipdrawer = 0; +} + +void reset() { + // Change to the projection matrix and set our viewing volume. + gl::matrixmode( GL_PROJECTION ); + gl::loadidentity(); + + //glu::perspective( 64.0, video::ratio, 1.0, 1024.0 ); + const float frustumsize=0.5f; + gl::frustum( -frustumsize * video::ratio, frustumsize * video::ratio, -frustumsize, frustumsize, 1.0, 1024.0); + /* + map world coordinates to GL coordinates + + The world coordinates are identical to GL coordinates, + but the default viewing pitch (0 degrees) + is the positive X-axis + */ + gl::rotate(90.0f, 0, 1.0, 0); +} + +void draw_background() +{ + using namespace gl; + + begin(Lines); + color(0.9f, 0.5f, 0.0f); + vertex(-2,1,0); + color(1.0f, 1.0f, 0.0f); + vertex(2,1,0); + + vertex(0,1,-0.5); + vertex(0,1,0.5); + + vertex(0,2.0f,0); + vertex(0,-1, 0); + end(); + + int gridsize = 32; + float s = 1.0f / gridsize; + float y = -4.0f; + + float dx = target->location.x - floorf(target->location.x); + float dz = target->location.z - floorf(target->location.z); + + color(0,0, 1.0f); + begin(Lines); + for (int i=-gridsize; i <= gridsize; i++) { + color(0,0, 0); + vertex(i-dx, y, -gridsize-dz); + color(0,0, (gridsize-abs(i))*s); + vertex(i-dx, y, -dz); + vertex(i-dx, y, -dz); + color(0,0, 0); + vertex(i-dx, y, gridsize-dz); + + vertex(-gridsize-dx, y, i-dz); + color(0,0, (gridsize-abs(i))*s); + vertex(-dx, y, i-dz); + vertex(-dx, y, i-dz); + color(0,0, 0); + vertex(gridsize-dx, y, i-dz); + } + end(); +} + +void draw_world(float elapsed) +{ + // draw the world + gl::push(); + + //std::cerr << "ship at " << game::ship.location << " translate " << game::ship.location - target->location << std::endl; + gl::translate(game::ship.location - target->location); + gl::scale(GAMESCALE, GAMESCALE, GAMESCALE); + shipdrawer->draw(elapsed); + gl::pop(); + + //std::cerr << "star at " << game::star.location << " translate " << game::star.location - game::ship.location << std::endl; + gl::translate(game::star.location - target->location); + stardrawer->draw(elapsed); + +} + +void draw(float elapsed) +{ + // Clear the color and depth buffers. + gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + // We don't want to modify the projection matrix. + gl::matrixmode( GL_MODELVIEW ); + gl::loadidentity(); + + // Camera transformation + camera::draw(elapsed); + + // draw the semi-static background + draw_background(); + + // draw the world + draw_world(elapsed); + + SDL_GL_SwapBuffers( ); +} + +} // namespace view + diff --git a/src/client/view.h b/src/client/view.h new file mode 100644 index 0000000..de73ab9 --- /dev/null +++ b/src/client/view.h @@ -0,0 +1,24 @@ +/* view.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_VIEW_H__ +#define __INCLUDED_VIEW_H__ + +#include "common/vector3f.h" + +/// Draws the view of the map +namespace view +{ + void init(); + void shutdown(); + + /// Update the chronometer and draw the game view + void draw(float elapsed); + + /// Reset the projection matrix + void reset(); +}; + +#endif // __INCLUDED_VIEW_H__ diff --git a/src/common/Makefile.am b/src/common/Makefile.am new file mode 100644 index 0000000..4f556bf --- /dev/null +++ b/src/common/Makefile.am @@ -0,0 +1,8 @@ + +METASOURCES = AUTO +libcommon_la_LDFLAGS = -avoid-version -no-undefined +noinst_LTLIBRARIES = libcommon.la +libcommon_la_SOURCES = color.cc color.h functions.cc functions.h osirion.h\ + vector3f.cc vector3f.h + + diff --git a/src/common/color.cc b/src/common/color.cc new file mode 100644 index 0000000..85892c8 --- /dev/null +++ b/src/common/color.cc @@ -0,0 +1,84 @@ +/* + *************************************************************************** + * Copyright (C) 2002-2004 by Stijn Buys * + * stijn.buys@pandora.be * + * * + * This software is redistributed under the terms of the * + * GNU General Public License. Please read LICENSE.txt. * + *************************************************************************** +*/ + +#include "color.h" + +Color::Color() { + _r = _g = _b = 0.0f; + _a = 1.0f; +} + +Color::Color(const float red, const float green , const float blue , const float alpha) { + _r = red; + _g = green; + _b = blue; + _a = alpha; +} + +Color::Color(const float grey, const float alpha) { + _r = _g = _b = grey; + _a = alpha; +} + +Color::Color(const Color &other) { + this->operator=(other); +} + +const Color & Color::operator=(const Color &other) { + this->_r = other._r; + this->_g = other._g; + this->_b = other._b; + this->_a = other._a; + return (*this); +} + +void Color::normalize() { + float tmp = _r; + + if (_g > tmp) + tmp = _g; + if ( _b > tmp) + tmp = _b; + + if (tmp > 1) { + _r /= tmp; + _g /= tmp; + _b /= tmp; + } +} + +float Color::red() const { + return _r; +} + +float Color::green() const { + return _g; +} + +float Color::blue() const { + return _b; +} + +float Color::alpha() const { + return _a; +} + +Color Color::operator*(float scalar) const { + return Color(red()*scalar, green()*scalar, blue()*scalar, alpha()); +} + +Color operator*(float scalar, const Color& color) { + return color * scalar; +} +std::ostream &operator<<(std::ostream &os, const Color &c) { + os << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha(); + return os; +} + diff --git a/src/common/color.h b/src/common/color.h new file mode 100644 index 0000000..d84d1a3 --- /dev/null +++ b/src/common/color.h @@ -0,0 +1,50 @@ +/* + *************************************************************************** + * Copyright (C) 2004 by Stijn Buys * + * stijn.buys@pandora.be * + * * + * This software is redistributed under the terms of the * + * GNU General Public License. Please read LICENSE.txt. * + *************************************************************************** +*/ + +#ifndef __COLOR_HEADER__ +#define __COLOR_HEADER__ + +#include + +/// a class representing an RGBA color value +class Color { + public: + Color(); + Color(const float, float const, const float, const float=1.0f); + Color(const float, const float=1.0f); + Color(const Color &); + + float red() const; + float green() const; + float blue() const; + float alpha() const; + + const Color &operator=(const Color &); + + Color operator*(const float scalar) const; + + // Some default colors + static const Color Black() { return Color(0.0f); }; + static const Color White() { return Color(1.0f); }; + static const Color Red() { return Color(1.0f,0.0f,0.0f); }; + static const Color Green() { return Color(0.0f,1.0f,0.0f); }; + static const Color Blue() { return Color(0.0f, 0.0f, 1.0f); }; + static const Color Yellow() { return Color(1.0f, 1.0f, 0.0f); }; + + private: + void normalize(); + float _r, _g, _b, _a; +}; + +std::ostream &operator<<(std::ostream &os, const Color &c); + +Color operator*(const float scalar, const Color& color); + +#endif // ___HEADER__ diff --git a/src/common/functions.cc b/src/common/functions.cc new file mode 100644 index 0000000..7152c91 --- /dev/null +++ b/src/common/functions.cc @@ -0,0 +1,48 @@ +/* functions.cc + * This file is part of the Osirion project + */ + +#include "functions.h" + +float min(float a, float b) { + return (a < b ? a : b); +} + +float max(float a, float b) { + return (a > b ? a : b); +} + +int min(int a, int b) { + return (a < b ? a : b); +} + +int max(int a, int b) { + return (a > b ? a : b); +} + +float randomf(const float max) { + return ((float) rand() / (float) RAND_MAX) * max; +} + +unsigned randomi(const unsigned int max) { + return ((unsigned int)(rand() % max)); +} + +float degreesf(float angle) { + float r = angle; + while (r <= -180.0f) + r += 360.0f; + while (r >= 180.0f) + r -= 360.0f; + return r; +} + +float sgnf(float value) +{ + if (value < 0 ) + return -1; + else if (value == 0 ) + return 0; + + return 1; +} diff --git a/src/common/functions.h b/src/common/functions.h new file mode 100644 index 0000000..02f25da --- /dev/null +++ b/src/common/functions.h @@ -0,0 +1,42 @@ +/* functions.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_FUNCTIONS_H__ +#define __INCLUDED_FUNCTIONS_H__ + +#include +#include + +/// return the smallest of two float values +float min(float a, float b); + +/// return the smallest of two integers +int min(int a, int b); + +/// return the largest of two float values +float max(float a, float b); + +/// return the largest of two integers +int max(int a, int b); + +/// returns a random float +/*! The value returned will be in the interval [0-max] + * @param max the maximum value returned +**/ +float randomf(const float max = 1); + +/// returns a random integer +/*! The value returned will be in the interval [0-(max-1)] + * @param max the maximum value returned +**/ +unsigned int randomi(const unsigned int max); + +/// return the sign of a float value +float sgnf(float value); + +/// return an angle in the ]-180,180] range +float degreesf(float angle); + +#endif // __INCLUDED_FUNCTIONS_H__ + diff --git a/src/common/osirion.h b/src/common/osirion.h new file mode 100644 index 0000000..56d8b4e --- /dev/null +++ b/src/common/osirion.h @@ -0,0 +1,11 @@ +/* osirion.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_OSIRION_H__ +#define __INCLUDED_OSIRION_H__ + +#define OSIRION_VERSION "0.0.1" +#define GAMESCALE 0.2f + +#endif // __INCLUDED_OSIRION_H__ diff --git a/src/common/vector3f.cc b/src/common/vector3f.cc new file mode 100644 index 0000000..3e469b6 --- /dev/null +++ b/src/common/vector3f.cc @@ -0,0 +1,151 @@ +/* vector3f.h + This file is part of the Osirion project +*/ +#include "vector3f.h" +#include + +Vector3f::Vector3f() : + x(coord[0]), y(coord[1]), z(coord[2]) +{ + for (int i=0; i < 3; i++) + coord[i] = 0; +} + + +Vector3f::Vector3f(const Vector3f &other) : + x(coord[0]), y(coord[1]), z(coord[2]) +{ + for (int i=0; i < 3; i++) + coord[i] = other.coord[i]; +} + +Vector3f::Vector3f(const float xv, const float yv, const float zv) : + x(coord[0]), y(coord[1]), z(coord[2]) +{ + coord[0] = xv; + coord[1] = yv; + coord[2] = zv; +} + +Vector3f::~Vector3f() +{ +} + + +Vector3f & Vector3f::operator=(const Vector3f & other) +{ + for (int i=0; i < 3; i++) + coord[i] = other.coord[i]; + return (*this); +} + + +Vector3f & Vector3f::operator*=(const float scalar) +{ + for (int i=0; i < 3; i++) + coord[i] *= scalar; + return (*this); +} + + +Vector3f & Vector3f::operator/=(const float scalar) { + for (int i=0; i < 3; i++) + coord[i] /= scalar; + return (*this); +} + + +Vector3f &Vector3f::operator-=(const Vector3f & other) { + for (int i=0; i < 3; i++) + coord[i] -= other[i]; + return (*this); +} + + +Vector3f &Vector3f::operator+=(const Vector3f &other) { + for (int i=0; i < 3; i++) + coord[i] += other[i]; + return (*this); +} + + +Vector3f Vector3f::operator*(const float scalar) const { + Vector3f r(*this); + for (int i=0; i < 3; i++) + r.coord[i] *= scalar; + return (r); + } + + +Vector3f Vector3f::operator/(const float scalar) const { + Vector3f r(*this); + for (int i=0; i < 3; i++) + r.coord[i] /= scalar; + return (r); +} + + +Vector3f Vector3f::operator-(const Vector3f& other) const { + Vector3f r(*this); + for (int i=0; i < 3; i++) + r.coord[i] -= other.coord[i]; + return (r); +} + + +Vector3f Vector3f::operator+(const Vector3f& other) const { + Vector3f r(*this); + for (int i=0; i < 3; i++) + r.coord[i] += other.coord[i]; + return (r); +} + + +float Vector3f::operator*(const Vector3f& other) const { + float r = 0; + for (int i=0; i < 3; i++) + r += coord[i] * other.coord[i]; + return (r); +} + + +bool Vector3f::operator==(const Vector3f& other) const { + for (int i=0; i < 3; i++) + if (coord[i] != other.coord[i]) + return (false); + return (true); +} + +float Vector3f::lengthsquared() const { + double r = 0; + for (int i=0; i < 3; i++) + r += coord[i]*coord[i]; + return ((float) r); +} + +float Vector3f::length() const { + double r = 0; + for (int i=0; i < 3; i++) + r += coord[i]*coord[i]; + + return ((float) sqrt(r)); +} + +void Vector3f::normalize() { + (*this) /= this->length(); +} + +std::ostream &operator<<(std::ostream & os, const Vector3f & vector) { + os << vector[0] << " " << vector[1] << " " << vector[2]; + return os; +} + +std::istream &operator>>(std::istream & is, Vector3f & vector) { + for (int i=0; i < 3; i++) + is >> vector[i]; + return is; +} + +Vector3f operator*(float scalar, const Vector3f& vector) { + return vector * scalar; +} diff --git a/src/common/vector3f.h b/src/common/vector3f.h new file mode 100644 index 0000000..53b2ce7 --- /dev/null +++ b/src/common/vector3f.h @@ -0,0 +1,138 @@ +/* vector3f.h + This file is part of the Osirion project +*/ + + #ifndef __INCLUDED_VECTOR3F_H__ + #define __INCLUDED_VECTOR3F_H__ + + #include +/// A point or vector in 3D-space +/*! + An instance of this class represents a point in 3D-space or a 3D-vector + and forms the basic building block for all spatial calculations. + */ +class Vector3f { + +public: + /// Default constructor, creates a Vector3f (0,0,0) + Vector3f(); + + /// Copy constructor + /*! Create a new Vector3f that is a copy from an other + * @param other the vector to copy values from + */ + Vector3f(const Vector3f &other); + + /// Create a Vector3f with given coordinates + /*! Create a new vector at a given position in a 3D-space + * @param xv the x-coordinate of the location + * @param yv the y-coordinate of the location + * @param zv the z-coordinate of the location + */ + Vector3f(const float xv, const float yv, const float zv); + + /// Destructor + ~Vector3f(); + + /* -- Assignment operators -- */ + /// assignment operator + Vector3f& operator=(const Vector3f &other); + + /// multiplicate each element of the vector with a given value + /// @param scalar multiplication factor + Vector3f& operator*=(const float scalar); + + /// divide each element of the vector by a given value + /// @param scalar divider + Vector3f& operator/=(const float scalar); + + /// perform an element-wise subtraction + Vector3f& operator-=(const Vector3f &other); + + /// perform ann element-wise addition + Vector3f& operator+=(const Vector3f &other); + + /* -- Mathematical operators -- */ + + /// return this Vector multiplied with scalar + Vector3f operator*(const float scalar) const; + + /// return this vector divided by a scalar + Vector3f operator/(const float scalar) const; + + /// return the element-wise difference between two vectors + Vector3f operator-(const Vector3f &other) const; + + /// return the element-wise sumn of two vectors + Vector3f operator+(const Vector3f &other) const; + + /// return the vector cross-product + float operator*(const Vector3f &other) const; + + /// comparison operator + bool operator==(const Vector3f &other) const; + + /// assign a value to an element of this vector + /*! WARNING: range is not checked + * @param index the index of the element to assign to ( 0 <= index < 3 ) + */ + inline float& operator[](const unsigned int index) { + return coord[index]; + } + + /// returns the value of an element of this vector + /*! WARNING: range is not checked + * @param index the index of the element to return ( 0 <= index < 3 ) + */ + inline float operator[](const unsigned int index) const { + return coord[index]; + } + + + float &x; + float &y; + float &z; + + /// Return the cartesian length of this vector + float length() const; + + /// Return the cartesian length squared (to speed up calculations) + float lengthsquared() const; + + /// Divide this Vector by it's length + /// @see normalized() + /// WARNING: vector must not be (0, 0, 0) + void normalize(); + + /* static functions */ + + /// Returns the unity vector on the X-axis + static inline Vector3f Xaxis() { return Vector3f(1.0f, 0.0f, 0.0f); } + + /// Returns the unity vector on the Y-axis + static inline Vector3f Yaxis() { return Vector3f(0.0f, 1.0f, 0.0f); } + + /// Returns the unity vector on the Z-axis + static inline Vector3f Zaxis() { return Vector3f(0.0f, 0.0f, 1.0f); } + + /// Return the cartesian length of a vector + static inline float length(const Vector3f& vector) { return vector.length(); } + + /// Return a vector divided by it's length + /// @see normalize() + /// WARNING: vector must not be (0, 0, 0) + static inline Vector3f normalized(const Vector3f& vector) { return (vector / vector.length()); } + + float coord[3]; +}; + +/// Write a Vector3f to a std::ostream +std::ostream &operator<<(std::ostream & os, const Vector3f & vector); + +/// Read a Vector3d from a std::istream +std::istream &operator>>(std::istream & is, Vector3f& vector); + +/// scalar*Vector3f operators +Vector3f operator*(float scalar, const Vector3f& vector); + +#endif // __INCLUDED_VECTOR3F_H__ diff --git a/src/game/Makefile.am b/src/game/Makefile.am new file mode 100644 index 0000000..754c168 --- /dev/null +++ b/src/game/Makefile.am @@ -0,0 +1,9 @@ + +METASOURCES = AUTO +libgame_la_LDFLAGS = -avoid-version +noinst_LTLIBRARIES = libgame.la + +libgame_la_SOURCES = game.cc game.h ship.cc ship.h player.h world.h star.cc \ + star.h sector.h +INCLUDES = -I$(top_srcdir)/src +noinst_HEADERS = world.h sector.h diff --git a/src/game/game.cc b/src/game/game.cc new file mode 100644 index 0000000..dfa5fe5 --- /dev/null +++ b/src/game/game.cc @@ -0,0 +1,31 @@ +/* game.cc + This file is part of the Osirion project +*/ + +// project headers +#include "ship.h" +#include "star.h" + +namespace game { + +Ship ship; +Star star; +bool initialized = false; + +void init() +{ + star.location = Vector3f(256.0f, 0.0f, 256.0f); + initialized = true; +} + +void shutdown() +{ + initialized = false; +} + +void update(float elapsed) +{ + ship.update(elapsed); +} + +}; // namespace game diff --git a/src/game/game.h b/src/game/game.h new file mode 100644 index 0000000..7c1cc0c --- /dev/null +++ b/src/game/game.h @@ -0,0 +1,33 @@ +/* game.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_GAME_H__ +#define __INCLUDED_GAME_H__ + +// project headers +#include "ship.h" +#include "star.h" + +namespace game +{ + /// initialize the game + void init(); + + /// shutdown the game + void shutdown(); + + /// update the game state + void update(float elapsed); + + /// the only ship in the game + extern Ship ship; + + /// the only star in the game + extern Star star; + + /// true while the game is running + extern bool initialized; +}; + +#endif // __INCLUDED_GAME_H__ diff --git a/src/game/player.h b/src/game/player.h new file mode 100644 index 0000000..4c75206 --- /dev/null +++ b/src/game/player.h @@ -0,0 +1,20 @@ +/* player.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_PLAyER_H__ +#define __INCLUDED_PLAyER_H__ + +#include + +/// A player in the game +class Player { +public: + Player(); + ~Player(); + + std::string name; +}; + +#endif // __INCLUDED_PLAyER_H__ diff --git a/src/game/sector.h b/src/game/sector.h new file mode 100644 index 0000000..10f548c --- /dev/null +++ b/src/game/sector.h @@ -0,0 +1,26 @@ +/* sector.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_SECTOR_H__ +#define __INCLUDED_SECTOR_H__ + +#include + +namespace game +{ +class Sector { +public: + Sector(); + ~Sector(); + + /// tag used in configuration files, e.g. "terran" + std::string tag; + /// name to display, e.g. "Terran System" + std::string name; +}; + +} // namespace game + +#endif // __INCLUDED_SECTOR_H__ diff --git a/src/game/ship.cc b/src/game/ship.cc new file mode 100644 index 0000000..a24be0d --- /dev/null +++ b/src/game/ship.cc @@ -0,0 +1,85 @@ +/* ship.cc + This file is part of the Osirion project +*/ + +// C++ headers +#include + +// project headers +#include "common/functions.h" +#include "common/osirion.h" + +#include "ship.h" + +Ship::Ship() +{ + speed = 0; + yaw = 0; + yaw_offset = 0; + + thrust = 0; + + // ship specs + acceleration = 6 * GAMESCALE; + max_speed = 16.0f * GAMESCALE; + + max_yaw_offset = 45; + yaw_speed = 4; +} + +Ship::~Ship() +{ +} + +void Ship::update(float elapsed) +{ + // update yaw + float d = yaw_speed * elapsed * yaw_offset; + yaw_offset -= d; + yaw +=d; + + // update thrust + if (speed < thrust * max_speed) { + speed += acceleration * elapsed; + if (speed > thrust * max_speed) { + speed = thrust * max_speed; + } + } else if(speed > thrust * max_speed) { + speed -= acceleration * elapsed; + if (speed < 0) speed = 0; + } + + // location TODO avoid sin/cos calculations + location.x += cosf(yaw * M_PI / 180) * speed * elapsed; + location.z -= sinf(yaw * M_PI / 180) * speed * elapsed; +} + + +void Ship::thrust_increase() +{ + thrust += 0.05; + if (thrust > 1) thrust = 1; +} + +void Ship::thrust_decrease() +{ + thrust -= 0.08; + if (thrust < 0) thrust = 0; +} + +void Ship::turn_left() +{ + //yaw = degreesf(yaw + 2); + yaw_offset += 2; + if (yaw_offset > max_yaw_offset) + yaw_offset = max_yaw_offset; +} + +void Ship::turn_right() +{ + //yaw = degreesf(yaw - 2); + yaw_offset -= 2; + if (yaw_offset < -max_yaw_offset) + yaw_offset = - max_yaw_offset; +} + diff --git a/src/game/ship.h b/src/game/ship.h new file mode 100644 index 0000000..d6a00ba --- /dev/null +++ b/src/game/ship.h @@ -0,0 +1,54 @@ +/* ship.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_SHIP_H__ +#define __INCLUDED_SHIP_H__ + +// project headers +#include "common/vector3f.h" + +class Ship +{ +public: + Ship(); + ~Ship(); + + /// update the ship state + void update(float elapsed); + + /// location of the ship in space + Vector3f location; + /// speed vector in units/second + float speed; + + /// turn left, increase yaw_offset + void turn_left(); + /// turn right, decrease yaw_offset + void turn_right(); + /// yaw, angle in the x/z plane + float yaw; + + /// increase thrust + void thrust_increase(); + /// decrease thrust + void thrust_decrease(); + /// forward thruster in % [0-1] + float thrust; + + /* -- Ship SPECS --*/ + /// acceleration + float acceleration; + /// maximum speed + float max_speed; + /// maximum yaw_offset + float max_yaw_offset; + /// yaw turn speed + float yaw_speed; + +private: + float yaw_offset; +}; + +#endif // __INCLUDED_SHIP_H__ + diff --git a/src/game/star.cc b/src/game/star.cc new file mode 100644 index 0000000..6aa82ac --- /dev/null +++ b/src/game/star.cc @@ -0,0 +1,17 @@ +/* star.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "star.h" + +Star::Star() : + location(0,0,0), + color(1,1,1,1) +{ + radius = 48; +} + +Star::~Star() +{ +} diff --git a/src/game/star.h b/src/game/star.h new file mode 100644 index 0000000..ef33ad5 --- /dev/null +++ b/src/game/star.h @@ -0,0 +1,29 @@ +/* star.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_STAR_H__ +#define __INCLUDED_STAR_H__ + +// C++ headers +#include + +// project headers +#include "common/vector3f.h" +#include "common/color.h" + +/// A star, that shines so bright +class Star { +public: + Star(); + ~Star(); + + Vector3f location; + Color color; + float radius; + + std::string name; +}; + +#endif // __INCLUDED_STAR_H__ diff --git a/src/game/world.h b/src/game/world.h new file mode 100644 index 0000000..8cbc232 --- /dev/null +++ b/src/game/world.h @@ -0,0 +1,24 @@ +/* world.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_WORLD_H__ +#define __INCLUDED_WORLD_H__ + +#include + +namespace game +{ + +/// The game world +namespace World { + /// load the intial game world into memory + void init(); + /// unload the game world + void shutdown(); +}; + +} // namespace game + +#endif // __INCLUDED_WORLD_H__ diff --git a/src/gl/Makefile.am b/src/gl/Makefile.am new file mode 100644 index 0000000..e7f6663 --- /dev/null +++ b/src/gl/Makefile.am @@ -0,0 +1,10 @@ + +METASOURCES = AUTO + +libosiriongl_la_LDFLAGS = -avoid-version +noinst_LTLIBRARIES = libosiriongl.la + +INCLUDES = -I$(top_srcdir)/src +libosiriongl_la_SOURCES = box.h box.cc sphere.cc sphere.h osiriongl.cc \ + osiriongl.h +noinst_HEADERS = box.h box.h diff --git a/src/gl/box.cc b/src/gl/box.cc new file mode 100644 index 0000000..67a8ed0 --- /dev/null +++ b/src/gl/box.cc @@ -0,0 +1,96 @@ +/* box.cc + This file is part of the Osirion project +*/ + +// project headers +#include "box.h" +#include "osiriongl.h" + +namespace gl { + +Box::Box(Vector3f const & tl, Vector3f const &br) : + topleft(tl), bottomright(br) +{ + topcolor = Color::White(); + bottomcolor= Color::White() * 0.7f; +} + +Box::Box(const Box & other) +{ + (*this) = other; +} + +Box& Box::operator=(const Box &other) +{ + bottomcolor = other.bottomcolor; + topcolor = other.topcolor; + + topleft = other.topleft; + bottomright = other.bottomright; + return (*this); +} + +void Box::draw() +{ + Vector3f v0(topleft.x, bottomright.y, bottomright.z); + Vector3f v1(topleft.x, topleft.y, bottomright.z); + Vector3f v2(topleft.x, topleft.y, topleft.z); + Vector3f v3(topleft.x, bottomright.y, topleft.z); + + Vector3f v4(bottomright.x, bottomright.y, bottomright.z); + Vector3f v5(bottomright.x, topleft.y, bottomright.z); + Vector3f v6(bottomright.x, topleft.y, topleft.z); + Vector3f v7(bottomright.x, bottomright.y, topleft.z); + + begin(Quads); + + // top + color(topcolor); + vertex(v2); + vertex(v1); + vertex(v5); + vertex(v6); + + // sides + color(bottomcolor); + vertex(v0); + color(topcolor); + vertex(v1); + vertex(v2); + color(bottomcolor); + vertex(v3); + + vertex(v3); + color(topcolor); + vertex(v2); + vertex(v6); + color(bottomcolor); + vertex(v7); + + vertex(v4); + color(topcolor); + vertex(v5); + vertex(v1); + color(bottomcolor); + vertex(v0); + + vertex(v7); + color(topcolor); + vertex(v6); + vertex(v5); + color(bottomcolor); + vertex(v4); + + // bottom + color(bottomcolor); + vertex(v4); + vertex(v0); + vertex(v3); + vertex(v7); + + end(); + +} + +} // namespace gl + diff --git a/src/gl/box.h b/src/gl/box.h new file mode 100644 index 0000000..f612243 --- /dev/null +++ b/src/gl/box.h @@ -0,0 +1,41 @@ +/* box.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_BOX_H__ +#define __INCLUDED_BOX_H__ + +#include "common/vector3f.h" +#include "common/color.h" + +namespace gl { + +/// a drawable OpenGL block shape +class Box +{ +public: + /// create a new standard cube with edge length 1 + Box(Vector3f const & tl, Vector3f const &br); + /// copy constructor + Box(const Box &other); + + /// assignment operator + Box& operator=(const Box &other); + + /// top left vertex (1,1,1) + Vector3f topleft; + /// bottom right vertex (-1,-1,-1) + Vector3f bottomright; + + /// draw the block + void draw(); + + /// Top color + Color topcolor; + /// bottom color + Color bottomcolor; +}; + +} // namespace gl + +#endif // __INCLUDED_BOX_H__ diff --git a/src/gl/osiriongl.cc b/src/gl/osiriongl.cc new file mode 100644 index 0000000..0917eec --- /dev/null +++ b/src/gl/osiriongl.cc @@ -0,0 +1,214 @@ +/* gl.cc + * This file is part of the Osirion project + */ + +// SDL headers +#include + +#include "osiriongl.h" + +namespace gl +{ + +typedef void (APIENTRY *glBegin_func)(GLenum); +typedef void (APIENTRY *glEnd_func)(); +typedef void (APIENTRY *glViewport_func)(GLint, GLint, GLsizei, GLsizei); +typedef void (APIENTRY *glDepthMask_func)(GLenum); +typedef void (APIENTRY *glFrontFace_func)(GLenum); +typedef void (APIENTRY *glCullFace_func)(GLenum); +typedef void (APIENTRY *glShadeModel_func)(GLenum); +typedef void (APIENTRY *glEnable_func)(GLenum); +typedef void (APIENTRY *glClear_func)(GLbitfield); +typedef void (APIENTRY *glClearColor_func)(GLclampf, GLclampf, GLclampf,GLclampf); +typedef void (APIENTRY *glRotatef_func)(GLfloat, GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glTranslatef_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glScalef_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glVertex3f_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glPushMatrix_func)(); +typedef void (APIENTRY *glPopMatrix_func)(); +typedef void (APIENTRY *glColor4f_func)(GLfloat, GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glMatrixMode_func)(GLenum); +typedef void (APIENTRY *glLoadIdentity_func)(); +typedef void (APIENTRY *glFrustum_func)(GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble); + +glBegin_func osglBegin = 0; +glEnd_func osglEnd = 0; +glViewport_func osglViewport = 0; +glDepthMask_func osglDepthMask = 0; +glFrontFace_func osglFrontFace = 0; +glCullFace_func osglCullFace = 0; +glShadeModel_func osglShadeModel = 0; +glEnable_func osglEnable = 0; +glClear_func osglClear = 0; +glClearColor_func osglClearColor = 0; +glRotatef_func osglRotatef = 0; +glTranslatef_func osglTranslatef = 0; +glScalef_func osglScalef = 0; +glVertex3f_func osglVertex3f = 0; +glPushMatrix_func osglPushMatrix = 0; +glPopMatrix_func osglPopMatrix = 0; +glColor4f_func osglColor4f = 0; +glMatrixMode_func osglMatrixMode = 0; +glLoadIdentity_func osglLoadIdentity = 0; +glFrustum_func osglFrustum = 0; + +// ------------- INITIALIZE FUNCTION POINTERS --------------// +void init() +{ + osglBegin = (glBegin_func) SDL_GL_GetProcAddress("glBegin"); + osglEnd = (glEnd_func) SDL_GL_GetProcAddress("glEnd"); + osglViewport = (glViewport_func) SDL_GL_GetProcAddress("glViewport"); + osglDepthMask = (glDepthMask_func) SDL_GL_GetProcAddress("glDepthMask"); + osglFrontFace = (glFrontFace_func) SDL_GL_GetProcAddress("glFrontFace"); + osglCullFace = (glCullFace_func) SDL_GL_GetProcAddress("glCullFace"); + osglShadeModel = (glShadeModel_func) SDL_GL_GetProcAddress("glShadeModel"); + osglEnable = (glEnable_func) SDL_GL_GetProcAddress("glEnable"); + osglClear = (glClear_func) SDL_GL_GetProcAddress("glClear"); + osglClearColor = (glClearColor_func) SDL_GL_GetProcAddress("glClearColor"); + osglRotatef = (glRotatef_func) SDL_GL_GetProcAddress("glRotatef"); + osglTranslatef = (glTranslatef_func) SDL_GL_GetProcAddress("glTranslatef"); + osglScalef = (glScalef_func) SDL_GL_GetProcAddress("glScalef"); + osglVertex3f = (glVertex3f_func) SDL_GL_GetProcAddress("glVertex3f"); + osglPushMatrix = (glPushMatrix_func) SDL_GL_GetProcAddress("glPushMatrix"); + osglPopMatrix = (glPopMatrix_func) SDL_GL_GetProcAddress("glPopMatrix"); + osglColor4f = (glColor4f_func) SDL_GL_GetProcAddress("glColor4f"); + osglMatrixMode = (glMatrixMode_func) SDL_GL_GetProcAddress("glMatrixMode"); + osglLoadIdentity = (glLoadIdentity_func) SDL_GL_GetProcAddress("glLoadIdentity"); + osglFrustum = (glFrustum_func) SDL_GL_GetProcAddress("glFrustum"); +} + + +void shutdown() +{ + osglBegin = 0; + osglEnd = 0; + osglViewport = 0; + osglDepthMask = 0; + osglFrontFace = 0; + osglCullFace = 0; + osglShadeModel = 0; + osglEnable = 0; + osglClear = 0; + osglClearColor = 0; + osglRotatef = 0; + osglTranslatef = 0; + osglScalef = 0; + osglVertex3f = 0; + osglPushMatrix = 0; + osglPopMatrix = 0; + osglColor4f = 0; + osglMatrixMode = 0; + osglLoadIdentity = 0; +} + +void begin(Primitive primitive) { + osglBegin(primitive); +} + +void end() { + osglEnd(); +} + +void viewport(GLint x, GLint y, GLsizei width, GLsizei height) +{ + osglViewport(x, y, width, height); +} + +void depthmask(GLenum mode) +{ + osglDepthMask(mode); +} + + +void frontface(GLenum mode) +{ + osglFrontFace(mode); +} + +void cullface(GLenum mode) +{ + osglCullFace(mode); +} + +void shademodel(GLenum mode) +{ + osglShadeModel(mode); +} + +void enable(GLenum cap) +{ + osglEnable(cap); +} + +void clear (GLbitfield mask) { + osglClear(mask); +} + +void clearcolor(Color const & color) { + osglClearColor(color.red(), color.green(), color.blue(), color.alpha()); +} + +void clearcolor(const float r, const float g, const float b, const float a) { + osglClearColor(r,g,b, a); +} + +void rotate(const float angle, const Vector3f& vector) { + osglRotatef(angle, vector[0], vector[1], vector[2]); +} + +void rotate(const float angle, const float x, const float y, const float z) { + osglRotatef(angle, x, y, z); +} + +void translate(const Vector3f& vector) { + osglTranslatef(vector[0], vector[1], vector[2]); +} + +void translate(const float x, const float y, const float z) { + osglTranslatef(x, y, z); +} +void scale(const Vector3f& vector) { + osglScalef(vector[0], vector[1], vector[2]); +} + +void scale(const float x, const float y, const float z) { + osglScalef(x, y, z); +} + +void vertex(const Vector3f& vector) { + osglVertex3f(vector[0], vector[1], vector[2]); +} + +void vertex(const float x, const float y, const float z) { + osglVertex3f(x, y, z); +} + +void push() { + osglPushMatrix(); +} + +void pop() { + osglPopMatrix(); +} + +void color(const float r, const float g, const float b, const float a) { + osglColor4f(r,g,b,a); +} +void color(Color const & color) { + osglColor4f(color.red(), color.green(), color.blue(), color.alpha()); +} + +void matrixmode(GLenum mode) { + osglMatrixMode(mode); +} + +void loadidentity() { + osglLoadIdentity(); +} + +void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble zfar) +{ + osglFrustum(left, right, bottom, top, znear, zfar); +} + +} // namespace gl diff --git a/src/gl/osiriongl.h b/src/gl/osiriongl.h new file mode 100644 index 0000000..92c0db3 --- /dev/null +++ b/src/gl/osiriongl.h @@ -0,0 +1,158 @@ +/* gl.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_OSIRIONGL_H__ +#define __INCLUDED_OSIRIONGL_H__ + +// OpenGL headers +#include + +// project headers +#include "common/vector3f.h" +#include "common/color.h" + +/// wrapper namespace for OpenGL operations +/*! The GL namespace provides a wrapper to the OpenGL library functions. + * All methods take floats or Vector3f and Color as parameters. + */ +namespace gl +{ + /// initialize the OpenGL subsystem + void init(); + + /// shutdown the OpenGL subsystem + void shutdown(); + + /// enum to denote Vertex drawing modes + enum Primitive { + Points=GL_POINTS, + Lines=GL_LINES, + LineStrip=GL_LINE_STRIP, + LineLoop=GL_LINE_LOOP, + Triangles=GL_TRIANGLES, + TriangleStrip=GL_TRIANGLE_STRIP, + TriangleFan=GL_TRIANGLE_FAN, + Quads=GL_QUADS, + QuadStrip=GL_QUAD_STRIP, + Polygon=GL_POLYGON + }; + + + /// glViewPort + void viewport(GLint x, GLint y, GLsizei width, GLsizei height ); + + /// set the color used to clear to buffer + void clearcolor(Color const &color); + /// set the color used to clear to buffer + void clearcolor(const float r, const float g, const float b, const float a); + + /// clear buffers to preset values + void clear (GLbitfield mask); + + /// glMatrixMode + void matrixmode(GLenum mode); + + /// glEnable + void enable(GLenum cap); + + /// glShadeModel + void shademodel(GLenum mode); + + /// glCullFace + void cullface(GLenum mode); + + /// glFrontFace + void frontface(GLenum mode); + + + /// glDepthMask + void depthmask(GLenum mode); + + /// Delimite the start of a sequence of verteces describing a primitive or group of primitives + /*! @param primitive The type of drawing primitive + * @see end() + */ + void begin(Primitive primitive); + + /// Delimit the end of a sequence of verteces describing a primitive or group of primitives + /*! @see begin() + */ + void end(); + + /// Add the next vertex the the current drawing operation + /*! From the glVertex() description: + * vertex() commands are used within begin()/end() pairs to specify point, + * line, and polygon vertices. The current color, normal, and texture + * coordinates are associated with the vertex when vertex() is called. + */ + void vertex(const Vector3f& vector); + + void vertex(const float x, const float y, const float z); + + /// Multiply the current matrix by a general rotation matrix + /*! @param angle The angle of the rotation, in degrees [0-360] + * @param vector The rotation axes, relative to the origin (0,0,0) + */ + void rotate(const float angle, const Vector3f& vector); + + /// Multiply the current matrix by a general rotation matrix + /*! @param angle The angle of the rotation, in degrees + * @param x The x-coordinate of the rotation vector + * @param y The y-coordinate of the rotation vector + * @param z The z-coordinate of the rotation vector + */ + void rotate(const float angle, const float x, const float y, const float z); + + /// Multiply the current matrix by a general translation matrix + /*! @param vector The translation vector, relative to the origin (0,0,0) + */ + void translate(const Vector3f& vector); + + /// Multiply the current matrix by a general translation matrix + /*! + * @param x The x-coordinate of the translation vector + * @param y The y-coordinate of the translation vector + * @param z The z-coordinate of the translation vector + */ + void translate(const float x, const float y, const float z); + + /// Multiply the current matrix by a general scaling matrix + /*! @param vector The scale factor for all 3 axes + */ + void scale(const Vector3f& vector); + + /// Multiply the current matrix by a general scaling matrix + /*! + * @param x x-scaling factor + * @param y y-scaling factor + * @param z z-scaling factor + */ + void scale(const float x, const float y, const float z); + + /// Specify the drawing color for the next GL functions + /*! @param color the new drawing color + */ + void color(Color const & color); + + /*! @param r red value of the new drawing color + * @param g green value of the new drawing color + * @param b blue value of the new drawing color + * @param a alpha value of the new drawing color + */ + void color(const float r, const float g, const float b, const float a=1.0f); + + /// Push the current transformation matrix to the stack + void push(); + + /// Replace the transformation matrix with the top from the stack + void pop(); + + /// Replace the transformation matrix with the identity matrtix + void loadidentity(); + + /// Perspective matrix + void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble zfar); +}; + +#endif // __INCLUDED_OSIRIONGL_H__ diff --git a/src/gl/sphere.cc b/src/gl/sphere.cc new file mode 100644 index 0000000..5e4e931 --- /dev/null +++ b/src/gl/sphere.cc @@ -0,0 +1,89 @@ +/* sphere.cc + This file is part of the Osirion project +*/ + +#include "common/functions.h" + +#include "osiriongl.h" +#include "sphere.h" + +namespace gl { + +const int segments = 33; + +Sphere::Sphere(Vector3f p , float r) +{ + position = p; + radius = r; + + // TODO make global sine-cosine lists + sintable = new float[segments]; + costable = new float[segments]; + float d = 2 * M_PI / segments; + + for (int i=0; i < segments; i++) { + sintable[i] = sin( d * (float) i ); + costable[i] = cos ( d * (float) i ); + } +} + +Sphere::~Sphere() +{ + delete sintable; + delete costable; +} + +Sphere::Sphere(const Sphere &other) +{ + (*this) = other; +} + +Sphere& Sphere::operator=(const Sphere &other) +{ + position = other.position; + radius = other.radius; +} + +void Sphere::draw() +{ + // draw top + // TODO upside-down + float r = radius*sintable[1]; + float h = radius*costable[1]; + + begin(LineLoop); + //begin(Polygon); + for (int i = segments-1; i >= 0; i--) + vertex(r*costable[i], h, r*sintable[i]); + end(); + + // draw bottom + // TODO upside-down + begin(LineLoop); + for (int i = 0; i< segments; i++) + vertex(r*costable[i], -h, r*sintable[i]); + end(); + + // draw body + for (int j=1; j < segments-1; j++) { + r = radius*sintable[j]; + float r1 = radius*sintable[j+1]; + + begin(QuadStrip); + vertex(r1, radius*costable[j+1], 0); + vertex(r, radius*costable[j], 0); + + for (int i = segments-1; i >= 0; i--) { + vertex(r1*costable[i], radius*costable[j+1], r1*sintable[i]); + vertex(r*costable[i], radius*costable[j], r*sintable[i]); + //vertex(r*costable[i-1], radius*costable[j], r*sintable[i-1]); + //vertex(r1*costable[i-1], radius*costable[j+1], r1*sintable[i-1]); + } + end(); + + } +} + + +} // namespace gl + diff --git a/src/gl/sphere.h b/src/gl/sphere.h new file mode 100644 index 0000000..f2bbf0a --- /dev/null +++ b/src/gl/sphere.h @@ -0,0 +1,50 @@ +/* sphere.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_SPHERE_H__ +#define __INCLUDED_SPHERE_H__ + +#include "common/vector3f.h" +#include "common/color.h" + +namespace gl { + +/// a drawable OpenGL block shape +class Sphere +{ +public: + /// create a new sphere + Sphere(Vector3f p = Vector3f(), float r = 1.0f); + + /// copy constructor + Sphere(const Sphere &other); + + /// destructor + ~Sphere(); + + /// assignment operator + Sphere& operator=(const Sphere &other); + + /// radius of the sphere + float radius; + + /// position of the sphere + Vector3f position; + + /// draw the sphere + void draw(); + + /// Top color + Color topcolor; + /// bottom color + Color bottomcolor; + +private: + float *sintable; + float *costable; +}; + +} // namespace gl + +#endif // __INCLUDED_SPHERE_H__ -- cgit v1.2.3