~ubuntu-branches/ubuntu/quantal/mesa-glw/quantal

« back to all changes in this revision

Viewing changes to progs/ggi/asc-view.c

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2008-05-06 16:19:15 UTC
  • Revision ID: james.westby@ubuntu.com-20080506161915-uynz7nftmfixu6bq
Tags: upstream-7.0.3
ImportĀ upstreamĀ versionĀ 7.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    test program for the ggi-mesa driver
 
3
 
 
4
    Copyright (C) 1997,1998  Uwe Maurer - uwe_maurer@t-online.de
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
*/
 
20
 
 
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>
 
29
 
 
30
ggi_visual_t vis,vis_mem;
 
31
 
 
32
GGIMesaContext ctx;
 
33
 
 
34
int screen_x=GGI_AUTO,screen_y=GGI_AUTO;
 
35
ggi_graphtype bpp=GT_AUTO;
 
36
 
 
37
//#define ZBUFFER
 
38
 
 
39
//#define SMOOTH_NORMALS
 
40
 
 
41
void Init()
 
42
{
 
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};
 
48
 
 
49
        int err;
 
50
 
 
51
        if (ggiInit()<0)
 
52
        {
 
53
                printf("ggiInit() failed\n");
 
54
                exit(1);
 
55
        }
 
56
        ctx=GGIMesaCreateContext();
 
57
        if (ctx==NULL)
 
58
        {
 
59
                printf("Can't create Context!\n");
 
60
                exit(1);
 
61
        }
 
62
 
 
63
        vis=ggiOpen(NULL);
 
64
        vis_mem=ggiOpen("display-memory",NULL);
 
65
        if (vis==NULL || vis_mem==NULL)
 
66
        {
 
67
                printf("Can't open ggi_visuals!\n");
 
68
                exit(1);
 
69
        }       
 
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)
 
73
        {
 
74
                printf("Can't set %ix%i\n",screen_x,screen_y);
 
75
                exit(1);
 
76
        }
 
77
 
 
78
        if (GGIMesaSetVisual(ctx,vis_mem,GL_TRUE,GL_FALSE)<0)
 
79
        {
 
80
                printf("GGIMesaSetVisual() failed!\n");
 
81
                exit(1);
 
82
        }
 
83
 
 
84
        GGIMesaMakeCurrent(ctx);
 
85
 
 
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);
 
94
 
 
95
        glFrontFace(GL_CW);
 
96
        glEnable(GL_CULL_FACE);
 
97
        glEnable(GL_LIGHTING);
 
98
        glEnable(GL_LIGHT0);
 
99
        
 
100
        glLightfv(GL_LIGHT0,GL_POSITION,pos);
 
101
        
 
102
        glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
 
103
        glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
 
104
        glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
 
105
 
 
106
        #ifdef ZBUFFER
 
107
                glEnable(GL_DEPTH_TEST);
 
108
        #endif
 
109
}
 
110
 
 
111
 
 
112
#define MAX_VERTS 1000
 
113
#define MAX_TRIS  2000
 
114
#define MAX_LEN 1024
 
115
#define MAX_F 100000000
 
116
 
 
117
void LoadAsc(GLuint *list,char *file)
 
118
{
 
119
        FILE *fp;
 
120
 
 
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"};
 
135
 
 
136
        fp=fopen(file,"r");
 
137
        if (!fp) 
 
138
        {
 
139
                printf("Can't open %s!\n",file);        
 
140
                exit(1);
 
141
        }
 
142
 
 
143
        while (strncmp(fgets(line,MAX_LEN,fp),"Tri-mesh",8)) ;
 
144
        
 
145
        s=strstr(line,":")+1;
 
146
        verts=atoi(s);
 
147
        s=strstr(s,":")+1;
 
148
        faces=atoi(s);
 
149
 
 
150
        if (verts>MAX_VERTS)    
 
151
        {       
 
152
                printf("Too many vertices..\n");
 
153
                exit(1);
 
154
        }
 
155
        
 
156
        while (strncmp(fgets(line,MAX_LEN,fp),"Vertex list",11)) ;      
 
157
 
 
158
        for (i=0;i<verts;i++)
 
159
        {
 
160
                while (strncmp(fgets(line,MAX_LEN,fp),"Vertex",6)) ;    
 
161
                for (j=0;j<3;j++)
 
162
                {       
 
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;
 
168
                }
 
169
                
 
170
        }
 
171
        len=0;
 
172
        for (i=0;i<3;i++)
 
173
        {
 
174
                k=max[i]-min[i];
 
175
                if (k>len) {len=k;j=i;}
 
176
                n[i]=(max[i]+min[i])/2;
 
177
        }
 
178
 
 
179
        len/=2;
 
180
 
 
181
        for (i=0;i<verts;i++)
 
182
        {
 
183
                for (j=0;j<3;j++)
 
184
                {
 
185
                        p[i][j]-=n[j];
 
186
                        p[i][j]/=len;
 
187
                }
 
188
        }
 
189
 
 
190
        *list=glGenLists(1);
 
191
        glNewList(*list,GL_COMPILE);
 
192
        glBegin(GL_TRIANGLES);
 
193
 
 
194
        memset(ncount,0,sizeof(ncount));
 
195
        memset(normal,0,sizeof(normal));
 
196
 
 
197
        while (strncmp(fgets(line,MAX_LEN,fp),"Face list",9)) ; 
 
198
        for (i=0;i<faces;i++)
 
199
        {
 
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]]++;
 
