PDA

View Full Version : smallest code to display anythiing



Flamingo2
10-28-2016, 02:00 PM
I currently have a program:


#include <iostream>
#include <stdlib.h>
#include <unistd.h>

#include <GLES/egl.h>
#include <GLES2/gl2.h>
#include <EGL/eglplatform.h>

int retval = 0;

const char * printableError()
{
EGLint error = eglGetError();
if (error == EGL_SUCCESS) return "No error";
if (error == EGL_BAD_DISPLAY) return "Bad display";
if (error == EGL_NOT_INITIALIZED) return "Not initialized";
if (error == EGL_BAD_CONFIG) return "Bad configuration";
if (error == EGL_BAD_NATIVE_WINDOW) return "Bad native window";
if (error == EGL_BAD_ATTRIBUTE) return "Bad attribute";
if (error == EGL_BAD_ALLOC) return "Bad alloc";
if (error == EGL_BAD_MATCH) return "Bad match";
if (error == EGL_BAD_SURFACE) return "Bad surface";
if (error == EGL_BAD_CONTEXT) return "Bad context";
if (error == EGL_BAD_ACCESS) return "Bad access";
if (error == EGL_BAD_NATIVE_PIXMAP) return "Bad native pixmap";
if (error == EGL_BAD_NATIVE_WINDOW) return "Bad native window";
if (error == EGL_BAD_CURRENT_SURFACE) return "Bad current surface";
if (error == EGL_CONTEXT_LOST) return "Context lost";
if (error == EGL_BAD_PARAMETER) return "Bad parameter";
return "Unrecognized egl error";
}

int main(int argc, char *argv[])
{
//Setup to output to HDMI (default display)
int fbnum = 0;
EGLNativeDisplayType nativeDisplay = fbGetDisplayByIndex(fbnum);
EGLNativeWindowType nativeWindow = fbCreateWindow(nativeDisplay, 0, 0, 0, 0);

//Initialize display
EGLDisplay display = eglGetDisplay(nativeDisplay);
std::cout << "eglGetDisplay: " << printableError() << std::endl;

eglInitialize(display, nullptr, nullptr);
std::cout << "eglInitialize: " << printableError() << std::endl;

eglBindAPI(EGL_OPENGL_ES_API);
std::cout << "eglBindAPI: " << printableError() << std::endl;

//Configure the display
EGLint num_config;

EGLint configAttrib[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_NONE
};
EGLConfig config;
eglChooseConfig(display, configAttrib, &config, 1, &num_config);
std::cout << "eglChooseConfig: " << printableError() << std::endl;

//Get the display context
EGLint context_attrib[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};

EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attrib);
std::cout << "eglCreateContext: " << printableError() << std::endl;

//Get the surface
EGLSurface surface = eglCreateWindowSurface(display, config, nativeWindow, NULL);
std::cout << "eglCreatePbufferSurface: " << printableError() << std::endl;

//Make the surface current
eglMakeCurrent(display, surface, surface, context);
std::cout << "eglMakeCurrent: " << printableError() << std::endl;

glClearColor((float)0xf8/0xff, (float)0x83/0xff, (float)0x79/0xff, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

//WHAT GOES HERE???????????????????????

eglSwapBuffers(display, surface);
std::cout << "eglSwapBuffers: " << printableError() << std::endl;

sleep(5);

eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
std::cout << "eglMakeCurrent: " << printableError() << std::endl;
eglDestroySurface(display, surface);
std::cout << "eglDestroySurface: " << printableError() << std::endl;
eglDestroyContext(display, context);
std::cout << "eglDestroyContext: " << printableError() << std::endl;
eglTerminate(display);

return retval;
}

This changes the display to a solid coral color. You will notice the line: //WHAT GOES HERE???????????????????????. I would like to know any small piece of code which will display something: a line, a triangle, etc. I'd prefer to avoid a shader, because it is overkill for my eventual application.
I have tried using Pbuffer as a texture and that does not yet work. I'm looking to verify that anything can be rendered onto the display.

Please note that the many examples that I've seen that use glBegin(GL_TRIANGLES) will not work because they do not exist in GLES2/gl2.h (see headers). These are the packages provided by Phytec for my hardware.