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