Advertisement
Guest User

Untitled

a guest
Aug 24th, 2010
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1.  
  2. #include <EGL/egl.h>
  3. #include <GLES2/gl2.h>
  4. #include <SDL/SDL_syswm.h>
  5.  
  6. #include <linux/matroxfb.h>
  7. #include <linux/fb.h>
  8. #include <sys/ioctl.h>
  9. #include <fcntl.h>
  10.  
  11. #include "X11/Xlib.h"
  12. #include "X11/Xutil.h"
  13.  
  14. #include <iostream>
  15. #include <fstream>
  16.  
  17. static EGLDisplay   eglDisplay  = 0;
  18. static EGLConfig    eglConfig   = 0;
  19. static EGLSurface   eglSurface  = 0;
  20. static EGLContext   eglContext  = 0;
  21.  
  22. // X11 variables
  23. Window              x11Window   = 0;
  24. Display*            x11Display  = 0;
  25. long                x11Screen   = 0;
  26. XVisualInfo*        x11Visual   = 0;
  27. Colormap            x11Colormap = 0;
  28.  
  29. unsigned int xRes = 640;
  30. unsigned int yRes = 400;
  31. int vsync         = 1;
  32. int fsaa          = 1;
  33.  
  34.  
  35. GLuint uiFragShader, uiVertShader;      // Used to hold the fragment and vertex shader handles
  36. GLuint uiProgramObject;                 // Used to hold the program handle (made out of the two previous shaders
  37.  
  38. // We're going to draw a triangle to the screen so create a vertex buffer object for our triangle
  39. // Interleaved vertex data
  40. GLfloat afVertices[] = {0.0f,0.5f,0.0f, // Position
  41.                         -0.5f ,-0.5f,0.0f,
  42.                         0.5f ,-0.5f ,0.0f};
  43.  
  44. const char* pszFragShader = "\
  45.         void main (void)\
  46.         {\
  47.             gl_FragColor = vec4(0.0, 1.0, 0.0 ,1.0);\
  48.         }";
  49.  
  50. const char* pszVertShader = "\
  51.    attribute   vec4    vPosition;\
  52.    void main(void)\
  53.    {\
  54.        gl_Position = vPosition;\
  55.    }";
  56.  
  57.  
  58.  
  59. int testEGLError( char* pszLocation )
  60. {
  61.     EGLint iErr = eglGetError();
  62.     if (iErr != EGL_SUCCESS)
  63.     {
  64.         printf("%s failed (%d).\n", pszLocation, iErr);
  65.         return 0;
  66.     }
  67.  
  68.     return 1;
  69. }
  70.  
  71. int setUpX11Window()
  72. {
  73.     /*
  74.         Step 0 - Create a NativeWindowType that we can use it for OpenGL ES output
  75.     */
  76.     Window                  sRootWindow;
  77.     XSetWindowAttributes    sWA;
  78.     unsigned int            ui32Mask;
  79.     int                     i32Depth;
  80.     int                     i32Width, i32Height;
  81.  
  82.     // Initializes the display and screen
  83.     x11Display = XOpenDisplay( 0 );
  84.     if (!x11Display)
  85.     {
  86.         printf("Error: Unable to open X display\n");
  87.         return 0;
  88.     }
  89.     x11Screen = XDefaultScreen( x11Display );
  90.  
  91.     // Gets the window parameters
  92.     sRootWindow = RootWindow(x11Display, x11Screen);
  93.     i32Depth = DefaultDepth(x11Display, x11Screen);
  94.     x11Visual = new XVisualInfo;
  95.     XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, x11Visual);
  96.     if (!x11Visual)
  97.     {
  98.         printf("Error: Unable to acquire visual\n");
  99.         return 0;
  100.     }
  101.     x11Colormap = XCreateColormap( x11Display, sRootWindow, x11Visual->visual, AllocNone );
  102.     sWA.colormap = x11Colormap;
  103.  
  104.     // Add to these for handling other events
  105.     sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
  106.     ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
  107.  
  108.     i32Width  = xRes < XDisplayWidth(x11Display, x11Screen) ? xRes : XDisplayWidth(x11Display, x11Screen);
  109.     i32Height = yRes < XDisplayHeight(x11Display,x11Screen) ? yRes: XDisplayHeight(x11Display,x11Screen);
  110.  
  111.     // Creates the X11 window
  112.     x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, i32Width, i32Height,
  113.                                  0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);
  114.     XMapWindow(x11Display, x11Window);
  115.     XFlush(x11Display);
  116.     return 1;
  117. }
  118.  
  119. int initVideo( int w, int h, int vsync, int fsaa )
  120. {
  121.     if (!setUpX11Window())
  122.     {
  123.         printf("X11 window setup borked!\n");
  124.         return 0;
  125.     }
  126.  
  127.     eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
  128.  
  129.     EGLint iMajorVersion, iMinorVersion;
  130.     if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
  131.     {
  132.         printf ("Error: eglInitialize() failed!");
  133.         return 0;
  134.     }
  135.  
  136.         /*
  137.         Make OpenGL ES the current API.
  138.         EGL provides ways to set up OpenGL ES and OpenVG contexts
  139.         (and possibly other graphics APIs in the future), so we need
  140.         to specify the "current API".
  141.     */
  142.     eglBindAPI(EGL_OPENGL_ES_API);
  143.  
  144.     EGLint pi32ConfigAttribs[9];
  145.     int attrib = 0;
  146.     pi32ConfigAttribs[attrib++] = EGL_SURFACE_TYPE;
  147.     pi32ConfigAttribs[attrib++] = EGL_WINDOW_BIT;
  148.     pi32ConfigAttribs[attrib++] = EGL_RENDERABLE_TYPE;
  149.     pi32ConfigAttribs[attrib++] = EGL_OPENGL_ES2_BIT;
  150.     if ( fsaa )
  151.     {
  152.         pi32ConfigAttribs[attrib++] = EGL_SAMPLE_BUFFERS;
  153.         pi32ConfigAttribs[attrib++] = 1;
  154.         pi32ConfigAttribs[attrib++] = EGL_SAMPLES;
  155.         pi32ConfigAttribs[attrib++] = 4;
  156.     }
  157.     pi32ConfigAttribs[attrib++] = EGL_NONE;
  158.  
  159.     int iConfigs;
  160.     if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
  161.     {
  162.         printf ("Error: eglChoseConfig() failed!\n");
  163.         return 0;
  164.     }
  165.  
  166.     eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
  167.     if (!testEGLError((char *)"eglCreateWindowSurface"))
  168.     {
  169.         return 0;
  170.     }
  171.  
  172.     EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  173.     eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
  174.     if (!testEGLError((char *)"eglCreateContext"))
  175.     {
  176.         return 0;
  177.     }
  178.  
  179.     eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
  180.     if (!testEGLError((char *)"eglMakeCurrent"))
  181.     {
  182.         exit(0);
  183.     }
  184.     return 1;
  185. }
  186.  
  187. int swapBuffers()
  188. {
  189.     if ( vsync )
  190.     {
  191.         int fd = open( "/dev/fb0" , O_RDWR );
  192.         if( 0 < fd )
  193.         {
  194.             int ret = 0;
  195.             ret = ioctl(fd, FBIO_WAITFORVSYNC, &ret );
  196.             if ( ret != 0 )
  197.             {
  198.                 printf ("FBIO_WAITFORVSYNC failed!\n");
  199.             }
  200.         }
  201.         close(fd);
  202.     }
  203.     eglSwapBuffers( eglDisplay, eglSurface );
  204.     if ( !testEGLError((char *) "eglSwapBuffers" ) )
  205.     {
  206.         return 0;
  207.     }
  208.     return 1;
  209. }
  210.  
  211. int quitEGL()
  212. {
  213.     eglMakeCurrent( eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
  214.     eglDestroyContext ( eglDisplay, eglContext );
  215.     eglDestroySurface ( eglDisplay, eglSurface );
  216.     eglTerminate ( eglDisplay );
  217.     return 1;
  218. }
  219.  
  220. int compileShaders()
  221. {
  222.  
  223.     // Create the fragment shader object
  224.     uiFragShader = glCreateShader(GL_FRAGMENT_SHADER);
  225.  
  226.     // Load the source code into it
  227.     glShaderSource(uiFragShader, 1, (const char**)&pszFragShader, NULL);
  228.  
  229.     // Compile the source code
  230.     glCompileShader(uiFragShader);
  231.  
  232.     // Check if compilation succeeded
  233.     GLint bShaderCompiled;
  234.     glGetShaderiv(uiFragShader, GL_COMPILE_STATUS, &bShaderCompiled);
  235.  
  236.     if (!bShaderCompiled)
  237.     {
  238.         // An error happened, first retrieve the length of the log message
  239.         int i32InfoLogLength, i32CharsWritten;
  240.         glGetShaderiv(uiFragShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
  241.  
  242.         // Allocate enough space for the message and retrieve it
  243.         char* pszInfoLog = new char[i32InfoLogLength];
  244.         glGetShaderInfoLog(uiFragShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
  245.  
  246.         // Displays the error
  247.         printf("Failed to compile fragment shader: %s\n", pszInfoLog);
  248.         delete [] pszInfoLog;
  249.         return 0;
  250.     }
  251.  
  252.     // Loads the vertex shader in the same way
  253.     uiVertShader = glCreateShader(GL_VERTEX_SHADER);
  254.     glShaderSource(uiVertShader, 1, (const char**)&pszVertShader, NULL);
  255.     glCompileShader(uiVertShader);
  256.     glGetShaderiv(uiVertShader, GL_COMPILE_STATUS, &bShaderCompiled);
  257.  
  258.     if (!bShaderCompiled)
  259.     {
  260.         int i32InfoLogLength, i32CharsWritten;
  261.         glGetShaderiv(uiVertShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
  262.         char* pszInfoLog = new char[i32InfoLogLength];
  263.         glGetShaderInfoLog(uiVertShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
  264.         printf("Failed to compile vertex shader: %s\n", pszInfoLog);
  265.         delete [] pszInfoLog;
  266.         return 0;
  267.     }
  268.  
  269.     // Create the shader program
  270.     uiProgramObject = glCreateProgram();
  271.  
  272.     // Attach the fragment and vertex shaders to it
  273.     glAttachShader(uiProgramObject, uiFragShader);
  274.     glAttachShader(uiProgramObject, uiVertShader);
  275.  
  276.     // Bind the custom vertex attribute "myVertex" to location VERTEX_ARRAY
  277.     glBindAttribLocation(uiProgramObject, 0, "vPosition"); // die 0 war VERTEX_ARRAY
  278.  
  279.     // Link the program
  280.     glLinkProgram(uiProgramObject);
  281.  
  282.     // Check if linking succeeded in the same way we checked for compilation success
  283.     GLint bLinked;
  284.     glGetProgramiv(uiProgramObject, GL_LINK_STATUS, &bLinked);
  285.  
  286.     if (!bLinked)
  287.     {
  288.         int ui32InfoLogLength, ui32CharsWritten;
  289.         glGetProgramiv(uiProgramObject, GL_INFO_LOG_LENGTH, &ui32InfoLogLength);
  290.         char* pszInfoLog = new char[ui32InfoLogLength];
  291.         glGetProgramInfoLog(uiProgramObject, ui32InfoLogLength, &ui32CharsWritten, pszInfoLog);
  292.         printf("Failed to link program: %s\n", pszInfoLog);
  293.         delete [] pszInfoLog;
  294.         return 0;
  295.     }
  296. }
  297.  
  298. void doStuff()
  299. {
  300.     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
  301.     glUseProgram(uiProgramObject);
  302. }
  303.  
  304. int draw(float i)
  305. {
  306.     glClearColor(i, 0.0f, 1.0-i, 1.0f);
  307.     glClear(GL_COLOR_BUFFER_BIT);
  308.     // Draws a triangle
  309.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, afVertices);
  310.     glEnableVertexAttribArray(0);
  311.     glDrawArrays(GL_TRIANGLES, 0, 3);
  312.     glDisableVertexAttribArray(0);
  313.  
  314.     return 1;
  315. }
  316.  
  317. int main( int argc, const char* argv[] )
  318. {
  319.     if (!initVideo(xRes, yRes, vsync, fsaa))
  320.     {
  321.         printf ("Error setting up GLES2!\n");
  322.         quitEGL(); // clean up
  323.         return 0;
  324.     }
  325.     if (!compileShaders)
  326.     {
  327.         printf ("Error compiling shaders!\n");
  328.         quitEGL(); // clean up
  329.         return 0;
  330.     }
  331.  
  332.     doStuff();
  333.     EGLint iErr = eglGetError();
  334.     if (iErr != EGL_SUCCESS)
  335.     {
  336.         printf("doStuff failed (%d).\n", iErr);
  337.         return 0;
  338.     }
  339.  
  340.     for (float i=0; i<=1; i=i+0.002f)
  341.     {
  342.         draw(i);
  343.         if (iErr != EGL_SUCCESS)
  344.         {
  345.             printf("Draw failed (%d).\n", iErr);
  346.             return 0;
  347.         }
  348.         if (!swapBuffers())
  349.         {
  350.             printf("Swap buffers failed!\n");
  351.             return 0;
  352.         }
  353.     }
  354.     quitEGL(); //clean up
  355.     printf ("Application shut down properly.\n");
  356.     return 1;
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement