remove generated file from the patch
[AROS-Contrib.git] / Demo / kdp / wire.c
bloba7091859c15a2e7a2ac3b3e47c0b4d9d342d3b0f
1 #include "kdp.h"
2 #include "kdpgfx.h"
3 #include <time.h>
4 #include <stdio.h>
5 #include <math.h>
7 float x[5000],y[5000],z[5000];
8 float xb[5000],yb[5000],zb[5000];
9 int sx[5000],sy[5000];
10 int fac1[5000],fac2[5000],fac3[5000];
11 UBYTE col[5000];
12 int nump,numf;
13 UBYTE pal[256*3];
14 UBYTE ctab[512];
15 UBYTE ctab2[512];
16 clock_t time1,time2;
19 int load3d(char *filename)
21 UBYTE *file;
22 int pointo;
23 int faceo;
24 int i;
25 float xofs,yofs,zofs;
26 if (!(file=KDPloadFile(filename)))
27 return 0;
28 if (KDPgetMlong(&file[0])!=0x33445257)
29 return 0;
30 pointo=KDPgetMlong(&file[6]);
31 numf=KDPgetMword(&file[10])+1;
32 nump=KDPgetMword(&file[pointo])+1;
33 faceo=12;
34 for(i=0;i<numf;i++)
36 faceo+=2;
37 fac1[i]=KDPgetMword(&file[faceo]);
38 faceo+=2;
39 fac2[i]=KDPgetMword(&file[faceo]);
40 faceo+=4;
41 fac3[i]=KDPgetMword(&file[faceo]);
42 faceo+=6;
44 pointo+=2;
45 for(i=0;i<nump;i++)
47 x[i]=(float)KDPgetMword(&file[pointo])/1000;
48 y[i]=(float)KDPgetMword(&file[pointo+2])/1000;
49 z[i]=(float)KDPgetMword(&file[pointo+4])/1000;
50 pointo+=6;
52 xofs=0;yofs=0;zofs=0;
53 for(i=0;i<nump;i++)
55 xofs+=x[i];
56 yofs+=y[i];
57 zofs+=z[i];
59 xofs/=nump;
60 yofs/=nump;
61 zofs/=nump;
62 for(i=0;i<nump;i++)
64 x[i]-=xofs;
65 y[i]-=yofs;
66 z[i]-=zofs;
68 return 1;
75 void project(float zx)
77 int i;
78 float zz;
79 for(i=0;i<nump;i++)
81 zz=-(zb[i]-zx);
82 if (zz!=0)
84 sx[i]=(int)((xb[i]*256)/zz)+160;
85 sy[i]=(int)((yb[i]*256)/zz)+100;
90 void rotate(float rx,float ry,float rz)
92 int i;
93 float xx,xy,xz;
94 float yx,yy,yz;
95 float zx,zy,zz;
96 float sx,sy,sz;
97 float cx,cy,cz;
98 sx=sin(rx);cx=cos(rx);
99 sy=sin(ry);cy=cos(ry);
100 sz=sin(rz);cz=cos(rz);
101 xx=cy*cz; xy=cy*sz; xz=-sy;
102 yx=sx*sy*cz-cx*sz; yy=sx*sy*sz+cx*cz; yz=sx*cy;
103 zx=cx*sy*cz+sx*sz; zy=cx*sy*sz-sx*cz; zz=cx*cy;
104 for(i=0;i<nump;i++)
106 xb[i]=x[i]*xx + y[i]*xy + z[i]*xz;
107 yb[i]=x[i]*yx + y[i]*yy + z[i]*yz;
108 zb[i]=x[i]*zx + y[i]*zy + z[i]*zz;
113 void pset(int x,int y,UBYTE c,UBYTE *vmem)
115 int o;
116 if((x<0) || (x>319) || (y<0) || (y>199)) return;
117 o=y*320+x;
118 vmem[o]=ctab[vmem[o]+c];
121 void pset2(int x,int y,UBYTE c,UBYTE *vmem)
123 int o;
124 if((x<0) || (x>318) || (y<0) || (y>198)) return;
125 o=y*320+x;
126 vmem[o]=ctab[vmem[o]+c];
127 vmem[o+1]=ctab[vmem[o+1]+c];
128 vmem[o+320]=ctab[vmem[o+320]+c];
129 vmem[o+321]=ctab[vmem[o+321]+c];
132 void sub(UBYTE *vmem)
134 int o;
135 for(o=0;o<64000;o++)
136 vmem[o]--;;
139 int main(int argc,char **argv)
141 KDPscreen screen;
142 KDPmouse mouse;
143 UBYTE *vmem;
144 float a=0,b=0,c=0;
145 float da,db,dc;
146 float zx=20;
147 int i;
148 int frame=0;
149 float secs;
150 int ia,ib,ic;
151 int kl = 0;
153 if (argc < 2)
155 printf("usage: wire <3dobj>\n");
156 return 0;
159 for(i=0;i<512;i++)
161 if(i>255) ctab[i]=255;
162 else ctab[i]=i;
164 for(i=0;i<256;i++)
165 ctab2[511-i]=i;
166 for(i=0;i<256;i++)
167 ctab2[i]=0;
169 if(!(load3d(argv[1])))
171 printf("hmmm..!\n");
172 return 0;
176 if(KDPopen(&screen))
179 KDPpalRange(0,0,0,
180 255,255,200,
181 0,255,pal);
182 KDPsetPal(pal);
183 vmem=screen.vmem;
184 KDPgetMouse(&mouse);
185 KDPgetMouse(&mouse);
186 time1=clock();
187 da=0;db=0;dc=0;
188 KDPsetColor(0,30,30,50);
189 while(mouse.button!=3)
191 if(mouse.button==0)
193 dc+=(float)mouse.yspeed/500;
194 db+=(float)mouse.xspeed/500;
196 if(mouse.button==1)
198 da+=(float)mouse.xspeed/500;
200 kl+=mouse.yspeed;
201 if(kl<0) kl=0;
202 if(kl>255) kl=255;
204 if(mouse.button==2)
206 da=0;db=0;dc=0;
207 zx+=(float)mouse.yspeed/10;
209 a+=da;
210 b+=db;
211 c+=dc;
212 //sub(vmem);
213 //KDPblur(2,vmem);
214 KDPcls(vmem);
215 rotate(a,b,c);
216 project(zx);
217 KDPpset2(kl,0,255,vmem);
219 for(i=0;i<numf;i++)
221 ia=fac1[i];
222 ib=fac2[i];
223 ic=fac3[i];
224 if ((zb[ia]<zx) && (zb[ib]<zx) && (zb[ic]<zx))
226 KDPline2(sx[ia],sy[ia],sx[ib],sy[ib],kl,vmem);
227 KDPline2(sx[ib],sy[ib],sx[ic],sy[ic],kl,vmem);
228 KDPline2(sx[ic],sy[ic],sx[ia],sy[ia],kl,vmem);
231 KDPwaitVr();
232 KDPshow(vmem);
233 KDPgetMouse(&mouse);
234 frame++;
237 time2=clock();
238 secs=(float)(time2-time1)/(float)CLOCKS_PER_SEC;
239 printf("frames: %d time:%f fps:%f\n",frame,secs,(float)frame/secs);
240 KDPclose(&screen);
241 return 0;