-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
252 lines (204 loc) · 7.64 KB
/
main.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "util/Shader.h"
#include <map>
#include "util/FontConfigs.h"
#include "components/TextDraw.h"
#include "components/InputText.h"
#include "components/Cube.h"
#include "components/ObjModel.h"
#include "util/Camera.h"
#include "util/ObjLoader.h"
#include "entities/RawModel.h"
using namespace std;
// default window size
int WINDOW_W = 1280;
int WINDOW_H = 720;
GLFWwindow *window;
GLFWmonitor* monitor;
const GLFWvidmode* mode;
// @todo remove these variables when GameLoop and GameWindow done.
int keyboardkey, mouseButton, mouseAction;
unsigned int charCodePoint;
// Camera Configs
void Do_Movement();
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
bool keys[1024];
GLfloat lastX = 400, lastY = 300;
bool firstMouse = true;
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
static void error_callback(int error, const char *desc) {
cout << "Error: " << desc << endl;
}
static void keyCallBack(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
keyboardkey = key;
if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
if (key == GLFW_KEY_F11) {
// Set full screen window
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
}
if (key >= 0 && key < 1024) {
keys[key] = true;
}
} else if (action == GLFW_RELEASE) {
keyboardkey = 0;
keys[key] = false;
}
}
static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
mouseButton = button;
mouseAction = action;
}
void character_callback(GLFWwindow* window, unsigned int codepoint, int mods) {
charCodePoint = codepoint;
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
Mouse::setXpos(xpos);
Mouse::setYpos(ypos);
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
int main(int argc, char** argv) {
// @todo used for alter monitor or NULL when we're create window.
string isDebug = (argc > 1 ? argv[1] : "");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
monitor = glfwGetPrimaryMonitor();
mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, "Project Melkor - Roch Studio", NULL, NULL);
if (!window) {
cout << "could not create Window" << endl;
glfwTerminate();
return -1;
}
// Setting Window pos Center in Monitor 1920 - 1080
// glfwSetWindowPos(window, 320, 180);
glfwSetErrorCallback(error_callback);
glfwSetKeyCallback(window, keyCallBack);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCharModsCallback(window, character_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwMakeContextCurrent(window);
// @todo remove this line
if (isDebug != "debug") {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
cout << "Failed to initialize Glew" << endl;
return -1;
}
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, mode->width, mode->height);
glMatrixMode(GL_MODELVIEW);
Shader modelViewProjectionTextured(
"assets/Shaders/ModelViewProjectionTextured.vert",
"assets/Shaders/ModelViewProjectionTextured.frag"
);
Shader modelViewProjectionColor(
"assets/Shaders/ModelViewProjection.vert",
"assets/Shaders/ModelViewProjection.frag"
);
ProjectionMatrix projectionMatrix;
projectionMatrix.setAspectRatio((GLfloat)mode->width / mode->height);
int imageW, imageH;
unsigned char *dirtTexture;
Entity *entity[50];
Cube *cubes[50];
for (int i = 0; i < 50; i++) {
// int num = (i >= 10 ? (i / 10) : 0);
// entity[i] = new Entity(0, vec3((i * 1), -2, (num * -1)), 0, 0, 0, 1);
//
if (i < 10)
entity[i] = new Entity(0, vec3(i * 1, -2, 0), 0, 0, 0, 1);
else if (i >= 10 && i < 20)
entity[i] = new Entity(0, vec3((i - 10) * 1, -2, -1), 0, 0, 0, 1);
else if (i >= 20 && i < 30)
entity[i] = new Entity(0, vec3((i - 20) * 1, -2, -2), 0, 0, 0, 1);
else if (i >= 30 && i < 40)
entity[i] = new Entity(0, vec3((i - 30) * 1, -2, -3), 0, 0, 0, 1);
else if (i >= 40 && i < 50)
entity[i] = new Entity(0, vec3((i - 40) * 1, -2, -4), 0, 0, 0, 1);
cubes[i] = new Cube(mode->width, mode->height);
cubes[i]->setEntity(*entity[i]);
cubes[i]->shader(&modelViewProjectionTextured);
dirtTexture = SOIL_load_image((i >= 30 ? "assets/images/stone.jpg" : "assets/images/dirt.jpg"), &imageW, &imageH, 0, SOIL_LOAD_RGB);
cubes[i]->textureImage(dirtTexture, imageW, imageH, GL_RGB);
}
// ### Font Configs
Shader fontShader("assets/Shaders/FontVertexShader.glsl", "assets/Shaders/FontFragmentShader.glsl");
FontConfigs fontConfigs(18);
// ### End Font Configs
TextDraw playerText(&fontShader, mode->width, mode->height);
playerText.characters(fontConfigs.Characters);
playerText.color(glm::vec3(0.5, 0.8f, 0.2f));
Loader loader;
ObjLoader stallObj = ObjLoader();
RawModel firstRawModel = stallObj.loadObj("assets/Models/stall.obj", loader);
ObjModel stallModel(modelViewProjectionTextured, firstRawModel);
unsigned char *stallTexture = SOIL_load_image("assets/images/stallTexture.png", &imageW, &imageH, 0, SOIL_LOAD_RGB);
stallModel.textureImage(stallTexture, imageW, imageH, GL_RGB);
Entity test(0, vec3(0, 0, 0), 0, 0, 0, 1);
stallModel.setEntity(test);
while (!glfwWindowShouldClose(window)) {
// Check and call events
glfwPollEvents();
// Game Logic here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0f);
// Set frame time
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
Do_Movement();
// Render Text
playerText.x(1.0f);
playerText.y(1.0f);
playerText.scale(1.0f);
playerText.text("Created By Cod3r Kane");
playerText.render();
for (int i = 0; i < 50; i++) {
cubes[i]->setViewMatrix(camera.GetViewMatrix());
cubes[i]->setProjectionMatrix(projectionMatrix);
cubes[i]->render();
}
stallModel.setViewMatrix(camera.GetViewMatrix());
stallModel.setProjectionMatrix(projectionMatrix);
stallModel.render();
glfwSwapBuffers(window);
// clean inputs
keyboardkey = 0;
charCodePoint = 0;
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
// Moves/alters the camera positions based on user input
void Do_Movement() {
// Camera controls
if(keys[GLFW_KEY_W])
camera.ProcessKeyboard(FORWARD, deltaTime);
if(keys[GLFW_KEY_S])
camera.ProcessKeyboard(BACKWARD, deltaTime);
if(keys[GLFW_KEY_A])
camera.ProcessKeyboard(LEFT, deltaTime);
if(keys[GLFW_KEY_D])
camera.ProcessKeyboard(RIGHT, deltaTime);
}