From 7f3a1909e7507e937d5d942286c5cb82c9bb52eb Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Thu, 16 Jun 2016 20:01:14 +0200 Subject: [PATCH] moved FPS to Graphix first Shader adjustments --- Weave/Events.cpp | 4 ++++ Weave/Fps.cpp | 2 +- Weave/Game.cpp | 24 +++--------------------- Weave/Graphix/Graphix.cpp | 28 ++++++++++++++++++---------- Weave/Graphix/Graphix.h | 9 +++++---- Weave/Scene/Scene.cpp | 30 ++++++++++++++++++++++++++---- shader/basicTexture_VS.hlsl | 6 ++++++ shader/lightingTexture_FS.hlsl | 4 ++++ 8 files changed, 67 insertions(+), 40 deletions(-) diff --git a/Weave/Events.cpp b/Weave/Events.cpp index 7c9eee3..121c2c4 100644 --- a/Weave/Events.cpp +++ b/Weave/Events.cpp @@ -113,6 +113,10 @@ void Events::KeyDown(int _key) break; case SDLK_F2: //FPS + if (key_toggle[SDLK_F2]) + Graphix::disableEffects(EF_FPS_OBJ); + else + Graphix::enableEffects(EF_FPS_OBJ); message("FPS", !key_toggle[SDLK_F2]); break; case SDLK_F3: diff --git a/Weave/Fps.cpp b/Weave/Fps.cpp index 06b76de..9bd7ece 100644 --- a/Weave/Fps.cpp +++ b/Weave/Fps.cpp @@ -3,7 +3,7 @@ #include "SDL2\SDL.h" #include "Average.h" -#define DELTA_CAP 100 +#define DELTA_CAP 50 /* Übernimmt die FPS berechnungen diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 5b60c14..b79d62f 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -90,7 +90,6 @@ void Game::play() { active = this; - Fps fps(0.3f); int sleep_time=0; float message_time=0; int framecount = 0; @@ -98,37 +97,20 @@ void Game::play() current_world->bindShader(); while (!Events::halt){ - fps.step(); + Graphix::fps.step(); #ifdef FRAMERATE_FIX - sleep_time += (int)(1000./ FRAMERATE_FIX - fps.getTicks()); + sleep_time += (int)(1000./ FRAMERATE_FIX - Graphix::fps.getTicks()); if (sleep_time < 0) sleep_time = 0; Sleep(sleep_time); #endif - if (Events::isKToggleActive(SDLK_F2)) - { - ++framecount; - message_time += fps.getDelta(); - if (message_time >= 0.5) - { - Graphix::updateFPS((float)framecount / message_time); - //Message::info((string)"Fps: " + std::to_string((float)framecount/message_time)); - message_time = 0; - framecount = 0; - } - - } - else - Graphix::updateFPS(-1); - - Events::processEvents(); //Message::info(std::to_string(fps.getDelta())); - update(fps.getCappedDelta()); + update(Graphix::fps.getCappedDelta()); Graphix::clear(); diff --git a/Weave/Graphix/Graphix.cpp b/Weave/Graphix/Graphix.cpp index 4595275..9faa62b 100644 --- a/Weave/Graphix/Graphix.cpp +++ b/Weave/Graphix/Graphix.cpp @@ -10,6 +10,9 @@ using std::string; #define MOTION_BLUR_COEFF 0.99f #define MOTION_BLUR_INTENS 0.99f +float fps_time = 99; +float fps_current; + void Graphix::setWindowSize(unsigned int _width, unsigned int _height) { width = _width; @@ -220,10 +223,20 @@ void Graphix::clear() string out = windowTitle; - if (fps > 0) - out += "(FPS:" + std::to_string(fps) + ")"; - if (obj >= 0) - out += "(NumObj:" + std::to_string(obj) + ")"; + if (testEffect(EF_FPS_OBJ)) + { + if (fps_time > .5f) + { + fps_current = fps.getAvFPS(); + fps_time = 0; + } + fps_time += fps.getDelta(); + + out += "(FPS:" + std::to_string((int)fps_current) + ")"; + if (obj >= 0) + out += "(NumObj:" + std::to_string(obj) + ")"; + } + SDL_SetWindowTitle(sdl_window, out.c_str()); } @@ -276,11 +289,6 @@ unsigned int Graphix::getGlError() return error; } -void Graphix::updateFPS(float _fps) -{ - fps = _fps; -} - void Graphix::updateNumObjects(int _obj) { obj = _obj; @@ -307,4 +315,4 @@ SDL_GLContext Graphix::sdl_glcontext; short Graphix::effects = EF_TRANSPARENCY | EF_BLOOM | EF_FRUSTUM_CULLING; int Graphix::obj = -1; -float Graphix::fps = -1; +Fps Graphix::fps(0.3f); diff --git a/Weave/Graphix/Graphix.h b/Weave/Graphix/Graphix.h index 5311c91..12018ea 100644 --- a/Weave/Graphix/Graphix.h +++ b/Weave/Graphix/Graphix.h @@ -4,6 +4,7 @@ #include #include +#include "../Fps.h" #define BIT(x) (1<<(x)) @@ -13,7 +14,8 @@ enum Effects { EF_TRANSPARENCY = BIT(2), EF_BLOOM = BIT(3), EF_FRUSTUM_CULLING = BIT(4), - EF_MOTION_BLUR = BIT(5) + EF_MOTION_BLUR = BIT(5), + EF_FPS_OBJ = BIT(6) }; class Graphix @@ -24,7 +26,6 @@ public: static unsigned int getWindowHeight(); static unsigned int getWindowWidth(); - static void updateFPS(float fps); static void updateNumObjects(int obj); static void FullScreen(bool enable, bool keepDesktopResolution = true); @@ -45,6 +46,8 @@ public: static unsigned int getGlError(); + static Fps fps; + private: static void updateEffects(); @@ -64,7 +67,5 @@ private: static short effects; static int obj; - static float fps; - }; diff --git a/Weave/Scene/Scene.cpp b/Weave/Scene/Scene.cpp index 5f202c8..c0ad8c6 100644 --- a/Weave/Scene/Scene.cpp +++ b/Weave/Scene/Scene.cpp @@ -143,6 +143,15 @@ Scene::~Scene() void Scene::update(float deltaT) { + /* Save Old Mat's*/ + if (Graphix::testEffect(EF_MOTION_BLUR)) { + camera.saveProjView(); + for (auto i = SceneObjects.begin(); i != SceneObjects.end(); ++i) + { + (*i)->saveModelMat(); + } + } + // XYAchse um den Player //float rotatex = 0.002f*Events::getViewX(); //float rotatey = 0.001f*Events::getViewY(); @@ -244,7 +253,6 @@ void Scene::draw() const } - //Directional Light Shadow glCullFace(GL_FRONT); glEnable(GL_POLYGON_OFFSET_FILL); @@ -260,6 +268,8 @@ void Scene::draw() const glCullFace(GL_BACK); glDisable(GL_POLYGON_OFFSET_FILL); + + /* Prepere DRAW SCENE */ camera.useCamera(); render->useBuffer(); dirLight->updateDirLightView(); @@ -281,15 +291,27 @@ void Scene::draw() const fBufferObject::clearBuffer(); } - /*DRAW SCENE*/ + /*DRAW SCENE -> SkyBox*/ 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(); + Shader::getShader(SH_LIGHTING)->useShader(); + /* MOTION BLUR */ + if (Graphix::testEffect(EF_MOTION_BLUR)) + { + camera.useOldProjView(); + Shader::getShader(SH_ACTIVE)->setUniformLocation("uDelta",Graphix::fps.getCappedDelta()); + //upload deltaT + drawSceneObjects(DRAW_ModelMotionBlur); + } + else + { + drawSceneObjects(); + } + //Pointlights & their shadows //for (auto i = pointlights.begin(); i != pointlights.end(); ++i) //{ diff --git a/shader/basicTexture_VS.hlsl b/shader/basicTexture_VS.hlsl index 86752b1..07a1b7f 100644 --- a/shader/basicTexture_VS.hlsl +++ b/shader/basicTexture_VS.hlsl @@ -9,6 +9,8 @@ out vec2 fUVs; out vec3 PointLightPosition1, DirectionalLightDirection1; +out mat4 new2old; + //out vec3 fPosition; out vec4 DirLightSpacePos; @@ -18,11 +20,15 @@ uniform vec3 DirectionalLightDirection; uniform mat4 uProjection, uView, uModel=mat4(1.f); uniform mat4 dirLightProj, dirLightView; +uniform mat4 uOldProjView, uInvProjView, uCur2OldModel; + uniform int uEnableBloom = 1; uniform int uEnableTransp = 1; void main() { + new2old = uOldProjView * uCur2OldModel * uInvProjView; + PointLightPosition1 = (uView * vec4(0.0f, 1.0f, 0.0f, 1.f)).xyz; DirectionalLightDirection1 = normalize((uView * vec4(DirectionalLightDirection, 0.0f)).xyz); diff --git a/shader/lightingTexture_FS.hlsl b/shader/lightingTexture_FS.hlsl index f6921f5..2e9bb76 100644 --- a/shader/lightingTexture_FS.hlsl +++ b/shader/lightingTexture_FS.hlsl @@ -5,6 +5,8 @@ in vec2 fUVs; in vec3 PointLightPosition1, DirectionalLightDirection1; +in mat4 new2old; + //in vec3 fPosition; in vec4 DirLightSpacePos; @@ -17,6 +19,8 @@ uniform vec4 material; //vec4 in the form (ambient, point, directional, glos uniform vec3 dirLightColor; uniform vec3 AmbientLightColor; +uniform float uDelta; + float DirLightCalcShadowFactor(vec4 LightSpacePos) { vec3 pos = LightSpacePos.xyz / LightSpacePos.w; -- 2.47.3