From 5d07aba7c4a5954598e57dc5d68154c7a4748b36 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 31 Mar 2019 23:35:07 +0200 Subject: [PATCH] Add some time usage statistics and minor cleanup and formatting --- src/program.cpp | 21 +++++++++++++++------ src/renderlogic.cpp | 24 ++++++++++++++---------- src/scene.cpp | 11 +++++------ src/utilities/timeutils.cpp | 12 +++++------- src/utilities/timeutils.h | 3 --- src/utilities/timeutils.hpp | 13 +++++++++++++ 6 files changed, 52 insertions(+), 32 deletions(-) delete mode 100644 src/utilities/timeutils.h create mode 100644 src/utilities/timeutils.hpp diff --git a/src/program.cpp b/src/program.cpp index 223d884..7e96ca4 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -6,14 +6,18 @@ // glm::translate, glm::rotate, glm::scale, glm::perspective #include #include +#include #include #include #include #include #include #include -#include +#include +using std::cout; +using std::endl; +using std::setprecision; void runProgram(GLFWwindow* window, CommandLineOptions options) { @@ -31,23 +35,28 @@ void runProgram(GLFWwindow* window, CommandLineOptions options) // Set default colour after clearing the colour buffer glClearColor(0.3f, 0.5f, 0.8f, 1.0f); - initRenderer(window, options); + int w, h; + glfwGetWindowSize(window, &w, &h); + + initRenderer(window, w, h); init_scene(options); - getTimeDeltaSeconds(); + Clock c, prof; // Rendering Loop while (!glfwWindowShouldClose(window)) { // Clear colour and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - int w, h; glfwGetWindowSize(window, &w, &h); - step_scene(getTimeDeltaSeconds()); + double td = c.getTimeDeltaSeconds(); + step_scene(td); + prof.getTimeDeltaSeconds(); updateFrame(window, w, h); + cout << "uf: " << setprecision(4) << prof.getTimeDeltaSeconds() / td * 100 << "\t"; renderFrame(window, w, h); + cout << "rf: " << setprecision(4) << prof.getTimeDeltaSeconds() / td * 100 << endl << endl; // Handle other events diff --git a/src/renderlogic.cpp b/src/renderlogic.cpp index ad419a0..32bb813 100644 --- a/src/renderlogic.cpp +++ b/src/renderlogic.cpp @@ -13,9 +13,14 @@ #include #include +#include + + using glm::vec3; using glm::vec4; using glm::mat4; +using std::cout; +using std::endl; typedef unsigned int uint; sf::Sound* sound; @@ -29,9 +34,9 @@ void mouse_callback(GLFWwindow* window, double x, double y) { double mx = (x - winw/2) / double(winh) * 2; // winh instead of winw, like the hudNode space double my = (winh/2 - y) / double(winh) * 2; - + bool reset_mouse = mouse_position_handler(mx, my, winh/2); - + if (reset_mouse) glfwSetCursorPos(window, winw/2, winh/2); if (reset_mouse != mouse_mode) { @@ -85,7 +90,7 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) { mat4 cameraTransform = glm::lookAt(cameraPosition, cameraLookAt, cameraUpward); - + // update scene with camera updateNodeTransformations(rootNode, mat4(1.0), cameraTransform, projection); @@ -93,10 +98,10 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) { // set orthographic VP for hud cameraTransform = mat4(1.0); projection = glm::ortho(-aspect, aspect, -1.0f, 1.0f); - + // update hud updateNodeTransformations(hudNode, mat4(1.0), cameraTransform, projection); - + // update spots for (SceneNode* node : lightNode) { if (node->nodeType == SPOT_LIGHT && node->spot_target) { @@ -105,7 +110,7 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) { - vec3(node->MV * vec4(0,0,0,1))); } } - + } // traverses and renders one and one node @@ -122,11 +127,11 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader, vectorlocation("light[" + std::to_string(id) + "]." #x) #define V(x) glUniform3fv(L(x), 1, glm::value_ptr(x)) @@ -233,9 +238,8 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader, vectorchildren) { + for(SceneNode* child : node->children) renderNode(child, node_shader, transparent_nodes, true); - } } // draw diff --git a/src/scene.cpp b/src/scene.cpp index 0cc8a98..00c008c 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -45,7 +45,6 @@ vector movingNodes; Gloom::Shader* default_shader; //Gloom::Shader* plain_shader; -//Gloom::Shader* post_shader; // todo: const the following: @@ -181,7 +180,7 @@ void init_scene(CommandLineOptions options) { lightNode[0]->position = {-600, 1400, 800}; lightNode[0]->position = {-600, 0, 800}; - lightNode[0]->attenuation = vec3(1.8, 0.0, 0.0); + lightNode[0]->attenuation = vec3(1.8, 0.0, 0.0); // the color of the first light affects the emissive component aswell //lightNode[0]->light_color = vec3(0.3, 0.3, 0.9); lightNode[0]->light_color = vec3(0.5, 0.5, 1.0); rootNode->children.push_back(lightNode[0]); @@ -265,9 +264,9 @@ void step_scene(double timeDelta) { float brh = DISPLACEMENT * (t_perlin.at_bilinear(br.x*3/1000, br.y*3/1000).x * 2 - 1); float blh = DISPLACEMENT * (t_perlin.at_bilinear(bl.x*3/1000, bl.y*3/1000).x * 2 - 1); - cout << o.x << " " << o.y << endl; - cout << frh << "\t" << flh << "\t" << blh << "\t" << brh << endl; - cout << ((frh+flh)-(brh+blh))/2 / 100 << endl; + //cout << o.x << " " << o.y << endl; + //cout << frh << "\t" << flh << "\t" << blh << "\t" << brh << endl; + //cout << ((frh+flh)-(brh+blh))/2 / 100 << endl; carNode->rotation.x = -glm::asin(((frh+flh)-(brh+blh)) / 2 / 100); carNode->rotation.y = glm::asin(((frh+brh)-(flh+blh)) / 2 / 60); diff --git a/src/utilities/timeutils.cpp b/src/utilities/timeutils.cpp index a51ed7e..e7d9493 100644 --- a/src/utilities/timeutils.cpp +++ b/src/utilities/timeutils.cpp @@ -1,12 +1,10 @@ -#include -#include "timeutils.h" +#include "timeutils.hpp" +Clock::Clock() { + _prev = std::chrono::steady_clock::now(); +} -// Calculates the elapsed time since the previous time this function was called. -double getTimeDeltaSeconds() { - static std::chrono::steady_clock::time_point _prev - = std::chrono::steady_clock::now(); - +double Clock::getTimeDeltaSeconds() { std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); diff --git a/src/utilities/timeutils.h b/src/utilities/timeutils.h deleted file mode 100644 index 6fa42ad..0000000 --- a/src/utilities/timeutils.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -double getTimeDeltaSeconds(); \ No newline at end of file diff --git a/src/utilities/timeutils.hpp b/src/utilities/timeutils.hpp new file mode 100644 index 0000000..a6491da --- /dev/null +++ b/src/utilities/timeutils.hpp @@ -0,0 +1,13 @@ +#pragma once +#include + +class Clock { +private: + std::chrono::steady_clock::time_point _prev; + +public: + Clock(); + + // Calculates the elapsed time since the previous time this function was called. + double getTimeDeltaSeconds(); +};