Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EGL/egl.h>
- #include <GLES2/gl2.h>
- #include <SDL/SDL_syswm.h>
- #include <linux/matroxfb.h>
- #include <linux/fb.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include "X11/Xlib.h"
- #include "X11/Xutil.h"
- #include <iostream>
- #include <fstream>
- static EGLDisplay eglDisplay = 0;
- static EGLConfig eglConfig = 0;
- static EGLSurface eglSurface = 0;
- static EGLContext eglContext = 0;
- // X11 variables
- Window x11Window = 0;
- Display* x11Display = 0;
- long x11Screen = 0;
- XVisualInfo* x11Visual = 0;
- Colormap x11Colormap = 0;
- unsigned int xRes = 640;
- unsigned int yRes = 400;
- int vsync = 1;
- int fsaa = 1;
- GLuint uiFragShader, uiVertShader; // Used to hold the fragment and vertex shader handles
- GLuint uiProgramObject; // Used to hold the program handle (made out of the two previous shaders
- // We're going to draw a triangle to the screen so create a vertex buffer object for our triangle
- // Interleaved vertex data
- GLfloat afVertices[] = {0.0f,0.5f,0.0f, // Position
- -0.5f ,-0.5f,0.0f,
- 0.5f ,-0.5f ,0.0f};
- const char* pszFragShader = "\
- void main (void)\
- {\
- gl_FragColor = vec4(0.0, 1.0, 0.0 ,1.0);\
- }";
- const char* pszVertShader = "\
- attribute vec4 vPosition;\
- void main(void)\
- {\
- gl_Position = vPosition;\
- }";
- int testEGLError( char* pszLocation )
- {
- EGLint iErr = eglGetError();
- if (iErr != EGL_SUCCESS)
- {
- printf("%s failed (%d).\n", pszLocation, iErr);
- return 0;
- }
- return 1;
- }
- int setUpX11Window()
- {
- /*
- Step 0 - Create a NativeWindowType that we can use it for OpenGL ES output
- */
- Window sRootWindow;
- XSetWindowAttributes sWA;
- unsigned int ui32Mask;
- int i32Depth;
- int i32Width, i32Height;
- // Initializes the display and screen
- x11Display = XOpenDisplay( 0 );
- if (!x11Display)
- {
- printf("Error: Unable to open X display\n");
- return 0;
- }
- x11Screen = XDefaultScreen( x11Display );
- // Gets the window parameters
- sRootWindow = RootWindow(x11Display, x11Screen);
- i32Depth = DefaultDepth(x11Display, x11Screen);
- x11Visual = new XVisualInfo;
- XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, x11Visual);
- if (!x11Visual)
- {
- printf("Error: Unable to acquire visual\n");
- return 0;
- }
- x11Colormap = XCreateColormap( x11Display, sRootWindow, x11Visual->visual, AllocNone );
- sWA.colormap = x11Colormap;
- // Add to these for handling other events
- sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
- ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
- i32Width = xRes < XDisplayWidth(x11Display, x11Screen) ? xRes : XDisplayWidth(x11Display, x11Screen);
- i32Height = yRes < XDisplayHeight(x11Display,x11Screen) ? yRes: XDisplayHeight(x11Display,x11Screen);
- // Creates the X11 window
- x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, i32Width, i32Height,
- 0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);
- XMapWindow(x11Display, x11Window);
- XFlush(x11Display);
- return 1;
- }
- int initVideo( int w, int h, int vsync, int fsaa )
- {
- if (!setUpX11Window())
- {
- printf("X11 window setup borked!\n");
- return 0;
- }
- eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display);
- EGLint iMajorVersion, iMinorVersion;
- if (!eglInitialize(eglDisplay, &iMajorVersion, &iMinorVersion))
- {
- printf ("Error: eglInitialize() failed!");
- return 0;
- }
- /*
- Make OpenGL ES the current API.
- EGL provides ways to set up OpenGL ES and OpenVG contexts
- (and possibly other graphics APIs in the future), so we need
- to specify the "current API".
- */
- eglBindAPI(EGL_OPENGL_ES_API);
- EGLint pi32ConfigAttribs[9];
- int attrib = 0;
- pi32ConfigAttribs[attrib++] = EGL_SURFACE_TYPE;
- pi32ConfigAttribs[attrib++] = EGL_WINDOW_BIT;
- pi32ConfigAttribs[attrib++] = EGL_RENDERABLE_TYPE;
- pi32ConfigAttribs[attrib++] = EGL_OPENGL_ES2_BIT;
- if ( fsaa )
- {
- pi32ConfigAttribs[attrib++] = EGL_SAMPLE_BUFFERS;
- pi32ConfigAttribs[attrib++] = 1;
- pi32ConfigAttribs[attrib++] = EGL_SAMPLES;
- pi32ConfigAttribs[attrib++] = 4;
- }
- pi32ConfigAttribs[attrib++] = EGL_NONE;
- int iConfigs;
- if (!eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs) || (iConfigs != 1))
- {
- printf ("Error: eglChoseConfig() failed!\n");
- return 0;
- }
- eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, NULL);
- if (!testEGLError((char *)"eglCreateWindowSurface"))
- {
- return 0;
- }
- EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
- eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
- if (!testEGLError((char *)"eglCreateContext"))
- {
- return 0;
- }
- eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
- if (!testEGLError((char *)"eglMakeCurrent"))
- {
- exit(0);
- }
- return 1;
- }
- int swapBuffers()
- {
- if ( vsync )
- {
- int fd = open( "/dev/fb0" , O_RDWR );
- if( 0 < fd )
- {
- int ret = 0;
- ret = ioctl(fd, FBIO_WAITFORVSYNC, &ret );
- if ( ret != 0 )
- {
- printf ("FBIO_WAITFORVSYNC failed!\n");
- }
- }
- close(fd);
- }
- eglSwapBuffers( eglDisplay, eglSurface );
- if ( !testEGLError((char *) "eglSwapBuffers" ) )
- {
- return 0;
- }
- return 1;
- }
- int quitEGL()
- {
- eglMakeCurrent( eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
- eglDestroyContext ( eglDisplay, eglContext );
- eglDestroySurface ( eglDisplay, eglSurface );
- eglTerminate ( eglDisplay );
- return 1;
- }
- int compileShaders()
- {
- // Create the fragment shader object
- uiFragShader = glCreateShader(GL_FRAGMENT_SHADER);
- // Load the source code into it
- glShaderSource(uiFragShader, 1, (const char**)&pszFragShader, NULL);
- // Compile the source code
- glCompileShader(uiFragShader);
- // Check if compilation succeeded
- GLint bShaderCompiled;
- glGetShaderiv(uiFragShader, GL_COMPILE_STATUS, &bShaderCompiled);
- if (!bShaderCompiled)
- {
- // An error happened, first retrieve the length of the log message
- int i32InfoLogLength, i32CharsWritten;
- glGetShaderiv(uiFragShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
- // Allocate enough space for the message and retrieve it
- char* pszInfoLog = new char[i32InfoLogLength];
- glGetShaderInfoLog(uiFragShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
- // Displays the error
- printf("Failed to compile fragment shader: %s\n", pszInfoLog);
- delete [] pszInfoLog;
- return 0;
- }
- // Loads the vertex shader in the same way
- uiVertShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(uiVertShader, 1, (const char**)&pszVertShader, NULL);
- glCompileShader(uiVertShader);
- glGetShaderiv(uiVertShader, GL_COMPILE_STATUS, &bShaderCompiled);
- if (!bShaderCompiled)
- {
- int i32InfoLogLength, i32CharsWritten;
- glGetShaderiv(uiVertShader, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
- char* pszInfoLog = new char[i32InfoLogLength];
- glGetShaderInfoLog(uiVertShader, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
- printf("Failed to compile vertex shader: %s\n", pszInfoLog);
- delete [] pszInfoLog;
- return 0;
- }
- // Create the shader program
- uiProgramObject = glCreateProgram();
- // Attach the fragment and vertex shaders to it
- glAttachShader(uiProgramObject, uiFragShader);
- glAttachShader(uiProgramObject, uiVertShader);
- // Bind the custom vertex attribute "myVertex" to location VERTEX_ARRAY
- glBindAttribLocation(uiProgramObject, 0, "vPosition"); // die 0 war VERTEX_ARRAY
- // Link the program
- glLinkProgram(uiProgramObject);
- // Check if linking succeeded in the same way we checked for compilation success
- GLint bLinked;
- glGetProgramiv(uiProgramObject, GL_LINK_STATUS, &bLinked);
- if (!bLinked)
- {
- int ui32InfoLogLength, ui32CharsWritten;
- glGetProgramiv(uiProgramObject, GL_INFO_LOG_LENGTH, &ui32InfoLogLength);
- char* pszInfoLog = new char[ui32InfoLogLength];
- glGetProgramInfoLog(uiProgramObject, ui32InfoLogLength, &ui32CharsWritten, pszInfoLog);
- printf("Failed to link program: %s\n", pszInfoLog);
- delete [] pszInfoLog;
- return 0;
- }
- }
- void doStuff()
- {
- glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
- glUseProgram(uiProgramObject);
- }
- int draw(float i)
- {
- glClearColor(i, 0.0f, 1.0-i, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- // Draws a triangle
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, afVertices);
- glEnableVertexAttribArray(0);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glDisableVertexAttribArray(0);
- return 1;
- }
- int main( int argc, const char* argv[] )
- {
- if (!initVideo(xRes, yRes, vsync, fsaa))
- {
- printf ("Error setting up GLES2!\n");
- quitEGL(); // clean up
- return 0;
- }
- if (!compileShaders)
- {
- printf ("Error compiling shaders!\n");
- quitEGL(); // clean up
- return 0;
- }
- doStuff();
- EGLint iErr = eglGetError();
- if (iErr != EGL_SUCCESS)
- {
- printf("doStuff failed (%d).\n", iErr);
- return 0;
- }
- for (float i=0; i<=1; i=i+0.002f)
- {
- draw(i);
- if (iErr != EGL_SUCCESS)
- {
- printf("Draw failed (%d).\n", iErr);
- return 0;
- }
- if (!swapBuffers())
- {
- printf("Swap buffers failed!\n");
- return 0;
- }
- }
- quitEGL(); //clean up
- printf ("Application shut down properly.\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement