-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
64 lines (51 loc) · 2.01 KB
/
engine.cpp
File metadata and controls
64 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "engine.h"
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
void PhysicsEngine::addRigidObject(const RigidBody& obj) {
rigidObjects.push_back(obj);
}
void PhysicsEngine::addSimpleObject(const SimpleBody& obj) {
simpleObjects.push_back(obj);
}
void PhysicsEngine::update(float deltaTime) {
for (auto& obj : rigidObjects) {
obj.velocity += obj.acceleration * deltaTime;
obj.velocity.y -= gravity * deltaTime;
obj.position += obj.velocity * deltaTime;
}
}
template <typename T>
void PhysicsEngine::renderObjects(const std::vector<T>& objects, glm::mat4 projectionView) {
for (const auto& obj : objects) {
// Create the model matrix to translate the object to its correct position
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, obj.position);
// Combine the model, view, and projection matrices
glm::mat4 mvp = projectionView * model;
// Load the model-view-projection matrix into OpenGL
glLoadMatrixf(glm::value_ptr(mvp));
// Draw the cube
glBegin(GL_QUADS);
glColor3f(obj.colour[0], obj.colour[1], obj.colour[2]);
for (const auto& v : obj.verticies) {
glVertex3f(v.x,v.y,v.z);
}
glEnd();
}
}
void PhysicsEngine::render(const Camera& camera, int windowWidth, int windowHeight) {
// Clear the screen
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Set up the projection and view
glm::mat4 projection = glm::perspective(glm::radians(camera.fov), (float)windowWidth / windowHeight, camera.nearPlane, camera.farPlane);
glm::mat4 view = camera.getViewMatrix();
// Combine the projection and view matrices
glm::mat4 projectionView = projection * view;
// Render each object
renderObjects(rigidObjects, projectionView);
renderObjects(simpleObjects, projectionView);
}