-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (114 loc) · 3.37 KB
/
main.cpp
File metadata and controls
134 lines (114 loc) · 3.37 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
#include "main.h"
int POS_X, POS_Y;
std::string model_name = "Models/Earth.obj";
GLfloat light_pos[] = {-10.0f, 10.0f, 100.00f, 1.0f};
float pos_x, pos_y, pos_z;
float angle_x = 30.0f, angle_y = 0.0f;
int x_old = 0, y_old = 0;
int current_scroll = 5;
float zoom_per_scroll;
bool is_holding_mouse = false;
bool is_updated = false;
Model model;
void init() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0, 1.0, 1.0, 2000.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
model.load(model_name.c_str());
pos_x = model.pos_x;
pos_y = model.pos_y;
pos_z = model.pos_z - 1.0f;
zoom_per_scroll = -model.pos_z / 10.0f;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(pos_x, pos_y, pos_z);
glRotatef(angle_x, 1.0f, 0.0f, 0.0f);
glRotatef(angle_y, 0.0f, 1.0f, 0.0f);
model.draw();
glutSwapBuffers();
}
void timer(int value) {
if (is_updated) {
is_updated = false;
glutPostRedisplay();
}
glutTimerFunc(INTERVAL, timer, 0);
}
void mouse(int button, int state, int x, int y) {
is_updated = true;
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
x_old = x;
y_old = y;
is_holding_mouse = true;
} else
is_holding_mouse = false;
} else if (state == GLUT_UP) {
switch (button) {
case 3:
if (current_scroll > 0) {
current_scroll--;
pos_z += zoom_per_scroll;
}
break;
case 4:
if (current_scroll < 15) {
current_scroll++;
pos_z -= zoom_per_scroll;
}
break;
}
}
}
void motion(int x, int y) {
if (is_holding_mouse) {
is_updated = true;
angle_y += (x - x_old);
x_old = x;
if (angle_y > 360.0f)
angle_y -= 360.0f;
else if (angle_y < 0.0f)
angle_y += 360.0f;
angle_x += (y - y_old);
y_old = y;
if (angle_x > 90.0f)
angle_x = 90.0f;
else if (angle_x < -90.0f)
angle_x = -90.0f;
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
glutSetOption(GLUT_MULTISAMPLE, 8);
POS_X = (glutGet(GLUT_SCREEN_WIDTH) - WIDTH) >> 1;
POS_Y = (glutGet(GLUT_SCREEN_HEIGHT) - HEIGHT) >> 1;
glutInitWindowPosition(POS_X, POS_Y);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Load Model");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}