-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
119 lines (95 loc) · 3.81 KB
/
simulator.cpp
File metadata and controls
119 lines (95 loc) · 3.81 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <GLFW/glfw3.h>
#include "engine.h"
#include "objects.h"
#include "camera.h"
#define WIN_WIDTH 1080
#define WIN_HEIGHT 720
#define WIN_TITLE "3D Engine"
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
Camera* cameraInstance = static_cast<Camera*>(glfwGetWindowUserPointer(window));
if (!cameraInstance) return;
cameraInstance->changefov(yoffset);
}
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
Camera* cameraInstance = static_cast<Camera*>(glfwGetWindowUserPointer(window));
if (!cameraInstance) return;
if (cameraInstance->firstMouse) {
cameraInstance->lastX = xpos;
cameraInstance->lastY = ypos;
cameraInstance->firstMouse = false;
}
// get the change in (x,y)
float xOffset = xpos - cameraInstance->lastX;
float yOffset = cameraInstance->lastY - ypos; // y is inverse
cameraInstance->lastX = xpos;
cameraInstance->lastY = ypos;
cameraInstance->rotateCamera(xOffset * cameraInstance->sensitivity, yOffset * cameraInstance->sensitivity);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
int main() {
// Initialize GLFW
if (!glfwInit()) return -1;
GLFWwindow* window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, WIN_TITLE, NULL, NULL);
if (!window) return -1;
glfwMakeContextCurrent(window);
glEnable(GL_DEPTH_TEST); // Enable depth testing
// Create the engine and add a rigid body (the cube)
PhysicsEngine engine;
RigidBody cube = {glm::vec3(0.0f, 0.0f, -3.0f), glm::vec3(1.0f, 3.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f,3.0f,2.0f), ShapeType::Cuboid, glm::vec3(1.0f, 0.0f, 1.0f), 1.0f};
engine.addRigidObject(cube);
// Create Camera (Positioned initially to view the cube)
Camera camera(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), 60.0f, 0.1f, 100.0f, 1.5f, 0.1f);
// scrollwheel actions for fov
glfwSetWindowUserPointer(window, &camera);
glfwSetScrollCallback(window, scroll_callback);
// mouse actions for rotation
glfwSetCursorPosCallback(window, mouse_callback);
// update for when screen size changes
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// The timing rates of the function
float prevTime = 0.0f;
float deltaTime = 0.0f;
// Main update loop
while (!glfwWindowShouldClose(window)) {
//update objects
double currTime = glfwGetTime();
deltaTime = currTime - prevTime;
prevTime = currTime;
engine.update(deltaTime);
// Handle camera movement
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera.moveForward(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
camera.moveBackward(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera.moveLeft(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
camera.moveRight(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
camera.moveUp(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
camera.moveDown(deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
engine.addRigidObject(cube);
}
// Render the scene (objects will stay still, only the camera moves)
int width, height;
glfwGetFramebufferSize(window, &width, &height);
engine.render(camera, width, height);
// Swap buffers and poll events
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}