Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.49 KB | None | 0 0
  1. #define _POSIX_C_SOURCE 199309L
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <assert.h>
  6.  
  7. #include <EGL/egl.h>
  8. #include <GLES2/gl2.h>
  9.  
  10. static const EGLint configAttribs[] =
  11. {
  12.     EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  13.  
  14.     // Uncomment the following to enable MSAA
  15.     //EGL_SAMPLE_BUFFERS, 1, // <-- Must be set to 1 to enable multisampling!
  16.     //EGL_SAMPLES, 4, // <-- Number of samples
  17.  
  18.     EGL_DEPTH_SIZE, 24,
  19.     EGL_ALPHA_SIZE, 8,
  20.     EGL_BLUE_SIZE, 8,
  21.     EGL_GREEN_SIZE, 8,
  22.     EGL_RED_SIZE, 8,
  23.  
  24.     // Uncomment the following to enable stencil buffer
  25.     //EGL_STENCIL_SIZE, 8,
  26.  
  27.     EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  28.     //EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  29.     EGL_NONE
  30. };
  31.  
  32. static const EGLint contextAttribs[] =
  33. {
  34.     EGL_CONTEXT_CLIENT_VERSION, 2,
  35.     EGL_NONE
  36. };
  37.  
  38. // The following array holds vec3 data of
  39. // three vertex positions
  40. static const GLfloat vertices[] =
  41. {
  42.     -1.0f, -1.0f, 0.0f,
  43.     1.0f, -1.0f, 0.0f,
  44.     0.0f,  1.0f, 0.0f,
  45. };
  46.  
  47. // The following are GLSL shaders for rendering a triangle on the screen
  48. #define STRINGIFY(x) #x
  49. static const char* vertexShaderCode = STRINGIFY(
  50.             attribute vec3 pos;
  51.         void main(){
  52.         gl_Position = vec4(pos, 1.0);
  53.         }
  54.         );
  55.  
  56. static const char* fragmentShaderCode = STRINGIFY(
  57.             uniform vec4 color;
  58.         void main() {
  59.         gl_FragColor = vec4(color);
  60.         }
  61.         );
  62.  
  63. static const char* eglGetErrorStr()
  64. {
  65.     switch(eglGetError())
  66.     {
  67.     case EGL_SUCCESS: return "The last function succeeded without error.";
  68.     case EGL_NOT_INITIALIZED: return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  69.     case EGL_BAD_ACCESS: return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  70.     case EGL_BAD_ALLOC: return "EGL failed to allocate resources for the requested operation.";
  71.     case EGL_BAD_ATTRIBUTE: return "An unrecognized attribute or attribute value was passed in the attribute list.";
  72.     case EGL_BAD_CONTEXT: return "An EGLContext argument does not name a valid EGL rendering context.";
  73.     case EGL_BAD_CONFIG: return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  74.     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.";
  75.     case EGL_BAD_DISPLAY: return "An EGLDisplay argument does not name a valid EGL display connection.";
  76.     case EGL_BAD_SURFACE: return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  77.     case EGL_BAD_MATCH: return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  78.     case EGL_BAD_PARAMETER: return "One or more argument values are invalid.";
  79.     case EGL_BAD_NATIVE_PIXMAP: return "A NativePixmapType argument does not refer to a valid native pixmap.";
  80.     case EGL_BAD_NATIVE_WINDOW: return "A NativeWindowType argument does not refer to a valid native window.";
  81.     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.";
  82.     default: break;
  83.     }
  84.     return "Unknown error!";
  85. }
  86.  
  87. static void printConfigInfo(int i, EGLDisplay display, EGLConfig* config)
  88. {
  89.     EGLint val;
  90.     eglGetConfigAttrib(display, *config, EGL_RED_SIZE, &val); printf("EGL_RED_SIZE: %i\n", val);
  91.     eglGetConfigAttrib(display, *config, EGL_GREEN_SIZE, &val); printf("EGL_GREEN_SIZE: %i\n", val);
  92.     eglGetConfigAttrib(display, *config, EGL_BLUE_SIZE, &val); printf("EGL_BLUE_SIZE: %i\n", val);
  93.     eglGetConfigAttrib(display, *config, EGL_ALPHA_SIZE, &val); printf("EGL_ALPHA_SIZE: %i\n", val);
  94.     eglGetConfigAttrib(display, *config, EGL_DEPTH_SIZE, &val); printf("EGL_DEPTH_SIZE: %i\n", val);
  95.     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" : "");
  96.     eglGetConfigAttrib(display, *config, EGL_SAMPLE_BUFFERS, &val); printf("EGL_SAMPLE_BUFFERS: %i\n", val);
  97.     eglGetConfigAttrib(display, *config, EGL_SAMPLES, &val); printf("EGL_SAMPLES: %i\n", val);
  98.     eglGetConfigAttrib(display, *config, EGL_MAX_PBUFFER_WIDTH, &val); printf("EGL_MAX_PBUFFER_WIDTH: %i\n", val);
  99.     eglGetConfigAttrib(display, *config, EGL_MAX_PBUFFER_HEIGHT, &val); printf("EGL_MAX_PBUFFER_HEIGHT: %i\n", val);
  100.     eglGetConfigAttrib(display, *config, EGL_NATIVE_RENDERABLE, &val); printf("EGL_NATIVE_RENDERABLE: %i\n", val);
  101.     eglGetConfigAttrib(display, *config, EGL_RENDERABLE_TYPE, &val); printf("EGL_RENDERABLE_TYPE: ");
  102.     if(val&EGL_OPENGL_BIT){ printf("OpenGL "); } if(val&EGL_OPENGL_ES_BIT){ printf("OpenGLES "); } printf("\n");
  103.     printf("\n");
  104. }
  105.  
  106. void printExtensions()
  107. {
  108.     const char* extensions = glGetString(GL_EXTENSIONS);
  109.     while(1)
  110.     {
  111.         const char* nextExtensions = strstr(extensions, " ");
  112.         if(!nextExtensions)
  113.         {
  114.             break;
  115.         }
  116.  
  117.         printf("%.*s\n", nextExtensions-extensions, extensions);
  118.         extensions = nextExtensions+1;
  119.     }
  120.     printf("\n");
  121. }
  122.  
  123. int main(int argv, char** argc)
  124. {
  125.     EGLDisplay display;
  126.     int major, minor;
  127.     GLuint program, vert, frag, vbo;
  128.     GLint posLoc, colorLoc;
  129.  
  130.     if((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
  131.     {
  132.         printf("Failed to get EGL display! Error: %s\n", eglGetErrorStr());
  133.         return EGL_FALSE;
  134.     }
  135.  
  136.     if(eglInitialize(display, &major, &minor) == EGL_FALSE)
  137.     {
  138.         printf("Failed to get EGL version! Error: %s\n", eglGetErrorStr());
  139.         eglTerminate(display);
  140.         return EGL_FALSE;
  141.     }
  142.  
  143.     printf("\nInitialized EGL version: %d.%d\n\n", major, minor);
  144.  
  145.     EGLint numConfigs;
  146.     eglGetConfigs(display, NULL, 0, &numConfigs);
  147.  
  148.     //printf("EGL has %i configs: \n\n", numConfigs);
  149.  
  150.     EGLConfig* configs = malloc(sizeof(EGLConfig) * numConfigs);
  151.     eglGetConfigs(display, configs, numConfigs, &numConfigs);
  152.  
  153.     for(int i = 0; i < numConfigs; ++i)
  154.     {
  155.         //printConfigInfo(i, display, &configs[i]);
  156.     }
  157.  
  158.     free(configs);
  159.  
  160.     EGLConfig config;
  161.     if(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE)
  162.     {
  163.         printf("Failed to get EGL config! Error: %s\n", eglGetErrorStr());
  164.         eglTerminate(display);
  165.         return EGL_FALSE;
  166.     }
  167.  
  168.     //print out chosen config data
  169.     printf("Config chosen: \n");
  170.     printConfigInfo(0, display, &config);
  171.  
  172. #define displayWidth 1920
  173. #define displayHeight 1080
  174.  
  175.     static const EGLint pbufferAttribs[] = {
  176.         EGL_WIDTH, displayWidth,
  177.         EGL_HEIGHT, displayHeight,
  178.         EGL_NONE,
  179.     };
  180.     EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttribs);
  181.     if(surface == EGL_NO_SURFACE)
  182.     {
  183.         printf("Failed to create EGL surface! Error: %s\n", eglGetErrorStr());
  184.         eglTerminate(display);
  185.         return EGL_FALSE;
  186.     }
  187.  
  188.     //EGLBoolean res = eglBindAPI(EGL_OPENGL_API);
  189.     EGLBoolean res = eglBindAPI(EGL_OPENGL_ES_API);
  190.     if(res != EGL_TRUE)
  191.     {
  192.         printf("Failed to bind GL API to EGL! Error: %s\n", eglGetErrorStr());
  193.         return EGL_FALSE;
  194.     }
  195.  
  196.     EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  197.     if(context == EGL_NO_CONTEXT)
  198.     {
  199.         printf("Failed to create EGL context! Error: %s\n", eglGetErrorStr());
  200.         eglDestroySurface(display, surface);
  201.         eglTerminate(display);
  202.         return EGL_FALSE;
  203.     }
  204.  
  205.     if(!eglMakeCurrent(display, surface, surface, context))
  206.     {
  207.         printf("Failed to make current! Error: %s\n", eglGetErrorStr());
  208.         return EGL_FALSE;
  209.     }
  210.  
  211.     ////////////////////////////
  212.     //GL Context live from here
  213.     ////////////////////////////
  214.     printf("Vendor: %s\n", glGetString(GL_VENDOR));
  215.     printf("Renderer: %s\n", glGetString(GL_RENDERER));
  216.     printf("Version: %s\n", glGetString(GL_VERSION));
  217.     printf("Shading language version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
  218.  
  219.     //printExtensions();
  220.  
  221.     // Set GL Viewport size, always needed!
  222.     glViewport(0, 0, displayWidth, displayHeight);
  223.  
  224.     // Get GL Viewport size and test if it is correct.
  225.     // NOTE! DO NOT UPDATE EGL LIBRARY ON RASPBERRY PI AS IT WILL INSTALL FAKE EGL!
  226.     // If you have fake/faulty EGL library, the glViewport and glGetIntegerv won't work!
  227.     // The following piece of code checks if the gl functions are working as intended!
  228.     GLint viewport[4];
  229.     glGetIntegerv(GL_VIEWPORT, viewport);
  230.  
  231.     // viewport[2] and viewport[3] are viewport width and height respectively
  232.     printf("GL Viewport size: %dx%d\n", viewport[2], viewport[3]);
  233.  
  234.     // Clear whole screen (front buffer)
  235.     glClearColor(0.8f, 0.2f, 0.5f, 1.0f);
  236.     //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  237.  
  238.     /*
  239.     // Create a shader program
  240.     // NO ERRRO CHECKING IS DONE! (for the purpose of this example)
  241.     // Read an OpenGL tutorial to properly implement shader creation
  242.     program = glCreateProgram();
  243.     glUseProgram(program);
  244.     vert = glCreateShader(GL_VERTEX_SHADER);
  245.     glShaderSource(vert, 1, &vertexShaderCode, NULL);
  246.     glCompileShader(vert);
  247.     frag = glCreateShader(GL_FRAGMENT_SHADER);
  248.     glShaderSource(frag, 1, &fragmentShaderCode, NULL);
  249.     glCompileShader(frag);
  250.     glAttachShader(program, frag);
  251.     glAttachShader(program, vert);
  252.     glLinkProgram(program);
  253.     glUseProgram(program);
  254.  
  255.     // Create Vertex Buffer Object
  256.     // Again, NO ERRRO CHECKING IS DONE! (for the purpose of this example)
  257.     glGenBuffers(1, &vbo);
  258.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  259.     glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), vertices, GL_STATIC_DRAW);
  260.  
  261.     // Get vertex attribute and uniform locations
  262.     posLoc = glGetAttribLocation(program, "pos");
  263.     colorLoc = glGetUniformLocation(program, "color");
  264.  
  265.     // Set the desired color of the triangle to pink
  266.     // 100% red, 0% green, 50% blue, 100% alpha
  267.     glUniform4f(colorLoc, 1.0, 0.0f, 0.5, 1.0);
  268.  
  269.     // Set our vertex data
  270.     glEnableVertexAttribArray(posLoc);
  271.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  272.     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  273.     */
  274.  
  275.     int counter = 0;
  276.     {
  277.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  278.  
  279.         // Render a triangle consisting of 3 vertices:
  280.         //glDrawArrays(GL_TRIANGLES, 0, 3);
  281.  
  282.         counter++;
  283.  
  284.         glFinish();
  285.         glFlush();
  286.         unsigned char* buffer = (unsigned char*)malloc(displayWidth * displayHeight * 3);
  287.         glReadPixels(0, 0, displayWidth, displayHeight, GL_RGB, GL_UNSIGNED_BYTE, buffer);
  288.         FILE* output = fopen("triangle.ppm", "wb");
  289.         if(output)
  290.         {
  291.             printf("writing triangle.ppm\n");
  292.         }
  293.         fprintf(output, "P6\n%d %d\n255\n", displayWidth, displayHeight);
  294.         fwrite(buffer, 1, displayWidth * displayHeight * 3, output);
  295.         fclose(output);
  296.         free(buffer);
  297.     }
  298.  
  299.  
  300.     // Cleanup
  301.     eglDestroySurface(display, surface);
  302.     eglMakeCurrent( display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
  303.     eglDestroyContext(display, context);
  304.     eglTerminate(display);
  305.  
  306.     return EGL_TRUE;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement