sol.cpp
12 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include "Renderer.h"
GLubyte mytexels[2048][2048][3];
Vertex* face;
Vertex* vt;
Vertex* vn;
int face_n;
void draw_center(void) {
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f); /* R */
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.2f, 0.0f, 0.0f);
glEnd();
glRasterPos3f(0.2f, 0.0f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'x');
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f); /* G */
glVertex3f(0.0f, 0.2f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glRasterPos3f(0.0f, 0.2f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'y');
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 1.0f); /* B */
glVertex3f(0.0f, 0.0f, -0.2f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glRasterPos3f(0.0f, 0.0f, -0.2f);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'z');
}
void idle() {
static GLuint previousClock = glutGet(GLUT_ELAPSED_TIME);
static GLuint currentClock = glutGet(GLUT_ELAPSED_TIME);
static GLfloat deltaT;
currentClock = glutGet(GLUT_ELAPSED_TIME);
deltaT = currentClock - previousClock;
if (deltaT < 1000.0 / 20.0) { return; }
else { previousClock = currentClock; }
//char buff[256];
//sprintf_s(buff, "Frame Rate = %f", 1000.0 / deltaT);
//frameRate = buff;
glutPostRedisplay();
}
void close() {
glDeleteTextures(1, &dispBindIndex);
glutLeaveMainLoop();
}
void add_quats(float q1[4], float q2[4], float dest[4]) {
static int count = 0;
float t1[4], t2[4], t3[4];
float tf[4];
vcopy(q1, t1);
vscale(t1, q2[3]);
vcopy(q2, t2);
vscale(t2, q1[3]);
vcross(q2, q1, t3);
vadd(t1, t2, tf);
vadd(t3, tf, tf);
tf[3] = q1[3] * q2[3] - vdot(q1, q2);
dest[0] = tf[0];
dest[1] = tf[1];
dest[2] = tf[2];
dest[3] = tf[3];
if (++count > RENORMCOUNT) {
count = 0;
normalize_quat(dest);
}
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(58, (double)width / height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
}
void motion(int x, int y) {
GLfloat spin_quat[4];
float gain;
gain = 2.0; /* trackball gain */
if (drag_state == GLUT_DOWN) {
if (button_state == GLUT_LEFT_BUTTON) {
trackball(spin_quat,
(gain * rot_x - 500) / 500,
(500 - gain * rot_y) / 500,
(gain * x - 500) / 500,
(500 - gain * y) / 500);
add_quats(spin_quat, quat, quat);
}
else if (button_state == GLUT_RIGHT_BUTTON) {
t[0] -= (((float)trans_x - x) / 500);
t[1] += (((float)trans_y - y) / 500);
} else if (button_state == GLUT_MIDDLE_BUTTON)
t[2] -= (((float)trans_z - y) / 500 * 4);
else if (button_state == 3 || button_state == 4) {
}
//glutPostRedisplay();
}
rot_x = x;
rot_y = y;
trans_x = x;
trans_y = y;
trans_z = y;
}
void mouse(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT_BUTTON) {
rot_x = x;
rot_y = y;
//t[0] = t[0] + 1;
}
else if (button == GLUT_RIGHT_BUTTON) {
trans_x = x;
trans_y = y;
} else if (button == GLUT_MIDDLE_BUTTON) {
trans_z = y;
} else if (button == 3 || button == 4) {
const float sign = (static_cast<float>(button)-3.5f) * 2.0f;
t[2] -= sign * 500 * 0.00015f;
}
}
drag_state = state;
button_state = button;
}
void vzero(float* v) {
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = 0.0f;
}
void vset(float* v, float x, float y, float z) {
v[0] = x;
v[1] = y;
v[2] = z;
}
void vsub(const float *src1, const float *src2, float *dst) {
dst[0] = src1[0] - src2[0];
dst[1] = src1[1] - src2[1];
dst[2] = src1[2] - src2[2];
}
void vcopy(const float *v1, float *v2) {
register int i;
for (i = 0; i < 3; i++)
v2[i] = v1[i];
}
void vcross(const float *v1, const float *v2, float *cross) {
float temp[3];
temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
vcopy(temp, cross);
}
float vlength(const float *v) {
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
void vscale(float *v, float div) {
v[0] *= div;
v[1] *= div;
v[2] *= div;
}
void vnormal(float *v) {
vscale(v, 1.0f / vlength(v));
}
float vdot(const float *v1, const float *v2) {
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}
void vadd(const float *src1, const float *src2, float *dst) {
dst[0] = src1[0] + src2[0];
dst[1] = src1[1] + src2[1];
dst[2] = src1[2] + src2[2];
}
void trackball(float q[4], float p1x, float p1y, float p2x, float p2y) {
float a[3]; /* Axis of rotation */
float phi; /* how much to rotate about axis */
float p1[3], p2[3], d[3];
float t;
if (p1x == p2x && p1y == p2y) {
/* Zero rotation */
vzero(q);
q[3] = 1.0;
return;
}
/*
* First, figure out z-coordinates for projection of P1 and P2 to
* deformed sphere
*/
vset(p1, p1x, p1y, tb_project_to_sphere(TRACKBALLSIZE, p1x, p1y));
vset(p2, p2x, p2y, tb_project_to_sphere(TRACKBALLSIZE, p2x, p2y));
/*
* Now, we want the cross product of P1 and P2
*/
vcross(p2, p1, a);
/*
* Figure out how much to rotate around that axis.
*/
vsub(p1, p2, d);
t = vlength(d) / (2.0f*TRACKBALLSIZE);
/*
* Avoid problems with out-of-control values...
*/
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
phi = 2.0f * asin(t);
axis_to_quat(a, phi, q);
}
void axis_to_quat(float a[3], float phi, float q[4]) {
vnormal(a);
vcopy(a, q);
vscale(q, sin(phi / 2.0f));
q[3] = cos(phi / 2.0f);
}
float tb_project_to_sphere(float r, float x, float y) {
float d, t, z;
d = sqrt(x*x + y*y);
if (d < r * 0.70710678118654752440f) { /* Inside sphere */
z = sqrt(r*r - d*d);
} else { /* On hyperbola */
t = r / 1.41421356237309504880f;
z = t*t / d;
}
return z;
}
void normalize_quat(float q[4]) {
int i;
float mag;
mag = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
for (i = 0; i < 4; i++) q[i] /= mag;
}
void build_rotmatrix(float m[4][4], float q[4]) {
m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
m[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
m[0][3] = 0.0f;
m[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
m[1][1] = 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
m[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
m[1][3] = 0.0f;
m[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
m[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
m[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
m[2][3] = 0.0f;
m[3][0] = 0.0f;
m[3][1] = 0.0f;
m[3][2] = 0.0f;
m[3][3] = 1.0f;
}
void InitializeWindow(int argc, char* argv[]) {
// initialize glut settings
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(1000 / 2, 1000 / 2);
glutInitWindowPosition(0, 0);
dispWindowIndex = glutCreateWindow("3D Model");
trackball(quat, 90.0, 0.0, 0.0, 0.0);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutCloseFunc(close);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
// bind textures
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
reshape(1000, 1000);
/*glGenTextures(1, &dispBindIndex);
glBindTexture(GL_TEXTURE_2D, dispBindIndex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);*/
}
void f(int i, int j);
void display() {
static int rd = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(10, 1, 1, 200);
glTranslatef(t[0], t[1] - 2, t[2] - 30);
glScalef(1, 1, 1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// 색이 보이도록 빛을 추가함
GLfloat diffuse0[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient0[4] = { 5.0, 5.0, 5.0, 1.0 };
GLfloat specular0[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light0_pos[4] = { 2.0, 2.0, 2.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 2048, 2048, 0, GL_RGB, GL_UNSIGNED_BYTE, mytexels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
GLfloat m[4][4],m1[4][4];
build_rotmatrix(m, quat);
m1[0][0] = 1.0f;
m1[0][1] = 0.0f;
m1[0][2] = -0.3f;
m1[0][3] = 0.0f;
m1[1][0] = 0.0f;;
m1[1][1] = 1.0f;
m1[1][2] = 0.3f;
m1[1][3] = 0.0f;
m1[2][0] = 0.0f;
m1[2][1] = 0.0f;
m1[2][2] = 1.0f;
m1[2][3] = 0.0f;
m1[3][0] = 0.0f;
m1[3][1] = 0.0f;
m1[3][2] = 0.0f;
m1[3][3] = 1.0f;
glMultMatrixf(&m[0][0]);
glMatrixMode(GL_PROJECTION);
glBegin(GL_QUADS);
// 실제 사과를 그리는 부분의 로직
for (register int j = 0; j < face_n; ++j) {
for (int i = 0; i < 4; ++i) {
f(i, j);
}
}
glEnd();
glutSwapBuffers();
}
// 좌표 하나를 준비하는 과정
inline void f(int i, int j) {
// 인덱스가 1을 기준으로 시작하기 때문에 보정함
int vidx = face[j * 4 + i].index_1 - 1;
int vtidx = face[j * 4 + i].index_2 - 1;
int vnidx = face[j * 4 + i].index_3 - 1;
glTexCoord2d(vt[vtidx].X, vt[vtidx].Y);
glVertex3f(vertex[vidx].X, vertex[vidx].Y, vertex[vidx].Z);
// 왜 있는지 모르겠음
glNormal3f(vn[vnidx].X, vn[vnidx].Y, vn[vnidx].Z);
}
int main(int argc, char* argv[]) {
vertex = new Vertex[100000];
vertex_color = new Vertex[100000];
vt = new Vertex[100000];
vn = new Vertex[100000];
face = new Vertex[100000];
FILE* fp;
fp = fopen("apple.obj", "r");
int count = 0;
char ch;
float x, y, z;
// point 로드
for (register int j = 0; j < 1000000; ++j) {
count = fscanf(fp, "%c %f %f %f\n", &ch, &x, &y, &z);
if (count == 4 && ch == 'v') {
vertex[j].X = x / scale;
vertex[j].Y = y / scale;
vertex[j].Z = z / scale;
} else {
break;
}
}
fclose(fp);
// 텍스쳐 로드
fp = fopen("applet.bmp", "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, fp);
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height;
unsigned char* data = new unsigned char[size];
printf("%d %d %d", width, height, size);
fread(data, sizeof(unsigned char), size, fp);
int k = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
mytexels[j][i][0] = data[k * 3 + 2];
mytexels[j][i][1] = data[k * 3 + 1];
mytexels[j][i][2] = data[k * 3];
k++;
}
}
fclose(fp);
fp = fopen("applef2.txt", "rb");
Vertex idx[4];
// face 정보 로드
for (register int j = 0; j < 100000; j = j + 1) {
count = fscanf(fp, "%c %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d\n", &ch,
&idx[0].index_1, &idx[0].index_2, &idx[0].index_3,
&idx[1].index_1, &idx[1].index_2, &idx[1].index_3,
&idx[2].index_1, &idx[2].index_2, &idx[2].index_3,
&idx[3].index_1, &idx[3].index_2, &idx[3].index_3);
if (count == -1) {
break;
}
face_n++;
face[j * 4 + 0] = idx[0];
face[j * 4 + 1] = idx[1];
face[j * 4 + 2] = idx[2];
face[j * 4 + 3] = idx[3];
}
fclose(fp);
// 텍스쳐 좌표, 노멀 로드
fp = fopen("applet.txt", "rb");
for (register int j = 0; j < 100000; j = j + 1) {
char c0;
char c1;
float x, y, z;
count = fscanf(fp, "%c%c %f %f %f\n", &c0, &c1, &x, &y, &z);
if (c0 == 'v' && c1 == 't') {
vt[j].X = x;
vt[j].Y = y;
} else if (c0 == 'v' && c1 == 'n') {
vn[j].X = x;
vn[j].Y = y;
vn[j].Z = z;
}
}
fclose(fp);
InitializeWindow(argc, argv);
display();
glutMainLoop();
delete[] vertex;
delete[] vertex_color;
delete[] vt;
delete[] vn;
return 0;
}