How to clear a region of a window in OpenGL-ES

Hi,

I know what i am asking is a silly question, but somehow i am not able to get the answer of it. I want to create a window & then clear a particular area of that window with a given color. I am able to clear the whole buffer/window using glClearColor, but not able to achieve clearing a specific region. I am putting my code here, if any one please point out where i am going wrong, it would be great.Thanks a lot.

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GLES/egl.h>

#define WIDTH 400
#define HEIGHT 400

EGLDisplay disp;
EGLSurface egl_surf;
void display(void)
{
/* draw white polygon (rectangle) with corners at

  • (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
    /
    glColor4f (0.0, 0.0, 1.0,1.0);
    //glBegin(GL_POLYGON);
    glVertex3f (100, 100, 0.0);
    glVertex3f (300, 100, 0.0);
    glVertex3f (300, 250, 0.0);
    glVertex3f (100, 250, 0.0);
    //glEnd();
    //HERE the above code does not work. i am not able to clear the area with BLUE
    /
    don’t wait!
  • start processing buffered OpenGL routines
    */
    glFlush ();
    eglSwapBuffers(disp,egl_surf);
    }

void init (void)
{
/* select clearing (background) color /
glClearColor (1.0, 0.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
eglSwapBuffers(disp,egl_surf); // ==>THIS IS SUCCESSFUL, i can see the buffer cleared with RED.
/
initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

/*

  • Declare initial window size, position, and display mode
  • (single buffer and RGBA). Open window with “hello”
  • in its title bar. Call initialization routines.
  • Register callback function to display graphics.
  • Enter main loop and process events.
    */

int main (int argc,
char *argv[])
{
//RAHAMAN -EGL related code

    EGLint cfg_attribs[] = {
EGL_BUFFER_SIZE,    EGL_DONT_CARE,
EGL_RED_SIZE,       5,
EGL_GREEN_SIZE,     6,
EGL_BLUE_SIZE,      5,
EGL_DEPTH_SIZE,      16,
EGL_ALPHA_SIZE,      EGL_DONT_CARE,
EGL_STENCIL_SIZE,    2,
EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
EGL_NONE
	};

EGLBoolean status=EGL_FALSE;
printf("Default value of status %d
",status);
EGLConfig configs[2];
EGLint config_count;
EGLContext egl_context;

GLshort rect_verts[8] = {
(GLshort) 100, (GLshort) 100,
(GLshort) 140, (GLshort) 100,
(GLshort) 100, (GLshort) 300,
(GLshort) 140, (GLshort) 300
};

int egl_major,egl_minor;

//RAHAMAN EGL context related code
disp = eglGetDisplay(EGL_DEFAULT_DISPLAY);

status = eglInitialize (disp,
&egl_major,
&egl_minor);
printf("eglInit returns status %d
",status);
status = eglGetConfigs (disp,
configs,
2,
&config_count);
printf("eglGetConfig returns status %d
",status);
status = eglChooseConfig (disp,
cfg_attribs,
configs,
1,
&config_count);
printf("eglChoose returns status %d
",status);
egl_surf=eglCreateWindowSurface (disp,configs[0],
//(NativeWindowType) stage_x11->xwin,
(NativeWindowType) updater_get_native_window(0, 0, WIDTH,HEIGHT),
NULL);

printf("eglCreateWindow returns status %d
",eglGetError());

egl_context=eglCreateContext (disp,configs[0],EGL_NO_CONTEXT,NULL);

printf("eglCreateContext returns status %d
",eglGetError());
eglMakeCurrent (disp,egl_surf,egl_surf,egl_context);
printf("eglMakeCurrent returns status %d
",eglGetError());

init ();
display();
//glutMainLoop();
return 0; /* ISO C requires main to return int. */
}

Are you using OpenGL or OpenGL ES?

Hi XMas,

Long time since we have talked if you remember.

I am using OpenGL-ES, i found the header file path problem after i posted it and rectified it, but of no use.

Now, i am only using
#include <stdio.h>
#include <GLES/gl.h>
//#include <GL/glut.h>
#include <GLES/egl.h> , so no glut.h also.

I am creating my contexts using EGL, so i dont need glut even.

Hi Xmas,

This is my modified code which only links to GL-ES and not to GL.

#include <stdio.h>
#include <GLES/gl.h>
//#include <GL/glut.h>
#include <GLES/egl.h>

#define WIDTH 400
#define HEIGHT 400

EGLDisplay disp;
EGLSurface egl_surf;

/*
GLshort rect_verts[8] = {
(GLshort) x, (GLshort) y,
(GLshort) (x + width), (GLshort) y,
(GLshort) x, (GLshort) (y + height),
(GLshort) (x + width), (GLshort) (y + height)
};
/
GLshort rect_verts[8] = {
(GLshort) 100, (GLshort) 100,
(GLshort) 140, (GLshort) 100,
(GLshort) 100, (GLshort) 300,
(GLshort) 140, (GLshort) 300
};
void display(void)
{
/
draw white polygon (rectangle) with corners at

  • (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
    */

    glVertexPointer (2, GL_SHORT, 0, rect_verts );
    glColor4f (0.0,0.0, 1.0,0.0);
    //glColor4x (0xffff0000,0xffff0000, 0,0xffff0000);
    glDrawArrays (GL_TRIANGLE_STRIP, 0, 4 );
    /*
    glBegin(GL_POLYGON);
    glVertex3f (100, 100, 0.0);
    glVertex3f (300, 100, 0.0);
    glVertex3f (300, 250, 0.0);
    glVertex3f (100, 250, 0.0);
    glEnd();
    /
    //HERE the above code does not work. i am not able to clear the area with BLUE
    /
    don’t wait!

  • start processing buffered OpenGL routines
    */
    glFlush ();
    eglSwapBuffers(disp,egl_surf);
    }

void init (void)
{
/* select clearing (background) color /
glViewport(0,0,WIDTH,HEIGHT);
glClearColor (0.0, 1.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
eglSwapBuffers(disp,egl_surf); // ==>THIS IS SUCCESSFUL, i can see the buffer cleared with RED.
/
initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glEnable(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_COLOR_ARRAY);
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

/*

  • Declare initial window size, position, and display mode
  • (single buffer and RGBA). Open window with “hello”
  • in its title bar. Call initialization routines.
  • Register callback function to display graphics.
  • Enter main loop and process events.
    */

int main (int argc,
char *argv[])
{
//RAHAMAN -EGL related code

    EGLint cfg_attribs[] = {
EGL_BUFFER_SIZE,    EGL_DONT_CARE,
EGL_RED_SIZE,       5,
EGL_GREEN_SIZE,     6,
EGL_BLUE_SIZE,      5,
EGL_DEPTH_SIZE,      16,
EGL_ALPHA_SIZE,      EGL_DONT_CARE,
EGL_STENCIL_SIZE,    2,
EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
EGL_NONE
	};

EGLBoolean status=EGL_FALSE;
printf("Default value of status %d
",status);
EGLConfig configs[2];
EGLint config_count;
EGLContext egl_context;

GLshort rect_verts[8] = {
(GLshort) 100, (GLshort) 100,
(GLshort) 140, (GLshort) 100,
(GLshort) 100, (GLshort) 300,
(GLshort) 140, (GLshort) 300
};

int egl_major,egl_minor;

//RAHAMAN EGL context related code
disp = eglGetDisplay(EGL_DEFAULT_DISPLAY);

status = eglInitialize (disp,
&egl_major,
&egl_minor);
printf("eglInit returns status %d
",status);
status = eglGetConfigs (disp,
configs,
2,
&config_count);
printf("eglGetConfig returns status %d
",status);
status = eglChooseConfig (disp,
cfg_attribs,
configs,
1,
&config_count);
printf("eglChoose returns status %d
",status);
egl_surf=eglCreateWindowSurface (disp,configs[0],
//(NativeWindowType) stage_x11->xwin,
(NativeWindowType) updater_get_native_window(0, 0, WIDTH,HEIGHT),
NULL);

printf("eglCreateWindow returns status %d
",eglGetError());

egl_context=eglCreateContext (disp,configs[0],EGL_NO_CONTEXT,NULL);

printf("eglCreateContext returns status %d
",eglGetError());
eglMakeCurrent (disp,egl_surf,egl_surf,egl_context);
printf("eglMakeCurrent returns status %d
",eglGetError());

init ();
display();
//glutMainLoop();
return 0; /* ISO C requires main to return int. */
}

Your vertices are far outside the clip volume. Try something like the code below to set up your protection matrix if you want to be able to specify pixel coordinates. Also, you might want to make sure that backface culling is disabled so the vertex winding order makes no difference, i.e. glDisable(GL_CULL_FACE).

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, WIDTH, HEIGHT, 0, -1, 1);

Why not set up a scissor rect for the area you want to fill and just use glClear?

Thanks Don, that is possible.But my sole purpose was to use glColor4f/glColor4x API & then use necessary methods to draw a rectangle/clear a specific region.

Because of some other reason, i have a feeling that my gl library’s glColor4X/glColor4f API might not be working properly, so i need to get a working simple code to test this.