-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
100 lines (76 loc) · 2.71 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
#include "loader.h"
#include "common.h"
#include "callbacks.h"
#include "GL/freeglut.h"
void myGlutInit(int *argc, char **argv) {
// Open window
glutInit(argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(GLUTwindow_width, GLUTwindow_height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // | GLUT_STENCIL
GLUTwindow = glutCreateWindow(TITLE_STRING);
glutKeyboardFunc( KeyboardDownCallback );
glutMouseFunc(MouseCallback);
glutReshapeFunc( WindowResizeCallback );
glutDisplayFunc( SceneDisplayCallback );
glutMotionFunc(MotionCallback);
glutIdleFunc(SceneDisplayCallback);
// Initialize lights
static GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glShadeModel(GL_SMOOTH);
static GLfloat light1_diffuse[] = {0.5, 0.5, 0.5, 1.0};
// glLightfv(GL_LIGHT0, GL_DIFFUSE, difflight_color);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
// Initialize graphics modes
glEnable(GL_DEPTH_TEST);
}
void myGlutMain(void) {
// Compute bounding box
float bbox[2][3] = {
{ 1.0E30F, 1.0E30F, 1.0E30F},
{ -1.0E30F, -1.0E30F, -1.0E30F}
};
for (int i = 0; i < mesh->nverts; i++) {
Vertex& vert = mesh->verts[i];
if (vert.x < bbox[0][0]) bbox[0][0] = vert.x;
else if (vert.x > bbox[1][0]) bbox[1][0] = vert.x;
if (vert.y < bbox[0][1]) bbox[0][1] = vert.y;
else if (vert.y > bbox[1][1]) bbox[1][1] = vert.y;
if (vert.z < bbox[0][2]) bbox[0][2] = vert.z;
else if (vert.z > bbox[1][2]) bbox[1][2] = vert.z;
}
// Setup initial viewing scale
float dx = bbox[1][0] - bbox[0][0];
float dy = bbox[1][1] - bbox[0][1];
float dz = bbox[1][2] - bbox[0][2];
scale = 2.0 / sqrt(dx * dx + dy * dy + dz * dz);
// Setup initial viewing center
center[0] = 0.5 * (bbox[1][0] + bbox[0][0]);
center[1] = 0.5 * (bbox[1][1] + bbox[0][1]);
center[2] = 0.5 * (bbox[1][2] + bbox[0][2]);
glutMainLoop();
}
int main(int argc, char **argv) {
myGlutInit(&argc, argv);
if (argc != 2) {
fprintf(stderr, "Usage: ./main [filename]\n");
return 1;
}
mesh = ReadOffFile(argv[1]);
if (!mesh) {
exit(1);
} else {
fprintf(stdout, "Mesh parsed successfully\n");
}
textures[0] = LoadTexture( "tex1.bmp" );
textures[1] = LoadTexture( "tex2.bmp" );
textures[2] = LoadTexture( "tex3.bmp" );
myGlutMain();
return 0;
}