Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Demo of off-screen Mesa rendering
  3.  *
  4.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  5.  *
  6.  * If you want to render BIG images you'll probably have to increase
  7.  * MAX_WIDTH and MAX_Height in src/config.h.
  8.  *
  9.  * This program is in the public domain.
  10.  *
  11.  * Brian Paul
  12.  *
  13.  * PPM output provided by Joerg Schmalzl.
  14.  * ASCII PPM output added by Brian Paul.
  15.  *
  16.  * Usage: osdemo [filename]
  17.  */
  18.  
  19.  
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/time.h>
  25. #include <assert.h>
  26. #define GL_GLEXT_PROTOTYPES
  27. #include "GL/osmesa.h"
  28. #include <GL/glext.h>
  29. #include "eglut.h"
  30. #include <kos32sys.h>
  31.  
  32. void init(void);
  33. void reshape(int width, int height);
  34. void draw(void);
  35. void keyboard(unsigned char key);
  36.  
  37. extern GLfloat angle;
  38.  
  39. static void idle(void)
  40. {
  41.     static uint32_t t0;
  42.     uint32_t t, dt;
  43.  
  44.     t = get_tick_count();
  45.  
  46.     dt = t - t0;
  47.     t0 = t;
  48.  
  49.     angle += 70.0 * dt / 100;  /* 70 degrees per second */
  50.     if (angle > 3600.0)
  51.         angle -= 3600.0;
  52. }
  53.  
  54. static int time_now(void)
  55. {
  56.    struct timeval tv;
  57.    struct timezone tz;
  58.    (void) gettimeofday(&tv, &tz);
  59.    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  60. }
  61.  
  62. int main(int argc, char *argv[])
  63. {
  64.    int Width  = 384;
  65.    int Height = 384;
  66.  
  67.     OSMesaContext ctx;
  68.     void *buffer;
  69.     char *filename = NULL;
  70.     int ev;
  71.     int repeat=1;
  72.  
  73.    /* Create an RGBA-mode context */
  74.    /* specify Z, stencil, accum sizes */
  75.  
  76.     ctx = OSMesaCreateContextExt( OSMESA_BGRA, 16, 0, 0, NULL );
  77.     if (!ctx) {
  78.         printf("OSMesaCreateContext failed!\n");
  79.         return 0;
  80.     }
  81.  
  82.    /* Allocate the image buffer */
  83.     buffer = malloc( Width * Height * 4 * sizeof(GLubyte) );
  84.     if (!buffer) {
  85.         printf("Alloc image buffer failed!\n");
  86.         return 0;
  87.     }
  88.  
  89.    /* Bind the buffer to the context and make it current */
  90.    if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) {
  91.       printf("OSMesaMakeCurrent failed!\n");
  92.       return 0;
  93.    }
  94.  
  95.    {
  96.       int z, s, a;
  97.       glGetIntegerv(GL_DEPTH_BITS, &z);
  98.       glGetIntegerv(GL_STENCIL_BITS, &s);
  99.       glGetIntegerv(GL_ACCUM_RED_BITS, &a);
  100.       printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a);
  101.    }
  102.  
  103.     reshape(Width, Height);
  104.  
  105.     glClearColor( 0, 0, 0, 1);
  106.  
  107.     init();
  108.     draw();
  109.  
  110.     DrawWindow(10, 10, Width+TYPE_3_BORDER_WIDTH*2, Height+TYPE_3_BORDER_WIDTH+get_skin_height(),
  111.                "OpenGL Engine Demo", 0x000000, 0x74);
  112.     Blit(buffer, TYPE_3_BORDER_WIDTH, get_skin_height(), 0, 0, Width, Height, Width,Height,Width*4);
  113.  
  114.     int start = time_now();
  115.     int frames = 0;
  116.  
  117.     while(repeat)
  118.     {
  119.         int now = time_now();
  120.         oskey_t   key;
  121.  
  122.         ev = check_os_event();
  123.         switch(ev)
  124.         {
  125.             case 1:
  126.                 DrawWindow(0,0,0,0, NULL, 0x000000,0x74);
  127.                 break;
  128.  
  129.             case 2:
  130.                 key = get_key();
  131.                 keyboard(key.code);
  132.                 break;
  133.  
  134.             case 3:
  135.                 if(get_os_button()==1)
  136.                     repeat=0;
  137.                     continue;
  138.         };
  139.  
  140.         if (now - start >= 5000)
  141.         {
  142.             double elapsed = (double) (now - start) / 1000.0;
  143.  
  144.             printf("%d frames in %3.1f seconds = %6.3f FPS\n",
  145.                     frames, elapsed, frames / elapsed);
  146.             fflush(stdout);
  147.  
  148.             start = now;
  149.             frames = 0;
  150.         }
  151.  
  152.         idle();
  153.         draw();
  154.         glFlush();
  155.         Blit(buffer, TYPE_3_BORDER_WIDTH, get_skin_height(), 0, 0, Width, Height, Width,Height,Width*4);
  156.         frames++;
  157.     };
  158.  
  159.    /* free the image buffer */
  160.    free( buffer );
  161.  
  162.    /* destroy the context */
  163.    OSMesaDestroyContext( ctx );
  164.  
  165.    return 0;
  166. }
  167.  
  168.