Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asc-view.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. test program for the ggi-mesa driver
  3. Copyright (C) 1997,1998 Uwe Maurer - uwe_maurer@t-online.de
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <sys/time.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <GL/gl.h>
  21. #include <GL/ggimesa.h>
  22. #include <ggi/ggi.h>
  23. #include <stdlib.h>
  24. ggi_visual_t vis,vis_mem;
  25. GGIMesaContext ctx;
  26. int screen_x=GGI_AUTO,screen_y=GGI_AUTO;
  27. ggi_graphtype bpp=GT_AUTO;
  28. //#define ZBUFFER
  29. //#define SMOOTH_NORMALS
  30. void Init()
  31. {
  32. GLfloat h=(GLfloat)3/4;
  33. GLfloat pos[4]={5,5,-20,0};
  34. GLfloat specular[4]={.4,.4,.4,1};
  35. GLfloat diffuse[4]={.3,.3,.3,1};
  36. GLfloat ambient[4]={.2,.2,.2,1};
  37. int err;
  38. if (ggiInit()<0)
  39. {
  40. printf("ggiInit() failed\n");
  41. exit(1);
  42. }
  43. ctx=GGIMesaCreateContext();
  44. if (ctx==NULL)
  45. {
  46. printf("Can't create Context!\n");
  47. exit(1);
  48. }
  49. vis=ggiOpen(NULL);
  50. vis_mem=ggiOpen("display-memory",NULL);
  51. if (vis==NULL || vis_mem==NULL)
  52. {
  53. printf("Can't open ggi_visuals!\n");
  54. exit(1);
  55. }
  56. err=ggiSetGraphMode(vis,screen_x,screen_y,screen_x,screen_y,bpp);
  57. err+=ggiSetGraphMode(vis_mem,screen_x,screen_y,screen_x,screen_y,bpp);
  58. if (err)
  59. {
  60. printf("Can't set %ix%i\n",screen_x,screen_y);
  61. exit(1);
  62. }
  63. if (GGIMesaSetVisual(ctx,vis_mem,GL_TRUE,GL_FALSE)<0)
  64. {
  65. printf("GGIMesaSetVisual() failed!\n");
  66. exit(1);
  67. }
  68. GGIMesaMakeCurrent(ctx);
  69. glViewport(0,0,screen_x,screen_y);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. glFrustum(-1,1,-h,h,1,50);
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity();
  75. glTranslatef(0,0,-9);
  76. glShadeModel(GL_FLAT);
  77. glFrontFace(GL_CW);
  78. glEnable(GL_CULL_FACE);
  79. glEnable(GL_LIGHTING);
  80. glEnable(GL_LIGHT0);
  81. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  82. glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
  83. glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
  84. glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  85. #ifdef ZBUFFER
  86. glEnable(GL_DEPTH_TEST);
  87. #endif
  88. }
  89. #define MAX_VERTS 1000
  90. #define MAX_TRIS 2000
  91. #define MAX_LEN 1024
  92. #define MAX_F 100000000
  93. void LoadAsc(GLuint *list,char *file)
  94. {
  95. FILE *fp;
  96. GLfloat p[MAX_VERTS][3];
  97. GLfloat normal[MAX_VERTS][3];
  98. float ncount[MAX_VERTS];
  99. int v[MAX_TRIS][3];
  100. char line[MAX_LEN];
  101. char *s;
  102. int i,j;
  103. int verts,faces;
  104. GLuint v0,v1,v2;
  105. GLfloat n[3];
  106. GLfloat len,k;
  107. GLfloat min[3]={MAX_F,MAX_F,MAX_F};
  108. GLfloat max[3]={-MAX_F,-MAX_F,-MAX_F};
  109. char *coord_str[]={"X","Z","Y"};
  110. fp=fopen(file,"r");
  111. if (!fp)
  112. {
  113. printf("Can't open %s!\n",file);
  114. exit(1);
  115. }
  116. while (strncmp(fgets(line,MAX_LEN,fp),"Tri-mesh",8)) ;
  117. s=strstr(line,":")+1;
  118. verts=atoi(s);
  119. s=strstr(s,":")+1;
  120. faces=atoi(s);
  121. if (verts>MAX_VERTS)
  122. {
  123. printf("Too many vertices..\n");
  124. exit(1);
  125. }
  126. while (strncmp(fgets(line,MAX_LEN,fp),"Vertex list",11)) ;
  127. for (i=0;i<verts;i++)
  128. {
  129. while (strncmp(fgets(line,MAX_LEN,fp),"Vertex",6)) ;
  130. for (j=0;j<3;j++)
  131. {
  132. s=strstr(line,coord_str[j])+2;
  133. k=atoi(s);
  134. if (k>max[j]) max[j]=k;
  135. if (k<min[j]) min[j]=k;
  136. p[i][j]=k;
  137. }
  138. }
  139. len=0;
  140. for (i=0;i<3;i++)
  141. {
  142. k=max[i]-min[i];
  143. if (k>len) {len=k;j=i;}
  144. n[i]=(max[i]+min[i])/2;
  145. }
  146. len/=2;
  147. for (i=0;i<verts;i++)
  148. {
  149. for (j=0;j<3;j++)
  150. {
  151. p[i][j]-=n[j];
  152. p[i][j]/=len;
  153. }
  154. }
  155. *list=glGenLists(1);
  156. glNewList(*list,GL_COMPILE);
  157. glBegin(GL_TRIANGLES);
  158. memset(ncount,0,sizeof(ncount));
  159. memset(normal,0,sizeof(normal));
  160. while (strncmp(fgets(line,MAX_LEN,fp),"Face list",9)) ;
  161. for (i=0;i<faces;i++)
  162. {
  163. while (strncmp(fgets(line,MAX_LEN,fp),"Face",4)) ;
  164. s=strstr(line,"A")+2;
  165. v0=v[i][0]=atoi(s);
  166. s=strstr(line,"B")+2;
  167. v1=v[i][1]=atoi(s);
  168. s=strstr(line,"C")+2;
  169. v2=v[i][2]=atoi(s);
  170. n[0]=((p[v1][1]-p[v0][1])*(p[v2][2]-p[v0][2])
  171. - (p[v1][2]-p[v0][2])*(p[v2][1]-p[v0][1]));
  172. n[1]=((p[v1][2]-p[v0][2])*(p[v2][0]-p[v0][0])
  173. - (p[v1][0]-p[v0][0])*(p[v2][2]-p[v0][2]));
  174. n[2]=((p[v1][0]-p[v0][0])*(p[v2][1]-p[v0][1])
  175. - (p[v1][1]-p[v0][1])*(p[v2][0]-p[v0][0]));
  176. len=n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
  177. len=sqrt(len);
  178. n[0]/=len;
  179. n[1]/=len;
  180. n[2]/=len;
  181. #ifdef SMOOTH_NORMALS
  182. for (j=0;j<3;j++){
  183. normal[v[i][j]][0]+=n[0];
  184. normal[v[i][j]][1]+=n[1];
  185. normal[v[i][j]][2]+=n[2];
  186. ncount[v[i][j]]++;
  187. }
  188. #else
  189. glNormal3fv(n);
  190. for (j=0;j<3;j++)
  191. glVertex3fv(p[v[i][j]]);
  192. #endif
  193. }
  194. #ifdef SMOOTH_NORMALS
  195. for (i=0;i<verts;i++) {
  196. for (j=0;j<3;j++) {
  197. normal[i][j]/=ncount[i];
  198. }
  199. }
  200. for (i=0;i<faces;i++) {
  201. for (j=0;j<3;j++) {
  202. glNormal3f(normal[v[i][j]][0],
  203. normal[v[i][j]][1],
  204. normal[v[i][j]][2]);
  205. glVertex3fv(p[v[i][j]]);
  206. }
  207. }
  208. #endif
  209. glEnd();
  210. glEndList();
  211. fclose(fp);
  212. }
  213. double Display(GLuint l,int *maxframes)
  214. {
  215. int x,y;
  216. GLfloat col[]={.25,0,.25,1};
  217. int frames=0;
  218. struct timeval start,stop;
  219. double len;
  220. GLfloat rotate=0;
  221. gettimeofday(&start,NULL);
  222. while(1)
  223. {
  224. glClearColor(0,0,0,0);
  225. glClearIndex(0);
  226. #ifdef ZBUFFER
  227. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  228. #else
  229. glClear(GL_COLOR_BUFFER_BIT);
  230. #endif
  231. glPushMatrix();
  232. glRotatef(30,1,0,0);
  233. glRotatef(rotate/10,0,0,1);
  234. glTranslatef(-6,-4,0);
  235. for (y=0;y<3;y++)
  236. {
  237. glPushMatrix();
  238. for (x=0;x<5;x++)
  239. {
  240. glPushMatrix();
  241. glRotatef(rotate,y+1,-x-1,0);
  242. col[0]=(GLfloat)(x+1)/4;
  243. col[1]=0;
  244. col[2]=(GLfloat)(y+1)/2;
  245. glMaterialfv(GL_FRONT,GL_AMBIENT,col);
  246. glCallList(l);
  247. glPopMatrix();
  248. glTranslatef(3,0,0);
  249. }
  250. glPopMatrix();
  251. glTranslatef(0,4,0);
  252. }
  253. glPopMatrix();
  254. glFinish();
  255. ggiPutBox(vis,0,0,screen_x,screen_y,ggiDBGetBuffer(vis,0)->read);
  256. rotate+=10;
  257. frames++;
  258. if (frames==(*maxframes)) break;
  259. if (ggiKbhit(vis))
  260. {
  261. *maxframes=frames;
  262. break;
  263. }
  264. }
  265. gettimeofday(&stop,NULL);
  266. len=(double)(stop.tv_sec-start.tv_sec)+
  267. (double)(stop.tv_usec-start.tv_usec)/1e6;
  268. return len;
  269. }
  270. void visible(int vis)
  271. {
  272. if (vis == GLUT_VISIBLE)
  273. glutIdleFunc(idle);
  274. else
  275. glutIdleFunc(NULL);
  276. }
  277. int main(int argc, char *argv[])
  278. {
  279. glutInit(&argc, argv);
  280. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  281. glutInitWindowPosition(0, 0);
  282. glutInitWindowSize(300, 300);
  283. glutCreateWindow("asc-view");
  284. init();
  285. glutDisplayFunc(draw);
  286. glutReshapeFunc(reshape);
  287. glutKeyboardFunc(key);
  288. glutSpecialFunc(special);
  289. glutVisibilityFunc(visible);
  290. glutMainLoop();
  291. #if 0
  292. GLuint l;
  293. char *file;
  294. int maxframes=0;
  295. double len;
  296. Init();
  297. file=(argc>1) ? argv[1] : "asc/box.asc";
  298. if (argc>2) maxframes=atoi(argv[2]);
  299. if (argc==1)
  300. {
  301. printf("usage: %s filename.asc\n",argv[0]);
  302. }
  303. LoadAsc(&l,file);
  304. len=Display(l,&maxframes);
  305. printf("\ttime: %.3f sec\n",len);
  306. printf("\tframes: %i\n",maxframes);
  307. printf("\tfps: %.3f \n",(double)maxframes/len);
  308. GGIMesaDestroyContext(ctx);
  309. ggiClose(vis);
  310. ggiClose(vis_mem);
  311. ggiExit();
  312. #endif
  313. return 0;
  314. }