comparison TaskManager/Test/test_render/Camera.cpp @ 221:d61fded0729e

Cameraの設定、Makefile 修正
author gongo@gendarme.local
date Tue, 10 Feb 2009 01:47:12 +0900
parents 0f1ff7b06157
children 5ffee529dc59
comparison
equal deleted inserted replaced
220:29e338dbc280 221:d61fded0729e
7 camera_move(SceneGraphPtr _node, int screen_w, int screen_h) 7 camera_move(SceneGraphPtr _node, int screen_w, int screen_h)
8 { 8 {
9 Pad *pad = sgroot->getController(); 9 Pad *pad = sgroot->getController();
10 CameraPtr node = (CameraPtr)_node; 10 CameraPtr node = (CameraPtr)_node;
11 11
12 if (pad->r2.isHold()) { 12 if (pad->right.isHold()) {
13 node->angle[1] += 2.0f; 13 node->xyz[0] += 10.0f;
14 if (node->angle[1] > 360.0f) { 14 } else if (pad->left.isHold()) {
15 node->angle[1] -= 360.0f; 15 node->xyz[0] -= 10.0f;
16 } 16 }
17 } else if (pad->l2.isHold()) { 17
18 node->angle[1] -= 2.0f;
19 if (node->angle[1] < 0.0f) {
20 node->angle[1] += 360.0f;
21 }
22 }
23
24 #if 0
25 if (pad->up.isPush() || pad->up.isHold()) { 18 if (pad->up.isPush() || pad->up.isHold()) {
26 node->xyz[1] += 2.0f; 19 node->xyz[1] += 2.0f;
27 } else if (pad->down.isPush() || pad->down.isHold()) { 20 } else if (pad->down.isPush() || pad->down.isHold()) {
28 node->xyz[1] -= 2.0f; 21 node->xyz[1] -= 2.0f;
29 } 22 }
30 #endif
31 23
32 if (pad->r1.isPush() || pad->r1.isHold()) { 24 if (pad->r1.isPush() || pad->r1.isHold()) {
33 node->xyz[2] += 10.0f; 25 node->xyz[2] += 10.0f;
34 } else if (pad->l1.isPush() || pad->l1.isHold()) { 26 } else if (pad->l1.isPush() || pad->l1.isHold()) {
35 node->xyz[2] -= 10.0f; 27 node->xyz[2] -= 10.0f;
36 } 28 }
29
30 if (pad->r2.isHold()) {
31 if (node->zd[0] <= 1.0f) {
32 node->zd[0] += 0.02f;
33 }
34 if (node->zd[2] >= 0.0f) {
35 node->zd[2] -= 0.02f;
36 }
37 } else if (pad->l2.isHold()) {
38 if (node->zd[0] > -1.0f) {
39 node->zd[0] -= -0.02f;
40 }
41 if (node->zd[2] >= 0.0f) {
42 node->zd[2] -= 0.02f;
43 }
44 } else {
45 node->zd[0] = 0.0f;
46 node->zd[2] = 1.0f;
47 }
48
37 } 49 }
38 50
39 static void 51 static void
40 camera_collision(SceneGraphPtr node, int screen_w, int screen_h, 52 camera_collision(SceneGraphPtr node, int screen_w, int screen_h,
41 SceneGraphPtr tree) 53 SceneGraphPtr tree)
42 { 54 {
43 } 55 }
44 56
45 Camera::Camera(void) 57 /**
58 * @param w Width of screen
59 * @param h Height of screen
60 */
61 Camera::Camera(float w, float h)
46 { 62 {
47 name = (const char*)"Camera"; 63 name = (const char*)"Camera";
48 64
49 zd[0] = 0.0f; 65 zd[0] = 0.0f;
50 zd[1] = 0.0f; 66 zd[1] = 0.0f;
58 74
59 fov = 60.0f; 75 fov = 60.0f;
60 near = 0.0f; 76 near = 0.0f;
61 far = 1000.0f; 77 far = 1000.0f;
62 78
63 //xyz[0] = 320.0f; 79 m_view = new float[16];
64 //xyz[1] = 240.0f; 80 m_pers = new float[16];
65 //xyz[2] = -100.0f; 81 m_screen = new float[16];
66 //xyz[3] = 1.0f; 82
83 // Screen の真ん中を初期値とする
84 xyz[0] = w/2.0f;
85 xyz[1] = h/2.0f;
86
87 // min(w, h) がちょうど一杯見えるような z の位置の計算
88 xyz[2] = -(((xyz[1] < xyz[0]) ? xyz[1] : xyz[0])/tanf((fov/2.0f)*M_PI/180.0f));
89 xyz[3] = 1.0f;
67 90
68 this->set_move_collision(camera_move, camera_collision); 91 this->set_move_collision(camera_move, camera_collision);
69 } 92 }
70 93
71 void 94 Camera::~Camera(void)
72 Camera::createRotMatrix(float *m) 95 {
96 delete [] m_view;
97 delete [] m_pers;
98 delete [] m_screen;
99 }
100
101 void
102 Camera::updateView(void)
73 { 103 {
74 float radx,rady,radz; 104 float radx,rady,radz;
75 float cx[4], cy[4], cz[4]; 105 float cx[4], cy[4], cz[4], p[4];
76 float p[4]; 106 float tm[16];
77 float *matrix = new float[16];
78 107
79 radx = angle[0]*3.14/180; 108 radx = angle[0]*3.14/180;
80 rady = angle[1]*3.14/180; 109 rady = angle[1]*3.14/180;
81 radz = angle[2]*3.14/180; 110 radz = angle[2]*3.14/180;
82 111
86 float cosy = cos(rady); 115 float cosy = cos(rady);
87 float sinz = sin(radz); 116 float sinz = sin(radz);
88 float cosz = cos(radz); 117 float cosz = cos(radz);
89 118
90 /* View Transform */ 119 /* View Transform */
91 matrix[0] = cosz*cosy+sinz*sinx*siny; 120 tm[0] = cosz*cosy+sinz*sinx*siny;
92 matrix[1] = sinz*cosx; 121 tm[1] = sinz*cosx;
93 matrix[2] = -cosz*siny+sinz*sinx*cosy; 122 tm[2] = -cosz*siny+sinz*sinx*cosy;
94 matrix[3] = 0.0f; 123 tm[3] = 0.0f;
95 matrix[4] = -sinz*cosy+cosz*sinx*siny; 124 tm[4] = -sinz*cosy+cosz*sinx*siny;
96 matrix[5] = cosz*cosx; 125 tm[5] = cosz*cosx;
97 matrix[6] = sinz*siny+cosz*sinx*cosy; 126 tm[6] = sinz*siny+cosz*sinx*cosy;
98 matrix[7] = 0.0f; 127 tm[7] = 0.0f;
99 matrix[8] = cosx*siny; 128 tm[8] = cosx*siny;
100 matrix[9] = -sinx; 129 tm[9] = -sinx;
101 matrix[10] = cosx*cosy; 130 tm[10] = cosx*cosy;
102 matrix[11] = 0.0f; 131 tm[11] = 0.0f;
103 matrix[12] = 0.0f; 132 tm[12] = 0.0f;
104 matrix[13] = 0.0f; 133 tm[13] = 0.0f;
105 matrix[14] = 0.0f; 134 tm[14] = 0.0f;
106 matrix[15] = 1.0f; 135 tm[15] = 1.0f;
107 136
108 applyMatrix(cz, matrix, zd); 137 applyMatrix(cz, tm, zd);
109 applyMatrix(cy, matrix, yd); 138 applyMatrix(cy, tm, yd);
110 applyMatrix(p, matrix, xyz); 139 applyMatrix(p, tm, xyz);
111
112 // matrix 使い回すためにクリア
113 unitMatrix(matrix);
114 140
115 outerProduct(cx, cy, cz); 141 outerProduct(cx, cy, cz);
116 normalize(&matrix[0], cx); 142 normalize(cx, cx);
117 normalize(&matrix[8], cz); 143 normalize(cz, cz);
118 outerProduct(&matrix[4], &matrix[8], &matrix[0]); 144 outerProduct(cy, cz, cx);
119 transMatrix(matrix, matrix, p); 145
120 inversMatrix(m, matrix); 146 m_view[ 0] = cx[0];
121 } 147 m_view[ 1] = cy[0];
122 148 m_view[ 2] = cz[0];
123 void 149 m_view[ 3] = 0.0f;
124 Camera::createViewTransformMatrix(float *view, float *cx, float *cy, float *cz) 150
125 { 151 m_view[ 4] = cx[1];
126 view[ 0] = cx[0]; 152 m_view[ 5] = cy[1];
127 view[ 1] = cy[0]; 153 m_view[ 6] = cz[1];
128 view[ 2] = cz[0]; 154 m_view[ 7] = 0.0f;
129 view[ 3] = 0.0f; 155
130 156 m_view[ 8] = cx[2];
131 view[ 4] = cx[1]; 157 m_view[ 9] = cy[2];
132 view[ 5] = cy[1]; 158 m_view[10] = cz[2];
133 view[ 6] = cz[1]; 159 m_view[11] = 0.0f;
134 view[ 7] = 0.0f; 160
135 161 m_view[12] = innerProduct(xyz, cx)*(-1);
136 view[ 8] = cx[2]; 162 m_view[13] = innerProduct(xyz, cy)*(-1);
137 view[ 9] = cy[2]; 163 m_view[14] = innerProduct(xyz, cz)*(-1);
138 view[10] = cz[2]; 164 m_view[15] = 1.0f;
139 view[11] = 0.0f; 165 }
140 166
141 view[12] = innerProduct(xyz, cx)*(-1); 167 void
142 view[13] = innerProduct(xyz, cy)*(-1); 168 Camera::updatePerspective(float w, float h)
143 view[14] = innerProduct(xyz, cz)*(-1);
144 view[15] = 1.0f;
145 }
146
147 void
148 Camera::createPerspectiveTransformMatrix(float *pm, float aspect)
149 { 169 {
150 float sx, sy, sz; 170 float sx, sy, sz;
151 171 float aspect = w/h;
152 sy = 1.0f/tanf((fov/2.0f)*M_PI/180.0f); 172
153 sx = sy / aspect; 173 sy = (1.0f/tanf((fov/2.0f)*M_PI/180.0f));
154 sz = far/(far+near); 174 sx = sy/aspect;
155 175 //sz = far/(far+near);
156 pm[ 0] = sx; 176 sz = far/(far-near);
157 pm[ 1] = 0.0f; 177
158 pm[ 2] = 0.0f; 178 m_pers[ 0] = sx;
159 pm[ 3] = 0.0f; 179 m_pers[ 1] = 0.0f;
160 180 m_pers[ 2] = 0.0f;
161 pm[ 4] = 0.0f; 181 m_pers[ 3] = 0.0f;
162 pm[ 5] = sy; 182
163 pm[ 6] = 0.0f; 183 m_pers[ 4] = 0.0f;
164 pm[ 7] = 0.0f; 184 m_pers[ 5] = sy;
165 185 m_pers[ 6] = 0.0f;
166 pm[ 8] = 0.0f; 186 m_pers[ 7] = 0.0f;
167 pm[ 9] = 0.0f; 187
168 pm[10] = sz; 188 m_pers[ 8] = 0.0f;
169 pm[11] = -near*sz; 189 m_pers[ 9] = 0.0f;
170 190 m_pers[10] = sz;
171 pm[12] = 0.0f; 191 m_pers[11] = 1.0f;
172 pm[13] = 0.0f; 192
173 pm[14] = -1.0f; 193 m_pers[12] = 0.0f;
174 pm[15] = 0.0f; 194 m_pers[13] = 0.0f;
175 } 195 m_pers[14] = -near*sz;
176 196 m_pers[15] = 0.0f;
177 void 197 }
178 Camera::createScreenTransformMatrix(float *sm, float _w, float _h) 198
199 void
200 Camera::updateScreen(float _w, float _h)
179 { 201 {
180 float w = _w/2.0f; 202 float w = _w/2.0f;
181 float h = _h/2.0f; 203 float h = _h/2.0f;
182 float r = far/(far-near); 204
183 205 m_screen[ 0] = w;
184 sm[ 0] = w; 206 m_screen[ 1] = 0.0f;
185 sm[ 1] = 0.0f; 207 m_screen[ 2] = 0.0f;
186 sm[ 2] = 0.0f; 208 m_screen[ 3] = 0.0f;
187 sm[ 3] = 0.0f; 209
188 210 m_screen[ 4] = 0.0f;
189 sm[ 4] = 0.0f; 211 m_screen[ 5] = h;
190 sm[ 5] = h; 212 m_screen[ 6] = 0.0f;
191 sm[ 6] = 0.0f; 213 m_screen[ 7] = 0.0f;
192 sm[ 7] = 0.0f; 214
193 215 m_screen[ 8] = 0.0f;
194 sm[ 8] = 0.0f; 216 m_screen[ 9] = 0.0f;
195 sm[ 9] = 0.0f; 217 m_screen[10] = 1.0f;
196 sm[10] = 1.0f; 218 m_screen[11] = 0.0f;
197 sm[11] = 0.0f; 219
198 220 m_screen[12] = w;
199 sm[12] = w; 221 m_screen[13] = h;
200 sm[13] = h; 222 m_screen[14] = 0.0f;
201 sm[14] = 0.0f; 223 m_screen[15] = 1.0f;
202 sm[15] = 1.0f;
203 } 224 }
204 225
205 void 226 void
206 Camera::setCamera(float *pose) 227 Camera::setCamera(float *pose)
207 { 228 {
208 memcpy(xyz, &pose[12], sizeof(float)*4); 229 //memcpy(xyz, &pose[12], sizeof(float)*4);
209 } 230 }
210 231
211 void 232 void
212 Camera::update(int w, int h) 233 Camera::update(float w, float h)
213 { 234 {
214 #if 0 235 #if 1
215 // うーん。。。
216 float view[16]; // view transform matrix
217 float pers[16]; // perspective transform matrix
218 float screen[16]; // screen transform matrix
219 float tmp[16]; 236 float tmp[16];
220 237
221 // view tansform 238 updateView();
222 createRotMatrix(view); 239 updatePerspective(w, h);
223 createPerspectiveTransformMatrix(pers, (float)w/(float)h); 240 updateScreen(w, h);
224 createScreenTransformMatrix(screen, (float)w, (float)h);
225 241
226 matrix4x4(tmp, view, pers); 242 matrix4x4(tmp, this->m_pers, this->m_screen);
227 matrix4x4(matrix, tmp, screen); 243 matrix4x4(this->matrix, this->m_view, tmp);
228 //matrix4x4(tmp, pers, view);
229 //matrix4x4(matrix, screen, tmp);
230 #else 244 #else
231 get_matrix(matrix, angle, xyz, NULL); 245 get_matrix(matrix, angle, xyz, NULL);
232 #endif 246 #endif
233 } 247 }