comparison Renderer/Engine/Camera.cc @ 507:735f76483bb2

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