home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 339.lha / 3d_Library_src_v1.0 / test.c < prev   
C/C++ Source or Header  |  1990-01-09  |  5KB  |  164 lines

  1. /* Test Demo Program - By Steve Ludtke */
  2. /*     cre : 1/3/90    mod : 1/5/90    */
  3. /*           Copyright 1990            */
  4.  
  5. /*
  6.           The 3d library and all associated software in this distribution
  7.           is Copyright 1990 by Steven J. Ludtke. You have permission to
  8.           use and/or modify this code for any purpose commercial or non-
  9.           commercial with two conditions : I must be given credit in any
  10.           distributed product's documentation, and if any part of this
  11.           package is used in any commercial product (even Shareware) one
  12.           free copy of said software must be sent to me at the following
  13.           address : Steven Ludtke, 406 Yale Cir., Glenwood Springs, CO
  14.           81601; all other royalties are waived. This Copyright notice
  15.           must accompany any distributions of any part of this package,
  16.           and in general, the package should be distributed intact, with
  17.           no modifications. This notice must not be removed from the 3d.c,
  18.           test.c, and cube.c source code in this release.
  19. */
  20.  
  21. /*
  22. INSTRUCTIONS : This program will display shapes stored in a standard file
  23. format from any perspective. It does not use double buffered animation. The
  24. user changes his viewpoint using the keys on the numeric keypad as is shown
  25. in the code. Also, the '5' key toggles filling on and off, and 'q' exits
  26. the program. Console i/o is used, so you have to press return before
  27. anything will take effect. Run the program as follows :
  28. 'test <file.3d> <scale, usu 1>'
  29.  
  30. The 3d file format is :
  31. number of vertices (n)
  32. x0,y0,z0
  33. x1,y1,z1
  34. .
  35. .
  36. .
  37. xn,yn,zn
  38. number of line array elements (m)
  39. a0 b0
  40. a1 b1
  41. .
  42. .
  43. .
  44. am bm
  45. <EOF>
  46.  
  47. where a are vertex numbers or colors, and b is : <0> - continue shape/line,
  48. <1> - begin new line, <2> - change fill color, <3> - change outline color.
  49.  
  50. look at the color changing warnings in the docs.
  51. */
  52.  
  53. #include<stdio.h>
  54.  
  55. #define D3VDIST 5000        /* distance to vanishing point */
  56. #define REZ     128         /* calc resolution must be 2^x */
  57. #define REZB    7           /* number of bits in REZ */
  58. #define XCEN    250         /* center of bitmap X */
  59. #define YCEN    80          /* center of bitmap Y */
  60. #define XHI     600
  61. #define YHI     180
  62. #define XLO     0
  63. #define YLO     0
  64. #define ASPECT  22/10       /* aspect ratio */
  65. #include "3d.c"
  66.  
  67. APTR IntuitionBase,GfxBase;
  68.  
  69.  
  70. struct NewWindow NW = { 0,1,600,180,1,1,0,
  71.     WINDOWDEPTH|WINDOWDRAG|GIMMEZEROZERO|SMART_REFRESH|NOCAREREFRESH,
  72.     NULL,NULL,"3D test window",NULL,NULL,0,0,0,0,WBENCHSCREEN };
  73.  
  74. long *xxx;   /* points to x vertex data */
  75. long *yyy;   /* ... */
  76. long *zzz;
  77.  
  78. LINES *line; /* points to beginning of LINES array */
  79.  
  80.  
  81. main(argc,argv)
  82. int argc;
  83. char *argv[];
  84. {
  85. VECTOR v;
  86. FILE *in;
  87. double a1,a2,a3;
  88. char c;
  89. int i,p,m,t,nl,nv,sca,f;
  90. struct Window *win;
  91.  
  92. GfxBase=(APTR) OpenLibrary("graphics.library",0);
  93. IntuitionBase=(APTR) OpenLibrary("intuition.library",0);
  94.  
  95. f=0;
  96. if (argc!=3) { printf("test <file> <scale>\n");  exit(0); }
  97.  
  98. in=fopen(argv[1],"r");                      /* read 3d data */
  99. if (in==NULL) { printf("fnf\n"); exit(0); }
  100. sscanf(argv[2]," %d",&sca);
  101. fscanf(in," %d",&nv);
  102. xxx=malloc(nv*sizeof(long));
  103. yyy=malloc(nv*sizeof(long));
  104. zzz=malloc(nv*sizeof(long));
  105. if (xxx==NULL || yyy==NULL || zzz==NULL) { printf("Out of memory\n"); exit(0); }
  106. for (p=0; p<nv; p++) fscanf(in," %ld , %ld , %ld",&xxx[p],&yyy[p],&zzz[p]);
  107. fscanf(in," %d",&nl);
  108. line=(LINES *)malloc(nl*sizeof(LINES));
  109. if (line==NULL) { printf("Out of memory\n"); exit(0); }
  110. for (p=0; p<nl; p++) { fscanf(in," %d %d",&t,&m);
  111.     line[p].l=t;
  112.     if (m==1) line[p].nl=1; else line[p].nl=0;
  113.     if (m==2) line[p].nc=1; else line[p].nc=0;
  114.     if (m==3) line[p].nco=1; else line[p].nco=0; }
  115.  
  116. win=OpenWindow(&NW);           /* open a window and prepare for d3surf() */
  117. Init3Ras(win->RPort,NULL);
  118. if (win==NULL) { printf("Window err\n"); exit(1); }
  119. v.x=xxx;                      /* set up VECTOR structure */
  120. v.y=yyy;
  121. v.z=zzz;
  122. v.tx=(long *) malloc(nv*sizeof(long));
  123. v.ty=(long *) malloc(nv*sizeof(long));
  124. v.tz=(long *) malloc(nv*sizeof(long));
  125.  
  126. a1=a2=a3=0;
  127. p=0;
  128. SetAPen(win->RPort,1);        /* set pens, in case no color data in file */
  129. SetOPen(win->RPort,2);
  130. SetDrMd(win->RPort,JAM1);
  131.  
  132. while ((c=getchar())!='q') {              /* read from keyboard */
  133. switch(c) {
  134. case '9': a1+=PI/20; break;
  135. case '7': a1-=PI/20; break;
  136. case '6': a2+=PI/20; break;
  137. case '4': a2-=PI/20; break;
  138. case '3': a3+=PI/20; break;
  139. case '1': a3-=PI/20; break;
  140. case '8': p+=100;     break;
  141. case '2': p-=100;     break;
  142. case '5': f^=1; break;
  143. }
  144.  
  145. setxfm(a1,a2,a3,0,p,0,sca,1);             /* set new rotation matrix */
  146. rotatev(&v,nv);                           /* rotate with perspective */
  147. SetRast(win->RPort,0);                    /* clear window */
  148. if (f==0) d3lines(&v,line,nl,win->RPort); /* draw lines or filled shapes */
  149. else d3surf(&v,line,nl,win->RPort);
  150. }
  151.  
  152. Exit3d(win->RPort);                       /* free up scratch memory */
  153. CloseWindow(win);
  154. free(v.tx);
  155. free(v.ty);
  156. free(v.tz);
  157. free(xxx);
  158. free(yyy);
  159. free(zzz);
  160. free(line);
  161. }
  162.  
  163.  
  164.