]> git.leopard-lacewing.eu Git - cgue_weave.git/commitdiff
corrected CameraView and ModelMove (X,Z)
authorPeter Schaefer <schaeferpm@gmail.com>
Sat, 4 Apr 2015 12:39:31 +0000 (14:39 +0200)
committerPeter Schaefer <schaeferpm@gmail.com>
Sat, 4 Apr 2015 12:39:31 +0000 (14:39 +0200)
Weave/Events.cpp
Weave/Game.cpp
Weave/Graphix/Scene.cpp
Weave/Graphix/Scene.h
Weave/Graphix/SceneObject.cpp
Weave/Graphix/SceneObject.h
Weave/Graphix/ViewPort.cpp
Weave/Graphix/ViewPort.h

index e7e6ea3423b09e80509e555b6516742d0da1b130..fe6d5627a5a9d33922d5741da6c1535564d09373 100644 (file)
@@ -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()
index 4199dd303ae355dee98d5ba58b282e39b89d0dfe..150f0a81f87072f066fad6bee335b3804bc31e36 100644 (file)
@@ -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"));
 }
index 6e441d7a675a64880aeda7c902c335853957442e..19c2d34999136acc17496f1fdfbbdccf46cca75d 100644 (file)
@@ -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<SceneObject*>), 
@@ -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)
 {
index cb4ca4a503bd3ae8d9782d52a390a28af696c19e..5fe190c33ef25a07171140eddc5fa7609b4cf550 100644 (file)
@@ -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);
index 0a3e0f1d3da629b37a1165db93f0276adf7fe483..1a33cabedccc31a40d083729246a5d70bad643e7 100644 (file)
@@ -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;
-
index 5cd58c10dede4cd4f3eded27c483b8d9903b4d76..bb0c990129c031dcff2c4e5b9744caa17b1ad322 100644 (file)
@@ -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;
-
-
 };
 
index 4791e428063453b6e50b687803f5d693f784b2a5..ab0bcd7fbec1653284f9b95748091ec9db2a949c 100644 (file)
@@ -1,10 +1,25 @@
 #include "ViewPort.h"
 
 #include <GL\glew.h>
+#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;
index efc53a3d53cc3a85cd471e696917419aacc06da0..cf6e108ceececc497a0301cb72822ac461e8b4c0 100644 (file)
@@ -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;
+
 };