-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
294 lines (250 loc) · 8.22 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
#include "helper.h"
#include <iostream>
#include <vector>
#include <unordered_map>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
static GLFWwindow* win = NULL;
// Shaders
GLuint idProgramShader;
GLuint idFragmentShader;
GLuint idVertexShader;
GLuint idJpegTexture;
GLuint idMVPMatrix;
GLuint idCameraPosition;
GLuint idHeightFactor;
GLuint idWidthTexture;
GLuint idHeightTexture;
GLuint idPosition;
int widthTexture, heightTexture;
int indexes_size;
glm::mat4 projectionMatrix;
glm::mat4 viewingMatrix;
glm::mat4 modelingMatrix;
glm::vec3 camera_gaze(0, 0, 1);
glm::vec3 camera_pos(0, 0, 0);
glm::vec3 camera_up(0, 1, 0);
GLfloat heightFactor = 10.0f;
std::unordered_map<int, bool> states;
bool fullscreen = false;
int windowHeight = 600;
int windowWidth = 600;
int windowX = 0;
int windowY = 0;
float speed = 0.0f;
#ifdef INEK
int profile = GLFW_OPENGL_COMPAT_PROFILE;
#else
int profile = GLFW_OPENGL_ANY_PROFILE;
#endif
static void errorCallback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void initVertices()
{
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
idPosition = glGetAttribLocation(idProgramShader, "position");
glEnableVertexAttribArray(idPosition);
GLuint vertexAttribBuffer, indexBuffer;
glGenBuffers(1, &vertexAttribBuffer);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexAttribBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
int width = widthTexture;
int height = heightTexture;
std::vector<float> vertices;
for (int i=0; i<=height; i++)
{
for (int j=0; j<=width; j++)
{
int xpos = j;
int zpos = i;
vertices.push_back(xpos);
vertices.push_back(0);
vertices.push_back(zpos);
//std::cout << "For vertex (" << j << "," << i << ") : x = " << xpos << ", z = " << zpos << std::endl;
}
}
std::vector<GLuint> indexes;
indexes.push_back(0);
//std::cout << std::endl;
// std::cout << 0 << std::endl;
for (int j=0; j<height; j++)
{
// indexes.push_back((j+1) * (width+1));
// indexes.push_back(j * (width+1) + 1);
//std::cout << (j+1) * (width+1) << std::endl << j * (width+1) + 1 << std::endl;
for (int i=0; i<=width; i++)
{
if (i != width)
{
indexes.push_back((j+1) * (width+1) + i);
indexes.push_back(j * (width+1) + i + 1);
//std::cout << (j+1) * (width+1) + i << std::endl << j * (width+1) + i + 1 << std::endl;
}
else if (j != height - 1)
{
//std::cout << (j+1) * (width+1) + i << std::endl;
//std::cout << (j+1) * (width+1) + i << std::endl;
//std::cout << (j) * (width+1) + i + 1 << std::endl;
//std::cout << (j) * (width+1) + i + 1 << std::endl;
indexes.push_back((j+1) * (width+1) + i);
indexes.push_back((j+1) * (width+1) + i);
indexes.push_back((j) * (width+1) + i + 1);
indexes.push_back((j) * (width+1) + i + 1);
}
else
{
//std::cout << (j+1) * (width+1) + i << std::endl;
indexes.push_back((j+1) * (width+1) + i);
}
}
}
indexes_size = indexes.size();
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexes_size * sizeof(indexes[0]), indexes.data(), GL_STATIC_DRAW);
glVertexAttribPointer(idPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
float fovyRad = M_PI / 4;
projectionMatrix = glm::perspective(fovyRad, 1.0f, 0.1f, 1000.0f);
camera_pos = glm::vec3(widthTexture/2, widthTexture/10, -widthTexture/4);
camera_up = glm::vec3(0, 1, 0);
camera_gaze = glm::vec3(0, 0, 1);
}
void makeChanges()
{
if (states[GLFW_KEY_O])
heightFactor += 0.5;
if (states[GLFW_KEY_L])
heightFactor -= 0.5;
if (states[GLFW_KEY_A] || states[GLFW_KEY_D])
{
const float const_angle = 0.5 * M_PI / 180.0;
float angle = 0.0f;
if (states[GLFW_KEY_A])
angle += const_angle;
if (states[GLFW_KEY_D])
angle -= const_angle;
auto left = glm::cross(camera_up, camera_gaze);
auto rot_matrix = glm::rotate(glm::mat4(1.0), angle, camera_up);
camera_gaze = glm::normalize(glm::vec3(rot_matrix * glm::vec4(camera_gaze, 0.0)));
}
if (states[GLFW_KEY_S] || states[GLFW_KEY_W])
{
const float const_angle = 0.5 * M_PI / 180.0;
float angle = 0.0f;
if (states[GLFW_KEY_W])
angle -= const_angle;
if (states[GLFW_KEY_S])
angle += const_angle;
auto left = glm::cross(camera_up, camera_gaze);
auto rot_matrix = glm::rotate(glm::mat4(1.0), angle, left);
camera_gaze = glm::normalize(glm::vec3(rot_matrix * glm::vec4(camera_gaze, 0.0)));
camera_up = glm::cross(camera_gaze, left);
}
if (states[GLFW_KEY_U])
speed += 0.01;
if (states[GLFW_KEY_J])
speed -= 0.01;
}
void renderFunction()
{
int width, height;
glfwGetFramebufferSize(win, &width, &height);
glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
makeChanges();
camera_pos += glm::normalize(camera_gaze) * speed;
viewingMatrix = glm::lookAt(camera_pos, camera_gaze + camera_pos, camera_up);
glUniform4f(idCameraPosition, camera_pos[0], camera_pos[1], camera_pos[2], 0);
glUniformMatrix4fv(idMVPMatrix, 1, GL_FALSE, &(projectionMatrix * viewingMatrix)[0][0]);
glUniform1f(idHeightFactor, heightFactor);
glUniform1i(idWidthTexture, widthTexture);
glUniform1i(idHeightTexture, heightTexture);
glDrawElements(GL_TRIANGLE_STRIP, indexes_size, GL_UNSIGNED_INT, 0);
}
void toggleFullscreen() {
fullscreen = !fullscreen;
GLFWmonitor *monitor;
const GLFWvidmode *mode;
if (fullscreen)
{
glfwGetWindowSize(win, &windowWidth, &windowHeight);
glfwGetWindowPos(win, &windowX, &windowY);
monitor = glfwGetPrimaryMonitor();
mode = glfwGetVideoMode(monitor);
}
glfwSetWindowMonitor(
win,
fullscreen ? monitor : nullptr,
fullscreen ? 0 : windowX,
fullscreen ? 0 : windowY,
fullscreen ? mode->width : windowWidth,
fullscreen ? mode->height : windowHeight,
fullscreen ? mode->refreshRate : 0
);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS && action != GLFW_RELEASE)
return;
if (action == GLFW_PRESS) // Buttons without hold
{
switch (key)
{
case GLFW_KEY_F:
toggleFullscreen();
return;
}
}
states[key] = action == GLFW_PRESS;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Please provide only a texture image\n");
exit(-1);
}
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
exit(-1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, profile);
win = glfwCreateWindow(600, 600, "CENG477 - HW4", NULL, NULL);
if (!win) {
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(win);
GLenum err = glewInit();
if (err != GLEW_OK) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
glfwTerminate();
exit(-1);
}
glfwSetKeyCallback(win, keyCallback);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_DEPTH_TEST);
initShaders();
idMVPMatrix = glGetUniformLocation(idProgramShader, "MVP");
idCameraPosition = glGetUniformLocation(idProgramShader, "idCameraPosition");
idHeightFactor = glGetUniformLocation(idProgramShader, "heightFactor");
idWidthTexture = glGetUniformLocation(idProgramShader, "widthTexture");
idHeightTexture = glGetUniformLocation(idProgramShader, "heightTexture");
idCameraPosition = glGetUniformLocation(idProgramShader, "idCameraPosition");
glUseProgram(idProgramShader);
initTexture(argv[1], &widthTexture, &heightTexture);
glEnable(GL_CULL_FACE);
initVertices();
while(!glfwWindowShouldClose(win)) {
glfwSwapBuffers(win);
renderFunction();
glfwPollEvents();
}
glfwDestroyWindow(win);
glfwTerminate();
return 0;
}