This repository was archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
296 lines (242 loc) · 9.72 KB
/
main.cpp
File metadata and controls
296 lines (242 loc) · 9.72 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
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
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <SOIL.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <cstring>
#include <fstream>
#include <streambuf>
#include "glhelper.hpp"
#include "verts.hpp"
#include "shaders.h"
#define WIDTH 1024
#define HEIGHT 768
#define PI 3.14159265f
int obj_length;
float xAngle = 0.0f;
float yAngle = 0.0f;
float radius = 1.5f;
GLuint obj_vao, obj_vbo;
GLuint blankShaderProgram;
GLFWwindow* window;
float backdrop_vert[] = {
// Position Texture Normal
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Bottom Right
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top Right
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top Left
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Bottom Right
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // Bottom Left
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top Left
};
void key_callback(GLFWwindow* localwindow, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS ){
// w - Screenshot
if(key == GLFW_KEY_W) {
char filename[] = "out.bmp";
int width, height;
glfwGetWindowSize(window, &width, &height);
SOIL_save_screenshot
(
filename,
SOIL_SAVE_TYPE_BMP,
0, 0, width, height
);
// k - Increase object xRotation
} else if(key == GLFW_KEY_K) {
xAngle += 15.0f;
xAngle = xAngle > 360 ? xAngle - 360 : xAngle;
// j - Decrease object xRotation
} else if(key == GLFW_KEY_J) {
xAngle -= 15.0f;
xAngle = xAngle < 0 ? 360 - xAngle : xAngle;
// s - Decrease object distance
} else if(key == GLFW_KEY_S) {
radius -= 0.5f;
radius = radius < 1 ? 1 : radius;
// d - Increase object distance
} else if(key == GLFW_KEY_D) {
radius += 0.5f;
radius = radius > 15 ? 15 : radius;
// ESC - Exit program
} else if ( key == GLFW_KEY_ESCAPE ) {
exit(0);
}
}
}
void update(glm::mat4 view, glm::mat4 proj, glm::mat4 model) {
// Setup Shader
glUseProgram(blankShaderProgram);
glUniformMatrix4fv(glGetUniformLocation(blankShaderProgram, "view"),
1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(blankShaderProgram, "proj"),
1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(glGetUniformLocation(blankShaderProgram, "model"),
1, GL_FALSE, glm::value_ptr(model));
glm::vec3 lighting = glm::vec3(radius*1.1,0.5,0.5);
glUniform3fv(glGetUniformLocation(blankShaderProgram, "lighting"),
1, glm::value_ptr(lighting));
// Draw Object
glBindVertexArray(obj_vao);
glDrawArrays(GL_TRIANGLES, 0, obj_length);
glBindVertexArray(0);
}
int main( int argc, char *argv[] ) {
// --- GLFW Init ---
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// --- Create window ---
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
window = glfwCreateWindow(mode->width, mode->height,
"OpenGL", glfwGetPrimaryMonitor() , nullptr); // Windowed
if(window == NULL) return 2;
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
// --- GLEW/GL Init ---
glewExperimental = GL_TRUE;
glewInit();
// --- Application Specific Setup ---
glBindVertexArray(0);
vector<Vert<float, 8>> obj_vertices;
// Create Object Vertex Arrays
glGenVertexArrays(1, &obj_vao);
glGenBuffers(1, &obj_vbo);
// Create Object 0 Vertex Buffer
glBindVertexArray(obj_vao);
glBindBuffer(GL_ARRAY_BUFFER, obj_vbo);
if(argc >= 2) {
loadOBJ(argv[1], obj_vertices);
} else {
loadOBJ("bb8.obj", obj_vertices);
}
glBufferData(GL_ARRAY_BUFFER, obj_vertices.size() * sizeof(Vert<float, 8>),
&obj_vertices[0], GL_STATIC_DRAW);
Vert<float, 8> max;
maximum(obj_vertices, max);
Vert<float, 8> min;
minimum(obj_vertices, min);
float dim[3] = {max[0]-min[0], max[1]-min[1], max[2]-min[2]};
float center[3] = {(dim[0]/2)+min[0], (dim[1]/2)+min[1], (dim[2]/2)+min[2]};
float maxdim;
if(dim[0] > dim[1] && dim[0] > dim[2]) {
maxdim = dim[0];
} else if(dim[1] > dim[2]) {
maxdim = dim[1];
} else {
maxdim = dim[2];
}
cout << max[0] << ',' << max[1] << ',' << max[2] << endl;
cout << min[0] << ',' << min[1] << ',' << min[2] << endl;
cout << dim[0] << ',' << dim[1] << ',' << dim[2] << endl;
cout << center[0] << ',' << center[1] << ',' << center[2] << endl;
cout << maxdim << endl;
obj_length = obj_vertices.size();
// Read Object 0 texture
// int obj_tex_width, obj_tex_height;
// unsigned char* obj_texture;
// if(argc >= 3) {
// obj_texture =
// SOIL_load_image(argv[2], &obj_tex_width, &obj_tex_height, 0, SOIL_LOAD_RGB);
// } else {
// obj_texture =
// SOIL_load_image("tex.png", &obj_tex_width, &obj_tex_height, 0, SOIL_LOAD_RGB);
// }
// Make Object shader program.
makeShader(SHADER_VERT3D, SHADER_FRAG_LIGHTING, blankShaderProgram);
glBindVertexArray(obj_vao);
glBindBuffer(GL_ARRAY_BUFFER, obj_vbo);
GLint bvpAttrib = glGetAttribLocation(blankShaderProgram, "position");
glEnableVertexAttribArray(bvpAttrib);
glVertexAttribPointer(bvpAttrib, 3, GL_FLOAT, GL_FALSE,
8*sizeof(float), 0);
GLint bvtAttrib = glGetAttribLocation(blankShaderProgram, "texcoord");
glEnableVertexAttribArray(bvtAttrib);
glVertexAttribPointer(bvtAttrib, 2, GL_FLOAT, GL_FALSE,
8*sizeof(float), (void*)(3*sizeof(float)));
GLint bvnAttrib = glGetAttribLocation(blankShaderProgram, "normal");
glEnableVertexAttribArray(bvnAttrib);
glVertexAttribPointer(bvnAttrib, 3, GL_FLOAT, GL_FALSE,
8*sizeof(float), (void*)(5*sizeof(float)));
// Create texture
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glGenerateMipmap(GL_TEXTURE_2D);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
// obj_tex_width, obj_tex_height, 0, GL_RGB,
// GL_UNSIGNED_BYTE, obj_texture);
//glGenerateMipmap(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
// --- Main Loop ---
while(!glfwWindowShouldClose(window)) {
// Check for Keypress
glfwPollEvents();
// Reset
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int width, height;
glfwGetWindowSize(window, &width, &height);
//Display Views
// Screen Layout:
// -----
// | T |
// | L R |
// | B |
// -----
glm::mat4 proj =
glm::perspective(glm::radians(45.0f), 1.0f, 0.5f, 20.0f);
glm::mat4 model = glm::mat4();
model = glm::scale(model, glm::vec3(1.0f/maxdim,1.0f/maxdim,1.0f/maxdim));
model = glm::translate(model,glm::vec3(-center[0],-center[1],-center[2]));
model = glm::rotate(model, (GLfloat)fmod((glfwGetTime()),2.0f*PI),glm::vec3(0.0f,0.0f,1.0f));
//model = glm::rotate(model, (GLfloat)(yAngle*PI/180),glm::vec3(0.0f,1.0f,0.0f));
glm::mat4 view = glm::lookAt(
glm::vec3(radius, 0.0f, 0.0f),
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 0.0f, 0.0f, 1.0f)
);
// Top View
glViewport(((width-height)/2)+(height/3), 2*height/3, height/3, height/3);
update(view, proj, model);
// Left View
view = glm::rotate(view, (GLfloat)(90.0f*PI/180),glm::normalize(glm::vec3(1.0f,0.0f,0.0f)));
model = glm::rotate(model, (GLfloat)(90.0f*PI/180),glm::vec3(0.0f,0.0f,1.0f));
glViewport(((width-height)/2), height/3, height/3, height/3);
update(view, proj, model);
// Bottom View
view = glm::rotate(view, (GLfloat)(90.0f*PI/180),glm::normalize(glm::vec3(1.0f,0.0f,0.0f)));
model = glm::rotate(model, (GLfloat)(90.0f*PI/180),glm::vec3(0.0f,0.0f,1.0f));
glViewport(((width-height)/2)+(height/3), 0, height/3, height/3);
update(view, proj, model);
// Right View
view = glm::rotate(view, (GLfloat)(90.0f*PI/180),glm::normalize(glm::vec3(1.0f,0.0f,0.0f)));
model = glm::rotate(model, (GLfloat)(90.0f*PI/180),glm::vec3(0.0f,0.0f,1.0f));
glViewport(((width-height)/2)+(2*(height/3)), height/3, height/3, height/3);
update(view, proj, model);
glfwSwapBuffers(window);
}
// --- Cleanup/Shutdown ---
glDeleteProgram(blankShaderProgram);
glDeleteBuffers(1, &obj_vbo);
glDeleteVertexArrays(1, &obj_vao);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}