如何使用 C++ OpenGL Glut 显示两个形状而不是一个?

How can I display two shapes instead of one by using C++ OpenGL Glut?

我开始为 C++ 使用 OpenGL / glut,但我一直坚持能够在 window 中显示两个对象而不是一个。因此,对于第一个形状,它绘制了一个类似房屋的形状,顶部有一个正方形和一个三角形,并具有抗锯齿功能。第二个形状,应该是相同的,但没有抗锯齿。现在,第二个形状只有两个点而不是整个形状。当我在 void render() 方法中同时使用 withoutAntiAliasingwithAntiAliasing 方法时,只有一个显示。我怎样才能使所描述的两种方法都显示在 window 中?代码如下:

#include <GL/glut.h>

void init()
{
    glClearColor(1.0, 1.0, 1.0, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 250.0, 0.0, 250.0);
}

void withAntiAliasing()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glBegin(GL_LINE_LOOP);
    glVertex2i(155, 150);
    glVertex2i(125, 125);
    glVertex2f(108.5, 162);
    glVertex2f(136, 185);
    glEnd();

    glBegin(GL_LINE_LOOP);
    glVertex2i(100, 155.9);
    glVertex2f(145, 192.5);
    glVertex2i(115, 185);
    glEnd();

    glFlush();
}

void withoutAntiAliasing()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_BLEND);

    glBegin(GL_POINTS);
    glColor3f(0, 0, 0);
    glVertex2i(170, 170);
    glColor3f(0, 0, 0);
    glVertex2i(150, 150);
    glEnd();

    glFlush();
}

void render()
{
    glColor3f(0.0, 0.0, 0.0);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    withoutAntiAliasing();
    withAntiAliasing();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Hey");

    init();
    glutDisplayFunc(render);

    glutMainLoop();
}

如果要画线,必须使用Line primitives instead of Point primitives
第一个网格未显示,因为您在绘制第二个网格之前清除了帧缓冲区。

render函数中移动glClearglFlush并使用原始类型GL_LINE_LOOP:

void withAntiAliasing()
{
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glBegin(GL_LINE_LOOP);
    glVertex2i(155, 150);
    glVertex2i(125, 125);
    glVertex2f(108.5, 162);
    glVertex2f(136, 185);
    glEnd();

    glBegin(GL_LINE_LOOP);
    glVertex2i(100, 155.9);
    glVertex2f(145, 192.5);
    glVertex2i(115, 185);
    glEnd();
}

void withoutAntiAliasing()
{
    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_BLEND);

    glBegin(GL_LINE_LOOP);
    glColor3f(0, 0, 0);
    glVertex2i(170, 170);
    glColor3f(0, 0, 0);
    glVertex2i(150, 150);
    glEnd();
}

void render()
{
    glColor3f(0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    withoutAntiAliasing();
    withAntiAliasing();

    glFlush();
}