comparison Renderer/test_render/Camera.cpp @ 283:15bfacccde99 draft

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