diff --git a/Makefile b/Makefile index c67aba7..7a0a708 100755 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ CXXFLAGS := $(CXXFLAGS) -g -DDEBUG endif -OBJECTS = main.o poly.o model.o light.o vec.o color.o +OBJECTS = main.o poly.o model.o light.o vec.o color.o thing.o animation.o all: $(OBJECTS) @if [ ! -d $(BINDIR) ]; then \ diff --git a/include/animation.h b/include/animation.h new file mode 100644 index 0000000..4441f1d --- /dev/null +++ b/include/animation.h @@ -0,0 +1,42 @@ +#ifndef _ANIMATION_H +#define _ANIMATION_H + +#include "thing.h" + +#include + +#ifndef PI +#define PI 3.14159265f +#endif + +using namespace std; + +class Animation { + public: + Animation(); + void init(); + void tick(); +}; + +class UserAnimation : public Animation { + private: + int mouseX; + int mouseY; + float speed; + int current; + std::vector things; + int kbdlock; + bool showNormals; + bool showMe; + float delta; + bool trace; + public: + UserAnimation(); + UserAnimation(Thing* t); + UserAnimation(std::vector ths); + void addThing(Thing* t); + void init(); + void tick(); +}; + +#endif diff --git a/include/light.h b/include/light.h index 3e4ccfb..d7f712c 100755 --- a/include/light.h +++ b/include/light.h @@ -2,10 +2,11 @@ #define _LIGHT_H #include "vec.h" +#include "thing.h" #include "color.h" #include -class Light { +class Light : public Thing { protected: void set(); bool enabled; diff --git a/include/poly.h b/include/poly.h index 33a24e5..e277fa0 100755 --- a/include/poly.h +++ b/include/poly.h @@ -3,13 +3,14 @@ #include "vec.h" #include "spoly.h" +#include "thing.h" #include "color.h" #include #include using namespace std; -class Poly : SPoly { +class Poly : SPoly, Thing { private: std::vector initpoints; int size; diff --git a/include/thing.h b/include/thing.h new file mode 100644 index 0000000..e0c147b --- /dev/null +++ b/include/thing.h @@ -0,0 +1,37 @@ +#ifndef _THING_H +#define _THING_H + +#include "vec.h" +#include "color.h" + +using namespace std; + +class Thing { + public: + void draw(); + void rotate(Vec rot); + void setColor(Color* c); + void setColor(Color c); + void setDelta(float d); + void showNormals(); + void hideNormals(); + void setPosition(Vec newpos); + void setCenter(Vec newcenter); + void move(Vec d); + void scale(float f); + void trace(bool s); +}; + +class View : public Thing { + private: + Vec* rotation; + public: + View(); + void rotate(Vec rot); + void setPosition(Vec newpos); + void setCenter(Vec newcenter); + void move(Vec d); + void draw(); +}; + +#endif diff --git a/sources/animation.cpp b/sources/animation.cpp new file mode 100644 index 0000000..a7d13c1 --- /dev/null +++ b/sources/animation.cpp @@ -0,0 +1,167 @@ +#include "animation.h" + +#ifndef UNIX +#include // This file import the SDL inteface +#else +#include // This file import the SDL inteface +#endif + +#ifndef PI +#define PI 3.14159265f +#endif + +/* class Animation */ + +Animation::Animation() { +} + +void Animation::tick() { +} + +void Animation::init() { +} + +/* class UserAnimation */ + +UserAnimation::UserAnimation() { + current = -1; +} + +UserAnimation::UserAnimation(Thing* ths) { + things.push_back(ths); + current = 0; +} + +UserAnimation::UserAnimation(std::vector ths) { + things = ths; + current = 0; +} + +void UserAnimation::init() { + speed = 1.0f; + current = -1; + kbdlock = 0; + mouseX = 0; + mouseY = 0; + delta = 0.0f; + trace = false; + showNormals = false; + showMe = true; +} + +void UserAnimation::tick() { + /* get mouse */ + int newmouseX; + int newmouseY; + unsigned mouseButtons = SDL_GetMouseState(&newmouseX, &newmouseY); + + if ( mouseButtons & SDL_BUTTON(1) ) { } + if ( mouseButtons & SDL_BUTTON(3) ) { } + + if ( newmouseX != mouseX || newmouseY != mouseY ) { + Vec v = Vec(speed*0.1f*(newmouseY-mouseY), speed*0.1f*(newmouseX-mouseX), 0.0f); + ((View*)things.at(current))->rotate(v); + mouseX = newmouseX; + mouseY = newmouseY; + } + + + /* get keyboard */ + if ( kbdlock > 0 ) { + kbdlock--; + } else { + Uint8 *kbd = SDL_GetKeyState(NULL); + + /* object selection */ + if ( kbd[SDLK_0] ) { current = 0; } + if ( kbd[SDLK_1] ) { current = 1; } + if ( kbd[SDLK_2] ) { current = 2; } + if ( kbd[SDLK_3] ) { current = 3; } + if ( kbd[SDLK_4] ) { current = 4; } + if ( kbd[SDLK_5] ) { current = 5; } + if ( kbd[SDLK_6] ) { current = 6; } + if ( kbd[SDLK_7] ) { current = 7; } + if ( kbd[SDLK_8] ) { current = 8; } + if ( kbd[SDLK_9] ) { current = 9; } + + if ( kbd[SDLK_RIGHT] ) { current++; kbdlock = 3; } + if ( kbd[SDLK_LEFT] ) { current--; kbdlock = 3; } + + if ( things.size() > 0 ) { + if ( current < 0 ) { + current = 0; + } else { + current = current % things.size(); + } + } else { + current = -1; + } + + /* speed */ + if ( kbd[SDLK_LSHIFT] || kbd[SDLK_RSHIFT] ) { + speed = 10.0f; + } else { + if ( kbd[SDLK_LCTRL] || kbd[SDLK_RCTRL] ) { + speed = 0.1f; + } else { + speed = 1.0f; + } + } + + /* move x */ + if ( kbd[SDLK_a] ) { things.at(current)->move(Vec(-1.0f*speed, 0.0f, 0.0f)); } + if ( kbd[SDLK_d] ) { things.at(current)->move(Vec(1.0f*speed, 0.0f, 0.0f)); } + /* move y */ + if ( kbd[SDLK_UP] ) { things.at(current)->move(Vec(0.0f, 1.0f*speed, 0.0f)); } + if ( kbd[SDLK_DOWN] ) { things.at(current)->move(Vec(0.0f, -1.0f*speed, 0.0f)); } + /* move z */ + if ( kbd[SDLK_w] ) { things.at(current)->move(Vec(0.0f, 0.0f, -1.0f*speed)); } + if ( kbd[SDLK_s] ) { things.at(current)->move(Vec(0.0f, 0.0f, 1.0f*speed)); } + + /* rotate z */ + if ( kbd[SDLK_q] ) { things.at(current)->rotate(Vec(0.0f, 0.0f, 1.0f*speed)); } + if ( kbd[SDLK_e] ) { things.at(current)->rotate(Vec(0.0f, 0.0f, -1.0f*speed)); } + + /* scaling */ + if ( kbd[SDLK_x] ) { things.at(current)->scale(1.1f*speed); } + if ( kbd[SDLK_y] ) { things.at(current)->scale(0.9f*speed); } + + /* cycle color */ + //TODO + + /* normals */ + if ( kbd[SDLK_n] ) { + if ( showNormals ) { + things.at(current)->hideNormals(); + showNormals = false; + } else { + things.at(current)->showNormals(); + showNormals = true; + } + kbdlock = 3; + } + + /* delta */ + if ( kbd[SDLK_m] ) { + if ( delta > 0.0f ) { + if ( delta < PI/8.0f ) { + delta = 0.0f; + } else { + delta = delta/2.0f; + } + } else { + delta = PI; + } + things.at(current)->setDelta(delta); + kbdlock = 3; + } + + /* trace */ + if ( kbd[SDLK_t] ) { things.at(current)->trace(trace); trace = !trace; kbdlock = 3; } + } +} + +void UserAnimation::addThing(Thing* t) { + things.push_back(t); + if ( current < 0 ) { current = 0; } +} diff --git a/sources/main.cpp b/sources/main.cpp index 3496f91..4de62f1 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -49,8 +49,10 @@ #include "vec.h" #include "color.h" +#include "thing.h" #include "poly.h" #include "model.h" +#include "animation.h" #include "light.h" using namespace std; @@ -171,6 +173,19 @@ } +void drawFog(GLuint filter) { + GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // Storage For Three Types Of Fog + GLfloat fogColor[4]= {0.5f, 0.5f, 0.5f, 1.0f}; // Fog Color + + glFogi(GL_FOG_MODE, fogMode[filter]); // Fog Mode + glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color + glFogf(GL_FOG_DENSITY, 0.35f); // How Dense Will The Fog Be + glHint(GL_FOG_HINT, GL_DONT_CARE); // Fog Hint Value + glFogf(GL_FOG_START, 1.0f); // Fog Start Depth + glFogf(GL_FOG_END, 5.0f); // Fog End Depth + glEnable(GL_FOG); // Enables GL_FOG +} + /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// @@ -178,8 +193,6 @@ // MAIN // int main( int argc, char **argv ) { - char a = '|'; - printf("%d", a); // Initialize SDL: if (SDL_Init(SDL_INIT_VIDEO) == -1) { @@ -214,8 +227,19 @@ Color cyan (0,1,1); Color grey (0.5f,0.5f,0.5f); + View* myView = new View(); + UserAnimation* userAnim = new UserAnimation(); + userAnim->init(); + userAnim->addThing((Thing*)myView); + std::vector models; + std::vector anims; + + anims.push_back((Animation*)userAnim); + + int fog = 3; + /* begin cube */ Vec* ulh = new Vec(0 ,0 ,0 ); Vec* ulv = new Vec(0 ,0 ,10); @@ -293,7 +317,7 @@ printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); Vec myCen = Vec(5,-10,5); myModel->setCenter(myCen); - //myModel->showNormals(); + myModel->showNormals(); myModel->setDelta(PI/2.0); //myModel->vardump(""); @@ -399,6 +423,8 @@ models.push_back(t); } + cube->showNormals(); + while (!done) { ///////////////////// // Do some rendering: @@ -424,9 +450,15 @@ glTranslatef(viewer_pos.x(), viewer_pos.y(), viewer_pos.z()); //rotate viewer - glRotatef(viewer_rot.z(), 0.0f, 0.0f, 1.0f); - glRotatef(viewer_rot.y(), 0.0f, 1.0f, 0.0f); - glRotatef(viewer_rot.x(), 1.0f, 0.0f, 0.0f); + //glRotatef(viewer_rot.z(), 0.0f, 0.0f, 1.0f); + //glRotatef(viewer_rot.y(), 0.0f, 1.0f, 0.0f); + //glRotatef(viewer_rot.x(), 1.0f, 0.0f, 0.0f); + myView->draw(); + + /* do animations */ + for ( std::vector::size_type anim_ct = 0; anim_ct < anims.size(); anim_ct++ ) { + ((UserAnimation*)anims.at(anim_ct))->tick(); + } /* draw x- axis */ //glBegin(GL_LINES); black.drawColor(); glVertex3f(-1000000.0f, 0.0f, 0.0f); glVertex3f(1000000.0f, 0.0f, 0.0f); glEnd(); @@ -450,7 +482,7 @@ //cube->rotate(Vec(0.1f, 0.5f, 1.0f)); //myModel->draw(); //myModel->rotate(Vec(0.2f, 0.3f, 1.2f)); - + //////////////// // Check events: @@ -463,7 +495,7 @@ } } - // Update mouse: + /* Update mouse: OldMouseX = MouseX; OldMouseY = MouseY; unsigned buttons = SDL_GetMouseState(&MouseX, &MouseY); @@ -482,7 +514,7 @@ } //if (MouseButtonLeft) - // delta += 1.0f; + // delta += 1.0f; */ // Update keyboard (used like a joypad): Uint8 *keystate = SDL_GetKeyState(NULL); @@ -521,6 +553,16 @@ glClearColor(0.0f, 0.0f, 0.5f, 1.0f); }*/ + + if ( keystate[SDLK_z] ) { + fog++; + fog = fog % 4; + } + + if ( fog < 3 ) { + drawFog(fog); + } + // speed if ( keystate[SDLK_LSHIFT] || keystate[SDLK_RSHIFT] ) { viewerMovingSpeed = 10.0f; diff --git a/sources/thing.cpp b/sources/thing.cpp new file mode 100644 index 0000000..dbc3825 --- /dev/null +++ b/sources/thing.cpp @@ -0,0 +1,33 @@ +#include "thing.h" + +#include +#include + +void Thing::draw() { } +void Thing::rotate(Vec rot) { } +void Thing::setColor(Color* c) { } +void Thing::setColor(Color c) { } +void Thing::setDelta(float d) { } +void Thing::showNormals() { } +void Thing::hideNormals() { } +void Thing::setPosition(Vec newpos) { } +void Thing::setCenter(Vec newcenter) { } +void Thing::move(Vec d) { } +void Thing::scale(float f) { } +void Thing::trace(bool s) { } + +View::View() { + rotation = new Vec(0.0f, 0.0f, 0.0f); +} + +void View::rotate(Vec rot) { + rotation->add((Vec)rot); +} +void View::setPosition(Vec newpos) { /*TODO*/ } +void View::setCenter(Vec newcenter) { /*TODO*/ } +void View::move(Vec d) { /*TODO*/ } +void View::draw() { + glRotatef(rotation->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rotation->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rotation->z(), 0.0f, 0.0f, 1.0f); +}