Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <assert.h>
- #include <string.h>
- #include <EGL/egl.h>
- #include <X11/X.h>
- #include <X11/Xlib.h>
- #include "GLES2/gl2.h"
- #include "GLES2/gl2ext.h"
- #define TRUE 1
- #define FALSE !TRUE
- EGLDisplay egldisplay;
- EGLConfig eglconfig;
- EGLSurface eglsurface;
- EGLContext eglcontext;
- EGLNativeWindowType eglNativeWindow;
- EGLNativeDisplayType eglNativeDisplayType; volatile sig_atomic_t quit = 0;
- EGLNativeDisplayType fsl_getNativeDisplay() {
- EGLNativeDisplayType eglNativeDisplayType = NULL;
- eglNativeDisplayType = XOpenDisplay(NULL);
- assert(eglNativeDisplayType != NULL);
- display = EGL_DEFAULT_DISPLAY;
- return eglNativeDisplayType;
- }
- EGLNativeWindowType fsl_createwindow(EGLDisplay egldisplay, EGLNativeDisplayType eglNativeDisplayType) {
- EGLNativeWindowType native_window = (EGLNativeWindowType)0;
- Window window, rootwindow;
- int screen = DefaultScreen(eglNativeDisplayType);
- rootwindow = RootWindow(eglNativeDisplayType,screen);
- window = XCreateSimpleWindow(eglNativeDisplayType, rootwindow, 0, 0, 400, 533, 0, 0, WhitePixel (eglNativeDisplayType, screen));
- XMapWindow(eglNativeDisplayType, window);
- native_window = window;
- return native_window;
- }
- void fsl_destroywindow(EGLNativeWindowType eglNativeWindowType, EGLNativeDisplayType eglNativeDisplayType) {
- //close x display
- XCloseDisplay(eglNativeDisplayType);
- }
- int init(void)
- {
- static const EGLint s_configAttribs[] =
- {
- EGL_RED_SIZE, 5,
- EGL_GREEN_SIZE, 6,
- EGL_BLUE_SIZE, 5,
- EGL_ALPHA_SIZE, 0,
- EGL_SAMPLES, 0,
- EGL_NONE
- };
- EGLint numconfigs;
- eglNativeDisplayType = fsl_getNativeDisplay();
- egldisplay = eglGetDisplay(eglNativeDisplayType);
- eglInitialize(egldisplay, NULL, NULL);
- assert(eglGetError() == EGL_SUCCESS);
- eglBindAPI(EGL_OPENGL_ES_API);
- eglChooseConfig(egldisplay, s_configAttribs, &eglconfig, 1, &numconfigs);
- assert(eglGetError() == EGL_SUCCESS);
- assert(numconfigs == 1);
- eglNativeWindow = fsl_createwindow(egldisplay, eglNativeDisplayType);
- assert(eglNativeWindow);
- eglsurface = eglCreateWindowSurface(egldisplay, eglconfig, eglNativeWindow, NULL);
- assert(eglGetError() == EGL_SUCCESS);
- EGLint ContextAttribList[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
- eglcontext = eglCreateContext( egldisplay, eglconfig, EGL_NO_CONTEXT, ContextAttribList );
- assert(eglGetError() == EGL_SUCCESS);
- eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext);
- assert(eglGetError() == EGL_SUCCESS);
- {
- // Compile the shaders
- GLuint hVertexShader = glCreateShader( GL_VERTEX_SHADER );
- glShaderSource( hVertexShader, 1, &g_strVertexShader, NULL );
- glCompileShader( hVertexShader );
- // Check for compile success
- GLint nCompileResult = 0;
- glGetShaderiv( hVertexShader, GL_COMPILE_STATUS, &nCompileResult );
- if( 0 == nCompileResult )
- {
- char strLog[1024];
- GLint nLength;
- glGetShaderInfoLog( hVertexShader, 1024, &nLength, strLog );
- return FALSE;
- }
- GLuint hFragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
- glShaderSource( hFragmentShader, 1, &g_strFragmentShader, NULL );
- glCompileShader( hFragmentShader );
- // Check for compile success
- glGetShaderiv( hFragmentShader, GL_COMPILE_STATUS, &nCompileResult );
- if( 0 == nCompileResult )
- {
- char strLog[1024];
- GLint nLength;
- glGetShaderInfoLog( hFragmentShader, 1024, &nLength, strLog );
- return FALSE;
- }
- // Attach the individual shaders to the common shader program
- g_hShaderProgram = glCreateProgram();
- glAttachShader( g_hShaderProgram, hVertexShader );
- glAttachShader( g_hShaderProgram, hFragmentShader );
- // Init attributes BEFORE linking
- glBindAttribLocation( g_hShaderProgram, g_hVertexLoc,
- "g_vPosition" );
- // Link the vertex shader and fragment shader together
- glLinkProgram( g_hShaderProgram );
- // Check for link success
- GLint nLinkResult = 0;
- glGetProgramiv( g_hShaderProgram, GL_LINK_STATUS, &nLinkResult );
- if( 0 == nLinkResult )
- {
- char strLog[1024];
- GLint nLength;
- glGetProgramInfoLog( g_hShaderProgram, 1024, &nLength, strLog );
- return FALSE;
- }
- // Get uniform locations
- g_hModelViewMatrixLoc = glGetUniformLocation( g_hShaderProgram, "g_matModelView" );
- g_hProjMatrixLoc = glGetUniformLocation( g_hShaderProgram,
- "g_matProj" );
- glDeleteShader( hVertexShader );
- glDeleteShader( hFragmentShader );
- // Set the shader program
- glUseProgram( g_hShaderProgram );
- // Clear the colorbuffer and depth-buffer
- glClearColor( 0.0f, 0.0f, 0.5f, 1.0f );
- // Set some state
- glEnable( GL_DEPTH_TEST );
- }
- return 1;
- }
Add Comment
Please, Sign In to add comment