How to display the image texture in GLES2?

Rajesh Gopu

How to display the image texture in GLES2.

In below source initializing GLES2 display,surface .., Creating offline framebuffer, Loading the RGBA image to texture,

Clearing the screen by BLUE color, Trying to display the loaded image texture (.. failed to find correct API for GLES2) Reading the FBO & writing to a file.

For displaying glEnableClientState&glVertexPointer API's is not supporting in GLES2 How to display the loaded image texture in GLES2. In the below source getting only blue color in buffer got from glReadPixels

unsigned char *video_raw = loadFile("./video.raw");//RGBA raw image
    int iConfigs;
    EGLConfig eglConfig;
    EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2,EGL_NONE };
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType)0);
    eglInitialize(eglDisplay, 0, 0);
    eglBindAPI(EGL_OPENGL_ES_API);
    EGLint pi32ConfigAttribs[5];
    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
    pi32ConfigAttribs[4] = EGL_NONE;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);
    EGLSurface  eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, NULL);
    EGLContext  eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    GLuint fboId = 0;
    GLuint renderBufferWidth = 960;
    GLuint renderBufferHeight = 540;

    glGenFramebuffers(1, &fboId);
    glBindFramebuffer(GL_FRAMEBUFFER, fboId);

    GLuint renderBuffer;
    glGenRenderbuffers(1, &renderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, renderBufferWidth, renderBufferHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);

    glClearColor(0.0,0.0,1.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);

    GLuint texture_object_id;
    glGenTextures(1, &texture_object_id);
    glBindTexture(GL_TEXTURE_2D, texture_object_id);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, renderBufferWidth, renderBufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, video_raw);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


    GLfloat vtx1[] = { -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 };
    GLfloat tex1[] = { 0, 0, 0, 1, 1, 1, 1, 0 };

    /*glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, vtx1);
    glTexCoordPointer(2, GL_FLOAT, 0, tex1);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);*/

    eglSwapBuffers( eglDisplay, eglSurface);

    //read & write to a file
    int size = 4 * renderBufferHeight * renderBufferWidth;
    unsigned char *data2 = new unsigned char[size];
    glReadPixels(0, 0, renderBufferWidth, renderBufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, data2);

    dumptoFile("./read1.raw", size, data2);

Edit 1:

@Rabbid76 , Thanks for the reply. When i used your vertx shader "in vec3 inPos;\n" shader compilation failed. i replace "in" with "uniform".

Getting black screen from the below source with your input added.

static const GLuint WIDTH = 960;
static const GLuint HEIGHT = 540;

static const GLchar* vertex_shader_source =
        "#version 100\n"
        "precision mediump float;\n"
        "uniform vec3 inPos;\n"
        "uniform vec2 inUV;\n"
        "varying vec2 vUV;\n"
        "void main(){\n"
        "    vUV = inUV;\n"
        "    gl_Position = vec4(inPos, 1.0);\n"
        "}\n";

static const GLchar* fragment_shader_source =
        "#version 100\n"
        "precision mediump float;\n"
        "varying vec2 vUV;\n"
        "uniform sampler2D u_texture;\n"
        "void main(){\n"
        "    gl_FragColor = texture2D(u_texture, vUV);\n"
        "}\n";

