From: Peter Schaefer Date: Sat, 4 Apr 2015 12:39:31 +0000 (+0200) Subject: corrected CameraView and ModelMove (X,Z) X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=7ff9402b864700462465146e836295b57a86bd67;p=cgue_weave.git corrected CameraView and ModelMove (X,Z) --- diff --git a/Weave/Events.cpp b/Weave/Events.cpp index e7e6ea3..fe6d562 100644 --- a/Weave/Events.cpp +++ b/Weave/Events.cpp @@ -174,8 +174,9 @@ void Events::MouseUp(int _key) void Events::keymotion() { - axis(move_x, SDLK_w, SDLK_s); - axis(move_y, SDLK_a, SDLK_d); + axis(move_x, SDLK_a, SDLK_d); + axis(move_y, SDLK_w, SDLK_s); + } void Events::padmotion() diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 4199dd3..150f0a8 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -33,7 +33,7 @@ Game::Game() : playing(true) { srand((int)time(NULL)); // Hauptfenster - Scene* tmp_Scene = new Scene(0, 0, Graphix::getWindowWidth(), Graphix::getWindowHeight(), 60, 0.1f, 100, vec3(0.0f, 0.0f, -3.0f)); + Scene* tmp_Scene = new Scene(0, 0, Graphix::getWindowWidth(), Graphix::getWindowHeight(), 45, 0.1f, 10, vec3(0.0f, 0.0f, -2.0f)); layer[0] = tmp_Scene; //Allg Shader @@ -76,7 +76,7 @@ Game::Game() : playing(true) // //cout << ((float)(rand() % (size*steps * 2)) / steps - size) << ((float)(rand() % (size*steps * 2)) / steps - size) << ((float)(rand() % (size*steps * 2)) / steps - size) << endl; //} //ein Gegner - //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(1.f, -2.f, 2.f)), "../models/sphere/sphere.dae", "../Textures/game_box.png")); + tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(1.f, 0.f, 1.f)), "cow/cow.dae", "model_cow.jpg")); //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(-1.f, 2.f, -2.f)), "../models/box/box.dae", "../Textures/sky_withstars.png")); //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(1.f, 3.f, -2.f)), "../models/cow/cow.dae", "../models/cow/texture.jpg")); } diff --git a/Weave/Graphix/Scene.cpp b/Weave/Graphix/Scene.cpp index 6e441d7..19c2d34 100644 --- a/Weave/Graphix/Scene.cpp +++ b/Weave/Graphix/Scene.cpp @@ -19,7 +19,7 @@ Scene::Scene() : }*/ Scene::Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : - viewPort(new ViewPort(x, y, width, height)), +viewPort(new ViewPort(x, y, width, height, fovy, zNear, zFar)), projection(perspective(fovy, (float)width / height, zNear, zFar)), view(translate(pos)), SceneObjects(new list), @@ -32,7 +32,7 @@ Scene::Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int he } Scene::Scene(Scene* _Scene, unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : - viewPort(new ViewPort(x, y, width, height)), +viewPort(new ViewPort(x, y, width, height, fovy, zNear, zFar)), projection(perspective(fovy, (float)width / height, zNear, zFar)), view(translate(pos)), SceneObjects(_Scene->SceneObjects), @@ -74,42 +74,31 @@ void Scene::update(float deltaT) vec3 pos; //Focus auf Player - - if (lookat->getMatter()<1e-2) - { - Message::error("GameOver"); + if (lookat != NULL){ + pos = lookat->getPosition(); + view = view*translate(-pos); } + int move_x = Events::getMoveX(); int move_y = Events::getMoveY(); //int shoot = Events::getAction1(); //int reset = Events::getAction2(); - //cout << move_x << " " << move_y << endl; - if (move_x) - lookat->turn(0.3f *move_x, vec3(0.f, 1.f, 0.f)); + lookat->move(0.03f *move_x * vec3(1.f, 0.f, 0.f)); if (move_y) - lookat->turn(0.3f *move_y, vec3(1.f, 0.f, 0.f)); - //if (shoot) - //{ - // vec3 direction = lookat->push(vec3(0.f, 0.f, shoot*0.1f)); - // //SceneObjects->push_back(self->copy();) - //} - //if (reset) - // lookat->stop(); + lookat->move(0.03f *move_y * vec3(0.f, 0.f, 1.f)); // XYAchse um den Player - if (Events::getViewX() || Events::getViewY()) - { - if (lookat!=NULL) - view = translate(view,lookat->getPosition()); - pos = (vec3)view[3]; - view = rotate(deltaT * 70, vec3(sign(Events::getViewX()), sign(Events::getViewY()), 0.0f))*view; - view = translate(pos - (vec3)view[3])*view; - if (lookat != NULL) - view = translate(view, -lookat->getPosition()); - // TODO Kamera wieder an die richtige Stelle schieben - } + viewPort->rotateView(.3f*Events::getViewX(), .3f*Events::getViewY()); + //int view_x = Events::getViewX(); + //int view_y = Events::getViewY(); + + //if ( view_x || view_y) + //{ + // //TODO Limit Rotation + // view = view * rotate(0.3f, vec3(view_y ,view_x, 0.f)); + //} //// Zoom auf Player @@ -123,8 +112,6 @@ void Scene::update(float deltaT) // //view = view*scale(vec3((float)(1 + deltaT * sign(Events::getViewZ())))); //} - if (lookat != NULL) - pos = lookat->getPosition(); // Alle Objekte der Scene aktualisieren if (newObjects) { @@ -142,23 +129,12 @@ void Scene::update(float deltaT) (*i)->collisions(*j); } } - for (auto i = SceneObjects->begin(); i != SceneObjects->end(); ++i) - { - if ((*i)->isEnemy() && (*i)->getMatter() < lookat->getMatter()) - (*i)->useTextureSave(); - else if ((*i)->isEnemy() && (*i)->getMatter() >= lookat->getMatter()) - (*i)->useTextureDanger(); - if ((*i)->getMatter() < 1.e-3 && newObjects) - { - //SceneObjects->erase(i); -// delete (*i); - } - } + } if (lookat != NULL) - view = translate(view,pos - lookat->getPosition()); + view = view*translate(lookat->getPosition()); } @@ -168,9 +144,10 @@ void Scene::draw() const for (auto i = ShaderSet->cbegin(); i != ShaderSet->cend(); ++i) { - (*i)->useShader(); - glUniformMatrix4fv((*i)->getUniformLocation("projection"), 1, false, value_ptr(projection)); - glUniformMatrix4fv((*i)->getUniformLocation("view"), 1, false, value_ptr(view)); + viewPort->bindView(*i,lookat->getPosition()+vec3(0.0f,1.0f,0.0f)); + //(*i)->useShader(); + //glUniformMatrix4fv((*i)->getUniformLocation("projection"), 1, false, value_ptr(projection)); + //glUniformMatrix4fv((*i)->getUniformLocation("view"), 1, false, value_ptr(view)); } @@ -232,10 +209,10 @@ void Scene::updateProjection(mat4 _projection) projection = _projection * projection; }*/ -void Scene::setViewPort(unsigned int x, unsigned int y, unsigned int width, unsigned int height) -{ - viewPort = new ViewPort(x, y, width, height); -} +//void Scene::setViewPort(unsigned int x, unsigned int y, unsigned int width, unsigned int height) +//{ +// viewPort = new ViewPort(x, y, width, height); +//} void Scene::setLookAt(SceneObject* _lookat) { diff --git a/Weave/Graphix/Scene.h b/Weave/Graphix/Scene.h index cb4ca4a..5fe190c 100644 --- a/Weave/Graphix/Scene.h +++ b/Weave/Graphix/Scene.h @@ -33,7 +33,7 @@ public: SceneList* getSceneObjects(); - void setViewPort(unsigned int, unsigned int, unsigned int, unsigned int); +// void setViewPort(unsigned int, unsigned int, unsigned int, unsigned int); // void setProjection(mat4 _projection); void setProjection(float fovy, float aspect, float zNear, float zFar); diff --git a/Weave/Graphix/SceneObject.cpp b/Weave/Graphix/SceneObject.cpp index 0a3e0f1..1a33cab 100644 --- a/Weave/Graphix/SceneObject.cpp +++ b/Weave/Graphix/SceneObject.cpp @@ -25,19 +25,11 @@ SceneObject::SceneObject(Shader* _shader, mat4& _model, string _modelpath, strin Model(_modelpath), model(_model), shader(_shader), -speed(_speed), -matter(_matter), -atack(_atack), -spin(1.0f), -direction(_direction), -matterDiff(0), mainScene(NULL), collision_ignore(false), -isenemy(false), texture(nullptr) { modelID = _shader->getUniformLocation("model"); - model = scale(model, vec3(pow(matter,1./3))); if (texturepath!="") texture = new Texture(texturepath); @@ -53,40 +45,11 @@ SceneObject::~SceneObject() void SceneObject::update(float deltaT) { - //Direction & Speed - model = translate(deltaT * speed * direction)*model; - //Matter Difference - if (abs(matterDiff)>1e-3) - { - float nmatter = (deltaT * matterDiff * MODEL_contractionspeed + matter); - //cout << faktor << endl; - model = scale(model, vec3(pow(nmatter / matter, 1. / 3))); - matterDiff -= deltaT * matterDiff * MODEL_contractionspeed; - matter = nmatter; - } - else if (matterDiff != 0) - { - float nmatter = (matterDiff + matter); - model = scale(model, vec3(pow(nmatter / matter, 1. / 3))); - matterDiff = 0; - matter = nmatter; - } //model = scale(model, vec3(matter)); //model = (deltaT * spin) * model; //TODO drehen scheint nicht zu funktionieren - // Im Ramen bleiben - vec3 pos = getPosition(); - if (fabs(pos.x) > BOX_size && direction.x*pos.x > 0){ - direction.x *= -1; - } - else if (fabs(pos.y) > BOX_size && direction.y*pos.y > 0){ - direction.y *= -1; - } - else if (fabs(pos.z) > BOX_size && direction.z*pos.z > 0){ - direction.z *= -1; - } } void SceneObject::draw() const @@ -111,57 +74,12 @@ void SceneObject::collisions(SceneObject* _other) if (collision_ignore || _other->collision_ignore) return; - float otherM = _other->getMatter(); - float diff = VektorAbs(getPosition() - _other->getPosition()) - pow(matter, 1. / 3) - pow(otherM, 1. / 3); - if (diff < 0) - { - float mass = matter - pow(pow(matter, 1. / 3) + diff / 2, 3) + otherM - pow(pow(otherM, 1. / 3) + diff / 2, 3); - - if (matter>_other->getMatter()) - { - _other->setMatter(otherM - mass); - setMatter(matter + mass); - } - else - { - _other->setMatter(otherM + mass); - setMatter(matter - mass); - } - } } vec3 SceneObject::getPosition() const{ return vec3(model[3]); } -vec3 SceneObject::push(vec3& _direction, float _matter) -{ - vec3 direct = normalize(vec3(model * vec4(_direction, 0.f))); - float dir_len = VektorAbs(_direction); - if (dir_len > 0) - { - //SceneObject* copy = this->copy(); - //mainScene->addObject(copy); - //copy->setModel(translate(getPosition() - direct * (float)(1.25*pow(matter*_matter, 1. / 3) + pow(matter, 1. / 3)))); - //copy->direction = direct * dir_len / -_matter; - //copy->setMatter(_matter*matter*.1); - //copy->matterDiff = _matter*matter*.9; - //copy->setEnemy(true); - - //delete copy->texture; - //copy->texture = new Texture("../Textures/enemy.png"); - - direction += direct * dir_len; - matterDiff -= _matter * matter; - } - return direct; -} - -void SceneObject::stop() -{ - push(vec3(inverse(model)*vec4(-direction,0))); - direction = vec3(0.f); -} void SceneObject::setModel(mat4& _model){ model = _model; @@ -174,11 +92,11 @@ void SceneObject::turn(float degree, vec3& axis){ model = model * rotate(degree, axis); } -void SceneObject::setDirection(vec3& _direction) -{ - direction = _direction; +void SceneObject::move(vec3& dist){ + model = model * translate(dist); } + void SceneObject::setCollision(bool _col) { collision_ignore = !_col; @@ -197,56 +115,13 @@ void SceneObject::setCollision(bool _col) // -float SceneObject::getMatter() -{ - return matter; -} - void SceneObject::setMainScene(Scene* _scene) { mainScene = _scene; } -void SceneObject::setMatter(float _matter) -{ - model = scale(model, vec3(pow(_matter / matter,1./3))); - matter = _matter; -} - - -void SceneObject::setTextureSave(string texture) -{ - default_save = new Texture(texture); -} -void SceneObject::setTextureDanger(string texture) -{ - default_danger = new Texture(texture); -} - -void SceneObject::useTextureSave() -{ - texture = default_save; -} -void SceneObject::useTextureDanger() -{ - texture = default_danger; -} - -void SceneObject::setEnemy(bool _isenemy) -{ - isenemy = _isenemy; -} Shader* SceneObject::getShader() { return shader; } - -bool SceneObject::isEnemy() -{ - return isenemy; -} - -Texture* SceneObject::default_danger = nullptr; -Texture* SceneObject::default_save = nullptr; - diff --git a/Weave/Graphix/SceneObject.h b/Weave/Graphix/SceneObject.h index 5cd58c1..bb0c990 100644 --- a/Weave/Graphix/SceneObject.h +++ b/Weave/Graphix/SceneObject.h @@ -31,29 +31,17 @@ public: virtual void collisions(SceneObject*); virtual vec3 getPosition() const; - virtual void setDirection(vec3&); - - virtual vec3 push(vec3&, float = 0.1f); - - virtual void stop(); virtual void setModel(mat4&); - virtual void turn(float, vec3&); + virtual void turn(float degree, vec3& axis); + virtual void move(vec3& dist); //virtual bool operator==(SceneObject); void setMainScene(Scene *); - float getMatter(); - void setCollision(bool); - static void setTextureSave(string texture); - static void setTextureDanger(string texture); - - void useTextureSave(); - void useTextureDanger(); - Shader* getShader(); void setEnemy(bool isenemy); @@ -72,26 +60,7 @@ protected: bool collision_ignore; - //virtual SceneObject* copy(); - - float matter; - float speed; - float atack; - - float matterDiff; - - vec3 direction; - mat4 spin; - Texture* texture; - bool isenemy; - -private: - - static Texture* default_danger; - static Texture* default_save; - - }; diff --git a/Weave/Graphix/ViewPort.cpp b/Weave/Graphix/ViewPort.cpp index 4791e42..ab0bcd7 100644 --- a/Weave/Graphix/ViewPort.cpp +++ b/Weave/Graphix/ViewPort.cpp @@ -1,10 +1,25 @@ #include "ViewPort.h" #include +#include "GLM.h" +#include "Shader.h" -ViewPort::ViewPort(unsigned int _x, unsigned int _y, unsigned int _width, unsigned int _height) : xpos(_x), ypos(_y), width(_width), height(_height) +#define VIEW_TOP_LIM 60.f +#define VIEW_BOT_LIM -30.f + + +ViewPort::ViewPort(unsigned int _x, unsigned int _y, unsigned int _width, unsigned int _height, float fovy, float zNear, float zFar) : + xpos(_x), + ypos(_y), + width(_width), + height(_height), + projection(perspective(fovy, (float)width / height, zNear, zFar)), + view_angle_x(0), + view_angle_y(0), + view_dist(2) { + rotateView(0.f, 0.f); } @@ -17,6 +32,38 @@ void ViewPort::useViewPort() const glViewport(xpos, ypos, width, height); } +void ViewPort::bindView(Shader* shader) const{ + shader->useShader(); + glUniformMatrix4fv(shader->getUniformLocation("projection"), 1, false, value_ptr(projection)); + glUniformMatrix4fv(shader->getUniformLocation("view"), 1, false, value_ptr(view)); +} + +void ViewPort::bindView(Shader* shader, vec3 pos) const{ + shader->useShader(); + glUniformMatrix4fv(shader->getUniformLocation("projection"), 1, false, value_ptr(projection)); + glUniformMatrix4fv(shader->getUniformLocation("view"), 1, false, value_ptr(translate(view,-pos))); +} + +void ViewPort::rotateView(float angle_x, float angle_y){ + + if (angle_y || angle_x){ + + view_angle_x += angle_x; + view_angle_y += angle_y; + + //Y-Limits + if (view_angle_y > VIEW_TOP_LIM) + view_angle_y = VIEW_TOP_LIM; + + if (view_angle_y < VIEW_BOT_LIM) + view_angle_y = VIEW_BOT_LIM; + + view = translate(vec3(0.f, 0.f, -view_dist))*rotate(view_angle_y / 50, vec3(1.f, 0.f, 0.f))*rotate(view_angle_x / 50, vec3(0.f, 1.f, 0.f)); + + } + +} + unsigned int ViewPort::getXPos() const { return xpos; diff --git a/Weave/Graphix/ViewPort.h b/Weave/Graphix/ViewPort.h index efc53a3..cf6e108 100644 --- a/Weave/Graphix/ViewPort.h +++ b/Weave/Graphix/ViewPort.h @@ -1,15 +1,23 @@ #pragma once -#include "Drawable.h" +//#include "Drawable.h" +#include "GLM.h" + +class Shader; class ViewPort { public: - ViewPort(unsigned int, unsigned int, unsigned int, unsigned int); + ViewPort(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar); ~ViewPort(); void useViewPort() const; + void bindView(Shader* shader) const; + void bindView(Shader* shader, vec3 lookat) const; + + void rotateView(float angle_x, float angle_y); + unsigned int getXPos() const; unsigned int getYPos() const; unsigned int getWidth() const; @@ -25,5 +33,12 @@ protected: unsigned int xpos; unsigned int ypos; + mat4 projection; + mat4 view; + + float view_dist; + float view_angle_x; + float view_angle_y; + };