PDA

View Full Version : Render image using texture with opengl es 1.1



Naseeb Panghal
08-18-2015, 08:36 AM
Hello Eveyone,

I have created a texture, bind it, copy image to it using API(glTexImage2D) then call glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
Everything goes fine. There is no opengl error, i checked using glGetError(); BUT image is not drawn on the surface.
Please anyone let me know which important API i am missing.

Below is the complete code snippet for reference.
--------------------------------------------------------


FILE *fp;
int byteRead = 0;
fp = fopen("DecodedFrame.bgr", "r");
unsigned char *imageData = (unsigned char *)malloc (width * height * 3);
if(fp != NULL)
byteRead = fread(imageData, 1, (width*height*3), fp);
if(byteRead == 0){
printf("Error in reading data\n");
exit(0);
}
else {
printf("Read bytes; %d\n", byteRead);
}

GLuint texture;

// use EGL to initialise GLES
g_x11Display = XOpenDisplay(NULL);

if (!g_x11Display)
{
fprintf(stderr, "ERROR: unable to get display!n");
return 0;
}

g_eglDisplay = eglGetDisplay((EGLNativeDisplayType)g_x11Display);
if (g_eglDisplay == EGL_NO_DISPLAY)
{
printf("Unable to initialise EGL display.");
return 0;
}

// Initialise egl
if (!eglInitialize(g_eglDisplay, NULL, NULL))
{
printf("Unable to initialise EGL display.");
return 0;
}

// Find a matching config
EGLint numConfigsOut = 0;
if (eglChooseConfig(g_eglDisplay, g_configAttribs, &g_eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0)
{
fprintf(stderr, "Unable to find appropriate EGL config.");
return 0;
}

XVisualInfo visual_template;
int nxvisuals;
nxvisuals = 0;
visual_template.screen = DefaultScreen(g_x11Display);
vi = XGetVisualInfo (g_x11Display, VisualScreenMask, &visual_template, &nxvisuals);

root = DefaultRootWindow(g_x11Display);

cmap = XCreateColormap(g_x11Display, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;

win = XCreateWindow(g_x11Display, root, 0, 0, 1280, 1024, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);

XMapWindow(g_x11Display, win);
XStoreName(g_x11Display, win, "VERY SIMPLE APPLICATION");

//g_eglSurface = eglCreateWindowSurface(g_eglDisplay, g_eglConfig, (EGLNativeWindowType)sysInfo.info.x11.window, 0);
g_eglSurface = eglCreateWindowSurface(g_eglDisplay, g_eglConfig, (EGLNativeWindowType)win, 0);
if ( g_eglSurface == EGL_NO_SURFACE)
{
printf("Unable to create EGL surface!");
return 0;
}

// Bind GLES and create the context
eglBindAPI(EGL_OPENGL_ES_API);
EGLint contextParams[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE}; // Use GLES version 1.x
g_eglContext = eglCreateContext(g_eglDisplay, g_eglConfig, NULL, NULL);
if (g_eglContext == EGL_NO_CONTEXT)
{
printf("Unable to create GLES context!");
return 0;
}

if (eglMakeCurrent(g_eglDisplay, g_eglSurface, g_eglSurface, g_eglContext) == EGL_FALSE)
{
printf("Unable to make GLES context current");
return 0;
}

/*******************************
* OpenGL ES Stuff ********
*******************************/
GLenum error;
// glEnable(GL_TEXTURE_2D);
// glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
//glMatrixMode(GL_TEXTURE);
// glViewport(0,0,1093, 785);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
error = glGetError();
printf("Error: %d\n", error);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glDisable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
// glRotatef(0.0, 1.0, 1.0, 1.0);

glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
error = glGetError();
printf("Error: %d\n", error);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
eglSwapBuffers(g_eglDisplay, g_eglSurface);
sleep(10);
return 0;


--------------------------------------------------------

Please help. I have done a lot of google but there is nothing found which can help.

Best Regards,
Naseeb Panghal

Dark Photon
08-18-2015, 03:38 PM
First step, check for GL errors (and EGL errors) at the end of the frame.

Second, it wouldn't take much for you to modify this so it's a standalone test program folks could compile and run for other folks. You might get more feedback that way.

Also, please specify which GLES implementation (or emulator) you're using, as well as which libraries you're linking against.