egl: Remove the demo driver.
[mesa/nouveau-pmpeg.git] / progs / ggi / asc-view.c
blobd37fba9d9f7b9dc07233cef7c8740962dc5d073e
1 /*
2 test program for the ggi-mesa driver
4 Copyright (C) 1997,1998 Uwe Maurer - uwe_maurer@t-online.de
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <sys/time.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include <GL/gl.h>
26 #include <GL/ggimesa.h>
27 #include <ggi/ggi.h>
28 #include <stdlib.h>
30 ggi_visual_t vis,vis_mem;
32 GGIMesaContext ctx;
34 int screen_x=GGI_AUTO,screen_y=GGI_AUTO;
35 ggi_graphtype bpp=GT_AUTO;
37 //#define ZBUFFER
39 //#define SMOOTH_NORMALS
41 void Init()
43 GLfloat h=(GLfloat)3/4;
44 GLfloat pos[4]={5,5,-20,0};
45 GLfloat specular[4]={.4,.4,.4,1};
46 GLfloat diffuse[4]={.3,.3,.3,1};
47 GLfloat ambient[4]={.2,.2,.2,1};
49 int err;
51 if (ggiInit()<0)
53 printf("ggiInit() failed\n");
54 exit(1);
56 ctx=GGIMesaCreateContext();
57 if (ctx==NULL)
59 printf("Can't create Context!\n");
60 exit(1);
63 vis=ggiOpen(NULL);
64 vis_mem=ggiOpen("display-memory",NULL);
65 if (vis==NULL || vis_mem==NULL)
67 printf("Can't open ggi_visuals!\n");
68 exit(1);
70 err=ggiSetGraphMode(vis,screen_x,screen_y,screen_x,screen_y,bpp);
71 err+=ggiSetGraphMode(vis_mem,screen_x,screen_y,screen_x,screen_y,bpp);
72 if (err)
74 printf("Can't set %ix%i\n",screen_x,screen_y);
75 exit(1);
78 if (GGIMesaSetVisual(ctx,vis_mem,GL_TRUE,GL_FALSE)<0)
80 printf("GGIMesaSetVisual() failed!\n");
81 exit(1);
84 GGIMesaMakeCurrent(ctx);
86 glViewport(0,0,screen_x,screen_y);
87 glMatrixMode(GL_PROJECTION);
88 glLoadIdentity();
89 glFrustum(-1,1,-h,h,1,50);
90 glMatrixMode(GL_MODELVIEW);
91 glLoadIdentity();
92 glTranslatef(0,0,-9);
93 glShadeModel(GL_FLAT);
95 glFrontFace(GL_CW);
96 glEnable(GL_CULL_FACE);
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
100 glLightfv(GL_LIGHT0,GL_POSITION,pos);
102 glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
103 glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
104 glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
106 #ifdef ZBUFFER
107 glEnable(GL_DEPTH_TEST);
108 #endif
112 #define MAX_VERTS 1000
113 #define MAX_TRIS 2000
114 #define MAX_LEN 1024
115 #define MAX_F 100000000
117 void LoadAsc(GLuint *list,char *file)
119 FILE *fp;
121 GLfloat p[MAX_VERTS][3];
122 GLfloat normal[MAX_VERTS][3];
123 float ncount[MAX_VERTS];
124 int v[MAX_TRIS][3];
125 char line[MAX_LEN];
126 char *s;
127 int i,j;
128 int verts,faces;
129 GLuint v0,v1,v2;
130 GLfloat n[3];
131 GLfloat len,k;
132 GLfloat min[3]={MAX_F,MAX_F,MAX_F};
133 GLfloat max[3]={-MAX_F,-MAX_F,-MAX_F};
134 char *coord_str[]={"X","Z","Y"};
136 fp=fopen(file,"r");
137 if (!fp)
139 printf("Can't open %s!\n",file);
140 exit(1);
143 while (strncmp(fgets(line,MAX_LEN,fp),"Tri-mesh",8)) ;
145 s=strstr(line,":")+1;
146 verts=atoi(s);
147 s=strstr(s,":")+1;
148 faces=atoi(s);
150 if (verts>MAX_VERTS)
152 printf("Too many vertices..\n");
153 exit(1);
156 while (strncmp(fgets(line,MAX_LEN,fp),"Vertex list",11)) ;
158 for (i=0;i<verts;i++)
160 while (strncmp(fgets(line,MAX_LEN,fp),"Vertex",6)) ;
161 for (j=0;j<3;j++)
163 s=strstr(line,coord_str[j])+2;
164 k=atoi(s);
165 if (k>max[j]) max[j]=k;
166 if (k<min[j]) min[j]=k;
167 p[i][j]=k;
171 len=0;
172 for (i=0;i<3;i++)
174 k=max[i]-min[i];
175 if (k>len) {len=k;j=i;}
176 n[i]=(max[i]+min[i])/2;
179 len/=2;
181 for (i=0;i<verts;i++)
183 for (j=0;j<3;j++)
185 p[i][j]-=n[j];
186 p[i][j]/=len;
190 *list=glGenLists(1);
191 glNewList(*list,GL_COMPILE);
192 glBegin(GL_TRIANGLES);
194 memset(ncount,0,sizeof(ncount));
195 memset(normal,0,sizeof(normal));
197 while (strncmp(fgets(line,MAX_LEN,fp),"Face list",9)) ;
198 for (i=0;i<faces;i++)
200 while (strncmp(fgets(line,MAX_LEN,fp),"Face",4)) ;
201 s=strstr(line,"A")+2;
202 v0=v[i][0]=atoi(s);
203 s=strstr(line,"B")+2;
204 v1=v[i][1]=atoi(s);
205 s=strstr(line,"C")+2;
206 v2=v[i][2]=atoi(s);
207 n[0]=((p[v1][1]-p[v0][1])*(p[v2][2]-p[v0][2])
208 - (p[v1][2]-p[v0][2])*(p[v2][1]-p[v0][1]));
209 n[1]=((p[v1][2]-p[v0][2])*(p[v2][0]-p[v0][0])
210 - (p[v1][0]-p[v0][0])*(p[v2][2]-p[v0][2]));
211 n[2]=((p[v1][0]-p[v0][0])*(p[v2][1]-p[v0][1])
212 - (p[v1][1]-p[v0][1])*(p[v2][0]-p[v0][0]));
213 len=n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
214 len=sqrt(len);
215 n[0]/=len;
216 n[1]/=len;
217 n[2]/=len;
218 #ifdef SMOOTH_NORMALS
219 for (j=0;j<3;j++){
220 normal[v[i][j]][0]+=n[0];
221 normal[v[i][j]][1]+=n[1];
222 normal[v[i][j]][2]+=n[2];
223 ncount[v[i][j]]++;
225 #else
226 glNormal3fv(n);
227 for (j=0;j<3;j++)
228 glVertex3fv(p[v[i][j]]);
229 #endif
232 #ifdef SMOOTH_NORMALS
233 for (i=0;i<verts;i++) {
234 for (j=0;j<3;j++) {
235 normal[i][j]/=ncount[i];
238 for (i=0;i<faces;i++) {
239 for (j=0;j<3;j++) {
240 glNormal3f(normal[v[i][j]][0],
241 normal[v[i][j]][1],
242 normal[v[i][j]][2]);
243 glVertex3fv(p[v[i][j]]);
246 #endif
248 glEnd();
249 glEndList();
250 fclose(fp);
253 double Display(GLuint l,int *maxframes)
255 int x,y;
256 GLfloat col[]={.25,0,.25,1};
257 int frames=0;
258 struct timeval start,stop;
259 double len;
260 GLfloat rotate=0;
262 gettimeofday(&start,NULL);
265 while(1)
267 glClearColor(0,0,0,0);
268 glClearIndex(0);
270 #ifdef ZBUFFER
271 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
272 #else
273 glClear(GL_COLOR_BUFFER_BIT);
274 #endif
276 glPushMatrix();
278 glRotatef(30,1,0,0);
279 glRotatef(rotate/10,0,0,1);
280 glTranslatef(-6,-4,0);
281 for (y=0;y<3;y++)
283 glPushMatrix();
284 for (x=0;x<5;x++)
286 glPushMatrix();
287 glRotatef(rotate,y+1,-x-1,0);
289 col[0]=(GLfloat)(x+1)/4;
290 col[1]=0;
291 col[2]=(GLfloat)(y+1)/2;
292 glMaterialfv(GL_FRONT,GL_AMBIENT,col);
293 glCallList(l);
294 glPopMatrix();
295 glTranslatef(3,0,0);
297 glPopMatrix();
298 glTranslatef(0,4,0);
300 glPopMatrix();
301 glFinish();
303 ggiPutBox(vis,0,0,screen_x,screen_y,ggiDBGetBuffer(vis,0)->read);
304 rotate+=10;
305 frames++;
306 if (frames==(*maxframes)) break;
308 if (ggiKbhit(vis))
310 *maxframes=frames;
311 break;
315 gettimeofday(&stop,NULL);
316 len=(double)(stop.tv_sec-start.tv_sec)+
317 (double)(stop.tv_usec-start.tv_usec)/1e6;
318 return len;
321 void visible(int vis)
323 if (vis == GLUT_VISIBLE)
324 glutIdleFunc(idle);
325 else
326 glutIdleFunc(NULL);
329 int main(int argc, char *argv[])
331 glutInit(&argc, argv);
332 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
334 glutInitWindowPosition(0, 0);
335 glutInitWindowSize(300, 300);
336 glutCreateWindow("asc-view");
337 init();
339 glutDisplayFunc(draw);
340 glutReshapeFunc(reshape);
341 glutKeyboardFunc(key);
342 glutSpecialFunc(special);
343 glutVisibilityFunc(visible);
345 glutMainLoop();
346 #if 0
347 GLuint l;
348 char *file;
349 int maxframes=0;
350 double len;
352 Init();
354 file=(argc>1) ? argv[1] : "asc/box.asc";
355 if (argc>2) maxframes=atoi(argv[2]);
357 if (argc==1)
359 printf("usage: %s filename.asc\n",argv[0]);
362 LoadAsc(&l,file);
364 len=Display(l,&maxframes);
366 printf("\ttime: %.3f sec\n",len);
367 printf("\tframes: %i\n",maxframes);
368 printf("\tfps: %.3f \n",(double)maxframes/len);
370 GGIMesaDestroyContext(ctx);
371 ggiClose(vis);
372 ggiClose(vis_mem);
373 ggiExit();
374 #endif
375 return 0;