Guest User

Initialization code - gles20 x11 - freescale

a guest
Jan 9th, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. #include <EGL/egl.h>
  7.  
  8. #include <X11/X.h>
  9. #include <X11/Xlib.h>
  10.  
  11. #include "GLES2/gl2.h"
  12. #include "GLES2/gl2ext.h"
  13.  
  14. #define TRUE 1
  15. #define FALSE !TRUE
  16.  
  17. EGLDisplay            egldisplay;
  18. EGLConfig            eglconfig;
  19. EGLSurface            eglsurface;
  20. EGLContext            eglcontext;
  21. EGLNativeWindowType eglNativeWindow;
  22. EGLNativeDisplayType eglNativeDisplayType; volatile sig_atomic_t quit = 0;
  23.  
  24.  
  25. EGLNativeDisplayType fsl_getNativeDisplay() {
  26.      EGLNativeDisplayType eglNativeDisplayType = NULL;
  27.  
  28.      eglNativeDisplayType = XOpenDisplay(NULL);
  29.      assert(eglNativeDisplayType != NULL);
  30.      display = EGL_DEFAULT_DISPLAY;
  31.      return eglNativeDisplayType;
  32. }
  33.  
  34.  
  35. EGLNativeWindowType fsl_createwindow(EGLDisplay egldisplay, EGLNativeDisplayType eglNativeDisplayType) {
  36.      EGLNativeWindowType native_window = (EGLNativeWindowType)0;
  37.  
  38.      Window window, rootwindow;
  39.      int screen = DefaultScreen(eglNativeDisplayType);
  40.      rootwindow = RootWindow(eglNativeDisplayType,screen);
  41.      window = XCreateSimpleWindow(eglNativeDisplayType, rootwindow, 0, 0, 400, 533, 0, 0, WhitePixel (eglNativeDisplayType, screen));
  42.      XMapWindow(eglNativeDisplayType, window);
  43.      native_window = window;
  44.  
  45.      return native_window;
  46. }
  47.  
  48.  
  49. void fsl_destroywindow(EGLNativeWindowType eglNativeWindowType, EGLNativeDisplayType eglNativeDisplayType) {
  50.      //close x display
  51.      XCloseDisplay(eglNativeDisplayType);
  52. }
  53.  
  54. int init(void)
  55. {
  56.  
  57.      static const EGLint s_configAttribs[] =
  58.      {
  59.          EGL_RED_SIZE,        5,
  60.          EGL_GREEN_SIZE,     6,
  61.          EGL_BLUE_SIZE,        5,
  62.          EGL_ALPHA_SIZE,     0,
  63.          EGL_SAMPLES,        0,
  64.          EGL_NONE
  65.      };
  66.  
  67.      EGLint numconfigs;
  68.  
  69.      eglNativeDisplayType = fsl_getNativeDisplay();
  70.      egldisplay = eglGetDisplay(eglNativeDisplayType);
  71.      eglInitialize(egldisplay, NULL, NULL);
  72.      assert(eglGetError() == EGL_SUCCESS);
  73.      eglBindAPI(EGL_OPENGL_ES_API);
  74.  
  75.      eglChooseConfig(egldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);
  76.      assert(eglGetError() == EGL_SUCCESS);
  77.      assert(numconfigs == 1);
  78.  
  79.      eglNativeWindow = fsl_createwindow(egldisplay, eglNativeDisplayType);
  80.      assert(eglNativeWindow);
  81.  
  82.      eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, eglNativeWindow, NULL);
  83.  
  84.      assert(eglGetError() == EGL_SUCCESS);
  85.      EGLint ContextAttribList[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  86.      eglcontext = eglCreateContext( egldisplay, eglconfig, EGL_NO_CONTEXT, ContextAttribList );
  87.      assert(eglGetError() == EGL_SUCCESS);
  88.      eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
  89.      assert(eglGetError() == EGL_SUCCESS);
  90.  
  91.      {
  92.          // Compile the shaders
  93.          GLuint hVertexShader = glCreateShader( GL_VERTEX_SHADER );
  94.          glShaderSource( hVertexShader, 1, &g_strVertexShader, NULL );
  95.          glCompileShader( hVertexShader );
  96.  
  97.          // Check for compile success
  98.          GLint nCompileResult = 0;
  99.          glGetShaderiv( hVertexShader, GL_COMPILE_STATUS, &nCompileResult );
  100.          if( 0 == nCompileResult )
  101.          {
  102.              char  strLog[1024];
  103.              GLint nLength;
  104.              glGetShaderInfoLog( hVertexShader, 1024, &nLength, strLog );
  105.              return FALSE;
  106.          }
  107.  
  108.          GLuint hFragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
  109.          glShaderSource( hFragmentShader, 1, &g_strFragmentShader, NULL );
  110.          glCompileShader( hFragmentShader );
  111.  
  112.          // Check for compile success
  113.          glGetShaderiv( hFragmentShader, GL_COMPILE_STATUS, &nCompileResult );
  114.          if( 0 == nCompileResult )
  115.          {
  116.              char  strLog[1024];
  117.              GLint nLength;
  118.              glGetShaderInfoLog( hFragmentShader, 1024, &nLength, strLog );
  119.              return FALSE;
  120.          }
  121.  
  122.          // Attach the individual shaders to the common shader program
  123.          g_hShaderProgram = glCreateProgram();
  124.          glAttachShader( g_hShaderProgram, hVertexShader );
  125.          glAttachShader( g_hShaderProgram, hFragmentShader );
  126.  
  127.          // Init attributes BEFORE linking
  128.          glBindAttribLocation( g_hShaderProgram, g_hVertexLoc,  
  129. "g_vPosition" );
  130.  
  131.          // Link the vertex shader and fragment shader together
  132.          glLinkProgram( g_hShaderProgram );
  133.  
  134.          // Check for link success
  135.          GLint nLinkResult = 0;
  136.          glGetProgramiv( g_hShaderProgram, GL_LINK_STATUS, &nLinkResult );
  137.          if( 0 == nLinkResult )
  138.          {
  139.              char strLog[1024];
  140.              GLint nLength;
  141.              glGetProgramInfoLog( g_hShaderProgram, 1024, &nLength, strLog );
  142.              return FALSE;
  143.          }
  144.  
  145.          // Get uniform locations
  146.          g_hModelViewMatrixLoc = glGetUniformLocation( g_hShaderProgram, "g_matModelView" );
  147.          g_hProjMatrixLoc      = glGetUniformLocation( g_hShaderProgram,
  148. "g_matProj" );
  149.  
  150.          glDeleteShader( hVertexShader );
  151.          glDeleteShader( hFragmentShader );
  152.  
  153.          // Set the shader program
  154.          glUseProgram( g_hShaderProgram );
  155.  
  156.          // Clear the colorbuffer and depth-buffer
  157.          glClearColor( 0.0f, 0.0f, 0.5f, 1.0f );
  158.  
  159.          // Set some state
  160.          glEnable( GL_DEPTH_TEST );
  161.  
  162.      }
  163.  
  164.      return 1;
  165. }
Add Comment
Please, Sign In to add comment