|
Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. |
../108-1/108-1-Ortho.c | 108-2-Viewpoint.c | |||
---|---|---|---|---|
286 lines 10979 bytes Last modified : Sat Nov 12 18:54:57 2011 |
303 lines 11727 bytes Last modified : Sat Nov 12 18:55:17 2011 |
|||
1 | // Keisanki Joron 2 (Introduction to Computing II) | 1 | // Keisanki Joron 2 (Introduction to Computing II) | |
2 | // Dept. of Engineering Systems, University of Tsukuba | 2 | // Dept. of Engineering Systems, University of Tsukuba | |
3 | // [UTF-8 / Unix] | 3 | // [UTF-8 / Unix] | |
4 | 4 | |||
5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | 5 | // 2011/11/07 kameda[at]iit.tsukuba.ac.jp | |
6 | // 8.1 直交投影ã®æ˜Žç¤º | 6 | // 8.2 ウィンドウサイズ変更ã«ã‚ˆã‚‹å½±éŸ¿ã®ç„¡åŠ¹åŒ– (Viewpoint変æ›ã®ç„¡åŠ¹åŒ–) | |
7 | 7 | |||
8 | #include <stdio.h> | 8 | #include <stdio.h> | |
9 | #include <stdlib.h> // exit() | 9 | #include <stdlib.h> // exit() | |
10 | #include <GL/glut.h> | 10 | #include <GL/glut.h> | |
11 | 11 | |||
12 | // *********************************************************************** | 12 | // *********************************************************************** | |
13 | // global variables ****************************************************** | 13 | // global variables ****************************************************** | |
14 | 14 | |||
15 | // +---------------------------------------------------- | 15 | // +---------------------------------------------------- | |
16 | // Global Variables | 16 | // Global Variables | |
17 | // +---------------------------------------------------- | 17 | // +---------------------------------------------------- | |
18 | 18 | |||
19 | // Windowサイズ | 19 | // Windowサイズ | |
20 | int window_w = 400; | 20 | int window_w = 400; | |
21 | int window_h = 400; | 21 | int window_h = 400; | |
22 | 22 | |||
23 | // 直交投影時ã®ã‚¹ã‚±ãƒ¼ãƒ«ãƒ•ã‚¡ã‚¯ã‚¿ [pixel / unit_of_imager] | 23 | // 直交投影時ã®ã‚¹ã‚±ãƒ¼ãƒ«ãƒ•ã‚¡ã‚¯ã‚¿ [pixel / unit_of_imager] | |
24 | // ã“ã“ã§ã¯æ£è¦åŒ–カメラã®æ’®åƒé¢ã§ã® 1.0 å˜ä½ã‚’ 200ç”»ç´ ã«ç›¸å½“ã•ã›ã‚‹ | 24 | // ã“ã“ã§ã¯æ£è¦åŒ–カメラã®æ’®åƒé¢ã§ã® 1.0 å˜ä½ã‚’ 200ç”»ç´ ã«ç›¸å½“ã•ã›ã‚‹ | |
25 | float ortho_unit = 200.0; | 25 | float ortho_unit = 200.0; | |
26 | 26 | |||
27 | // Windowã®ID(æ画管ç†ç”¨ï¼‰ | |||
28 | int window_id = -1; | |||
29 | ||||
27 | // *********************************************************************** | 30 | // *********************************************************************** | |
28 | // gl utilitiess ********************************************************* | 31 | // gl utilitiess ********************************************************* | |
29 | 32 | |||
30 | // +---------------------------------------------------- | 33 | // +---------------------------------------------------- | |
31 | // MODELVIEW Matrix 㨠PROJECTION を表示ã™ã‚‹ | 34 | // MODELVIEW Matrix 㨠PROJECTION を表示ã™ã‚‹ | |
32 | // +---------------------------------------------------- | 35 | // +---------------------------------------------------- | |
33 | void ic2_ShowMATRIX (char *str) { | 36 | void ic2_ShowMATRIX (char *str) { | |
34 | GLfloat m[16]; // GL_MODELVIEW matrix | 37 | GLfloat m[16]; // GL_MODELVIEW matrix | |
35 | GLfloat p[16]; // GL_PROJECTION matrix | 38 | GLfloat p[16]; // GL_PROJECTION matrix | |
36 | 39 | |||
37 | glGetFloatv(GL_MODELVIEW_MATRIX , m); // MODELVIEWã®ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—matrixã‚’mã«ã‚³ãƒ”ー | 40 | glGetFloatv(GL_MODELVIEW_MATRIX , m); // MODELVIEWã®ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—matrixã‚’mã«ã‚³ãƒ”ー | |
38 | glGetFloatv(GL_PROJECTION_MATRIX, p); // PROJECTIONã®ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—matrixã‚’pã«ã‚³ãƒ”ー | 41 | glGetFloatv(GL_PROJECTION_MATRIX, p); // PROJECTIONã®ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—matrixã‚’pã«ã‚³ãƒ”ー | |
39 | if (str != NULL) printf("<< %s >>\n", str); | 42 | if (str != NULL) printf("<< %s >>\n", str); | |
40 | printf("MODELVIEW Matrix PROJECTION Matrix\n"); | 43 | printf("MODELVIEW Matrix PROJECTION Matrix\n"); | |
41 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | 44 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |
42 | m[ 0], m[ 4], m[ 8], m[12], p[ 0], p[ 4], p[ 8], p[12]); | 45 | m[ 0], m[ 4], m[ 8], m[12], p[ 0], p[ 4], p[ 8], p[12]); | |
43 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | 46 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |
44 | m[ 1], m[ 5], m[ 9], m[13], p[ 1], p[ 5], p[ 9], p[13]); | 47 | m[ 1], m[ 5], m[ 9], m[13], p[ 1], p[ 5], p[ 9], p[13]); | |
45 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | 48 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |
46 | m[ 2], m[ 6], m[10], m[14], p[ 2], p[ 6], p[10], p[14]); | 49 | m[ 2], m[ 6], m[10], m[14], p[ 2], p[ 6], p[10], p[14]); | |
47 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | 50 | printf("%7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f %7.3f\n", | |
48 | m[ 3], m[ 7], m[11], m[15], p[ 3], p[ 7], p[11], p[15]); | 51 | m[ 3], m[ 7], m[11], m[15], p[ 3], p[ 7], p[11], p[15]); | |
49 | } | 52 | } | |
50 | 53 | |||
51 | // *********************************************************************** | 54 | // *********************************************************************** | |
52 | // objects_embeded ******************************************************* | 55 | // objects_embeded ******************************************************* | |
53 | 56 | |||
54 | // +---------------------------------------------------- | 57 | // +---------------------------------------------------- | |
55 | // æ£æ–¹å½¢ã‚’æã | 58 | // æ£æ–¹å½¢ã‚’æã | |
56 | // +---------------------------------------------------- | 59 | // +---------------------------------------------------- | |
57 | void ic2_FigSquare (float s) { | 60 | void ic2_FigSquare (float s) { | |
58 | glDisable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’一旦切る | 61 | glDisable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’一旦切る | |
59 | 62 | |||
60 | // æ£æ–¹å½¢ï¼ˆZ=0ã®å¹³é¢å†…ã€+/- 0.9) | 63 | // æ£æ–¹å½¢ï¼ˆZ=0ã®å¹³é¢å†…ã€+/- 0.9) | |
61 | glBegin(GL_LINE_LOOP); { | 64 | glBegin(GL_LINE_LOOP); { | |
62 | glColor3f(1.0, 1.0, 1.0); | 65 | glColor3f(1.0, 1.0, 1.0); | |
63 | glVertex3f(s * -1, s * -1, 0.0); | 66 | glVertex3f(s * -1, s * -1, 0.0); | |
64 | glVertex3f(s * +1, s * -1, 0.0); | 67 | glVertex3f(s * +1, s * -1, 0.0); | |
65 | glVertex3f(s * +1, s * +1, 0.0); | 68 | glVertex3f(s * +1, s * +1, 0.0); | |
66 | glVertex3f(s * -1, s * +1, 0.0); | 69 | glVertex3f(s * -1, s * +1, 0.0); | |
67 | } glEnd(); | 70 | } glEnd(); | |
68 | 71 | |||
69 | // 3軸 | 72 | // 3軸 | |
70 | glBegin(GL_LINES); { | 73 | glBegin(GL_LINES); { | |
71 | glColor3f(1.0, 0.5, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.85, 0.0, 0.0); // X (red) | 74 | glColor3f(1.0, 0.5, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.85, 0.0, 0.0); // X (red) | |
72 | glColor3f(0.5, 1.0, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.70, 0.0); // Y (green) | 75 | glColor3f(0.5, 1.0, 0.5); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.70, 0.0); // Y (green) | |
73 | glColor3f(0.5, 0.5, 1.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); // Z (blue) | 76 | glColor3f(0.5, 0.5, 1.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); // Z (blue) | |
74 | } glEnd(); | 77 | } glEnd(); | |
75 | 78 | |||
76 | glEnable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’開始ã™ã‚‹ | 79 | glEnable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’開始ã™ã‚‹ | |
77 | } | 80 | } | |
78 | 81 | |||
79 | // +---------------------------------------------------- | 82 | // +---------------------------------------------------- | |
80 | // ティーãƒãƒƒãƒˆã‚’æã (glutã®ä½œã‚Šä»˜ã‘関数ã®ï¼‘ã¤) | 83 | // ティーãƒãƒƒãƒˆã‚’æã (glutã®ä½œã‚Šä»˜ã‘関数ã®ï¼‘ã¤) | |
81 | // +---------------------------------------------------- | 84 | // +---------------------------------------------------- | |
82 | void ic2_FigSolidTeapot (float s) { | 85 | void ic2_FigSolidTeapot (float s) { | |
83 | GLfloat obj_ref[] = {1.0, 1.0, 0.3, 1.0}; // teapotã®è‰²æƒ…å ± (DIFFUSE用) | 86 | GLfloat obj_ref[] = {1.0, 1.0, 0.3, 1.0}; // teapotã®è‰²æƒ…å ± (DIFFUSE用) | |
84 | GLfloat obj_shn[] = {10.0}; // teapotã®è‰²æƒ…å ± (SHININESS用) | 87 | GLfloat obj_shn[] = {10.0}; // teapotã®è‰²æƒ…å ± (SHININESS用) | |
85 | 88 | |||
86 | // 色ã®è¨å®š | 89 | // 色ã®è¨å®š | |
87 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, obj_ref); | 90 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, obj_ref); | |
88 | glMaterialfv(GL_FRONT, GL_SHININESS, obj_shn); | 91 | glMaterialfv(GL_FRONT, GL_SHININESS, obj_shn); | |
89 | 92 | |||
90 | glutSolidTeapot(s); | 93 | glutSolidTeapot(s); | |
91 | } | 94 | } | |
92 | 95 | |||
93 | // *********************************************************************** | 96 | // *********************************************************************** | |
94 | // lighting ************************************************************** | 97 | // lighting ************************************************************** | |
95 | 98 | |||
96 | // +---------------------------------------------------- | 99 | // +---------------------------------------------------- | |
97 | // å…‰æºã‚’ç”¨æ„ | 100 | // å…‰æºã‚’ç”¨æ„ | |
98 | // +---------------------------------------------------- | 101 | // +---------------------------------------------------- | |
99 | // X Y Z Diff(R,G,B) Spec(R,G,B) | 102 | // X Y Z Diff(R,G,B) Spec(R,G,B) | |
100 | // 1.0 2.0 3.0 0.2 0.2 0.2 0.4 0.4 0.4 | 103 | // 1.0 2.0 3.0 0.2 0.2 0.2 0.4 0.4 0.4 | |
101 | // -1.0 2.0 3.0 0.4 0.4 0.4 0.4 0.4 0.4 | 104 | // -1.0 2.0 3.0 0.4 0.4 0.4 0.4 0.4 0.4 | |
102 | // 0.0 4.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 | 105 | // 0.0 4.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 | |
103 | void ic2_LightSetA (void) { | 106 | void ic2_LightSetA (void) { | |
104 | static int initflag = 0; | 107 | static int initflag = 0; | |
105 | 108 | |||
106 | if (initflag == 0) { | 109 | if (initflag == 0) { | |
107 | glEnable(GL_DEPTH_TEST); // デプスãƒãƒƒãƒ•ã‚¡ã«ã‚ˆã‚‹æ画を行ㆠ| 110 | glEnable(GL_DEPTH_TEST); // デプスãƒãƒƒãƒ•ã‚¡ã«ã‚ˆã‚‹æ画を行ㆠ| |
108 | glEnable(GL_NORMALIZE); // 法線ベクトルを常ã«æ£è¦åŒ–ã—ã¦è§£é‡ˆã•ã›ã‚‹ | 111 | glEnable(GL_NORMALIZE); // 法線ベクトルを常ã«æ£è¦åŒ–ã—ã¦è§£é‡ˆã•ã›ã‚‹ | |
109 | glEnable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’開始ã™ã‚‹ | 112 | glEnable(GL_LIGHTING); // å…‰æºã«ã‚ˆã‚‹ã‚·ã‚§ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’開始ã™ã‚‹ | |
110 | glEnable(GL_LIGHT0); // LIGHT0 を利用 | 113 | glEnable(GL_LIGHT0); // LIGHT0 を利用 | |
111 | glEnable(GL_LIGHT1); // LIGHT1 を利用 | 114 | glEnable(GL_LIGHT1); // LIGHT1 を利用 | |
112 | glEnable(GL_LIGHT2); // LIGHT2 を利用 | 115 | glEnable(GL_LIGHT2); // LIGHT2 を利用 | |
113 | initflag = 1; | 116 | initflag = 1; | |
114 | } | 117 | } | |
115 | 118 | |||
116 | GLfloat val[4]; | 119 | GLfloat val[4]; | |
117 | 120 | |||
118 | val[0] = 1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_POSITION, val); | 121 | val[0] = 1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_POSITION, val); | |
119 | val[0] = 0.2; val[1] = 0.2; val[2] = 0.2; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_DIFFUSE, val); | 122 | val[0] = 0.2; val[1] = 0.2; val[2] = 0.2; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_DIFFUSE, val); | |
120 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_SPECULAR, val); | 123 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT0, GL_SPECULAR, val); | |
121 | 124 | |||
122 | val[0] = -1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_POSITION, val); | 125 | val[0] = -1.0; val[1] = 2.0; val[2] = 3.0; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_POSITION, val); | |
123 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_DIFFUSE, val); | 126 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_DIFFUSE, val); | |
124 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_SPECULAR, val); | 127 | val[0] = 0.4; val[1] = 0.4; val[2] = 0.4; val[3] = 1.0; glLightfv(GL_LIGHT1, GL_SPECULAR, val); | |
125 | 128 | |||
126 | val[0] = 0.0; val[1] = 4.0; val[2] = 0.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_POSITION, val); | 129 | val[0] = 0.0; val[1] = 4.0; val[2] = 0.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_POSITION, val); | |
127 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_DIFFUSE, val); | 130 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_DIFFUSE, val); | |
128 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_SPECULAR, val); | 131 | val[0] = 1.0; val[1] = 1.0; val[2] = 1.0; val[3] = 1.0; glLightfv(GL_LIGHT2, GL_SPECULAR, val); | |
129 | } | 132 | } | |
130 | 133 | |||
131 | // *********************************************************************** | 134 | // *********************************************************************** | |
132 | // camera work *********************************************************** | 135 | // camera work *********************************************************** | |
133 | 136 | |||
134 | // +---------------------------------------------------- | 137 | // +---------------------------------------------------- | |
135 | // カメラã®æŠ•å½±è¡Œåˆ—ã‚’è¨å®š | 138 | // カメラã®æŠ•å½±è¡Œåˆ—ã‚’è¨å®š | |
136 | // +---------------------------------------------------- | 139 | // +---------------------------------------------------- | |
137 | // 利用ã™ã‚‹å¤§åŸŸå¤‰æ•°ï¼š window_w, window_h | 140 | // 利用ã™ã‚‹å¤§åŸŸå¤‰æ•°ï¼š window_w, window_h | |
138 | // window_w/window_hã®å¤‰åŒ–ã«å¯¾ã—ã¦ã€ç‰©ä½“ã®è¦‹ã‹ã‘ã®å¤§ãã•ãŒå¤‰ã‚らãªã„よã†ã«æç”» | 141 | // window_w/window_hã®å¤‰åŒ–ã«å¯¾ã—ã¦ã€ç‰©ä½“ã®è¦‹ã‹ã‘ã®å¤§ãã•ãŒå¤‰ã‚らãªã„よã†ã«æç”» | |
139 | // → ortho_unit ãŒé‡è¦ï¼ | 142 | // → ortho_unit ãŒé‡è¦ï¼ | |
140 | // | 143 | // | |
141 | void ic2_SetUpCamera_Ortho (void) { | 144 | void ic2_SetUpCamera_Ortho (void) { | |
142 | float wlimit, hlimit; | 145 | float wlimit, hlimit; | |
143 | wlimit = (window_w/2) / ortho_unit; | 146 | wlimit = (window_w/2) / ortho_unit; | |
144 | hlimit = (window_h/2) / ortho_unit; | 147 | hlimit = (window_h/2) / ortho_unit; | |
145 | 148 | |||
146 | // glOrtho(左端, å³ç«¯, 下端, 上端, 近接å´ã‚¯ãƒªãƒƒãƒ”ングé¢, é æ–¹å´ã‚¯ãƒªãƒƒãƒ”ングé¢) | 149 | // glOrtho(左端, å³ç«¯, 下端, 上端, 近接å´ã‚¯ãƒªãƒƒãƒ”ングé¢, é æ–¹å´ã‚¯ãƒªãƒƒãƒ”ングé¢) | |
147 | glMatrixMode(GL_PROJECTION); | 150 | glMatrixMode(GL_PROJECTION); | |
148 | glOrtho(-wlimit, wlimit, -hlimit, hlimit, -1.0, 1.0); | 151 | glOrtho(-wlimit, wlimit, -hlimit, hlimit, -1.0, 1.0); | |
149 | glMatrixMode(GL_MODELVIEW); | 152 | glMatrixMode(GL_MODELVIEW); | |
150 | } | 153 | } | |
151 | 154 | |||
152 | // *********************************************************************** | 155 | // *********************************************************************** | |
153 | // rendering ************************************************************* | 156 | // rendering ************************************************************* | |
154 | 157 | |||
155 | // +---------------------------------------------------- | 158 | // +---------------------------------------------------- | |
156 | // スクリーンã«æç”»ã™ã‚‹ | 159 | // スクリーンã«æç”»ã™ã‚‹ | |
157 | // +---------------------------------------------------- | 160 | // +---------------------------------------------------- | |
158 | void ic2_DrawFrame (void) { | 161 | void ic2_DrawFrame (void) { | |
159 | static int initflag = 0; | 162 | static int initflag = 0; | |
160 | 163 | |||
161 | // (å‰å‡¦ç†) 以å‰ã«glClearColor()ã§æŒ‡å®šã—ãŸè‰²ã§å¡—ã‚Šæ½°ã™ | 164 | // (å‰å‡¦ç†) 以å‰ã«glClearColor()ã§æŒ‡å®šã—ãŸè‰²ã§å¡—ã‚Šæ½°ã™ | |
162 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 165 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
163 | 166 | |||
164 | // (1) カメラã®è¨ç½® [åˆå›žã®ã¿] | 167 | // (1) カメラã®è¨ç½® [åˆå›žã®ã¿] | |
165 | if (initflag == 0) { | 168 | if (initflag == 0) { | |
166 | ic2_SetUpCamera_Ortho(); | 169 | ic2_SetUpCamera_Ortho(); | |
167 | initflag = 1; | 170 | initflag = 1; | |
168 | } | 171 | } | |
169 | 172 | |||
170 | // (2) å…‰æºã®è¨ç½® | 173 | // (2) å…‰æºã®è¨ç½® | |
171 | ic2_LightSetA(); | 174 | ic2_LightSetA(); | |
172 | 175 | |||
173 | // (3) 物体ã®è¨ç½® | 176 | // (3) 物体ã®è¨ç½® | |
174 | // 物体æç”»:æ£æ–¹å½¢ | 177 | // 物体æç”»:æ£æ–¹å½¢ | |
175 | ic2_FigSquare(0.9); | 178 | ic2_FigSquare(0.9); | |
176 | // 物体æç”»:ティーãƒãƒƒãƒˆ | 179 | // 物体æç”»:ティーãƒãƒƒãƒˆ | |
177 | ic2_FigSolidTeapot(0.5); | 180 | ic2_FigSolidTeapot(0.5); | |
178 | 181 | |||
179 | // (後処ç†) スクリーンã®åˆ‡ã‚Šæ›¿ãˆ | 182 | // (後処ç†) スクリーンã®åˆ‡ã‚Šæ›¿ãˆ | |
180 | glutSwapBuffers(); | 183 | glutSwapBuffers(); | |
181 | 184 | |||
182 | } | 185 | } | |
183 | 186 | |||
184 | // *********************************************************************** | 187 | // *********************************************************************** | |
185 | // callbacks ************************************************************* | 188 | // callbacks ************************************************************* | |
186 | 189 | |||
187 | // +---------------------------------------------------- | 190 | // +---------------------------------------------------- | |
188 | // ã‚ーãŒä½•ã‹æŠ¼ã•ã‚ŒãŸã¨ãã®å¯¾ç–用関数 | 191 | // ã‚ーãŒä½•ã‹æŠ¼ã•ã‚ŒãŸã¨ãã®å¯¾ç–用関数 | |
189 | // +---------------------------------------------------- | 192 | // +---------------------------------------------------- | |
190 | // glutKeyboardFunc()ã«ã¦ç™»éŒ²äºˆå®š | 193 | // glutKeyboardFunc()ã«ã¦ç™»éŒ²äºˆå®š | |
191 | // 引数 : key ... å…¥åŠ›æ–‡å— | 194 | // 引数 : key ... å…¥åŠ›æ–‡å— | |
192 | // 引数 : x ... æ–‡å—ãŒæŠ¼ã•ã‚ŒãŸã¨ãã®ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã®ï¼¸ä½ç½® | 195 | // 引数 : x ... æ–‡å—ãŒæŠ¼ã•ã‚ŒãŸã¨ãã®ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã®ï¼¸ä½ç½® | |
193 | // 引数 : y ... æ–‡å—ãŒæŠ¼ã•ã‚ŒãŸã¨ãã®ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã®ï¼¹ä½ç½® | 196 | // 引数 : y ... æ–‡å—ãŒæŠ¼ã•ã‚ŒãŸã¨ãã®ãƒžã‚¦ã‚¹ã‚«ãƒ¼ã‚½ãƒ«ã®ï¼¹ä½ç½® | |
194 | void ic2_NormalKeyInput (unsigned char key, int x, int y) { | 197 | void ic2_NormalKeyInput (unsigned char key, int x, int y) { | |
195 | float delta_t = 0.1; // [unit] | 198 | float delta_t = 0.1; // [unit] | |
196 | float delta_r = 1.0; // [degree] | 199 | float delta_r = 1.0; // [degree] | |
197 | 200 | |||
198 | switch (key) { | 201 | switch (key) { | |
199 | case 'q' : | 202 | case 'q' : | |
200 | case 'Q' : | 203 | case 'Q' : | |
201 | case 27 : // ESCã‚ーã®ã“㨠| 204 | case 27 : // ESCã‚ーã®ã“㨠| |
202 | exit (0); | 205 | exit (0); | |
203 | break; | 206 | break; | |
204 | 207 | |||
205 | // Translation -_+ : [X]h_l [Y]n_u [Z]j_k | 208 | // Translation -_+ : [X]h_l [Y]n_u [Z]j_k | |
206 | case 'h': glTranslatef(delta_t * -1, 0, 0); break; | 209 | case 'h': glTranslatef(delta_t * -1, 0, 0); break; | |
207 | case 'l': glTranslatef(delta_t * +1, 0, 0); break; | 210 | case 'l': glTranslatef(delta_t * +1, 0, 0); break; | |
208 | case 'n': glTranslatef(0, delta_t * -1, 0); break; | 211 | case 'n': glTranslatef(0, delta_t * -1, 0); break; | |
209 | case 'u': glTranslatef(0, delta_t * +1, 0); break; | 212 | case 'u': glTranslatef(0, delta_t * +1, 0); break; | |
210 | case 'j': glTranslatef(0, 0, delta_t * -1); break; | 213 | case 'j': glTranslatef(0, 0, delta_t * -1); break; | |
211 | case 'k': glTranslatef(0, 0, delta_t * +1); break; | 214 | case 'k': glTranslatef(0, 0, delta_t * +1); break; | |
212 | 215 | |||
213 | // Rotation -_+ : [Y]a_f [Z]s_d [X]x_w | 216 | // Rotation -_+ : [Y]a_f [Z]s_d [X]x_w | |
214 | case 'x': glRotatef(delta_r * -1, 1, 0, 0); break; | 217 | case 'x': glRotatef(delta_r * -1, 1, 0, 0); break; | |
215 | case 'w': glRotatef(delta_r * +1, 1, 0, 0); break; | 218 | case 'w': glRotatef(delta_r * +1, 1, 0, 0); break; | |
216 | case 'a': glRotatef(delta_r * -1, 0, 1, 0); break; | 219 | case 'a': glRotatef(delta_r * -1, 0, 1, 0); break; | |
217 | case 'f': glRotatef(delta_r * +1, 0, 1, 0); break; | 220 | case 'f': glRotatef(delta_r * +1, 0, 1, 0); break; | |
218 | case 's': glRotatef(delta_r * -1, 0, 0, 1); break; | 221 | case 's': glRotatef(delta_r * -1, 0, 0, 1); break; | |
219 | case 'd': glRotatef(delta_r * +1, 0, 0, 1); break; | 222 | case 'd': glRotatef(delta_r * +1, 0, 0, 1); break; | |
220 | 223 | |||
221 | // [Scale] v_b | 224 | // [Scale] v_b | |
222 | case 'v': glScalef(0.95, 0.95, 0.95); break; | 225 | case 'v': glScalef(0.95, 0.95, 0.95); break; | |
223 | case 'b': glScalef(1.05, 1.05, 1.05); break; | 226 | case 'b': glScalef(1.05, 1.05, 1.05); break; | |
224 | 227 | |||
225 | // [Reset] | 228 | // [Reset] | |
226 | case 'R': | 229 | case 'R': | |
227 | glMatrixMode(GL_MODELVIEW); | 230 | glMatrixMode(GL_MODELVIEW); | |
228 | glPopMatrix(); // ä¿è·ã•ã‚Œã¦ãŸç¬¬ï¼‘階層ã«é™ã‚Šã‚‹ | 231 | glPopMatrix(); // ä¿è·ã•ã‚Œã¦ãŸç¬¬ï¼‘階層ã«é™ã‚Šã‚‹ | |
229 | glPushMatrix(); // ä¿è·ã•ã‚Œã¦ãŸç¬¬ï¼‘階層ã‹ã‚‰ã‚³ãƒ”ーã—ã¦ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—を1ã¤ä¸Šã’ã‚‹ | 232 | glPushMatrix(); // ä¿è·ã•ã‚Œã¦ãŸç¬¬ï¼‘階層ã‹ã‚‰ã‚³ãƒ”ーã—ã¦ã‚¹ã‚¿ãƒƒã‚¯ãƒˆãƒƒãƒ—を1ã¤ä¸Šã’ã‚‹ | |
230 | break; | 233 | break; | |
231 | 234 | |||
232 | // [Show Stacktop MODELVIEW Matrix] | 235 | // [Show Stacktop MODELVIEW Matrix] | |
233 | case 'p': | 236 | case 'p': | |
234 | ic2_ShowMATRIX("Current status"); | 237 | ic2_ShowMATRIX("Current status"); | |
235 | break; | 238 | break; | |
236 | 239 | |||
237 | } | 240 | } | |
241 | ||||
242 | // 次ã®ãƒ¡ã‚¤ãƒ³ãƒ«ãƒ¼ãƒ—(glutMainLoop)ã§ã®ç¹°ã‚Šè¿”ã—時ã«æ画をè¦æ±‚ | |||
243 | glutPostWindowRedisplay(window_id); | |||
244 | } | |||
245 | ||||
246 | // +---------------------------------------------------- | |||
247 | // ウィンドウサイズã®å¤‰æ›´ãŒç”Ÿã˜ãŸã¨ãã®å¯¾ç–用関数 | |||
248 | // +---------------------------------------------------- | |||
249 | // glutReshapeFunc()ã«ã¦ç™»éŒ² | |||
250 | void ic2_ReshapeWindow (int w, int h) { | |||
251 | ||||
252 | // 次ã®ãƒ¡ã‚¤ãƒ³ãƒ«ãƒ¼ãƒ—(glutMainLoop)ã§ã®ç¹°ã‚Šè¿”ã—時ã«æ画をè¦æ±‚ | |||
253 | glutPostWindowRedisplay(window_id); | |||
238 | } | 254 | } | |
239 | 255 | |||
240 | // +---------------------------------------------------- | 256 | // +---------------------------------------------------- | |
241 | // OpenGLã¨ã—ã¦ã®Windowã®åˆæœŸåŒ– | 257 | // OpenGLã¨ã—ã¦ã®Windowã®åˆæœŸåŒ– | |
242 | // +---------------------------------------------------- | 258 | // +---------------------------------------------------- | |
243 | void ic2_BootWindow (char winname[]) { | 259 | void ic2_BootWindow (char winname[]) { | |
244 | 260 | |||
245 | // ダブルãƒãƒƒãƒ•ã‚¡,RGB表色モード,デプスãƒãƒƒãƒ•ã‚¡ を利用 | 261 | // ダブルãƒãƒƒãƒ•ã‚¡,RGB表色モード,デプスãƒãƒƒãƒ•ã‚¡ を利用 | |
246 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); | 262 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); | |
247 | 263 | |||
248 | // ウィンドウã®å¤§ãã• | 264 | // ウィンドウã®å¤§ãã• | |
249 | glutInitWindowSize(window_w, window_h); | 265 | glutInitWindowSize(window_w, window_h); | |
250 | 266 | |||
251 | // ウィンドウを開ã | 267 | // ウィンドウを開ã | |
252 | glutCreateWindow(winname); | 268 | window_id = glutCreateWindow(winname); | |
253 | 269 | |||
254 | // レンダリングã«ã¯Smooth Shadingを採用 | 270 | // レンダリングã«ã¯Smooth Shadingを採用 | |
255 | glShadeModel(GL_SMOOTH); | 271 | glShadeModel(GL_SMOOTH); | |
256 | 272 | |||
257 | // ウィンドウ全体を書ãç›´ã™ã¨ãã®è‰²(ã“ã“ã§ã¯é»’) | 273 | // ウィンドウ全体を書ãç›´ã™ã¨ãã®è‰²(ã“ã“ã§ã¯é»’) | |
258 | glClearColor(0.0, 0.0, 0.0, 0.0); | 274 | glClearColor(0.0, 0.0, 0.0, 0.0); | |
259 | 275 | |||
260 | // åˆæœŸMODELVIEW matrixã®ä¿è· | 276 | // åˆæœŸMODELVIEW matrixã®ä¿è· | |
261 | glMatrixMode(GL_MODELVIEW); | 277 | glMatrixMode(GL_MODELVIEW); | |
262 | glPushMatrix(); // 以後本プãƒã‚°ãƒ©ãƒ ã§ã¯ GL_MODELVIEW スタックã®ï¼’層目以上ã§ä½œæ¥ | 278 | glPushMatrix(); // 以後本プãƒã‚°ãƒ©ãƒ ã§ã¯ GL_MODELVIEW スタックã®ï¼’層目以上ã§ä½œæ¥ | |
263 | 279 | |||
264 | // Callback関数をè¨å®š (イベント処ç†) | 280 | // Callback関数をè¨å®š (イベント処ç†) | |
265 | glutIdleFunc(ic2_DrawFrame); // 暇ã ã£ãŸã‚‰ãƒ•ãƒ¬ãƒ¼ãƒ ã‚’æã(よã„実装ã§ã¯ãªã„) | 281 | glutIdleFunc(ic2_DrawFrame); // 暇ã ã£ãŸã‚‰ãƒ•ãƒ¬ãƒ¼ãƒ ã‚’æã(よã„実装ã§ã¯ãªã„) | |
266 | glutKeyboardFunc(ic2_NormalKeyInput); // ã‚ーãŒæŠ¼ã•ã‚ŒãŸã¨ãã®å¯¾ç– | 282 | glutKeyboardFunc(ic2_NormalKeyInput); // ã‚ーãŒæŠ¼ã•ã‚ŒãŸã¨ãã®å¯¾ç– | |
283 | glutReshapeFunc(ic2_ReshapeWindow); // ウィンドウサイズ変更ãŒæ¤œçŸ¥ã•ã‚ŒãŸã¨ãã®å¯¾ç– | |||
267 | } | 284 | } | |
268 | 285 | |||
269 | // *********************************************************************** | 286 | // *********************************************************************** | |
270 | // main ***************************************************************** | 287 | // main ***************************************************************** | |
271 | // +---------------------------------------------------- | 288 | // +---------------------------------------------------- | |
272 | // Main Function | 289 | // Main Function | |
273 | // +---------------------------------------------------- | 290 | // +---------------------------------------------------- | |
274 | int main (int argc, char *argv[]) { | 291 | int main (int argc, char *argv[]) { | |
275 | 292 | |||
276 | // glutライブラリã«ã‚ˆã‚‹å¼•æ•°ã®è§£é‡ˆ | 293 | // glutライブラリã«ã‚ˆã‚‹å¼•æ•°ã®è§£é‡ˆ | |
277 | glutInit(&argc, argv); | 294 | glutInit(&argc, argv); | |
278 | 295 | |||
279 | // OpenGL Window ã®åˆæœŸåŒ– | 296 | // OpenGL Window ã®åˆæœŸåŒ– | |
280 | ic2_BootWindow(argv[0]); | 297 | ic2_BootWindow(argv[0]); | |
281 | 298 | |||
282 | // ç„¡é™ãƒ«ãƒ¼ãƒ—ã®é–‹å§‹ | 299 | // ç„¡é™ãƒ«ãƒ¼ãƒ—ã®é–‹å§‹ | |
283 | glutMainLoop(); | 300 | glutMainLoop(); | |
284 | 301 | |||
285 | return 0; | 302 | return 0; | |
286 | } | 303 | } |