Benjin
Benjin

Reputation: 2409

Drawing Individual Pixels in OpenGL

I'm trying to highlight ("color #00FFFF") specific individual pixels in an image (already displayed in the background) using OpenGL/C++. The pixel coordinates and the image exist in exact 2D space, but all the OpenGL code I'm seeing in the project so far - glTranslatef(), glScalef() - is 3D and float-based, and the positioning of the object appears to happen separately from the time it's drawn.

I'm used to Java's Graphics2D package, where I can call something to the effect of

width = 1; height = 1;
buffer.drawRect(width, height, xPosition, yPosition);

and it'll fill in a pixel at the specified location. Is there anything similar to that syntax - where I can set size, set position, and draw all in one line - in OpenGL? If not, how would I go about adapting my 2D+pixel input to OpenGL's float and 3D structure?

I currently have this:

glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f);
glColor3f(0, 1, 1);
glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex3f(1.0f, 1.0f, 1.0f);
glPopMatrix();

which I pieced together from some Google searches and other parts of my code, but I don't see anything being draw. I have no idea as to the units for the translate, vertex, or pointsize commands. It'd be awesome if I could replace all of that with something like the Java command up above. If not, is there some way I can guarantee whatever I draw here will be "on top" of everything else, but still not behind the camera.

Upvotes: 1

Views: 4916

Answers (1)

genpfault
genpfault

Reputation: 52084

Is there anything similar to that syntax - where I can set size, set position, and draw all in one line - in OpenGL?

glRect():

#include <GL/glut.h>

void display()
{
    glEnable( GL_CULL_FACE );
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE );

    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );

    glDepthMask( GL_TRUE );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // draw teapot
    glEnable( GL_DEPTH_TEST );
    glDepthMask( GL_TRUE );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 60, w / h, 1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -5 );

    glColor4ub( 255, 0, 0, 255 );
    glPushMatrix(); 
    float angle = 60.0f * ( glutGet(GLUT_ELAPSED_TIME) / 1000.0f );
    glRotatef( angle, 0.1, 0.95, 0.05 );
    glutSolidTeapot( 1.0 );
    glPopMatrix();

    // draw rectangle
    glDisable( GL_DEPTH_TEST );
    glDepthMask( GL_FALSE );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, w, 0, h, -1, 1);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor4ub( 0, 255, 0, 128 );
    glRecti( 0 + 50, 0 + 50, w - 50, h - 50 );

    glutSwapBuffers();
}

void timer( int extra )
{
    glutPostRedisplay();
    glutTimerFunc( 16, timer, 0 );
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "Rect" );
    glutDisplayFunc( display );
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return 0;
}

Upvotes: 1

Related Questions