-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·464 lines (382 loc) · 14.7 KB
/
main.cpp
File metadata and controls
executable file
·464 lines (382 loc) · 14.7 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Include standard headers
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <ctime>
// Include GLEW
#include <GL/glew.h>
// glm::vec3, glm::vec4, glm::ivec4, glm::mat4
#include <glm/glm.hpp>
// glm::translate, glm::rotate, glm::scale, glm::perspective
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
#include "shader.h"
#include "texture.h"
#include "libobj.h"
#include "skybox/skybox.h"
#define WINDOW_WIDTH 720
#define WINDOW_HEIGHT 720
const char* texture_path;
const char* model_path;
const char* skybox_path;
GLMmodel* objModel;
GLuint programID;
GLuint modelID;
GLuint viewID;
GLuint projectionID;
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
Texture* pTextures = NULL;
GLuint gSampler;
GLuint vertexbuffer;
GLuint normalbuffer;
int image_count = 0;
SkyBox* Sky_Box;
ofstream file;
// Rotation interface
float xrot = 0;
float yrot = 0;
float xdiff = 0;
float ydiff = 0;
float zoom = 3;
float view_x = 0;
float view_y = 0;
bool mouseDown = false;
float rot = 0;
void mouseMotionCB(int x, int y)
{
if (mouseDown)
{
yrot = 0.01 * (x - xdiff);
xrot = 0.01 * (y + ydiff);
glutPostRedisplay();
}
}
void SaveScene()
{
file.open ("../ForSegUltra/IMAGE_LIST.txt", std::ios_base::app);
// Make the BYTE array, factor of 3 because it's RBG.
unsigned char* pixels = new unsigned char[3 * WINDOW_WIDTH * WINDOW_HEIGHT];
// Read pixwls from OpenGL scene
glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Make Blob and Image
Magick::Blob blob(&pixels[0], WINDOW_WIDTH * WINDOW_HEIGHT * 3);
Magick::Image img;
// Setup image parameters
img.size(to_string(WINDOW_WIDTH) + "x" + to_string(WINDOW_HEIGHT));
img.magick("RGB");
img.depth(8);
try{
img.read(blob);
}
catch(std::exception e)
{
return;
}
img.flip();
img.magick( "PNG" );
img.type(Magick::GrayscaleType);
// img.threshold(128);
// Fill file name
std::string image_name = "../ForSegUltra/images/abrams_" + std::to_string(image_count) + ".png";
// file << "images/support_" << image_count << ".png " << 3 << std::endl;
file << "images/abrams_" << image_count << ".png " << " labels/0/abrams_" << image_count << ".png " << std::endl;
// Write original image
img.write(image_name.data());
image_count++;
delete [] pixels;
file.close();
return;
}
void mouseCB(int button, int state, int x, int y)
{
switch(button){
case GLUT_LEFT_BUTTON:
switch(state)
{
case GLUT_DOWN:
mouseDown = true;
xdiff = x - yrot;
ydiff = -y + xrot;
break;
case GLUT_UP:
mouseDown = false;
break;
default:
break;
}
break;
case GLUT_MIDDLE_BUTTON:
break;
case 3:
// printf("%s", "Wheel up\n");
zoom -= 0.1;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
case 4:
// printf("%s", "Wheel down\n");
zoom += 0.1;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
default:
break;
}
}
void specialKeyboardCB(int key, int x, int y)
{
switch(key){
case GLUT_KEY_LEFT:
yrot -= 0.05;
break;
case GLUT_KEY_RIGHT:
yrot += 0.05;
break;
case GLUT_KEY_UP:
xrot += 0.05;
break;
case GLUT_KEY_DOWN:
xrot -= 0.05;
break;
default:
break;
}
glutPostRedisplay();
x = x;
y = y;
}
void keyboardCB(unsigned char key, int x, int y)
{
switch(key){
case 27:
exit(0);
break;
case 'w':
view_y -= 0.03;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
case 'a':
view_x += 0.03;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
case 's':
view_y += 0.03;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
case 'd':
view_x -= 0.03;
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(view_x, view_y, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
glutPostRedisplay();
break;
case 'f':
SaveScene();
break;
default: break;
}
x = x;
y = y;
}
void init(const char* fname)
{
std::cout << std::endl << "Loading .obj model: " << fname;
objModel = glmReadOBJ(fname);
if (!objModel)
{
std::cout << " - FAIL ON LOAD" << std::endl;
exit(0);
}
else std::cout << " - LOADED" << std::endl;
// Normilize vertices
glmUnitize(objModel);
// Compute facet normals
glmFacetNormals(objModel);
// Comput vertex normals
glmVertexNormals(objModel, 90.0);
// Load the model (vertices and normals) into a vertex buffer
glmLoadInVBO(objModel);
std::cout << std::endl << "Loading textures for model: " << fname << std::endl;
// Load textures into array. Need to put path to folder with textures
pTextures = new Texture[objModel->numgroups];
LoadTextures(objModel, pTextures, texture_path);
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
std::cout << "Loading shader program for model: " << fname << std::endl << std::endl;
// Create and compile our GLSL program from the shaders
programID = LoadShaders( "../shaders/vertShader.glsl",
"../shaders/fragShader.glsl" );
std::cout << std::endl;
// Get a handle for our model, view and projection uniforms
modelID = glGetUniformLocation(programID, "model");
viewID = glGetUniformLocation(programID, "view");
projectionID = glGetUniformLocation(programID, "projection");
glm::vec4 light_ambient = glm::vec4 ( 0.1, 0.1, 0.1, 0.5 );
glm::vec4 light_diffuse = glm::vec4 ( 0.8, 1.0, 1.0, 1.0 );
glm::vec4 light_specular = glm::vec4 ( 0.8, 1.0, 1.0, 1.0 );
glUseProgram (programID);
glUniform4fv (glGetUniformLocation(programID, "light_ambient"), 1, &light_ambient[0]);
glUniform4fv (glGetUniformLocation(programID, "light_diffuse"), 1, &light_diffuse[0]);
glUniform4fv (glGetUniformLocation(programID, "light_specular"), 1, &light_specular[0]);
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
projection = glm::perspective(45.0f, 16.0f / 9.0f, 5.0f, 500.0f);
// Camera matrix
view = glm::lookAt( glm::vec3(view_x, view_y, zoom), // Camera position in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
// Model matrix : an identity matrix (model will be at the origin)
model = glm::mat4(.0f);
// Initialize a light
glm::vec4 lightPosition = glm::vec4(-20, -10, 0, 0);
glUniform4fv( glGetUniformLocation(programID, "lightPos"),1, &lightPosition[0]);
std::cout << "Groups and materials of model: " << fname << std::endl << std::endl;
glmDisplayVBOMaterials(objModel);
std::cout << "Load SkyBox with SkyBoxShader: " << fname << std::endl << std::endl;
Sky_Box = new SkyBox();
Sky_Box->init();
Sky_Box->SkyBoxProgramID = LoadShaders( "../skybox/shaders/vertSkyBox.glsl",
"../skybox/shaders/fragSkyBox.glsl" );
std::cout << std::endl << "DISPLAYING RESULT...\n"
"1) Use WASD for translation; \n"
"2) Use ARROW_KEYS or MOUSE for rotation; \n"
"3) Use MOUSE_WHEEL for scaling\n"
"4) Use F for save render \n"
"5) Injoy the result" << std::endl ;
}
void display(void)
{
// Clear the screne
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
model = glm::mat4(1.0f);
glm::mat4 xRotMat = glm::rotate(glm::mat4(1.0f), xrot, glm::normalize(glm::vec3(glm::inverse(model) * glm::vec4(1, 0, 0, 1))) );
model = model * xRotMat;
glm::mat4 yRotMat = glm::rotate(glm::mat4(1.0f), yrot, glm::normalize(glm::vec3(glm::inverse(model) * glm::vec4(0, 1, 0, 1))) );
model = model * yRotMat;
glm::mat4 modelViewMatrix = view * model;
glm::mat3 normalMatrix = glm::inverseTranspose(glm::mat3(modelViewMatrix)); // Normal Matrix
// Use our shader
glUseProgram(programID);
// Send the model, view and projection matrices to the shader
glUniformMatrix4fv(modelID, 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(viewID, 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projection[0][0]);
glUniformMatrix3fv( glGetUniformLocation(programID, "normalMatrix"), 1, GL_FALSE, &normalMatrix[0][0]);
glmDrawVBO(objModel, pTextures, programID);
Sky_Box->Render(model, view, projection);
// Swap buffers
glutSwapBuffers();
// for (int j = 0; j <= 4; j++ )
// {
// for (int i = 0; i < 20; i++ )
// {
// float zoom_zoom = zoom;
// for (int k = 0; k <= 5; k++ )
// {
// // Clear the screne
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// model = glm::mat4(1.0f);
// glm::mat4 xRotMat = glm::rotate(glm::mat4(1.0f), xrot /*- 1.57f*/, glm::normalize(glm::vec3(glm::inverse(model) * glm::vec4(1, 0, 0, 1))) );
// model = model * xRotMat;
// glm::mat4 yRotMat = glm::rotate(glm::mat4(1.0f), yrot, glm::normalize(glm::vec3(glm::inverse(model) * glm::vec4(0, 1, 0, 1))) );
// model = model * yRotMat;
// glm::mat4 modelViewMatrix = view * model;
// glm::mat3 normalMatrix = glm::inverseTranspose(glm::mat3(modelViewMatrix)); // Normal Matrix
// // Use our shader
// glUseProgram(programID);
// // Send the model, view and projection matrices to the shader
// glUniformMatrix4fv(modelID, 1, GL_FALSE, &model[0][0]);
// glUniformMatrix4fv(viewID, 1, GL_FALSE, &view[0][0]);
// glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projection[0][0]);
// glUniformMatrix3fv( glGetUniformLocation(programID, "normalMatrix"), 1, GL_FALSE, &normalMatrix[0][0]);
// glmDrawVBO(objModel, pTextures, programID);
// Sky_Box->Render(model, view, projection);
//// // Swap buffers
// glutSwapBuffers();
// view = glm::lookAt( glm::vec3(view_x, view_y, zoom_zoom), // Camera position in World Space
// glm::vec3(0, -0.5, 0), // and looks at the origin
//// glm::vec3(rot, rot, rot)); // Head is up (set to 0,-1,0 to look upside-down)
// glm::vec3(0, 1, 0)); // Head is up (set to 0,-1,0 to look upside-down)
// SaveScene();
// rot += 0.5;
// zoom_zoom -= 0.8;
// std::cout << "Progress: [" << std::setw(8) << std::left << (float) (image_count) * 100/ (100 * 6) << "]%" << std::endl;
// }
// yrot += 0.4;
// }
// view_y += 1;
// }
}
// Get OpenGL version information
void getGLinfo()
{
std::cout << "GL Vendor : " << glGetString(GL_VENDOR) << std::endl;
std::cout << "GL Renderer : " << glGetString(GL_RENDERER) << std::endl;
std::cout << "GL Version : " << glGetString(GL_VERSION) << std::endl;
}
void reshape( int width, int height )
{
glViewport(0, 0, width, height);
GLfloat aspectRatio = GLfloat(width)/height;
projection = glm::perspective(45.0f, aspectRatio, 0.1f, 500.0f);
glutPostRedisplay();
}
int main( int argc, char **argv )
{
texture_path = "../Models/T-90/";
model_path = "../Models/T-90/T-90.obj";
glutInitContextVersion (3, 3);
glewExperimental = GL_TRUE;
// // TODO: Command-line parsing and error checking
// if (argc == 2)
// {
// model_path = argv[1];
// }
// else
// {
// std::cerr << "ERROR: You have to specify an OBJ file as first argument.\n"
// "For example \"./Support/Support.obj\"" << std::endl;
// return 1;
// }
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glutCreateWindow( "objview" );
glewInit();
getGLinfo();
init(model_path);
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc(mouseCB);
glutMotionFunc(mouseMotionCB);
glutSpecialFunc(specialKeyboardCB);
glutKeyboardFunc(keyboardCB);
glutMainLoop();
return 0;
}