Add delta time stuff

This commit is contained in:
Ben Visness
2018-05-28 12:31:17 -05:00
parent 373e9517b4
commit ba5982b2a9
4 changed files with 34 additions and 9 deletions

View File

@@ -2043,6 +2043,11 @@ HMM_INLINE hmm_mat4 &operator*=(hmm_mat4 &Left, float Right)
return (Left = Left * Right);
}
HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, hmm_quaternion Right)
{
return (Left = Left * Right);
}
HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, float Right)
{
return (Left = Left * Right);

View File

@@ -106,9 +106,11 @@ public:
renderComponent = c;
}
void Tick(float deltaTime) override {
x += 0.1f;
position.X = HMM_SINF(x);
void Tick(float deltaSeconds) override {
x += deltaSeconds;
position.X = 2.0f * HMM_SINF(x);
rotation *= HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), deltaSeconds * HMM_ToRadians(45.0f));
}
};

View File

@@ -17,7 +17,7 @@ public:
children.push_back(e);
}
virtual void Tick(float deltaTime) {}
virtual void Tick(float deltaSeconds) {}
struct RenderComponent {
GLuint vaoID;

View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <chrono>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@@ -10,9 +11,11 @@
#include "Entity.h"
#include "Cube.h"
void TickTree(Entity *e);
void TickTree(Entity *e, float deltaSeconds);
void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix);
using std::chrono::high_resolution_clock;
int main()
{
// Initialise GLFW
@@ -66,8 +69,15 @@ int main()
Cube child = Cube();
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
Cube c = Cube();
child.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
child.AddChild(&c);
root.AddChild(&child);
bool hasTicked = false;
high_resolution_clock::time_point lastTickTime;
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -75,7 +85,15 @@ int main()
hmm_mat4 view = HMM_LookAt(HMM_Vec3(3.0f, 3.0f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 vp = projection * view;
TickTree(&root);
auto now = high_resolution_clock::now();
if (hasTicked) {
auto elapsedNanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now - lastTickTime).count();
float elapsedSeconds = elapsedNanoseconds / 1000000000.0f;
TickTree(&root, elapsedSeconds);
}
lastTickTime = now;
hasTicked = true;
ComputeModelMatrices(&root, HMM_Mat4d(1.0f));
auto it = EntityIterator(&root);
@@ -140,11 +158,11 @@ int main()
);
}
void TickTree(Entity *e) {
e->Tick(0);
void TickTree(Entity *e, float deltaSeconds) {
e->Tick(deltaSeconds);
for (auto child : e->children) {
TickTree(child);
TickTree(child, deltaSeconds);
}
}