fix remapping behavior. Remapping is only necessary if we are rendering on the workbe...
[AROS-Contrib.git] / Demo / kdp / motionblur.c
blob7b15cba1da62f82ed934c8350842d4533badca86
2 #include "kdp.h"
3 #include "kdpgfx.h"
4 //#include "kdppoly.h"
5 #include <time.h>
6 #include <stdio.h>
7 #include <math.h>
9 float x[5000],y[5000],z[5000];
10 float xb[5000],yb[5000],zb[5000];
11 int size[5000];
12 int sx[5000],sy[5000];
13 int fac1[5000],fac2[5000],fac3[5000];
14 UBYTE col[5000];
15 int nump,numf;
16 UBYTE pal[256*3];
17 UBYTE ctab[512];
18 UBYTE ctab2[512];
19 clock_t time1,time2;
20 LONG rad1[5000],rad2[5000];
21 LONG zsrt1[5000],zsrt2[5000];
22 UBYTE vme[16][64000];
23 //KDPvertex vert[3];
24 UBYTE tex[256*256];
29 void radix(int byte, LONG N,LONG *source,LONG *dest,LONG *srts,LONG *srtd)
31 int i;
32 int w;
33 LONG count[256];
34 LONG index[256];
35 memset(count,0,sizeof(count));
36 for(i=0;i<N;i++) count[(source[i]>>(byte*8))& 0xff]++;
37 index[0]=0;
38 for(i=1;i<256;i++) index[i]=index[i-1]+count[i-1];
39 for(i=0;i<N;i++)
41 w=index[(source[i]>>(byte*8))& 0xff]++;
42 dest[w]=source[i];
43 srtd[w]=srts[i];
47 void radixsort(LONG *source,LONG *temp, LONG N, LONG *srts,LONG *srtd)
49 radix(0,N,source,temp,srts,srtd);
50 radix(1,N,temp,source,srtd,srts);
51 radix(2,N,source,temp,srts,srtd);
52 radix(3,N,temp,source,srtd,srts);
58 int load3d(char *filename)
60 UBYTE *file;
61 int pointo;
62 int faceo;
63 int i;
64 float xofs,yofs,zofs;
65 //nump=40;
66 /*for(i=0;i<nump;i++)
68 x[i]=(10000-(float)(rand()%20000))/1000;
69 y[i]=(10000-(float)(rand()%20000))/1000;
70 z[i]=(10000-(float)(rand()%20000))/1000;
72 return 1;*/
73 if (!(file=KDPloadFile(filename)))
74 return 0;
75 if (KDPgetMlong(&file[0])!=0x33445257)
76 return 0;
77 pointo=KDPgetMlong(&file[6]);
78 numf=KDPgetMword(&file[10])+1;
79 nump=KDPgetMword(&file[pointo])+1;
80 faceo=12;
81 for(i=0;i<numf;i++)
83 faceo+=2;
84 fac1[i]=KDPgetMword(&file[faceo]);
85 faceo+=2;
86 fac2[i]=KDPgetMword(&file[faceo]);
87 faceo+=4;
88 fac3[i]=KDPgetMword(&file[faceo]);
89 faceo+=6;
91 pointo+=2;
92 for(i=0;i<nump;i++)
94 x[i]=(float)KDPgetMword(&file[pointo])/1000;
95 y[i]=(float)KDPgetMword(&file[pointo+2])/1000;
96 z[i]=(float)KDPgetMword(&file[pointo+4])/1000;
97 pointo+=6;
99 xofs=0;yofs=0;zofs=0;
100 for(i=0;i<nump;i++)
102 xofs+=x[i];
103 yofs+=y[i];
104 zofs+=z[i];
106 xofs/=nump;
107 yofs/=nump;
108 zofs/=nump;
109 for(i=0;i<nump;i++)
111 x[i]-=xofs;
112 y[i]-=yofs;
113 z[i]-=zofs;
115 printf("points:%d faces:%d \n",nump,numf);
116 return 1;
123 void project(float zx)
125 int i;
126 float zz;
127 for(i=0;i<nump;i++)
129 zz=-(zb[i]-zx);
130 if (zz!=0)
132 sx[i]=(int)((xb[i]*256)/zz)+160;
133 sy[i]=(int)((yb[i]*256)/zz)+100;
134 size[i]=(256/zz);
139 void rotate(float rx,float ry,float rz)
141 int i;
142 float xx,xy,xz;
143 float yx,yy,yz;
144 float zx,zy,zz;
145 float sx,sy,sz;
146 float cx,cy,cz;
147 sx=sin(rx);cx=cos(rx);
148 sy=sin(ry);cy=cos(ry);
149 sz=sin(rz);cz=cos(rz);
150 xx=cy*cz; xy=cy*sz; xz=-sy;
151 yx=sx*sy*cz-cx*sz; yy=sx*sy*sz+cx*cz; yz=sx*cy;
152 zx=cx*sy*cz+sx*sz; zy=cx*sy*sz-sx*cz; zz=cx*cy;
153 for(i=0;i<nump;i++)
155 xb[i]=x[i]*xx + y[i]*xy + z[i]*xz;
156 yb[i]=x[i]*yx + y[i]*yy + z[i]*yz;
157 zb[i]=x[i]*zx + y[i]*zy + z[i]*zz;
162 void pset(int x,int y,UBYTE c,UBYTE *vmem)
164 int o;
165 if((x<0) || (x>319) || (y<0) || (y>199)) return;
166 o=y*320+x;
167 vmem[o]=ctab[vmem[o]+c];
170 void pset2(int x,int y,UBYTE c,UBYTE *vmem)
172 int o;
173 if((x<0) || (x>318) || (y<0) || (y>198)) return;
174 o=y*320+x;
175 vmem[o]=ctab[vmem[o]+c];
176 vmem[o+1]=ctab[vmem[o+1]+c];
177 vmem[o+320]=ctab[vmem[o+320]+c];
178 vmem[o+321]=ctab[vmem[o+321]+c];
181 void sub(UBYTE *vmem)
183 int o;
184 for(o=0;o<64000;o++)
185 vmem[o]--;
189 void addbuffers(UBYTE *vmem)
191 int i;
192 UBYTE *v0,*v1,*v2,*v3,*v4;
193 v0=vmem;
194 v1=vme[0];
195 v2=vme[1];
196 v3=vme[2];
197 v4=vme[3];
199 for(i=0;i<64000;i++)
200 *(v0++)=(*(v1++)+*(v2++)+*(v3++)+*(v4++))>>2;
203 void addbuffers8(UBYTE *vmem)
205 int i;
206 UBYTE *v0,*v1,*v2,*v3,*v4,*v5,*v6,*v7,*v8;
207 v0=vmem;
208 v1=vme[0];
209 v2=vme[1];
210 v3=vme[2];
211 v4=vme[3];
212 v5=vme[4];
213 v6=vme[5];
214 v7=vme[6];
215 v8=vme[7];
218 for(i=0;i<64000;i++)
219 *(v0++)=(*(v1++)+*(v2++)+*(v3++)+*(v4++)+*(v5++)+*(v6++)+*(v7++)+*(v8++))>>3;
221 void addbuffers16(UBYTE *vmem)
223 int i;
224 UBYTE *v0,*v1,*v2,*v3,*v4,*v5,*v6,*v7,*v8,*v9,*v10,*v11,*v12,*v13,*v14,*v15,*v16;
225 v0=vmem;
226 v1=vme[0];
227 v2=vme[1];
228 v3=vme[2];
229 v4=vme[3];
230 v5=vme[4];
231 v6=vme[5];
232 v7=vme[6];
233 v8=vme[7];
234 v9=vme[8];
235 v10=vme[9];
236 v11=vme[10];
237 v12=vme[11];
238 v13=vme[12];
239 v14=vme[13];
240 v15=vme[14];
241 v16=vme[15];
245 for(i=0;i<64000;i++)
246 *(v0++)=(*(v1++)+*(v2++)+*(v3++)+*(v4++)+*(v5++)+*(v6++)+*(v7++)+*(v8++)+*(v9++)+*(v10++)+*(v11++)+*(v12++)+*(v13++)+*(v14++)+*(v15++)+*(v16++))>>4;
249 int main(int argc,char **argv)
251 KDPscreen screen;
252 KDPmouse mouse;
253 UBYTE *vmem = NULL;
254 float a=0,b=0,c=0;
255 float da,db,dc;
256 float zx=20;
257 int i;
258 UBYTE sp1[16*16];
259 int frame=0;
260 float secs;
261 int frm;
262 int tel=0,flip=0;
263 int kl;
264 float speed = 0.0;
265 float dz = 0.0;
267 if (argc < 2)
269 printf("usage: motionblur <3dobj>\n");
270 return 0;
273 for(i=0;i<512;i++)
275 if(i>255) ctab[i]=255;
276 else ctab[i]=i;
278 for(i=0;i<256;i++)
279 ctab2[511-i]=i;
280 for(i=0;i<256;i++)
281 ctab2[i]=0;
283 if(!(load3d(argv[1])))
285 printf("hmmm..!\n");
286 return 0;
293 if(KDPopen(&screen))
297 KDPreadBMP("bmp/brush1.bmp",pal,sp1);
299 KDPpalRange(0,0,0,
300 50,150,255,
301 0,200,pal);
302 KDPpalRange(50,150,255,
303 255,255,255,
304 200,255,pal);
306 KDPsetPal(pal);
309 vmem=screen.vmem;
310 KDPgetMouse(&mouse);
311 KDPgetMouse(&mouse);
312 time1=clock();
313 da=0;db=0;dc=0;
314 KDPreadBMP("bmp/cir.bmp",0,tex);
315 //for(i=0;i<256*256;i++)
316 //tex[i]=KDPcoltab[tex[i]+10];
317 while(mouse.button!=3)
319 if(mouse.button==0)
321 dc+=(float)mouse.yspeed/500;
322 db+=(float)mouse.xspeed/500;
323 dz=0;
325 if(mouse.button==1)
327 da+=(float)mouse.xspeed/500;
328 kl+=mouse.yspeed;
329 if(kl<0) kl=0;
330 if(kl>255) kl=255;
332 if(mouse.button==2)
334 tel++;
335 da=0;db=0;dc=0;
336 speed+=(float)mouse.xspeed/500;
337 dz=(float)mouse.yspeed/50;
339 else
340 tel=0;
342 if(tel==1) flip=1-flip;
345 for(frm=0;frm<16;frm++)
347 a+=da;
348 b+=db;
349 c+=dc;
350 zx+=(dz);
351 //sub(vmem);
352 //KDPblur(2,vmem);
353 KDPcls(vme[frm]);
354 for(i=0;i<nump;i++)
356 x[i]+=speed;
357 if(x[i]<-10) x[i]=10;
358 if(x[i]>10) x[i]=-10;
360 rotate(a,b,c);
361 project(zx);//+((float)(frm-4)/2));
364 for(i=0;i<nump;i++)
365 if(zb[i]!=0)
366 KDPsp256(sx[i],sy[i],size[i],tex,vme[frm]);
368 a-=da*7;
369 b-=db*7;
370 c-=dc*7;
371 /*for(i=0;i<nump;i++)
373 x[i]-=(speed*7);
374 if(x[i]<-10) x[i]=10;
375 if(x[i]>10) x[i]=-10;
378 addbuffers16(vmem);
379 //KDPblur2(vmem);
380 KDPwaitVr();
381 KDPshow(vmem);
382 KDPgetMouse(&mouse);
383 KDPcls(vmem);
384 frame++;
387 time2=clock();
388 KDPwriteBMP("bmp/test.bmp",pal,vmem,320,200);
389 secs=(float)(time2-time1)/(float)CLOCKS_PER_SEC;
390 printf("frames: %d time:%f fps:%f\n",frame,secs,(float)frame/secs);
391 KDPclose(&screen);
392 return 0;