-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
351 lines (305 loc) · 15.7 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <SDL2/SDL.h>
/*We need this weirdo define for the core profile gl3 stuff to work*/
#define GL_GLEXT_PROTOTYPES 1
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <sstream>
#include "Shader.h"
#include "MD2Model.h"
#include "Vec4.h"
#include "Mat4.h"
#include "Camera.h"
#include "Timer.h"
#include "ObjLoader.h"
#include "Math.h"
#include "Box.h"
#include "Renderer.h"
const GLfloat PI = 3.14;
enum vaoIds {TRIANGLES, NUM_VAOS};
enum bufferIds {ARRAY_BUFFER, NUM_BUFFERS};
enum indexIds{INDEXS, NUM_INDEXS};
static const float MAX_FPS = 60.0f;
GLint vPosition;
GLint vColour;
GLuint vertArrays[NUM_VAOS];
GLuint buffers[NUM_BUFFERS];
GLuint ibo[NUM_INDEXS];
const GLuint NUM_VERTICES = 8;
Renderer renderer;
void init()
{
renderer.initSDL();
Vec4<float> min(-10.0, -10.0, -10.0, 1.0f);
Vec4<float> max(10.0, 10.0, 10.0, 1.0f);
Box b(min, max);
std::vector<struct ShaderList> list;
list.push_back((struct ShaderList){GL_VERTEX_SHADER, "resources/shaders/shader_multilight.vert"});
list.push_back((struct ShaderList){GL_FRAGMENT_SHADER, "resources/shaders/shader_multilight.frag"});
renderer.initGL(list);
//renderer.loadPrimitiveData(b.Vertices(), b.vsize(), b.getIndices(), b.isize(), colours, sizeof(colours), b.tsize(), b.getTextureCoords(), b.getNormals(), b.nsize());
ObjLoader loader("resources/models/plane.obj");
renderer.loadPrimitiveData(loader.getVertices(), loader.vsize(), NULL, 0, loader.getColours(), loader.csize(), loader.tsize(), loader.getTexCoords(), loader.getAvgNormals(), loader.nsize());
renderer.loadTexture("resources/textures/plane.png");
renderer.loadTexture("resources/textures/normalmap2.png");
//renderer.loadPrimitiveData(b.Vertices(), b.vsize(), b.getIndices(), b.isize(), b.Colours(), b.csize(), b.Normals(), b.nsize());
}
int main(int argc, char *argv[])
{
renderer = Renderer(1280, 720);
SDL_bool relativeMouse = SDL_FALSE;
Timer GameTimer; //Keep track of time between game frames
Timer FrameTimer; //Keep track of time between actual drawn frames
Camera cam;
Mat4<float> transMatrix(30.0f);
transMatrix[3][2] = 0.0f;
MD2Model skel;
skel.loadModel("./hueteotl/tris.md2");
bool quit = false;
SDL_Event event;
init();
float rotStep = 0;
GameTimer.start();
float previousTime = GameTimer.getTicks();
float currentTime = previousTime;
const float FRAME_TIME = 1000.0f / MAX_FPS; //Amount of time each frame should take
int frame = 0;
int ticks = 0;
float timeSinceLastFrame = 0;
bool uncappedFps = false;
FrameTimer.start();
int lightIndex = 0;
std::vector<Light> lights;
lights.insert(lights.end(), renderer.newLight(lights.size()));
lights[0].setAmbientLight(Vec4<GLfloat>(0.3f, 0.3f, 0.3f, 1.0f));
lights[0].setDiffuseLight(Vec4<GLfloat>(0.4f, 0.4f, 0.4f, 1.0f));
lights[0].setSpecularLight(Vec4<GLfloat>(0.6f, 0.6f, 0.6f, 1.0f));
lights[0].setIsEnabled(true);
lights[0].enableDiffuse(true);
lights[0].enableSpecular(true);
lights[0].setPosition(Vec4<GLfloat>(0.0f, 5.0f, 0.0f, 0.0f));
lights[0].setNormal(Vec4<GLfloat>(0.0f, 1.0f, 0.0f, 0.0f));
lights[0].setShininess(16.0f);
lights[0].setConstAttenuation(1.0f);
lights[0].setLinearAtten(0.0f);
lights[0].setQuadAtten(1.0f);
lights[0].setIsPointLight(true);
lights[0].setAngle(90.0f);
lights[0].setSpotponent(16.0f);
lights.insert(lights.end(), renderer.newLight(lights.size()));
lights[1].setAmbientLight(Vec4<GLfloat>(0.4f, 0.4f, 0.4f, 1.0f));
lights[1].setDiffuseLight(Vec4<GLfloat>(0.4f, 0.4f, 0.4f, 1.0f));
lights[1].setSpecularLight(Vec4<GLfloat>(1.0f, 1.0f, 1.0f, 1.0f));
lights[1].setIsEnabled(true);
lights[1].enableDiffuse(true);
lights[1].enableSpecular(true);
lights[1].setPosition(Vec4<GLfloat>(5.0f, 5.0f, 0.0f, 0.0f));
lights[1].setNormal(Vec4<GLfloat>(0.0f, 1.0f, 0.0f, 0.0f));
lights[1].setShininess(1.0f);
lights[1].setConstAttenuation(1.0f);
lights[1].setIsSpotLight(true);
lights[1].setLinearAtten(1.0f);
lights[1].setAngle(90.0f);
lights[1].setSpotponent(16.0f);
renderer.updateLights(lights);
renderer.setNumEnabledLights(lights.size());
while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
else if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_Q:
quit = true;
break;
case SDL_SCANCODE_W:
cam.dirz(1.0f);
break;
case SDL_SCANCODE_S:
cam.dirz(-1.0f);
break;
case SDL_SCANCODE_A:
cam.dirx(-1.0f);
break;
case SDL_SCANCODE_D:
cam.dirx(1.0f);
break;
case SDL_SCANCODE_F:
relativeMouse = (SDL_bool)!relativeMouse;
SDL_SetRelativeMouseMode(relativeMouse);
break;
case SDL_SCANCODE_L:
{
Light tmpLight = lights[lightIndex];
GLboolean isEnabled = !tmpLight.getIsEnabled();
lights[lightIndex].setIsEnabled(isEnabled);
renderer.updateLight(tmpLight);
std::string lightState = isEnabled ? "On" : "Off";
std::cout << "Toggled light:" << lightState << std::endl;
}
break;
case SDL_SCANCODE_O:
{
GLboolean diffuse = !lights[lightIndex].getIsDiffuseEnabled();
lights[lightIndex].enableDiffuse(diffuse);
renderer.updateLight(lights[lightIndex]);
std::string diffuseState = diffuse ? "On" : "Off";
std::cout << "Toggled Diffuse:" << diffuseState << std::endl;
}
break;
case SDL_SCANCODE_P:
{
GLboolean specular = !lights[lightIndex].getIsSpecularEnabled();
lights[lightIndex].enableSpecular(specular);
renderer.updateLight(lights[lightIndex]);
std::string specularState = specular ? "On" : "Off";
std::cout << "Toggled Specular:" << specularState << std::endl;
}
break;
case SDL_SCANCODE_U:
{
GLboolean specularMode = !lights[lightIndex].getSpecularMode();
lights[lightIndex].setSpecularMode(specularMode);
renderer.updateLight(lights[lightIndex]);
std::string specularModeState = specularMode ? "Blin-Phong" : "Phong";
std::cout << "Toggled Specular Mode:" << specularModeState << std::endl;
}
break;
case SDL_SCANCODE_F2:
{
uncappedFps = !uncappedFps;
std::string fpsMode = uncappedFps ? "off" : "on";
std::cout << "Toggled FPS cap:" << fpsMode << std::endl;
}
break;
case SDL_SCANCODE_1:
{
GLfloat shininess = lights[lightIndex].getShininess() - 1.0f;
lights[lightIndex].setShininess(shininess);
renderer.updateLight(lights[lightIndex]);
}
break;
case SDL_SCANCODE_2:
{
GLfloat shininess = lights[lightIndex].getShininess() + 1.0f;
lights[lightIndex].setShininess(shininess);
renderer.updateLight(lights[lightIndex]);
}
break;
case SDL_SCANCODE_MINUS:
{
lightIndex = lightIndex > 0 ? --lightIndex : lightIndex;
std::cout << "light[" << lightIndex << "] selected" << std::endl;
}
break;
case SDL_SCANCODE_EQUALS:
{
/*Yeah I'm using the ternery operator like an ass hat*/
lightIndex = lightIndex < (lights.size() - 1) ? ++lightIndex : lightIndex;
std::cout << "light[" << lightIndex << "] selected" << std::endl;
}
break;
}
}
else if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_W:
cam.dirz(0.0f);
break;
case SDL_SCANCODE_S:
cam.dirz(0.0f);
break;
case SDL_SCANCODE_A:
cam.dirx(0.0f);
break;
case SDL_SCANCODE_D:
cam.dirx(0.0f);
break;
case SDL_SCANCODE_F1:
{
/*Implement fullscreen switching*/
}
}
}
else if(event.type == SDL_MOUSEMOTION)
{
cam.updatePitch((float)event.motion.yrel);
cam.updateYaw((float)event.motion.xrel);
}
}
currentTime = GameTimer.getTicks();
float dt = (float)(currentTime - previousTime) / 1000.0f;
timeSinceLastFrame += currentTime - previousTime;
cam.update(dt);
previousTime = currentTime;
/*Frame limiting*/
if(timeSinceLastFrame > FRAME_TIME || uncappedFps)
{
timeSinceLastFrame = 0;
//SDL_Delay(FRAME_TIME - dt);
float radians = Math::toRadians(rotStep);
//std::cout << "LightPos[z]=" << lightPos[z] << std::endl;
//renderer.updateLights(lights);
renderer.updateCameraMatrix(cam.cameraMatrix());
renderer.updateCameraPosition(cam.getPosition());
Mat4<float> rotMatrix(1.0f);
Mat4<float> modelMatrix = rotMatrix * transMatrix;
renderer.updateModelMatrix(modelMatrix);
renderer.draw();
rotStep += 1.0f;
rotStep = rotStep < 360 ? rotStep : 0;
frame += 1;
}
float frameTime = FrameTimer.getTicks();
if(frameTime > 1000.0f)
{
std::stringstream convert;
convert << frame;
renderer.setWindowTitle(convert.str());
frame = 0;
FrameTimer.stop();
FrameTimer.start();
}
}
renderer.cleanup();
return 0;
}
void checkGL()
{
GLenum error = glGetError();
switch(error)
{
case GL_INVALID_ENUM:
std::cout << "ERROR: GL Invalid Enum" << std::endl;
case GL_INVALID_VALUE:
std::cout << "ERROR: GL Invalid Value" << std::endl;
break;
case GL_INVALID_OPERATION:
std::cout << "ERROR: GL Invalid Operation" << std::endl;
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
std::cout << "ERROR: GL Invalid Framebuffer Operation" << std::endl;
break;
case GL_OUT_OF_MEMORY:
std::cout << "ERROR: GL Out of Memory" << std::endl;
break;
case GL_STACK_UNDERFLOW:
std::cout << "ERROR:GL Stack Underflow" << std::endl;
break;
case GL_STACK_OVERFLOW:
std::cout << "ERROR:GL Stack Overflow" << std::endl;
break;
case GL_NO_ERROR:
break;
default:
break;
}
}