-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (147 loc) · 6.1 KB
/
main.cpp
File metadata and controls
190 lines (147 loc) · 6.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <chrono>
#include <unistd.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <HandmadeMath.h>
#include "input.h"
#include "shaders.h"
#include "Entity.h"
#include "Cube.h"
#include "MeshRenderComponent.h"
#include "FPSCam.h"
void TickTree(Entity *e, float deltaSeconds, Input previousInput, Input input);
void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix);
void HandleMouseMove(GLFWwindow *window, double mouseX, double mouseY);
using std::chrono::high_resolution_clock;
#define WIDTH 1024
#define HEIGHT 768
int main() {
// Initialise GLFW
glewExperimental = true; // Needed for core profile
if (!glfwInit()) {
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow(WIDTH, HEIGHT, "Handmade Math Example", NULL, NULL);
if (window == NULL) {
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Hide the mouse cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
// glfwSetCursorPosCallback(window, [](GLFWwindow *window, double mouseX, double mouseY) {
// printf("%f\t%f\n", mouseX, mouseY);
// // glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
// });
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("src/vertex.glsl", "src/fragment.glsl");
if (!programID) {
return 1;
}
// Get a handle for our "MVP" uniform
// Only during the initialisation
GLuint uniformID_M = glGetUniformLocation(programID, "M");
GLuint uniformID_V = glGetUniformLocation(programID, "V");
GLuint uniformID_MVP = glGetUniformLocation(programID, "MVP");
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
Cube c1 = Cube();
Entity monkey = Entity();
monkey.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
monkey.renderComponent = new MeshRenderComponent("MonkeySmooth.obj");
Entity backmonkey = Entity();
backmonkey.position = HMM_Vec3(0.0f, 0.0f, 5.0f);
backmonkey.renderComponent = new MeshRenderComponent("MonkeySmooth.obj");
FPSCam fpsCam = FPSCam();
fpsCam.position = HMM_Vec3(-1.0f, 1.0f, 3.0f);
Entity *cam = &fpsCam.cam;
// Cube c = Cube();
// monkey.position = HMM_Vec3(2.1f, 0.0f, 0.0f);
// monkey.AddChild(&c);
c1.AddChild(&monkey);
Entity root = Entity();
root.AddChild(&c1);
root.AddChild(&fpsCam);
root.AddChild(&backmonkey);
Entity axes = Entity();
axes.renderComponent = new MeshRenderComponent("Axes.obj");
root.AddChild(&axes);
bool hasTicked = false;
high_resolution_clock::time_point lastTickTime;
Input previousInput = GetInput(window);
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Get inputs
Input input = GetInput(window);
// Tick
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, previousInput, input);
}
lastTickTime = now;
hasTicked = true;
previousInput = input;
// Compute model positions for rendering
ComputeModelMatrices(&root, HMM_Mat4d(1.0f));
// Render!
hmm_mat4 projection = cam->projectionMatrix();
hmm_mat4 view = cam->viewMatrix();
hmm_mat4 vp = projection * view;
auto it = EntityIterator(&root);
while (it.HasNext()) {
Entity *e = it.Next();
if (e->renderComponent) {
// Use our shader
glUseProgram(programID);
// Send uniforms
glUniformMatrix4fv(uniformID_M, 1, GL_FALSE, &e->modelMatrix.Elements[0][0]);
glUniformMatrix4fv(uniformID_V, 1, GL_FALSE, &view.Elements[0][0]);
hmm_mat4 mvp = vp * e->modelMatrix;
glUniformMatrix4fv(uniformID_MVP, 1, GL_FALSE, &mvp.Elements[0][0]);
e->renderComponent->Draw();
}
}
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} while (
// Check if the ESC key was pressed or the window was closed
glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& glfwWindowShouldClose(window) == 0
);
}
void TickTree(Entity *e, float deltaSeconds, Input previousInput, Input input) {
e->Tick(deltaSeconds, previousInput, input);
for (auto child : e->children) {
TickTree(child, deltaSeconds, previousInput, input);
}
}
void ComputeModelMatrices(Entity *e, hmm_mat4 parentModelMatrix) {
e->parentModelMatrix = parentModelMatrix;
e->modelMatrix = parentModelMatrix * HMM_Translate(e->position) * HMM_QuaternionToMat4(e->rotation) * HMM_Scale(e->scale);
for (int i = 0; i < e->children.size(); i++) {
ComputeModelMatrices(e->children[i], e->modelMatrix);
}
}