224
                }
 
225
        #else
 
226
                glNormal3fv(n);
 
227
                for (j=0;j<3;j++)
 
228
                        glVertex3fv(p[v[i][j]]);
 
229
        #endif
 
230
        }
 
231
 
 
232
        #ifdef SMOOTH_NORMALS
 
233
                for (i=0;i<verts;i++) {
 
234
                        for (j=0;j<3;j++) {
 
235
                                normal[i][j]/=ncount[i];
 
236
                        }
 
237
                }
 
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]]);
 
244
                        }
 
245
                }
 
246
        #endif
 
247
 
 
248
        glEnd();
 
249
        glEndList();
 
250
        fclose(fp);
 
251
}
 
252
 
 
253
double Display(GLuint l,int *maxframes)
 
254
{
 
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;
 
261
 
 
262
        gettimeofday(&start,NULL);
 
263
 
 
264
 
 
265
        while(1)
 
266
        {
 
267
                glClearColor(0,0,0,0);
 
268
                glClearIndex(0);
 
269
 
 
270
                #ifdef ZBUFFER
 
271
                        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 
272
                #else
 
273
                        glClear(GL_COLOR_BUFFER_BIT);
 
274
                #endif  
 
275
 
 
276
                glPushMatrix();
 
277
        
 
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++)
 
282
                {
 
283
                        glPushMatrix();
 
284
                        for (x=0;x<5;x++)
 
285
                        {
 
286
                                glPushMatrix();
 
287
                                glRotatef(rotate,y+1,-x-1,0);
 
288
 
 
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);
 
296
                        }
 
297
                        glPopMatrix();
 
298
                        glTranslatef(0,4,0);
 
299
                }
 
300
                glPopMatrix();
 
301
                glFinish();
 
302
 
 
303
                ggiPutBox(vis,0,0,screen_x,screen_y,ggiDBGetBuffer(vis,0)->read);
 
304
                rotate+=10;
 
305
                frames++;
 
306
                if (frames==(*maxframes)) break;
 
307
 
 
308
                if (ggiKbhit(vis))
 
309
                {
 
310
                        *maxframes=frames;
 
311
                        break;
 
312
                }
 
313
        }
 
314
 
 
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;
 
319
}
 
320
 
 
321
void visible(int vis)
 
322
{
 
323
        if (vis == GLUT_VISIBLE)
 
324
          glutIdleFunc(idle);
 
325
        else
 
326
          glutIdleFunc(NULL);
 
327
}
 
328
 
 
329
int main(int argc, char *argv[])
 
330
{
 
331
        glutInit(&argc, argv);
 
332
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
 
333
        
 
334
        glutInitWindowPosition(0, 0);
 
335
        glutInitWindowSize(300, 300);
 
336
        glutCreateWindow("asc-view");
 
337
        init();
 
338
        
 
339
        glutDisplayFunc(draw);
 
340
        glutReshapeFunc(reshape);
 
341
        glutKeyboardFunc(key);
 
342
        glutSpecialFunc(special);
 
343
        glutVisibilityFunc(visible);
 
344
        
 
345
        glutMainLoop();
 
346
#if 0
 
347
        GLuint l;
 
348
        char *file;
 
349
        int maxframes=0;
 
350
        double len;
 
351
 
 
352
        Init();
 
353
 
 
354
        file=(argc>1) ? argv[1] : "asc/box.asc";
 
355
        if (argc>2) maxframes=atoi(argv[2]);
 
356
 
 
357
        if (argc==1)
 
358
        {
 
359
                printf("usage: %s filename.asc\n",argv[0]);
 
360
        }
 
361
 
 
362
        LoadAsc(&l,file);
 
363
 
 
364
        len=Display(l,&maxframes);
 
365
 
 
366
        printf("\ttime: %.3f sec\n",len);
 
367
        printf("\tframes: %i\n",maxframes);
 
368
        printf("\tfps: %.3f \n",(double)maxframes/len);
 
369
 
 
370
        GGIMesaDestroyContext(ctx);
 
371
        ggiClose(vis);
 
372
        ggiClose(vis_mem);
 
373
        ggiExit();
 
374
#endif
 
375
        return 0;
 
376
}
 
377