int main(int argc, char **argv)
{
    unsigned char *video_raw = loadFile("./video.raw");

    int iConfigs;
    EGLConfig eglConfig;
    EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType) 0);
    eglInitialize(eglDisplay, 0, 0);
    eglBindAPI(EGL_OPENGL_ES_API);
    EGLint pi32ConfigAttribs[5];
    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
    pi32ConfigAttribs[4] = EGL_NONE;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);
    EGLSurface eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, NULL);
    EGLContext eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);
    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    GLuint shader_program, framebuffer, renderBuffer;

    glGenRenderbuffers(1, &renderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, WIDTH, HEIGHT);

    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);

    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glViewport(0, 0, WIDTH, HEIGHT);
    glEnable(GL_TEXTURE_2D);

    shader_program = common_get_shader_program(vertex_shader_source, fragment_shader_source);

    GLint vert_inx = glGetAttribLocation(shader_program, "inPos");
    GLint uv_inx = glGetAttribLocation(shader_program, "inUV");
    GLint tex_loc = glGetUniformLocation(shader_program, "u_texture");

    GLuint texture_object_id;
    glGenTextures(1, &texture_object_id);
    glBindTexture(GL_TEXTURE_2D, texture_object_id);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, video_raw);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    GLfloat vtx1[] = { -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 };
    GLfloat tex1[] = { 0, 0, 0, 1, 1, 1, 1, 0 };

    glVertexAttribPointer(vert_inx, 3, GL_FLOAT, GL_FALSE, 0, vtx1);
    glEnableVertexAttribArray(vert_inx);
    glVertexAttribPointer(uv_inx, 2, GL_FLOAT, GL_FALSE, 0, tex1);
    glEnableVertexAttribArray(uv_inx);

    glViewport(0,0,renderBufferWidth,renderBufferHeight);

    glUseProgram(shader_program);
    glUniform1i(tex_loc, 0);
    glDrawArrays( GL_TRIANGLE_FAN, 0, 4);

    glFlush();

    int size = 4 * WIDTH * HEIGHT;
    unsigned char *data2 = new unsigned char[size];
    glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, data2);
    dumptoFile("./read1.raw", size, data2);

    return EXIT_SUCCESS;
}
Rabbid76

You have to use a shader program, and to define the arrays of generic vertex attribute data. See also Vertex Specification.

Create, compile and link a very simple shader program like the following:

const char *sh_vert =
"#version 100\n"\
"precision mediump float;\n"\
"attribute vec3 inPos;\n"\
"attribute vec2 inUV;\n"\
"varying vec2 vUV;\n"\
"void main()\n"\
"{\n"\
"    vUV = inUV;\n"\
"    gl_Position = vec4(inPos, 1.0);\n"\
"}";

const char *sh_frag =
"#version 100\n"\
"precision mediump float;\n"\
"varying vec2 vUV;\n"\
"uniform sampler2D u_texture;\n"\
"void main()\n"\
"{\n"\
"    gl_FragColor = texture2D(u_texture, vUV);\n"\
"}";

GLuint v_sh = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( v_sh, 1, &sh_vert, nullptr );
glCompileShader( v_sh );
GLint status = GL_TRUE;
glGetShaderiv( v_sh, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    // compile error
}

GLuint f_sh = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( f_sh, 1, &sh_frag, nullptr );
glCompileShader( f_sh );
status = GL_TRUE;
glGetShaderiv( f_sh, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    // compile error
}

GLuint prog = glCreateProgram();
glAttachShader( prog, v_sh );
glAttachShader( prog, f_sh );
glLinkProgram( prog );
status = GL_TRUE;
glGetProgramiv( prog, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
    // link error
}

Get the attribute indices and the location of the texture sampler uniform:

GLint vert_inx = glGetAttribLocation( prog, "inPos" );
GLint uv_inx   = glGetAttribLocation( prog, "inUV" );
GLint tex_loc  = glGetUniformLocation( prog, "u_texture" );

Then define the arrays of generic vertex attribute data by (glVertexAttribPointer) and enable them by glEnableVertexAttribArray:

GLfloat vtx1[] = { -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 };
GLfloat tex1[] = { 0, 0, 0, 1, 1, 1, 1, 0 };

glVertexAttribPointer( vert_inx, 3, GL_FLOAT, GL_FALSE, 0, vtx1);
glEnableVertexAttribArray( vert_inx );
glVertexAttribPointer( uv_inx, 2, GL_FLOAT, GL_FALSE, 0, tex1);
glEnableVertexAttribArray( uv_inx );

Setup the renderbuffer and the framebuffer and adjust the viewport:

glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fboId);

GLuint renderBuffer;
glGenRenderbuffers(1, &renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, renderBufferWidth, renderBufferHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);

glViewport(0,0,renderBufferWidth,renderBufferHeight);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

Use the program, set the texture sampler uniform and draw the geometry:

// use the program
glUseProgram( prog );
glUniform1i( tex_loc, 0 ); // 0 == texture unit 0

// draw the geometry
glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );

glUseProgram( 0 );

Finally the image can be read:

int size = 4 * renderBufferHeight * renderBufferWidth;
unsigned char *data2 = new unsigned char[size];
glReadPixels(0, 0, renderBufferWidth, renderBufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, data2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related