-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
213 lines (178 loc) · 6.55 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
/*
TODO:
1. Independent window class with UI
2. Extend camera functionality (rotations with quaternions, camera rotating around specific point ability)
3. Integrate any physics library (bullet) and create some examples
4. // ...
*/
#define GLEW_STATIC
#define EXIT_SUCCESS 0
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/random.hpp>
#include <SOIL.h>
#include <string>
#include <vector>
#include <cstring>
#include <fstream>
#include <cassert>
#include <iostream>
#include <stdexcept>
#include "shader_manager.h"
#include "camera.h"
#include "model.h"
glm::vec2 WINDOW_SIZE(1200, 800);
glm::vec2 lastMousePos(WINDOW_SIZE.x/2.0, WINDOW_SIZE.y/2.0);
glm::vec3 cameraPosition(0.0, 0.0, 25.0), lightPosition(-3.0, 15.0, 1.0);
Camera camera(cameraPosition, glm::vec3(0.0, 0.0, -1.0), glm::vec3(0.0, 1.0, 0.0));
bool keys[1024];
void key_callback(GLFWwindow* window, int, int, int, int);
void do_movement(const GLfloat&);
void mouse_callback(GLFWwindow* window, double, double);
void scroll_callback(GLFWwindow* window, double, double);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS)
{
keys[key] = true;
}
if (action == GLFW_RELEASE)
{
keys[key] = false;
}
}
void do_movement(const GLdouble& dt)
{
if (keys[GLFW_KEY_W]) // forward
{
camera.handleKeyInput(FORWARD, dt);
}
if (keys[GLFW_KEY_S]) // backward
{
camera.handleKeyInput(BACKWARD, dt);
}
if (keys[GLFW_KEY_A]) // left
{
camera.handleKeyInput(LEFT, dt);
}
if (keys[GLFW_KEY_D]) // right
{
camera.handleKeyInput(RIGHT, dt);
}
}
void mouse_callback(GLFWwindow* window, double currentMouseX, double currentMouseY)
{
GLdouble dx = currentMouseX - lastMousePos.x;
GLdouble dy = lastMousePos.y - currentMouseY;
lastMousePos.x = currentMouseX;
lastMousePos.y = currentMouseY;
camera.handleMouseInput(dx, dy);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.handleMouseScrollInput(xoffset, yoffset);
}
int main(int argc, char *argv[])
{
// initialise GLFW
if (!glfwInit())
{
throw std::runtime_error("glfwInit failed");
return -1;
}
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 16);
GLFWwindow* window = glfwCreateWindow((int)WINDOW_SIZE.x, (int)WINDOW_SIZE.y, "Opengl demo", nullptr, nullptr);
if (!window)
{
throw std::runtime_error("glfwOpenWindow failed. Can your hardware handle OpenGL 4.5?");
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetWindowPos(window, 10, 50);
glViewport(0, 0, (GLsizei)WINDOW_SIZE.x, (GLsizei)WINDOW_SIZE.y);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
// initialise GLEW
glewExperimental = GL_TRUE; // stops glew crashing on OSX :-/
if (glewInit() != GLEW_OK)
{
throw std::runtime_error("glewInit failed");
}
ShaderManager shaderManager;
Shader vshader(GL_VERTEX_SHADER, "shaders/vshader");
Shader fshader(GL_FRAGMENT_SHADER, "shaders/fshader");
Shader gshader(GL_GEOMETRY_SHADER, "shaders/gshader");
GLuint shaderProgram = shaderManager.buildProgram(vshader, fshader, gshader);
shaderManager.use(shaderProgram);
//Model handgun("models/Handgun/Handgun_Obj/Handgun_obj.obj");
Model nanosuit("models/nanosuit/nanosuit.obj");
glm::mat4
projection,
view,
model,
T = glm::translate(glm::mat4(1.0), glm::vec3(0.0, 0.0, 0.0)),
Tback = glm::translate(Tback, glm::vec3(0.0, 10.0, 0.0)),
R = glm::mat4(1.0),
S = glm::mat4(1.0),
mvp, pv, freeTranslate, normalMatrix;
projection = glm::perspective(camera.getZOOM(), WINDOW_SIZE.x/WINDOW_SIZE.y, 0.1f, 10000.0f);
GLuint cameraPositionLoc = glGetUniformLocation(shaderProgram, "cameraPosition"),
lightPositonLoc = glGetUniformLocation(shaderProgram, "dirLigh1.position"),
lightAmbientLoc = glGetUniformLocation(shaderProgram, "dirLigh1.ambient"),
lightDiffuseLoc = glGetUniformLocation(shaderProgram, "dirLigh1.diffuse"),
lightSpecularLoc = glGetUniformLocation(shaderProgram, "dirLigh1.specular"),
modelLoc = glGetUniformLocation(shaderProgram, "model"),
viewLoc = glGetUniformLocation(shaderProgram, "view"),
mvpLoc = glGetUniformLocation(shaderProgram, "mvp"),
normalMatrixLoc = glGetUniformLocation(shaderProgram, "normalMatrix");
GLdouble currentFrame = 0.0f,
lastFrame = 0.0f,
dt = 0.0f;
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
currentFrame = glfwGetTime();
dt = currentFrame - lastFrame;
lastFrame = currentFrame;
do_movement(dt);
projection = glm::perspective(camera.getZOOM(), WINDOW_SIZE.x/WINDOW_SIZE.y, 0.1f, 10000.0f);
view = camera.getViewMatrix();
pv = projection * view;
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
//R = glm::rotate(glm::mat4(1.0), (GLfloat)glfwGetTime(), glm::vec3(0.0, 1.0, 0.0));
T = glm::translate(glm::mat4(1.0), glm::vec3(0.0, -10, 0.0));
model = R * T;
mvp = pv * model;
normalMatrix = glm::transpose(glm::inverse(model));
glUniform3f(cameraPositionLoc, camera.position.x, camera.position.y, camera.position.z);
glUniform3f(lightPositonLoc, lightPosition.x, lightPosition.y, lightPosition.z);
glUniform3f(lightAmbientLoc, 1.0, 1.0, 1.0);
glUniform3f(lightDiffuseLoc, 1.0, 1.0, 1.0);
glUniform3f(lightSpecularLoc, 1.0, 1.0, 1.0);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(mvp));
glUniformMatrix4fv(normalMatrixLoc, 1, GL_FALSE, glm::value_ptr(normalMatrix));
nanosuit.render(shaderProgram);
glfwSwapBuffers(window);
}
glfwTerminate();
return EXIT_SUCCESS;
}