Add some time usage statistics and minor cleanup and formatting

This commit is contained in:
Peder Bergebakken Sundt 2019-03-31 23:35:07 +02:00
parent b29537273b
commit 5d07aba7c4
6 changed files with 52 additions and 32 deletions

View File

@ -6,14 +6,18 @@
// glm::translate, glm::rotate, glm::scale, glm::perspective
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <iomanip>
#include <SFML/Audio.hpp>
#include <SFML/System/Time.hpp>
#include <utilities/shapes.h>
#include <utilities/glutils.h>
#include <utilities/shader.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <utilities/timeutils.h>
#include <utilities/timeutils.hpp>
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

View File

@ -13,9 +13,14 @@
#include <utilities/glfont.h>
#include <utilities/shader.hpp>
#include <utilities/timeutils.hpp>
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, vector<NodeDistSh
vec3 position; // MV
vec3 attenuation;
vec3 color;
bool is_spot;
vec3 spot_direction; // MV, must be normalized
float spot_cuttof_cos;
void push_to_shader(Gloom::Shader* shader, uint id) {
#define L(x) shader->location("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, vector<NodeDistSh
#undef cache
if (do_recursive)
for(SceneNode* child : node->children) {
for(SceneNode* child : node->children)
renderNode(child, node_shader, transparent_nodes, true);
}
}
// draw

View File

@ -11,7 +11,7 @@
#include <utilities/mesh.h>
#include <utilities/shader.hpp>
#include <utilities/shapes.h>
#include <utilities/timeutils.h>
#include <utilities/timeutils.hpp>
#include <utilities/glfont.h>
#include <utilities/glmhelpers.hpp>
@ -45,7 +45,6 @@ vector<SceneNode*> 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);

View File

@ -1,12 +1,10 @@
#include <chrono>
#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();

View File

@ -1,3 +0,0 @@
#pragma once
double getTimeDeltaSeconds();

View File

@ -0,0 +1,13 @@
#pragma once
#include <chrono>
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();
};