我正在尝试使用 OpenGL/C++ 突出显示(“颜色 #00FFFF”)图像中的特定单个像素(已显示在背景中)。像素坐标和图像存在于精确的二维空间中,但到目前为止我在项目中看到的所有 OpenGL 代码 - glTranslatef()
、glScalef()
- 是3D 和基于 float ,对象的定位似乎与其绘制时间分开发生。
习惯了Java的Graphics2D包,里面可以调用一些效果
width = 1; height = 1;
buffer.drawRect(width, height, xPosition, yPosition);
它会在指定位置填充一个像素。在 OpenGL 中,是否有任何类似于该语法的内容 - 我可以在其中设置大小、设置位置并在一行中绘制所有内容?如果不是,我将如何调整我的 2D+ 像素输入以适应 OpenGL 的 float 和 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();
我从一些谷歌搜索和我的代码的其他部分拼凑而成,但我没有看到任何东西被绘制。我不知道平移、顶点或点大小命令的单位。如果我能用上面的 Java 命令之类的东西替换所有这些,那就太棒了。如果不是,有什么方法可以保证我在这里绘制的任何内容都将位于其他所有内容的“顶部”,但仍然不在镜头后面。
最佳答案
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;
}
关于c++ - 在 OpenGL 中绘制单个像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14146249/