source: trunk/src/testing/app/atlantis-fbo/atlantis.c @ 4

Revision 4, 14.5 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <math.h>
6#include <assert.h>
7#if defined(WIN32)
8#define WIN32_LEAN_AND_MEAN
9#include <windows.h>
10#include <winsock.h>
11#include <sys\timeb.h>
12int gettimeofday(struct timeval *tp, void *tzp);
13#else
14#include <sys/time.h>
15#endif
16
17#if defined(__APPLE__)
18#include <GLUT/glut.h>
19#include <OpenGL/glu.h>
20#else
21#define GL_GLEXT_PROTOTYPES 1
22#include <GL/glut.h>
23#include <GL/glu.h>
24#endif
25
26
27#include "atlantis.h"
28
29// headers for SAGE
30#include "sail.h"
31#include "misc.h"
32int winWidth, winHeight;
33GLubyte *rgbBuffer = 0;
34sail sageInf; // sail object
35
36
37GLuint tname;
38void *pixels;
39GLint viewport[4];
40
41
42GLuint g_dynamicTextureID;
43GLuint g_frameBuffer;
44GLuint g_depthRenderBuffer;
45int xwin, ywin;
46
47
48
49#if defined(WIN32)
50int
51gettimeofday(struct timeval *tp, void *tzp)
52{
53        struct _timeb t;
54
55    _ftime(&t);
56    tp->tv_sec = t.time;
57    tp->tv_usec = t.millitm * 1000;
58    return 0;
59}
60#endif
61
62fishRec sharks[NUM_SHARKS];
63fishRec momWhale;
64fishRec babyWhale;
65fishRec dolph;
66
67GLboolean moving;
68int screenshot = 0;
69int fr = 0;
70float t1,t2;
71struct timeval tv_start;
72
73double getTime()
74{
75    struct timeval tv;
76                       
77    gettimeofday(&tv,0);
78    return (double)(tv.tv_sec - tv_start.tv_sec) +
79        (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0;
80}
81
82void
83InitFishs(void)
84{
85    int i;
86
87    for (i = 0; i < NUM_SHARKS; i++) {
88        sharks[i].x = 15000.0 + rand() % 6000;
89        sharks[i].y = (rand() % 20000) - 10000;
90        sharks[i].z = (rand() % 6000) - 5000;
91        sharks[i].psi = rand() % 360 - 180.0;
92        sharks[i].v = 1.0;
93    }
94
95    dolph.x = 30000.0;
96    dolph.y = 0.0;
97    dolph.z = 6000.0;
98    dolph.psi = 90.0;
99    dolph.theta = 0.0;
100    dolph.v = 3.0;
101
102    momWhale.x = 70000.0;
103    momWhale.y = 0.0;
104    momWhale.z = 0.0;
105    momWhale.psi = 90.0;
106    momWhale.theta = 0.0;
107    momWhale.v = 3.0;
108
109    babyWhale.x = 60000.0;
110    babyWhale.y = -2000.0;
111    babyWhale.z = -2000.0;
112    babyWhale.psi = 90.0;
113    babyWhale.theta = 0.0;
114    babyWhale.v = 3.0;
115}
116
117void
118Init(void)
119{
120    static float ambient[] =
121    {0.1, 0.1, 0.1, 1.0};
122    static float diffuse[] =
123    {1.0, 1.0, 1.0, 1.0};
124    static float position[] =
125    {0.0, 1.0, 0.0, 0.0};
126    static float mat_shininess[] =
127    {90.0};
128    static float mat_specular[] =
129    {0.8, 0.8, 0.8, 1.0};
130    static float mat_diffuse[] =
131    {0.46, 0.66, 0.795, 1.0};
132    static float mat_ambient[] =
133    {0.0, 0.1, 0.2, 1.0};
134    static float lmodel_ambient[] =
135    {0.4, 0.4, 0.4, 1.0};
136    static float lmodel_localviewer[] =
137    {1.0};
138
139    glFrontFace(GL_CW);
140
141    glDepthFunc(GL_LEQUAL);
142    glEnable(GL_DEPTH_TEST);
143
144    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
145    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
146    glLightfv(GL_LIGHT0, GL_POSITION, position);
147    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
148    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
149    glEnable(GL_LIGHTING);
150    glEnable(GL_LIGHT0);
151
152    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
153    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
154    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
155    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
156
157    InitFishs();
158
159    glEnable(GL_TEXTURE_2D);
160    glEnable(GL_TEXTURE_RECTANGLE_ARB);
161    glGenTextures(1,&tname);
162    glBindTexture(GL_TEXTURE_RECTANGLE_ARB,tname);
163    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_S,GL_REPEAT);
164    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_WRAP_T,GL_REPEAT);
165    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
166    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
167    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
168
169    glDisable(GL_TEXTURE_RECTANGLE_ARB);
170    glDisable(GL_TEXTURE_2D);
171
172    glClearColor(0.0, 0.5, 0.9, 0.0);
173
174    glPixelStorei(GL_PACK_SKIP_ROWS, 0);
175    glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
176    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
177    //    glPixelStorei(GL_PACK_ALIGNMENT, 1);
178
179
180    //
181    // Create a frame-buffer object and a render-buffer object...
182    //
183   
184    glGenFramebuffersEXT( 1, &g_frameBuffer );
185    glGenRenderbuffersEXT( 1, &g_depthRenderBuffer );
186
187    // Initialize the render-buffer for usage as a depth buffer.
188    // We don't really need this to render things into the frame-buffer object,
189    // but without it the geometry will not be sorted properly.
190
191    glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
192    glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, xwin, ywin);
193
194    //
195    // Check for errors...
196    //
197
198    GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
199
200    switch( status )
201      {
202      case GL_FRAMEBUFFER_COMPLETE_EXT:
203        fprintf(stderr, "GL_FRAMEBUFFER_COMPLETE_EXT!\n");
204        break;
205     
206      case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
207        fprintf(stderr, "GL_FRAMEBUFFER_UNSUPPORTED_EXT!\n");
208        exit(0);
209        break;
210       
211      default:
212        exit(0);
213      }
214
215
216    glGenTextures(1, &g_dynamicTextureID );
217    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, g_dynamicTextureID );
218
219    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB,
220                  xwin, ywin, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );
221
222    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
223    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
224}
225
226
227extern "C"
228{
229#define GL_READ_PIXEL_DATA_RANGE_NV       0x8879
230void glPixelDataRangeNV(GLenum target, GLsizei length, GLvoid *pointer);
231}
232
233void
234Reshape(int width, int height)
235{
236    winWidth = width;
237    winHeight = height;
238
239    glViewport(0, 0, width, height);
240
241    glMatrixMode(GL_PROJECTION);
242    glLoadIdentity();
243    gluPerspective(4.0, 2.0, 10000.0, 400000.0);
244    glMatrixMode(GL_MODELVIEW);
245        glLoadIdentity();
246
247    viewport[0] = 0;
248    viewport[1] = 0;
249    viewport[2] = width;
250    viewport[3] = height;
251   
252#if 0
253        // Video Memory : 0,0,1
254        // AGP memory   : 0,0,0.5
255        pixels = glXAllocateMemoryNV(width * height * 4, 1.0,0.0,1.0);
256        glEnable(GL_READ_PIXEL_DATA_RANGE_NV);
257        glEnableClientState(GL_READ_PIXEL_DATA_RANGE_NV);
258        glPixelDataRangeNV(GL_READ_PIXEL_DATA_RANGE_NV, width*height*3, pixels);
259        int rs = glIsEnabled(GL_READ_PIXEL_DATA_RANGE_NV);
260        fprintf(stderr, "Is Range enabled : %d\n", rs);
261        //void glFlushPixelDataRangeNV(enum target);
262#else
263        pixels = malloc( width * height * 3 );
264#endif
265
266        assert( pixels );
267        memset( pixels, 0, width * height * 3 );
268
269    fprintf(stderr, "buffer there (%p): %d %d \n\n", pixels,width, height);
270}
271
272void
273Animate(void)
274{
275    int i;
276
277    for (i = 0; i < NUM_SHARKS; i++) {
278        SharkPilot(&sharks[i]);
279        SharkMiss(i);
280    }
281    WhalePilot(&dolph);
282    dolph.phi++;
283    glutPostRedisplay();
284    WhalePilot(&momWhale);
285    momWhale.phi++;
286    WhalePilot(&babyWhale);
287    babyWhale.phi++;
288}
289
290void
291Key(unsigned char key, int x, int y)
292{
293        (void) x;
294        (void) y;
295    switch (key)
296        {
297          case 27:           /* Esc will quit */
298        exit(1);
299        break;
300          case ' ':          /* space will advance frame */
301        if (!moving) {
302            Animate();
303        }
304    }
305}
306
307
308void
309save_frame( void )
310{
311  int x, y, width, height;
312  double t1, t2;
313  float p[4];
314  GLboolean valid;
315  int i, j, stepi, stepj, block;
316  static int first = 1;
317   
318  x = viewport[0];
319  y = viewport[1];
320  width = viewport[2];
321  height = viewport[3];
322
323  t1 = getTime();
324    glReadPixels( x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels );
325    //fprintf(stderr, "Return Read [%s]\n", gluErrorString(glGetError()));
326  t2 = getTime();
327  fprintf(stderr, "Read %12.4f -- %.2f Hz -- %.1f Mpixels/sec -- %.1f MB/sec\n",
328          t2-t1, 1.0/(t2-t1), ((double)(width*height))/(1000000*(t2-t1)),
329           ((double)(width*height*4))/(1000000*(t2-t1)));
330
331 
332  glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
333
334
335  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
336
337  glMatrixMode(GL_PROJECTION);
338  glLoadIdentity();
339  glOrtho(0, width, 0, height, -1.0, 1.0);
340  glDisable(GL_DEPTH_TEST);
341  glDepthMask(GL_FALSE);
342
343  t1 = getTime();
344
345
346#if 1
347  glDrawPixels( width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels );
348  //fprintf(stderr, "Return Draw [%s]\n", gluErrorString(glGetError()));
349#else
350    glColor4f(0.5f,0.5f,0.5f,1.0f);
351   
352    glEnable(GL_TEXTURE_2D);
353    glEnable(GL_TEXTURE_RECTANGLE_ARB);
354    glBindTexture(GL_TEXTURE_RECTANGLE_ARB,tname);
355    glDisable(GL_LIGHTING);
356    glDisable(GL_BLEND);
357
358    if (first)
359      {
360        //glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_COMPRESSED_RGB_S3TC_DXT1_ARB,width,height,0,GL_RGB, GL_UNSIGNED_BYTE,pixels);
361        glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGB8,width,height,0,GL_RGB, GL_UNSIGNED_BYTE,pixels);
362        first = 0;
363        //fprintf(stderr, "Return glTexImage2D [%s]\n", gluErrorString(glGetError()));
364      }
365    else
366      {
367        glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,0,0,0,width,height,GL_RGB, GL_UNSIGNED_BYTE,pixels);
368        //fprintf(stderr, "Return glTexSubImage2D [%s]\n", gluErrorString(glGetError()));
369      }
370   
371    glBegin(GL_QUADS);
372
373        glTexCoord2f(0.0, 0.0);
374        glVertex3f(10, 10, 0.2);
375
376        glTexCoord2f(0.0, height);
377        glVertex3f(10, height-10, 0.2);
378 
379        glTexCoord2f(width, height);
380        glVertex3f( width-10,  height-10, 0.2);
381
382        glTexCoord2f(width, 0.0);
383        glVertex3f( width-10, 10, 0.2);
384
385    glEnd();
386
387    glDisable(GL_TEXTURE_RECTANGLE_ARB);
388    glDisable(GL_TEXTURE_2D);
389
390#endif
391
392  t2 = getTime();
393  fprintf(stderr, "Draw %12.4f -- %.2f Hz -- %.1f Mpixels/sec\n",
394          t2-t1, 1.0/(t2-t1), ((double)(width*height))/(1000000*(t2-t1)));
395
396  glMatrixMode(GL_MODELVIEW);
397  glEnable(GL_DEPTH_TEST);
398  glDepthMask(GL_TRUE);
399}
400
401void
402DisplayFrame( void )
403{
404    float ambient[] = {0.1, 0.1, 0.1, 1.0};
405    float diffuse[] = {1.0, 1.0, 1.0, 1.0};
406    float position[] = {0.0, 1.0, 0.0, 0.0};
407    float mat_shininess[] = {90.0};
408    float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
409    float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
410    float mat_ambient[] = {0.0, 0.1, 0.2, 1.0};
411    float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
412    float lmodel_localviewer[] = {0.0};
413
414    int i;
415
416
417    //
418    // Bind the frame-buffer object and attach to it a render-buffer object
419    // set up as a depth-buffer.
420    //
421   
422    glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_frameBuffer );
423    glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
424                               GL_TEXTURE_RECTANGLE_ARB, g_dynamicTextureID, 0 );
425    glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
426                                  GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
427   
428    //
429    // Set up the frame-buffer object just like you would set up a window.
430    //
431    glViewport( 0, 0, xwin, ywin);
432
433
434
435    glFrontFace(GL_CCW);
436
437    glDepthFunc(GL_LEQUAL);
438    glEnable(GL_DEPTH_TEST);
439    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
440    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
441    glLightfv(GL_LIGHT0, GL_POSITION, position);
442    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
443    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
444    glEnable(GL_LIGHTING);
445    glEnable(GL_LIGHT0);
446
447    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
448    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
449    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
450    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
451
452    glClearColor(0.0, 0.5, 0.9, 0.0);
453    glMatrixMode(GL_PROJECTION);
454    glLoadIdentity();
455    gluPerspective(40.0, 2.0, 10000.0, 400000.0);
456    glMatrixMode(GL_MODELVIEW);
457    glLoadIdentity();
458   
459    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
460   
461    for (i = 0; i < NUM_SHARKS; i++) {
462      glPushMatrix();
463      FishTransform(&sharks[i]);
464      DrawShark(&sharks[i]);
465      glPopMatrix();
466    }
467
468    glPushMatrix();
469    FishTransform(&dolph);
470    DrawDolphin(&dolph);
471    glPopMatrix();
472
473    glPushMatrix();
474    FishTransform(&momWhale);
475    DrawWhale(&momWhale);
476    glPopMatrix();
477
478    glPushMatrix();
479    FishTransform(&babyWhale);
480    glScalef(0.45, 0.45, 0.3);
481    DrawWhale(&babyWhale);
482    glPopMatrix();
483
484    if (winWidth > 0) {
485                glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, rgbBuffer);   
486                sageInf.swapBuffer();
487        rgbBuffer = (GLubyte *)sageInf.getBuffer();
488         }
489
490         sageMessage msg;
491         if (sageInf.checkMsg(msg, false) > 0) {
492                 switch (msg.getCode()) {
493                         case APP_QUIT : {
494                                sageInf.shutdown();
495                                exit(0);
496                                break;
497                        }
498                 }     
499         }
500         
501
502    //save_frame();
503   
504    glFinish();
505
506   
507   
508    glutSwapBuffers( );
509    /*
510         if (fr> 1000)
511    {                                   
512        t2 = getTime();
513        printf("Time: %f, Framerate: %f\n", t2-t1, fr/(t2-t1));
514        exit(0);
515    }
516    fr++;
517         */
518}
519
520void
521Visible(int state)
522{
523    if (state == GLUT_VISIBLE) {
524        if (moving)
525            glutIdleFunc(Animate);
526    } else {
527        if (moving)
528            glutIdleFunc(NULL);
529    }
530}
531
532void
533menuSelect(int value)
534{
535    switch (value) {
536    case 1:
537        moving = GL_TRUE;
538        glutIdleFunc(Animate);
539        break;
540    case 2:
541        moving = GL_FALSE;;
542        glutIdleFunc(NULL);
543        break;
544    case 3:
545        exit(0);
546        break;
547    }
548}
549
550int
551main(int argc, char **argv)
552{
553         int appID;
554         if (argc < 2)
555                appID = 0;
556         else
557                appID = atoi(argv[1]);
558               
559         int nodeId = 0;
560
561         int resX = 300, resY = 300;
562         if (argc > 3) {
563                 resX = atoi(argv[2]);
564                 resY = atoi(argv[3]);
565         }     
566       
567         sageRect atlantisImageMap;
568         atlantisImageMap.left = 0.0;
569         atlantisImageMap.right = 1.0;
570         atlantisImageMap.bottom = 0.0;
571         atlantisImageMap.top = 1.0;
572         
573         sailConfig scfg;
574         scfg.init("atlantis.conf");
575         scfg.setAppName("atlantis");
576         scfg.rank = nodeId;
577         scfg.appID = appID;
578         scfg.resX = resX;
579         scfg.resY = resY;
580         scfg.imageMap = atlantisImageMap;
581         scfg.pixFmt = PIXFMT_888;
582         scfg.rowOrd = BOTTOM_TO_TOP;
583         scfg.master = true;
584         scfg.nwID = 1;
585         
586         if (argc > 4)
587                scfg.nwID = atoi(argv[4]);
588                                 
589         sageInf.init(scfg);
590       
591         std::cout << "sail initialized " << std::endl;
592
593
594    gettimeofday(&tv_start,0);
595       
596    glutInit(&argc, argv);
597    xwin = resX;
598    ywin = resY;
599    glutInitWindowSize(xwin, ywin);
600    glutInitWindowPosition(0,0);
601    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
602    glutCreateWindow("GLUT Atlantis Demo");
603    //glutFullScreen();
604    printf("sharks: %d\n",NUM_SHARKS);
605
606    Init();
607   
608    glutDisplayFunc(DisplayFrame);
609    glutReshapeFunc(Reshape);
610    glutKeyboardFunc(Key);
611    moving = GL_TRUE;
612    glutIdleFunc(Animate);
613    // glutVisibilityFunc(Visible);
614    glutCreateMenu(menuSelect);
615    glutAddMenuEntry("Start motion", 1);
616    glutAddMenuEntry("Stop motion", 2);
617    glutAddMenuEntry("Quit", 3);
618    glutAttachMenu(GLUT_RIGHT_BUTTON);
619
620        if (rgbBuffer)
621            delete [] rgbBuffer;
622    rgbBuffer = (GLubyte *)sageInf.getBuffer();
623
624   
625    t1 = getTime();
626           
627
628    glutMainLoop();
629    return 0;             /* ANSI C requires main to return int. */
630}
Note: See TracBrowser for help on using the repository browser.