-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
159 lines (135 loc) · 3.87 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
#include <GL/glfw.h>
#include <math.h>
#include "obj_parse.h"
#define WIDTH 640
#define HEIGHT 480
void draw_cube() {
glBegin(GL_QUADS);
// Top
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, 0.5, -0.5); // 0
glVertex3f(0.5, 0.5, -0.5); // 1
glVertex3f(0.5, 0.5, 0.5); // 2
glVertex3f(-0.5, 0.5, 0.5); // 3
// Front
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5); // 5
glVertex3f(-0.5, -0.5, 0.5); // 4
// Bottom
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, -0.5); // 7
glVertex3f(-0.5, -0.5, -0.5); // 6
// Back
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.5, -0.5, -0.5); // 8
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5); // 9
// Left
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
// Right
glColor3f(0.0, 1.0, 1.0);
glVertex3f(0.5, 0.5, -0.5); // 1
glVertex3f(0.5, 0.5, 0.5); // 2
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, -0.5);
glEnd();
}
int main(int argc, char** argv) {
glfwInit();
glfwOpenWindow(WIDTH, HEIGHT, 8, 8, 8, 8, 32, 0, GLFW_WINDOW);
// Setup OpenGL stuff
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (float)WIDTH/(float)HEIGHT, 0.1, 10.0);
glMatrixMode(GL_MODELVIEW);
obj_model m(argv[1]);
if (argc > 1 && argv[1]) {
std::cout << "size is " << m.v.size() << std::endl;
std::cout << "Vertex is " << m.v[0].a << "," << m.v[0].b << "," << m.v[0].c
<< std::endl;
std::cout << "Vertex2is " << m.v[1].a << "," << m.v[1].b << "," << m.v[1].c
<< std::endl;
}
int running = 1;
int jumping = 0;
float rotZ = 0;
float rotY = 0;
float rotX = 0;
float posnZ = 2;
float posnY = 0;
float posnX = 0;
float velo = 0;
const float GRAVITY = -0.005;
const float LOOK_X_TOP = +M_PI/3.;
const float LOOK_X_BOT = -M_PI/3.;
float lookX = 0; /* in radians */
float lookY = 0; /* aka THETA, in radians */
while(running) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Look around */
if (glfwGetKey('F')) lookX -= 0.02;
if (glfwGetKey('R')) lookX += 0.02;
if (lookX >= LOOK_X_TOP) lookX = LOOK_X_TOP;
if (lookX <= LOOK_X_BOT) lookX = LOOK_X_BOT;
glRotatef(-lookX * (180./M_PI), 1, 0, 0);
if (glfwGetKey('H')) lookY -= 0.02;
if (glfwGetKey('G')) lookY += 0.02;
glRotatef(-lookY * (180./M_PI), 0, 1, 0);
// Setup camera transformation
if (glfwGetKey(GLFW_KEY_UP)) {
posnX -= 0.01 * sin(lookY);
posnZ -= 0.01 * cos(lookY);
}
if (glfwGetKey(GLFW_KEY_DOWN)) {
posnX += 0.01 * sin(lookY);
posnZ += 0.01 * cos(lookY);
}
if (glfwGetKey(GLFW_KEY_LEFT)) {
posnX -= 0.01 * cos(lookY);
posnZ += 0.01 * sin(lookY);
}
if (glfwGetKey(GLFW_KEY_RIGHT)) {
posnX += 0.01 * cos(lookY);
posnZ -= 0.01 * sin(lookY);
}
if (glfwGetKey(GLFW_KEY_SPACE)) { velo = 0.1; jumping = 1; }
if (jumping) {
posnY += velo;
if (posnY >= 0) {
velo += GRAVITY;
} else {
jumping = 0;
posnY = 0;
}
}
glTranslatef(-posnX, -posnY, -posnZ);
// Setup model transformation
if (glfwGetKey('Q')) rotZ += 0.2;
if (glfwGetKey('E')) rotZ -= 0.2;
if (glfwGetKey('A')) rotY += 0.2;
if (glfwGetKey('D')) rotY -= 0.2;
if (glfwGetKey('W')) rotX += 0.2;
if (glfwGetKey('S')) rotX -= 0.2;
glRotatef(rotZ, 0, 0, 1);
glRotatef(rotY, 0, 1, 0);
glRotatef(rotX, 1, 0, 0);
// Draw!
////draw_cube();
draw_object(m);
glfwSwapBuffers();
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}
}