在OpenGL中绘制单个像素

时间:2013-01-03 19:58:07

标签: c++ opengl

我正在尝试使用OpenGL / C ++突出显示(“颜色#00FFFF”)图像中的特定单个像素(已在背景中显示)。像素坐标和图像存在于精确的2D空间中,但到目前为止我在项目中看到的所有OpenGL代码 - glTranslatef()glScalef() - 都是基于3D和浮点数的,并且该对象似乎与绘制时间分开。

我已经习惯了Java的Graphics2D软件包,在那里我可以调用一些东西来实现

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

它将填充指定位置的像素。有什么类似的语法 - 我可以在OpenGL中设置大小,设置位置,并在一行中绘制所有内容吗?如果没有,我将如何调整我的2D +像素输入到OpenGL的浮点和3D结构?

我目前有这个:

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();

我从一些Google搜索和我的代码的其他部分拼凑而成,但我没有看到任何正在绘制的内容。我不知道translate,vertex或pointsize命令的单位。如果我可以用上面的Java命令替换所有这些内容,那将是非常棒的。如果没有,是否有某种方式可以保证我在这里绘制的任何东西都会在其他所有东西的“顶部”,但仍然不在镜头后面。

1 个答案:

答案 0 :(得分:1)

  

是否有类似于该语法的内容 - 我可以在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;
}
相关问题