Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _POSIX_C_SOURCE 199309L
- #include <stdio.h>
- #include <unistd.h>
- #include <assert.h>
- #include <EGL/egl.h>
- #include <GLES2/gl2.h>
- static const EGLint configAttribs[] =
- {
- EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
- // Uncomment the following to enable MSAA
- //EGL_SAMPLE_BUFFERS, 1, // <-- Must be set to 1 to enable multisampling!
- //EGL_SAMPLES, 4, // <-- Number of samples
- EGL_DEPTH_SIZE, 24,
- EGL_ALPHA_SIZE, 8,
- EGL_BLUE_SIZE, 8,
- EGL_GREEN_SIZE, 8,
- EGL_RED_SIZE, 8,
- // Uncomment the following to enable stencil buffer
- //EGL_STENCIL_SIZE, 8,
- EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
- //EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
- EGL_NONE
- };
- static const EGLint contextAttribs[] =
- {
- EGL_CONTEXT_CLIENT_VERSION, 2,
- EGL_NONE
- };
- // The following array holds vec3 data of
- // three vertex positions
- static const GLfloat vertices[] =
- {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- };
- // The following are GLSL shaders for rendering a triangle on the screen
- #define STRINGIFY(x) #x
- static const char* vertexShaderCode = STRINGIFY(
- attribute vec3 pos;
- void main(){
- gl_Position = vec4(pos, 1.0);
- }
- );
- static const char* fragmentShaderCode = STRINGIFY(
- uniform vec4 color;
- void main() {
- gl_FragColor = vec4(color);
- }
- );
- static const char* eglGetErrorStr()
- {
- switch(eglGetError())
- {
- case EGL_SUCCESS: return "The last function succeeded without error.";
- case EGL_NOT_INITIALIZED: return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
- case EGL_BAD_ACCESS: return "EGL cannot access a requested resource (for example a context is bound in another thread).";
- case EGL_BAD_ALLOC: return "EGL failed to allocate resources for the requested operation.";
- case EGL_BAD_ATTRIBUTE: return "An unrecognized attribute or attribute value was passed in the attribute list.";
- case EGL_BAD_CONTEXT: return "An EGLContext argument does not name a valid EGL rendering context.";
- case EGL_BAD_CONFIG: return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
- case EGL_BAD_CURRENT_SURFACE: return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
- case EGL_BAD_DISPLAY: return "An EGLDisplay argument does not name a valid EGL display connection.";
- case EGL_BAD_SURFACE: return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
- case EGL_BAD_MATCH: return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
- case EGL_BAD_PARAMETER: return "One or more argument values are invalid.";
- case EGL_BAD_NATIVE_PIXMAP: return "A NativePixmapType argument does not refer to a valid native pixmap.";
- case EGL_BAD_NATIVE_WINDOW: return "A NativeWindowType argument does not refer to a valid native window.";
- case EGL_CONTEXT_LOST: return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering.";
- default: break;
- }
- return "Unknown error!";
- }
- static void printConfigInfo(int i, EGLDisplay display, EGLConfig* config)
- {
- EGLint val;
- eglGetConfigAttrib(display, *config, EGL_RED_SIZE, &val); printf("EGL_RED_SIZE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_GREEN_SIZE, &val); printf("EGL_GREEN_SIZE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_BLUE_SIZE, &val); printf("EGL_BLUE_SIZE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_ALPHA_SIZE, &val); printf("EGL_ALPHA_SIZE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_DEPTH_SIZE, &val); printf("EGL_DEPTH_SIZE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &val); printf("EGL_CONFIG_CAVEAT: %s\n", val == EGL_NONE ? "EGL_NONE" : val == EGL_SLOW_CONFIG ? "EGL_SLOW_CONFIG" : "");
- eglGetConfigAttrib(display, *config, EGL_SAMPLE_BUFFERS, &val); printf("EGL_SAMPLE_BUFFERS: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_SAMPLES, &val); printf("EGL_SAMPLES: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_MAX_PBUFFER_WIDTH, &val); printf("EGL_MAX_PBUFFER_WIDTH: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_MAX_PBUFFER_HEIGHT, &val); printf("EGL_MAX_PBUFFER_HEIGHT: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_NATIVE_RENDERABLE, &val); printf("EGL_NATIVE_RENDERABLE: %i\n", val);
- eglGetConfigAttrib(display, *config, EGL_RENDERABLE_TYPE, &val); printf("EGL_RENDERABLE_TYPE: ");
- if(val&EGL_OPENGL_BIT){ printf("OpenGL "); } if(val&EGL_OPENGL_ES_BIT){ printf("OpenGLES "); } printf("\n");
- printf("\n");
- }
- void printExtensions()
- {
- const char* extensions = glGetString(GL_EXTENSIONS);
- while(1)
- {
- const char* nextExtensions = strstr(extensions, " ");
- if(!nextExtensions)
- {
- break;
- }
- printf("%.*s\n", nextExtensions-extensions, extensions);
- extensions = nextExtensions+1;
- }
- printf("\n");
- }
- int main(int argv, char** argc)
- {
- EGLDisplay display;
- int major, minor;
- GLuint program, vert, frag, vbo;
- GLint posLoc, colorLoc;
- if((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
- {
- printf("Failed to get EGL display! Error: %s\n", eglGetErrorStr());
- return EGL_FALSE;
- }
- if(eglInitialize(display, &major, &minor) == EGL_FALSE)
- {
- printf("Failed to get EGL version! Error: %s\n", eglGetErrorStr());
- eglTerminate(display);
- return EGL_FALSE;
- }
- printf("\nInitialized EGL version: %d.%d\n\n", major, minor);
- EGLint numConfigs;
- eglGetConfigs(display, NULL, 0, &numConfigs);
- //printf("EGL has %i configs: \n\n", numConfigs);
- EGLConfig* configs = malloc(sizeof(EGLConfig) * numConfigs);
- eglGetConfigs(display, configs, numConfigs, &numConfigs);
- for(int i = 0; i < numConfigs; ++i)
- {
- //printConfigInfo(i, display, &configs[i]);
- }
- free(configs);
- EGLConfig config;
- if(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE)
- {
- printf("Failed to get EGL config! Error: %s\n", eglGetErrorStr());
- eglTerminate(display);
- return EGL_FALSE;
- }
- //print out chosen config data
- printf("Config chosen: \n");
- printConfigInfo(0, display, &config);
- #define displayWidth 1920
- #define displayHeight 1080
- static const EGLint pbufferAttribs[] = {
- EGL_WIDTH, displayWidth,
- EGL_HEIGHT, displayHeight,
- EGL_NONE,
- };
- EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttribs);
- if(surface == EGL_NO_SURFACE)
- {
- printf("Failed to create EGL surface! Error: %s\n", eglGetErrorStr());
- eglTerminate(display);
- return EGL_FALSE;
- }
- //EGLBoolean res = eglBindAPI(EGL_OPENGL_API);
- EGLBoolean res = eglBindAPI(EGL_OPENGL_ES_API);
- if(res != EGL_TRUE)
- {
- printf("Failed to bind GL API to EGL! Error: %s\n", eglGetErrorStr());
- return EGL_FALSE;
- }
- EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
- if(context == EGL_NO_CONTEXT)
- {
- printf("Failed to create EGL context! Error: %s\n", eglGetErrorStr());
- eglDestroySurface(display, surface);
- eglTerminate(display);
- return EGL_FALSE;
- }
- if(!eglMakeCurrent(display, surface, surface, context))
- {
- printf("Failed to make current! Error: %s\n", eglGetErrorStr());
- return EGL_FALSE;
- }
- ////////////////////////////
- //GL Context live from here
- ////////////////////////////
- printf("Vendor: %s\n", glGetString(GL_VENDOR));
- printf("Renderer: %s\n", glGetString(GL_RENDERER));
- printf("Version: %s\n", glGetString(GL_VERSION));
- printf("Shading language version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
- //printExtensions();
- // Set GL Viewport size, always needed!
- glViewport(0, 0, displayWidth, displayHeight);
- // Get GL Viewport size and test if it is correct.
- // NOTE! DO NOT UPDATE EGL LIBRARY ON RASPBERRY PI AS IT WILL INSTALL FAKE EGL!
- // If you have fake/faulty EGL library, the glViewport and glGetIntegerv won't work!
- // The following piece of code checks if the gl functions are working as intended!
- GLint viewport[4];
- glGetIntegerv(GL_VIEWPORT, viewport);
- // viewport[2] and viewport[3] are viewport width and height respectively
- printf("GL Viewport size: %dx%d\n", viewport[2], viewport[3]);
- // Clear whole screen (front buffer)
- glClearColor(0.8f, 0.2f, 0.5f, 1.0f);
- //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- /*
- // Create a shader program
- // NO ERRRO CHECKING IS DONE! (for the purpose of this example)
- // Read an OpenGL tutorial to properly implement shader creation
- program = glCreateProgram();
- glUseProgram(program);
- vert = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vert, 1, &vertexShaderCode, NULL);
- glCompileShader(vert);
- frag = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(frag, 1, &fragmentShaderCode, NULL);
- glCompileShader(frag);
- glAttachShader(program, frag);
- glAttachShader(program, vert);
- glLinkProgram(program);
- glUseProgram(program);
- // Create Vertex Buffer Object
- // Again, NO ERRRO CHECKING IS DONE! (for the purpose of this example)
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), vertices, GL_STATIC_DRAW);
- // Get vertex attribute and uniform locations
- posLoc = glGetAttribLocation(program, "pos");
- colorLoc = glGetUniformLocation(program, "color");
- // Set the desired color of the triangle to pink
- // 100% red, 0% green, 50% blue, 100% alpha
- glUniform4f(colorLoc, 1.0, 0.0f, 0.5, 1.0);
- // Set our vertex data
- glEnableVertexAttribArray(posLoc);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- */
- int counter = 0;
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Render a triangle consisting of 3 vertices:
- //glDrawArrays(GL_TRIANGLES, 0, 3);
- counter++;
- glFinish();
- glFlush();
- unsigned char* buffer = (unsigned char*)malloc(displayWidth * displayHeight * 3);
- glReadPixels(0, 0, displayWidth, displayHeight, GL_RGB, GL_UNSIGNED_BYTE, buffer);
- FILE* output = fopen("triangle.ppm", "wb");
- if(output)
- {
- printf("writing triangle.ppm\n");
- }
- fprintf(output, "P6\n%d %d\n255\n", displayWidth, displayHeight);
- fwrite(buffer, 1, displayWidth * displayHeight * 3, output);
- fclose(output);
- free(buffer);
- }
- // Cleanup
- eglDestroySurface(display, surface);
- eglMakeCurrent( display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
- eglDestroyContext(display, context);
- eglTerminate(display);
- return EGL_TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement