Simple 2D drawing one pixel at a time

Hi,

I’m having problems with OpenGL on Android using the NDK:

Very simply, I am trying to draw on the screen, one pixel at a time. I am not trying to do 3D or textures or anything complicated.

Using Canvas is not an option, the (nearly identical) code runs on linux, so I know it can be done, I just can’t figure out how to do it on Android.

Once the Init function has run, then the pixel function is called to draw however many dots desired and once the dots are drawn the drawscreen function is called and the pixels appear.

The code works on linux, but on Android (v 2.3), all I get is a black screen.

Please, can anyone help?

Thanks!

I’ve simplified the code and removed error-checking to make it more concise:

[i]#ifdef ANDROID

EGLDisplay dpy;
EGLSurface surface;

int Init( struct android_app * app ) {

dpy = eglGetDisplay( EGL_DEFAULT_DISPLAY );

eglInitialize( dpy, 0, 0 );
   
const EGLint attribs[] = {

    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
   
    EGL_BLUE_SIZE,              8,
    EGL_GREEN_SIZE,             8,
    EGL_RED_SIZE,               8,
   
    EGL_NONE

};

EGLConfig config;
EGLint numConfigs;

eglChooseConfig( dpy, attribs, &config, 1, &numConfigs );

EGLint format;

eglGetConfigAttrib( dpy, config, EGL_NATIVE_VISUAL_ID, &format );

ANativeWindow_setBuffersGeometry( app->window, 0, 0, format );

EGLSurface surface = eglCreateWindowSurface( dpy, config, app->window, NULL );

EGLContext context = eglCreateContext( dpy, config, NULL, NULL );

eglMakeCurrent( dpy, surface, surface, context );

int XSize;
eglQuerySurface( GLapp.dpy, GLapp.surface, EGL_WIDTH, &XSize );

int YSize;
eglQuerySurface( GLapp.dpy, GLapp.surface, EGL_HEIGHT, &YSize );

#else
int Init( int argc, char *argv[] ) {

int XSize = 1024;
int YSize = 768;

glutInit( &argc, argv );

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );

glutInitWindowSize( XSize, YSize );

glutCreateWindow( "test" );

glutDisplayFunc( drawscreen );

#endif

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

#ifdef ANDROID
glOrthof( 0, XSize, YSize, 0, 0, 1 );
#else
glOrtho( 0, XSize, YSize, 0, 0, 1 );
#endif

glMatrixMode( GL_MODELVIEW );

/* Displacement trick for exact pixelization */
glTranslatef( 0.375, 0.375, 0 );

glDisable( GL_DEPTH_TEST );

glClear( GL_COLOR_BUFFER_BIT );

#ifdef ANDROID

// Not sure about this bit
const GLbyte KVertices [] = {
    0, 1, 0,
   -1, 0, 0,
    1, 0, 0,
    0, 0, 1
};

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 4, GL_BYTE , 0, KVertices );
glDrawArrays( /*GL_QUADS */ GL_TRIANGLE_STRIP, 0, 4 );

#endif

return 0;

}

void pixel( int x, int y ) {

// White color
glColor4f( 1.0, 1.0, 1.0, 0 );

#ifdef ANDROID
glVertexAttrib2f( 0, x + 0.5, y + 0.5 );
#else
glVertex2f( x + 0.5, y + 0.5 );
#endif

}

void drawscreen( void ) {

#ifdef ANDROID
eglSwapBuffers( dpy, surface );
#else
glutSwapBuffers();
#endif
}[/i]

Using OpenGL ES with the Android NDK is not for beginners. I recommend that you start with either the native-activity or hello-gl2 sample apps in the NDK. This article will help:

http://software.intel.com/en-us/article … ors-part-1

Regards, Clay

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.