diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-10-15 07:17:50 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-10-15 07:17:50 +0000 |
commit | 9277d8724e33e42695b0ab7049be788f69dfe56e (patch) | |
tree | 4456243a976600a40d60f2f5e3c1aecd2ad379f1 /apps/recorder/cube.c | |
parent | fead32cb8dd6e980a4521758cb47701c97b9c8c3 (diff) | |
download | rockbox-9277d8724e33e42695b0ab7049be788f69dfe56e.tar.gz rockbox-9277d8724e33e42695b0ab7049be788f69dfe56e.zip |
Rotating cube demo.
Original code by Damien Teney, modified to integer math by Andreas Zwirtes,
code policed by Daniel Stenberg (think 'static').
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2627 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/recorder/cube.c')
-rw-r--r-- | apps/recorder/cube.c | 334 |
1 files changed, 334 insertions, 0 deletions
diff --git a/apps/recorder/cube.c b/apps/recorder/cube.c new file mode 100644 index 0000000000..5d93f8de1f --- /dev/null +++ b/apps/recorder/cube.c | |||
@@ -0,0 +1,334 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Damien Teney | ||
11 | * modified to use int instead of float math by Andreas Zwirtes | ||
12 | * | ||
13 | * All files in this archive are subject to the GNU General Public License. | ||
14 | * See the file COPYING in the source tree root for full license agreement. | ||
15 | * | ||
16 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
17 | * KIND, either express or implied. | ||
18 | * | ||
19 | ****************************************************************************/ | ||
20 | |||
21 | #include "config.h" | ||
22 | #include "options.h" | ||
23 | |||
24 | #ifdef USE_DEMOS | ||
25 | |||
26 | #include <stdlib.h> | ||
27 | #include "lcd.h" | ||
28 | #include "config.h" | ||
29 | #include "kernel.h" | ||
30 | #include "menu.h" | ||
31 | #include "button.h" | ||
32 | #include "sprintf.h" | ||
33 | |||
34 | typedef struct | ||
35 | {long x,y,z;} point3D; | ||
36 | |||
37 | typedef struct | ||
38 | {long x,y;} point2D; | ||
39 | |||
40 | static point3D Sommet[8]; | ||
41 | static point3D Point3D[8]; | ||
42 | static point2D Point2D[8]; | ||
43 | |||
44 | static int Nb_points = 8; | ||
45 | |||
46 | static int Xoff = 56; | ||
47 | static int Yoff = 95; | ||
48 | static int Zoff = 600; | ||
49 | |||
50 | /* Precalculated sine and cosine * 10000 (four digit fixed point math) */ | ||
51 | static int SinTable[91] = | ||
52 | { | ||
53 | 0, 174, 348, 523, 697, | ||
54 | 871,1045,1218,1391,1564, | ||
55 | 1736,1908,2079,2249,2419, | ||
56 | 2588,2756,2923,3090,3255, | ||
57 | 3420,3583,3746,3907,4067, | ||
58 | 4226,4383,4539,4694,4848, | ||
59 | 5000,5150,5299,5446,5591, | ||
60 | 5735,5877,6018,6156,6293, | ||
61 | 6427,6560,6691,6819,6946, | ||
62 | 7071,7193,7313,7431,7547, | ||
63 | 7660,7771,7880,7986,8090, | ||
64 | 8191,8290,8386,8480,8571, | ||
65 | 8660,8746,8829,8910,8987, | ||
66 | 9063,9135,9205,9271,9335, | ||
67 | 9396,9455,9510,9563,9612, | ||
68 | 9659,9702,9743,9781,9816, | ||
69 | 9848,9876,9902,9925,9945, | ||
70 | 9961,9975,9986,9993,9998, | ||
71 | 10000 | ||
72 | }; | ||
73 | |||
74 | static long Sin(int val) | ||
75 | { | ||
76 | /* Speed improvement through sukzessive lookup */ | ||
77 | if (val<181) | ||
78 | { | ||
79 | if (val<91)/* phase 0-90 degree */ | ||
80 | { | ||
81 | return (long)SinTable[val]; | ||
82 | } | ||
83 | else/* phase 91-180 degree */ | ||
84 | { | ||
85 | return (long)SinTable[180-val]; | ||
86 | } | ||
87 | } | ||
88 | else | ||
89 | { | ||
90 | if (val<271)/* phase 181-270 degree */ | ||
91 | { | ||
92 | return (-1L)*(long)SinTable[val-180]; | ||
93 | } | ||
94 | else/* phase 270-359 degree */ | ||
95 | { | ||
96 | return (-1L)*(long)SinTable[360-val]; | ||
97 | } | ||
98 | } | ||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | static long Cos(int val) | ||
103 | { | ||
104 | /* Speed improvement through sukzessive lookup */ | ||
105 | if (val<181) | ||
106 | { | ||
107 | if (val<91)/* phase 0-90 degree */ | ||
108 | { | ||
109 | return (long)SinTable[90-val]; | ||
110 | } | ||
111 | else/* phase 91-180 degree */ | ||
112 | { | ||
113 | return (-1L)*(long)SinTable[val-90]; | ||
114 | } | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | if (val<271)/* phase 181-270 degree */ | ||
119 | { | ||
120 | return (-1L)*(long)SinTable[270-val]; | ||
121 | } | ||
122 | else/* phase 270-359 degree */ | ||
123 | { | ||
124 | return (long)SinTable[val-270]; | ||
125 | } | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | static long matrice[3][3]; | ||
131 | |||
132 | static void cube_rotate(int Xa, int Ya, int Za) | ||
133 | { | ||
134 | int i; | ||
135 | |||
136 | /* Just to prevent unnecessary lookups */ | ||
137 | long sxa,cxa,sya,cya,sza,cza; | ||
138 | sxa=Sin(Xa); | ||
139 | cxa=Cos(Xa); | ||
140 | sya=Sin(Ya); | ||
141 | cya=Cos(Ya); | ||
142 | sza=Sin(Za); | ||
143 | cza=Cos(Za); | ||
144 | |||
145 | /* calculate overall translation matrix */ | ||
146 | matrice[0][0] = cza*cya/10000L; | ||
147 | matrice[1][0] = sza*cya/10000L; | ||
148 | matrice[2][0] = -sya; | ||
149 | |||
150 | matrice[0][1] = cza*sya/10000L*sxa/10000L - sza*cxa/10000L; | ||
151 | matrice[1][1] = sza*sya/10000L*sxa/10000L + cxa*cza/10000L; | ||
152 | matrice[2][1] = sxa*cya/10000L; | ||
153 | |||
154 | matrice[0][2] = cza*sya/10000L*cxa/10000L + sza*sxa/10000L; | ||
155 | matrice[1][2] = sza*sya/10000L*cxa/10000L - cza*sxa/10000L; | ||
156 | matrice[2][2] = cxa*cya/10000L; | ||
157 | |||
158 | /* apply translation matrix to all points */ | ||
159 | for(i=0;i<Nb_points;i++) | ||
160 | { | ||
161 | Point3D[i].x = matrice[0][0]*Sommet[i].x | ||
162 | + matrice[1][0]*Sommet[i].y | ||
163 | + matrice[2][0]*Sommet[i].z; | ||
164 | |||
165 | Point3D[i].y = matrice[0][1]*Sommet[i].x | ||
166 | + matrice[1][1]*Sommet[i].y | ||
167 | + matrice[2][1]*Sommet[i].z; | ||
168 | |||
169 | Point3D[i].z = matrice[0][2]*Sommet[i].x | ||
170 | + matrice[1][2]*Sommet[i].y | ||
171 | + matrice[2][2]*Sommet[i].z; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | static void cube_viewport(void) | ||
176 | { | ||
177 | int i; | ||
178 | |||
179 | /* Do viewport transformation for all points */ | ||
180 | for(i=0;i<Nb_points;i++) | ||
181 | { | ||
182 | Point2D[i].x=(((Point3D[i].x)<<8)/10000L)/ | ||
183 | (Point3D[i].z/10000L+Zoff)+Xoff; | ||
184 | Point2D[i].y=(((Point3D[i].y)<<8)/10000L)/ | ||
185 | (Point3D[i].z/10000L+Zoff)+Yoff; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | static void cube_init(void) | ||
190 | { | ||
191 | /* Original 3D-position of cube's corners */ | ||
192 | Sommet[0].x = -40; Sommet[0].y = -40; Sommet[0].z = -40; | ||
193 | Sommet[1].x = 40; Sommet[1].y = -40; Sommet[1].z = -40; | ||
194 | Sommet[2].x = 40; Sommet[2].y = 40; Sommet[2].z = -40; | ||
195 | Sommet[3].x = -40; Sommet[3].y = 40; Sommet[3].z = -40; | ||
196 | Sommet[4].x = 40; Sommet[4].y = -40; Sommet[4].z = 40; | ||
197 | Sommet[5].x = -40; Sommet[5].y = -40; Sommet[5].z = 40; | ||
198 | Sommet[6].x = -40; Sommet[6].y = 40; Sommet[6].z = 40; | ||
199 | Sommet[7].x = 40; Sommet[7].y = 40; Sommet[7].z = 40; | ||
200 | } | ||
201 | |||
202 | static void line(int a, int b) | ||
203 | { | ||
204 | lcd_drawline(Point2D[a].x,Point2D[a].y,Point2D[b].x,Point2D[b].y); | ||
205 | } | ||
206 | |||
207 | static void cube_draw(void) | ||
208 | { | ||
209 | /* Draws front face */ | ||
210 | line(0,1); line(1,2); | ||
211 | line(2,3); line(3,0); | ||
212 | |||
213 | /* Draws rear face */ | ||
214 | line(4,5); line(5,6); | ||
215 | line(6,7); line(7,4); | ||
216 | |||
217 | /* Draws the other edges */ | ||
218 | line(0,5); | ||
219 | line(1,4); | ||
220 | line(2,7); | ||
221 | line(3,6); | ||
222 | } | ||
223 | |||
224 | |||
225 | /* Loops that the values are displayed */ | ||
226 | #define DISP_TIME 30 | ||
227 | |||
228 | bool cube(void) | ||
229 | { | ||
230 | int t_disp=0; | ||
231 | char buffer[30]; | ||
232 | |||
233 | int xa=0; | ||
234 | int ya=0; | ||
235 | int za=0; | ||
236 | int xs=1; | ||
237 | int ys=3; | ||
238 | int zs=1; | ||
239 | bool highspeed=0; | ||
240 | bool exit=0; | ||
241 | |||
242 | cube_init(); | ||
243 | |||
244 | while(!exit) | ||
245 | { | ||
246 | if (!highspeed) sleep(4); | ||
247 | |||
248 | lcd_clear_display(); | ||
249 | cube_rotate(xa,ya,za); | ||
250 | cube_viewport(); | ||
251 | cube_draw(); | ||
252 | if (t_disp>0) | ||
253 | { | ||
254 | t_disp--; | ||
255 | snprintf(buffer, 30, "x:%d y:%d z:%d h:%d",xs,ys,zs,highspeed); | ||
256 | lcd_putsxy(0, 56, buffer); | ||
257 | } | ||
258 | lcd_update(); | ||
259 | |||
260 | xa+=xs; | ||
261 | if (xa>359) | ||
262 | xa-=360; | ||
263 | if (xa<0) | ||
264 | xa+=360; | ||
265 | ya+=ys; | ||
266 | if (ya>359) | ||
267 | ya-=360; | ||
268 | if (ya<0) | ||
269 | ya+=360; | ||
270 | za+=zs; | ||
271 | if (za>359) | ||
272 | za-=360; | ||
273 | if (za<0) | ||
274 | za+=360; | ||
275 | |||
276 | switch(button_get(false)) | ||
277 | { | ||
278 | case BUTTON_RIGHT: | ||
279 | xs+=1; | ||
280 | if (xs>10) | ||
281 | xs=10; | ||
282 | t_disp=DISP_TIME; | ||
283 | break; | ||
284 | case BUTTON_LEFT: | ||
285 | xs-=1; | ||
286 | if (xs<-10) | ||
287 | xs=-10; | ||
288 | t_disp=DISP_TIME; | ||
289 | break; | ||
290 | case BUTTON_UP: | ||
291 | ys+=1; | ||
292 | if (ys>10) | ||
293 | ys=10; | ||
294 | t_disp=DISP_TIME; | ||
295 | break; | ||
296 | case BUTTON_DOWN: | ||
297 | ys-=1; | ||
298 | if (ys<-10) | ||
299 | ys=-10; | ||
300 | t_disp=DISP_TIME; | ||
301 | break; | ||
302 | case BUTTON_F2: | ||
303 | zs+=1; | ||
304 | if (zs>10) | ||
305 | zs=10; | ||
306 | t_disp=DISP_TIME; | ||
307 | break; | ||
308 | case BUTTON_F1: | ||
309 | zs-=1; | ||
310 | if (zs<-10) | ||
311 | zs=-10; | ||
312 | t_disp=DISP_TIME; | ||
313 | break; | ||
314 | case BUTTON_PLAY: | ||
315 | highspeed=!highspeed; | ||
316 | t_disp=DISP_TIME; | ||
317 | break; | ||
318 | case BUTTON_OFF: | ||
319 | exit=1; | ||
320 | break; | ||
321 | } | ||
322 | } | ||
323 | return 1; | ||
324 | } | ||
325 | |||
326 | #endif /* USE_DEMOS */ | ||
327 | |||
328 | /* ----------------------------------------------------------------- | ||
329 | * local variables: | ||
330 | * eval: (load-file "../../firmware/rockbox-mode.el") | ||
331 | * end: | ||
332 | * vim: et sw=4 ts=8 sts=4 tw=78 | ||
333 | */ | ||
334 | |||