Results 1 to 2 of 2

Thread: Simple 2D drawing one pixel at a time

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1

    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:

    #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
    }

  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Texas
    Posts
    61

    Re: Simple 2D drawing one pixel at a time

    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

Similar Threads

  1. Drawing a simple square in webgl
    By thequarrymen33 in forum Developers Coding:Beginner
    Replies: 4
    Last Post: 08-18-2012, 07:57 PM
  2. Drawing two Rendering Context in same time.
    By danielkim in forum OpenGL ES general technical discussions
    Replies: 4
    Last Post: 03-30-2007, 06:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •