From 717472d795c6fd101d167a1a920ea9b2c8903e65 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Mon, 2 May 2016 12:13:17 +0200 Subject: [PATCH] =?utf8?q?removed=20ViewPort=20new=20Camera=20Class=20bugF?= =?utf8?q?ix=20(Rotierende=20Kamera=20bei=20Fenster=20=C3=84nderungen)=20P?= =?utf8?q?rojection=20only=20updated=20after=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit preps for Frustum Culling --- Weave/Events.cpp | 11 ++ Weave/GLM.h | 7 +- Weave/Game.cpp | 20 +++- Weave/Game.h | 3 + Weave/Graphix/Graphix.cpp | 4 + Weave/Graphix/Model/BBox.cpp | 1 + Weave/Graphix/Model/IMetaMesh.cpp | 9 ++ Weave/Graphix/Model/Model.cpp | 179 ++++++++++++++++++++++++++++++ Weave/Graphix/Model/Model.h | 41 ++++++- Weave/Graphix/ViewPort.cpp | 117 ------------------- Weave/Graphix/ViewPort.h | 51 --------- Weave/Scene/Camera.cpp | 128 +++++++++++++++++++++ Weave/Scene/Camera.h | 51 +++++++++ Weave/Scene/Marvin.cpp | 13 ++- Weave/Scene/Scene.cpp | 74 +++++++----- Weave/Scene/Scene.h | 15 ++- Weave/Scene/SceneObject.h | 1 - Weave/Scene/Timestamp.cpp | 4 +- Weave/Scene/Timestamp.h | 4 +- Weave/Weave.vcxproj | 4 +- Weave/Weave.vcxproj.filters | 12 +- 21 files changed, 518 insertions(+), 231 deletions(-) delete mode 100644 Weave/Graphix/ViewPort.cpp delete mode 100644 Weave/Graphix/ViewPort.h create mode 100644 Weave/Scene/Camera.cpp create mode 100644 Weave/Scene/Camera.h diff --git a/Weave/Events.cpp b/Weave/Events.cpp index e19a331..d294895 100644 --- a/Weave/Events.cpp +++ b/Weave/Events.cpp @@ -2,6 +2,7 @@ #include #include "Graphix\Graphix.h" +#include "Game.h" #include "GLM.h" @@ -15,6 +16,7 @@ using std::endl; void Events::processEvents() { SDL_Event sdl_event; + bool focus_gained = false; while (SDL_PollEvent(&sdl_event)){ //cout << sdl_event.type << endl; switch (sdl_event.type){ @@ -28,6 +30,7 @@ void Events::processEvents() case SDL_WINDOWEVENT_FOCUS_GAINED: window_focus = true; Graphix::hideMouse(true); + focus_gained = true; break; case SDL_WINDOWEVENT_FOCUS_LOST: window_focus = false; @@ -35,6 +38,7 @@ void Events::processEvents() break; case SDL_WINDOWEVENT_RESIZED: Graphix::setWindowSize(sdl_event.window.data1, sdl_event.window.data2); + Game::updateActiveView(); break; default: break; @@ -73,6 +77,13 @@ void Events::processEvents() break; } } + + if (focus_gained) + { + view_x = 0; + view_y = 0; + } + keymotion(); } diff --git a/Weave/GLM.h b/Weave/GLM.h index 5617289..61a23de 100644 --- a/Weave/GLM.h +++ b/Weave/GLM.h @@ -15,9 +15,10 @@ using glm::translate; using glm::rotate; //using glm::scale; using glm::determinant; -using glm::perspective; +//using glm::perspective; +//using glm::lookAt; using glm::normalize; -using glm::inverse; +//using glm::inverse; using glm::transpose; using glm::dot; using glm::cross; @@ -43,7 +44,7 @@ typedef aiMatrix4x4t aiMatrix4x4; #define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */ #define M_D_R 57.2957795131 /* 180/pi */ -__declspec(deprecated) +__declspec(deprecated("Use glm::lenght(vec3)")) float VektorAbs(const vec3& vek); mat4 removeScale(const mat4& matrix); diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 4071729..6001e41 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -87,12 +87,13 @@ Game::~Game() void Game::play() { + active = this; + Fps fps(0.3f); int sleep_time=0; float message_time=0; int framecount = 0; - current_world->bindShader(); while (!Events::halt){ @@ -137,14 +138,19 @@ void Game::play() } } +void Game::updateActiveView() +{ + active->current_world->setView(Graphix::getWindowWidth(), Graphix::getWindowHeight()); +} + void Game::update(float deltaT) { /* ADJUST WINDOWS SIZE*/ - if (Graphix::getWindowHeight() != current_world->getViewport().getHeight() || Graphix::getWindowWidth() != current_world->getViewport().getWidth()) - { - current_world->setView(0, 0, Graphix::getWindowWidth(), Graphix::getWindowHeight()); + //if (Graphix::getWindowHeight() != current_world->getViewport().getHeight() || Graphix::getWindowWidth() != current_world->getViewport().getWidth()) + //{ + // current_world->setView(0, 0, Graphix::getWindowWidth(), Graphix::getWindowHeight()); - } + //} if (playing){ current_world->update(deltaT); @@ -169,4 +175,6 @@ void Game::draw() const //else // l_menu->draw(); -} \ No newline at end of file +} + +Game* Game::active = nullptr; \ No newline at end of file diff --git a/Weave/Game.h b/Weave/Game.h index 1e584f1..863a67f 100644 --- a/Weave/Game.h +++ b/Weave/Game.h @@ -17,6 +17,7 @@ public: //void loadLVL(); + static void updateActiveView(); private: @@ -29,5 +30,7 @@ private: void update(float); void draw() const; + static Game* active; + }; diff --git a/Weave/Graphix/Graphix.cpp b/Weave/Graphix/Graphix.cpp index d6d9e09..4f81869 100644 --- a/Weave/Graphix/Graphix.cpp +++ b/Weave/Graphix/Graphix.cpp @@ -14,6 +14,8 @@ void Graphix::setWindowSize(unsigned int _width, unsigned int _height) { width = _width; height = _height; + + glViewport(0, 0, width, height); } void Graphix::setWindowPos(unsigned int _xpos, unsigned int _ypos) @@ -148,6 +150,8 @@ void Graphix::init() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearAccum(0.f, 0.f, 0.f, 0.f); glClear(GL_ACCUM_BUFFER_BIT); + + glViewport(0, 0, width, height); } void Graphix::FullScreen(bool _enable, bool _keepDesktopResolution) diff --git a/Weave/Graphix/Model/BBox.cpp b/Weave/Graphix/Model/BBox.cpp index cd5ef1e..e109364 100644 --- a/Weave/Graphix/Model/BBox.cpp +++ b/Weave/Graphix/Model/BBox.cpp @@ -15,6 +15,7 @@ BBox::BBox() //bindModel(); bt_init(); + box_init(); name = "BBox"; diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index 6d6b8f3..a405820 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -45,6 +45,12 @@ IMetaMesh::IMetaMesh(const string& _modelpath, const vec3& _scale, bool _isConve tmpIMesh = new IMesh(scene->mMeshes[i], _scale* getScale(tmpModelMat)); models.push_back(std::pair(tmpIMesh, tmpModelMatClean)); + tmpIMesh->box_init(tmpModelMatClean); + + if (i == 0) + box = tmpIMesh->getBox(); + else + box.update(tmpIMesh->getBox()); } } @@ -212,10 +218,13 @@ void IMetaMesh::bt_init(bool _isConvex) //} + + string IMetaMesh::type2str() const { return "IMetaMesh"; } + // //void IMetaMesh::setMargin(btScalar _margin) //{ diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 68e07d7..9866b51 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -21,6 +21,8 @@ using namespace std; +typedef Model::Box Box; + Model::Model() { } @@ -435,6 +437,38 @@ bool Model::loadMesh(const aiMesh* mesh, const vec3& _scale) return true; } +void Model::box_init(const mat4& _model) +{ + if (vertex == nullptr) + return; + + vec3 tmp = vec3(_model * vec4(vertex[0], vertex[1], vertex[2],1.f)); + + vec3 min = tmp, max = tmp; + + for (uint i = 1; i < numvertices; i++) + { + tmp = vec3(_model * vec4(vertex[3 * i], vertex[3 * i + 1], vertex[3 * i + 2], 1.f)); + for (uint k = 0; k < 3; k++) + { + if (tmp[k] > max[k]) + max[k] = tmp[k]; + if (tmp[k] < min[k]) + min[k] = tmp[k]; + } + } + + box = Box((max + min) * .5f, (max - min) * .5f); +} + +Box Model::getBox() +{ + if (box.size() == vec3(0.f)) + box_init(); + return box; +} + + Model::operator btCollisionShape*() const { return bt_collision_shape; @@ -474,3 +508,148 @@ Model* Model::PlaneModel = nullptr; Model* Model::SkyBoxModel = nullptr; std::unordered_map Model::IMetaModel; std::unordered_map Model::IMetaModel_count; + + +//CLASS BOX + +Box::Box() : cen(0.f), siz(0.f), rad(0.f) +{} + +Model::Box::Box(const unsigned int num, const float * verts) +{ +} + +Box::Box(const vec3& _center, const vec3& _size) : cen(_center), siz(_size), rad(length(_size)) +{ +} + +Box::~Box() +{ +} + +void Box::update(const Box& _box) +{ + vec3 diff = _box.cen - cen; + float size; + + for (int i = 0; i<3; ++i) + { + if (abs(siz[i] - _box.siz[i]) - abs(diff[i]) >= 0) + { + if (siz[i] < _box.siz[i]) + { + siz[i] = _box.siz[i]; + cen[i] = _box.cen[i]; + } + } + else + { + size = (siz[i] + _box.siz[i] + abs(diff[i])) * .5f; + cen[i] = cen[i] - sign(diff[i]) * (siz[i] - size); + siz[i] = size; + } + + } + rad = length(siz); +} + + +void Box::getBBcs(vec3& _center, vec3& _size) const +{ + _center = cen; + _size = siz; +} + +vec3 Box::center() const +{ + return cen; +} + +vec3 Box::size() const +{ + return siz; +} + +float Box::radius() const +{ + return rad; +} + +void Box::rotate(const mat4& _modelMat) +{ + //Rotate Center + cen = (vec3)(_modelMat*vec4(cen, 1.f)); + + //Rotate Size + vec3 newSize(0.f); + mat3 tmpModelMat(_modelMat); + for (auto i = 0; i < 3; ++i) + newSize += abs(tmpModelMat[i]) * siz[i]; + siz = newSize; + rad = length(siz); +} + +void Box::scale(const vec3& _scale) +{ + for (uint i = 0; i < 3; ++i) + { + cen[i] *= _scale[i]; + siz[i] *= _scale[i]; + } + + rad = length(siz); +} + +float Box::getDist(const vec3& _pos) const +{ + vec3 diff = _pos - cen; + vec3 direc = normalize(diff); + + float size(0.f); + for (auto i = 0; i < 3; ++i) + size += abs(siz[i] * direc[i]); + return diff.length() - size; +} + +float Box::getDist(const vec3& _pos, const vec3& _direc) const +{ + vec3 diff = _pos - cen; + + float size(0.f); + for (auto i = 0; i < 3; ++i) + size += abs(siz[i] * _direc[i]); + return dot(diff, _direc) - size; +} + + +Box Box::merge(const Box& _boxA, const Box& _boxB) +{ + vec3 diff = _boxB.cen - _boxA.cen; + vec3 size; + vec3 center; + + for (int i = 0; i<3; ++i) + { + if (abs(_boxA.siz[i] - _boxB.siz[i]) - abs(diff[i]) >= 0) + { + if (_boxA.siz[i] > _boxB.siz[i]) + { + size[i] = _boxA.siz[i]; + center[i] = _boxA.cen[i]; + } + else + { + size[i] = _boxB.siz[i]; + center[i] = _boxB.cen[i]; + } + } + else + { + size[i] = (_boxA.siz[i] + _boxB.siz[i] + abs(diff[i])) * .5f; + center[i] = _boxA.cen[i] - sign(diff[i]) * (_boxA.siz[i] - size[i]); + } + + } + return Box(center, size); +} + diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index 5d43284..60daeff 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -22,6 +22,8 @@ class Model { public: + class Box; + /* Binds Model to GL*/ virtual void bindModel(); virtual void unbindModel(); @@ -55,10 +57,43 @@ public: virtual void bt_init(bool _convex = true); virtual void bt_setMargin(btScalar margin); + virtual void box_init(const mat4& modelMat = mat4(1.f)); + virtual Box getBox(); + // Mesh Speichern? bool loadMesh(const std::string& modelpath, uint mindex = 0); bool loadMesh(const aiMesh* mesh, const vec3& scale = vec3(1.f)); + class Box + { + public: + Box(); + Box(const unsigned int num, const float* verts); + Box(const vec3& center, const vec3& size); + ~Box(); + + void update(const Box& box); + + void getBBcs(vec3& center, vec3& size) const; + + vec3 center() const; + vec3 size() const; + float radius() const; + + void rotate(const mat4& modelMat); + void scale(const vec3& scale); + + float getDist(const vec3& pos) const; + float getDist(const vec3& pos, const vec3& direc) const; + + static Box merge(const Box& boxA, const Box& boxB); + + protected: + vec3 cen; + vec3 siz; + float rad; + }; + protected: Model(); virtual ~Model(); @@ -71,8 +106,10 @@ protected: float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; uint *index = nullptr; - vec3 center; - float radius; + //vec3 center = vec3(0.f), size = vec3(0.f); + //float radius = 0.f; + + Box box; std::unordered_map shader_map; diff --git a/Weave/Graphix/ViewPort.cpp b/Weave/Graphix/ViewPort.cpp deleted file mode 100644 index 8b93506..0000000 --- a/Weave/Graphix/ViewPort.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "ViewPort.h" - -#include -#include "../GLM.h" - -#include "Shader.h" - -#define VIEW_TOP_LIM 0.9f -#define VIEW_BOT_LIM -0.3f - - -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), - fovy(_fovy), - zNear(_zNear), - zFar(_zFar), - projection(perspective(fovy, (float)width / height, zNear, zFar)), - view_angle_x(0), - view_angle_y(0), - view_dist(2), - view(0.f), - shadow_dist(2) -{ - rotateView(0.f, -.5f); -} - - -ViewPort::~ViewPort() -{ -} - -void ViewPort::useViewPort() const -{ - glViewport(xpos, ypos, width, height); -} - -void ViewPort::bindView(const Shader* shader) const{ - shader->useShader(); - - shader->setUniformLocation("uProjection", projection); - shader->setUniformLocation("uInvProjection", glm::inverse(projection)); - shader->setUniformLocation("uView", view); - -} - -void ViewPort::bindView(const Shader* shader, vec3 pos) const{ - shader->useShader(); - - shader->setUniformLocation("uProjection", projection); - shader->setUniformLocation("uInvProjection", glm::inverse(projection)); - shader->setUniformLocation("uView", translate(view, -pos)); -} - -void ViewPort::bindViewShadowDir(const Shader* shader) const { - shader->useShader(); - - shader->setUniformLocation("uProjection", glm::mat4(1 / shadow_dist)); - shader->setUniformLocation("uInvProjection", glm::mat4(shadow_dist)); - //TODO the real thing - shader->setUniformLocation("uView", view); -} - -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 *(float)M_PI_2, vec3(1.f, 0.f, 0.f))*rotate(view_angle_x * (float)M_PI_2, vec3(0.f, 1.f, 0.f)); - } - -} - -vec3 ViewPort::rotateDirection(const vec3 & direction) const{ - return glm::rotateY(direction, -view_angle_x * (float)M_PI_2); -} - -unsigned int ViewPort::getXPos() const -{ - return xpos; -} - -unsigned int ViewPort::getYPos() const -{ - return ypos; -} - -unsigned int ViewPort::getWidth() const -{ - return width; -} - -unsigned int ViewPort::getHeight() const -{ - return height; -} - -void ViewPort::setView(uint _x, uint _y, uint _width, uint _height, bool _updateProjection) -{ - xpos = _x; - ypos = _y; - width = _width; - height = _height; - if (_updateProjection) - projection = perspective(fovy, (float)width / height, zNear, zFar); -} diff --git a/Weave/Graphix/ViewPort.h b/Weave/Graphix/ViewPort.h deleted file mode 100644 index 62c02a6..0000000 --- a/Weave/Graphix/ViewPort.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -//#include "Drawable.h" -#include "../GLM.h" - -class Shader; - -class ViewPort -{ -public: - ViewPort(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar); - ~ViewPort(); - - void useViewPort() const; - - void setView(unsigned int x, unsigned int y, unsigned int width, unsigned int height, bool updateProjection = true); - - void bindView(const Shader* shader) const; - void bindView(const Shader* shader, vec3 lookat) const; - void bindViewShadowDir(const Shader* shader) const; - - void rotateView(float angle_x, float angle_y); - vec3 rotateDirection(const vec3 & direction) const; - - unsigned int getXPos() const; - unsigned int getYPos() const; - unsigned int getWidth() const; - unsigned int getHeight() const; - -// Drawable* content; - - -protected: - unsigned int width; - unsigned int height; - - unsigned int xpos; - unsigned int ypos; - - float fovy, zNear, zFar; - - mat4 projection; - mat4 view; - - float view_dist; - float view_angle_x; - float view_angle_y; - - float shadow_dist; -}; - diff --git a/Weave/Scene/Camera.cpp b/Weave/Scene/Camera.cpp new file mode 100644 index 0000000..3c18120 --- /dev/null +++ b/Weave/Scene/Camera.cpp @@ -0,0 +1,128 @@ +#include "Camera.h" + +#include "../Graphix/Shader.h" + +using glm::lookAt; +using glm::perspective; +using glm::inverse; + +#define VIEW_PITCH_MAX 0.3f +#define VIEW_PITCH_MIN -0.9f + +#define WORLD_UP vec3(0.f, 1.f, 0.f) + +Camera::Camera(float _yFov, float _xyRatio, float _zNear, float _zFar) : + yFov(_yFov), + xyRatio(_xyRatio), + zNear(_zNear), + zFar(_zFar), + projection(perspective(yFov, xyRatio, zNear, zFar)), + pitch(0.f), + yaw(0.f), + dist(2.f), //DIST + cPosition(0.f) +{ + updateVectors(); +} + + +Camera::~Camera() +{ +} + +void Camera::useCamera() const +{ + useProjection(); + useView(); +} + +void Camera::useView() const +{ + Shader::getShader(SH_ACTIVE)->setUniformLocation("uView", lookAt(cPosition - dist* cFront,cPosition, cUpVector)); +} + +void Camera::useProjection() const +{ + Shader::getShader(SH_ACTIVE)->setUniformLocation("uProjection", projection); + Shader::getShader(SH_ACTIVE)->setUniformLocation("uInvProjection", glm::inverse(projection)); +} + +void Camera::updateView(const vec3& _target) +{ + + if (_target == cPosition) + return; + + cPosition = _target; +} + +void Camera::updateView_pitch(float _pitch) +{ + if (_pitch == 0) + return; + + float piTmp = pitch + _pitch; + + if (piTmp > VIEW_PITCH_MAX) + piTmp = VIEW_PITCH_MAX; + else if(piTmp < VIEW_PITCH_MIN) + piTmp = VIEW_PITCH_MIN; + + if (pitch == piTmp) + return; + + pitch = piTmp; + + updateVectors(); +} + +void Camera::updateView_yaw(float _yaw) +{ + if (_yaw == 0) + return; + + yaw += _yaw; + + updateVectors(); +} + + +void Camera::updateProjection(float _xyRatio) +{ + if (xyRatio == _xyRatio) + return; + + xyRatio = _xyRatio; + + projection = perspective(yFov, xyRatio, zNear, zFar); +} + +void Camera::updateProjection(float _yFov, float _xyRatio, float _zNear, float _zFar) +{ + if (yFov == _yFov && xyRatio == _xyRatio&&zNear == _zNear&&zFar == _zFar) + return; + + yFov = _yFov; + xyRatio = _xyRatio; + zNear = _zNear; + zFar = _zFar; + + projection = perspective(yFov, xyRatio, zNear, zFar); +} + +bool Camera::frustum_sphere(vec3 center, float radius) +{ + return false; +} + +void Camera::updateVectors() +{ + cFront.x = cos(yaw*(float)M_PI_2) * cos(pitch*(float)M_2_PI); + cFront.y = sin(pitch*(float)M_2_PI); + cFront.z = sin(yaw*(float)M_PI_2) * cos(pitch*(float)M_2_PI); + cFront = normalize(cFront); + + + cRight = normalize(cross(cFront, WORLD_UP)); + cUpVector = normalize(cross(cRight, cFront)); +} \ No newline at end of file diff --git a/Weave/Scene/Camera.h b/Weave/Scene/Camera.h new file mode 100644 index 0000000..418dd74 --- /dev/null +++ b/Weave/Scene/Camera.h @@ -0,0 +1,51 @@ +#pragma once + +#include "../GLM.h" + + +//TURN AROUND TARGET! +class Camera +{ +public: + Camera(float yFov, float xyRatio, float zNear, float zFar); + ~Camera(); + + + + /*use Camera (bind View&Projection at current Shader) if unchanged*/ + void useCamera() const; + + void useView() const; + void useProjection() const; + + void updateView(const vec3& target); + void updateView_pitch(float); + void updateView_yaw(float); + + void updateProjection(float xyRatio); + void updateProjection(float yFov, float xyRatio, float zNear, float zFar); + + vec3 getDirection() { return cFront;} + vec3 getRight() { return cRight; } + + vec3 getWorldDirection() { return normalize(vec3(cFront.x, 0.f, cFront.z)); } + vec3 getWorldRight() { return normalize(vec3(cRight.x, 0.f, cRight.z)); } + + bool frustum_sphere(vec3 center, float radius = 0.f); + + +protected: + + void updateVectors(); + + /*Projection*/ + float yFov, xyRatio, zNear, zFar; + mat4 projection = mat4(1.f); + + /*View*/ + vec3 cPosition, cFront, cUpVector, cRight; + float pitch, yaw; + float dist; + +}; + diff --git a/Weave/Scene/Marvin.cpp b/Weave/Scene/Marvin.cpp index ab03b84..0c86498 100644 --- a/Weave/Scene/Marvin.cpp +++ b/Weave/Scene/Marvin.cpp @@ -3,7 +3,7 @@ #include "../Graphix/Shader.h" #include "../GLM.h" #include "../Events.h" -#include "../Graphix/ViewPort.h" +#include "Camera.h" #include "Scene.h" #define TIME_TILL_MAX_MOVE 2 @@ -28,7 +28,8 @@ Marvin::~Marvin() void Marvin::update(float deltaT) { - ViewPort& viewPort(mainScene->getViewport()); + //ViewPort& viewPort(mainScene->getViewport()); + Camera& cam(mainScene->getCam()); @@ -40,7 +41,7 @@ void Marvin::update(float deltaT) int move_y = Events::getMoveY(); // MOVE Player if (move_x) - move(SPEED_MOVE_NORMAL *move_x * deltaT * viewPort.rotateDirection(vec3(-1.f, 0.f, 0.f))); + move(-SPEED_MOVE_NORMAL *move_x * deltaT * cam.getWorldRight()); if (move_y){ if (move_y > 0){ @@ -48,10 +49,10 @@ void Marvin::update(float deltaT) move_delta += deltaT; if (move_delta > TIME_TILL_MAX_MOVE) move_delta = TIME_TILL_MAX_MOVE; - move(SPEED_MOVE_NORMAL * (MOVE_FASTER * move_delta / TIME_TILL_MAX_MOVE + 1) *move_y * deltaT * viewPort.rotateDirection(vec3(0.f, 0.f, -1.f))); + move(SPEED_MOVE_NORMAL * (MOVE_FASTER * move_delta / TIME_TILL_MAX_MOVE + 1) *move_y * deltaT * cam.getWorldDirection()); } else - move(SPEED_MOVE_NORMAL *move_y * deltaT * viewPort.rotateDirection(vec3(0.f, 0.f, -1.f))); + move(SPEED_MOVE_NORMAL *move_y * deltaT * cam.getWorldDirection()); } else { @@ -60,7 +61,7 @@ void Marvin::update(float deltaT) //Rotate Play in move direction if (move_x || move_y) - turnTo(viewPort.rotateDirection(vec3(0.f, 0.f, -1.f)), 3 * deltaT / TIME_TILL_DIRECTION_ROTATE); + turnTo(cam.getWorldDirection(), 3 * deltaT / TIME_TILL_DIRECTION_ROTATE); SceneObject::update(deltaT); } diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index a39bbaa..5f45d92 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -1,13 +1,14 @@ #include "Scene.h" #include "SceneObject.h" -#include "Sky.h" +//#include "Sky.h" +//#include "Camera.h" #include -#include "../GLM.h" +//#include "../GLM.h" #include "../Graphix/Graphix.h" #include "../Graphix/Shader.h" -#include "../Graphix/ViewPort.h" +//#include "../Graphix/ViewPort.h" #include "../Graphix/Textures.h" #include "../Graphix/Model.h" @@ -20,6 +21,8 @@ #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) +ShaderTarget shader[] = { SH_BASIC,SH_BASICTEXTURE,SH_LIGHTING, SH_SHADOWDIR }; + using std::list; using std::set; @@ -33,12 +36,13 @@ using std::stack; #define YFALL_SPEED 9.8f 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(x, y, width, height, fovy, zNear, zFar), +//viewPort(x, y, width, height, fovy, zNear, zFar), +camera(fovy, (float)width/height, zNear, zFar), lookat(_lookat) { currenttime = 0.0f; if (lookat != nullptr) - timestamps.push(Timestamp(0.0f, _lookat->getModelMat(), x, lookat->ySpeed)); + timestamps.push(Timestamp(0.0f, _lookat->getModelMat(), lookat->ySpeed)); bt_collision_configuration = new btDefaultCollisionConfiguration(); bt_dispatcher = new btCollisionDispatcher(bt_collision_configuration); @@ -79,6 +83,12 @@ lookat(_lookat) fBufferObject::clearBuffer(); Graphix::disableEffects(EF_BLOOM); + + for (int s = 0; s < NELEMS(shader); s++) + { + Shader::getShader(shader[s])->useShader(); + camera.useCamera(); + } } @@ -121,9 +131,11 @@ Scene::~Scene() void Scene::update(float deltaT) { // XYAchse um den Player - float rotatex = 0.002f*Events::getViewX(); - float rotatey = 0.001f*Events::getViewY(); - viewPort.rotateView(rotatex, rotatey); + //float rotatex = 0.002f*Events::getViewX(); + //float rotatey = 0.001f*Events::getViewY(); + camera.updateView_pitch(0.001f*Events::getViewY()); + camera.updateView_yaw(-0.002f*Events::getViewX()); + //viewPort.rotateView(rotatex, rotatey); if (!Events::getTimeShift()) { @@ -131,7 +143,7 @@ void Scene::update(float deltaT) if (ceil((currenttime + deltaT) / timestep) > ceil(currenttime / timestep)) { - timestamps.push(Timestamp(currenttime, lookat->getModelMat(), viewPort.getXPos(), lookat->ySpeed)); + timestamps.push(Timestamp(currenttime, lookat->getModelMat(), lookat->ySpeed)); } currenttime += deltaT; @@ -164,7 +176,7 @@ void Scene::update(float deltaT) { float prev_time = timestamps.top().time; mat4 prev_mat = timestamps.top().Playermodel; - unsigned int prev_camera = timestamps.top().CameraX; + //unsigned int prev_camera = timestamps.top().CameraX; int prev_ySpeed = timestamps.top().ySpeed; while (max(0.0f, currenttime - deltaT) < prev_time) { @@ -172,7 +184,7 @@ void Scene::update(float deltaT) prev_time = timestamps.top().time; prev_mat = timestamps.top().Playermodel; - prev_camera = timestamps.top().CameraX; + //prev_camera = timestamps.top().CameraX; prev_ySpeed = timestamps.top().ySpeed; } @@ -192,21 +204,19 @@ void Scene::update(float deltaT) currenttime = max(0.0f, currenttime - deltaT); } + camera.updateView(lookat->getPosition()+vec3(0.f,1.f,0.f)); + } void Scene::draw() const { - //Skybox - viewPort.bindView(Shader::getShader(SH_BASICTEXTURE), lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); - //BBox - viewPort.bindView(Shader::getShader(SH_BASIC), lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); - //SceneObjects - viewPort.bindView(Shader::getShader(SH_LIGHTING), lookat->getPosition() + vec3(0.0f, 1.0f, 0.0f)); - //ShadowDir - viewPort.bindViewShadowDir(Shader::getShader(SH_SHADOWDIR)); - viewPort.useViewPort(); + for (int s = 0; s < NELEMS(shader); s++) + { + Shader::getShader(shader[s])->useShader(); + camera.useView(); + } /*Draw WireFrame ONLY*/ if (Events::isKToggleActive(SDLK_F3)) @@ -241,6 +251,8 @@ void Scene::draw() const /*DRAW SCENE*/ Shader::getShader(SH_BASICTEXTURE)->useShader(); SkyBox.draw(); + //Shader::getShader(SH_BASICTEXTURE)->useShader(); + //Shader::getShader(SH_BASICTEXTURE)->setUniformLocation("uFar", false); Shader::getShader(SH_LIGHTING)->useShader(); drawSceneObjects(); @@ -345,14 +357,22 @@ void Scene::setLookAt(SceneObject* _lookat) lookat = _lookat; } -ViewPort& Scene::getViewport() -{ - return viewPort; -} +//ViewPort& Scene::getViewport() +//{ +// return viewPort; +//} -void Scene::setView(unsigned int x, unsigned int y, unsigned int width, unsigned int height, bool updateProjection) +void Scene::setView(unsigned int width, unsigned int height, bool updateProjection) { - viewPort.setView(x, y, width, height, updateProjection); + if(updateProjection) + camera.updateProjection((float)width / height); + + for (int s = 0; s < NELEMS(shader); s++) + { + Shader::getShader(shader[s])->useShader(); + camera.useProjection(); + } + render->updateSize(width, height); postRender->updateSize(width, height); for (int i = 0; i < 2; i++) @@ -364,13 +384,13 @@ void Scene::setView(unsigned int x, unsigned int y, unsigned int width, unsigned void Scene::bindShader() { /*SceneObject*/ - ShaderTarget shader[] = {SH_BASIC,SH_BASICTEXTURE,SH_LIGHTING, SH_SHADOWDIR}; for (int s = 0; s < NELEMS(shader); s++) { for (auto i = SceneObjects.cbegin(); i != SceneObjects.cend(); ++i) (*i)->bindShader(Shader::getShader(shader[s])); } + /*Skybox*/ SkyBox.bindShader(Shader::getShader(SH_BASICTEXTURE)); diff --git a/Weave/Scene/Scene.h b/Weave/Scene/Scene.h index d873d18..63f7754 100644 --- a/Weave/Scene/Scene.h +++ b/Weave/Scene/Scene.h @@ -2,8 +2,8 @@ #include "../GLM.h" -#include "../Graphix/ViewPort.h" #include "Sky.h" +#include "Camera.h" #include "Timestamp.h" #include @@ -12,10 +12,11 @@ #include class SceneObject; -class ViewPort; -class Sky; +//class ViewPort; +//class Sky; class Shader; class fBufferObject; +//class Camera; class Scene { @@ -36,12 +37,14 @@ public: std::list* getSceneObjects(); void setLookAt(SceneObject* _lookat); - ViewPort& getViewport(); + //ViewPort& getViewport(); + Camera& getCam() { return camera; } - void setView(unsigned int x, unsigned int y, unsigned int width, unsigned int height, bool updateProjection = true); + void setView(unsigned int width, unsigned int height, bool updateProjection = true); protected: - ViewPort viewPort; + //ViewPort viewPort; + Camera camera; Sky SkyBox; SceneObject* lookat; diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index db16528..0232a66 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -44,7 +44,6 @@ public: virtual void draw(drawTarget target=DRAW_Model) const; virtual void bindShader(const Shader* shader); - virtual void bindShaderCollision(const Shader* shader); virtual void collides(SceneObject* other, btPersistentManifold* contactManifold, float deltaT=1.f); diff --git a/Weave/Scene/Timestamp.cpp b/Weave/Scene/Timestamp.cpp index e6d32e5..974861b 100644 --- a/Weave/Scene/Timestamp.cpp +++ b/Weave/Scene/Timestamp.cpp @@ -1,9 +1,9 @@ #include "Timestamp.h" -Timestamp::Timestamp(float _time, mat4& _Playermodel, unsigned int x, float _ySpeed) : +Timestamp::Timestamp(float _time, mat4& _Playermodel, float _ySpeed) : time(_time), Playermodel(_Playermodel), -CameraX(x), +//CameraX(x), ySpeed(_ySpeed) { diff --git a/Weave/Scene/Timestamp.h b/Weave/Scene/Timestamp.h index 88350df..26753e7 100644 --- a/Weave/Scene/Timestamp.h +++ b/Weave/Scene/Timestamp.h @@ -5,12 +5,12 @@ class Timestamp { public: - Timestamp(float _time, mat4& _Playermodel, unsigned int _Camera, float ySpeed); + Timestamp(float _time, mat4& _Playermodel, float ySpeed); virtual ~Timestamp(); float time; mat4 Playermodel; - unsigned int CameraX; + //unsigned int CameraX; float ySpeed; }; \ No newline at end of file diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index f6da46d..2497198 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -109,13 +109,13 @@ + - @@ -146,13 +146,13 @@ + - diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 820ddde..89e5f83 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -33,9 +33,6 @@ Source Files - - Source Files - Source Files @@ -102,6 +99,9 @@ Source Files + + Source Files + @@ -122,9 +122,6 @@ Header Files - - Header Files - Header Files @@ -206,5 +203,8 @@ Header Files + + Header Files + \ No newline at end of file -- 2.47.3