Search code examples
c++openglglut

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


I started to use OpenGL / glut for C++ and I am stuck on being able to display two objects in the window rather than one. So, for the first shape it draws a house-like shape with a square and triangle on the top, with anti-aliasing. The second shape, is supposed to be the same, but does not have anti-aliasing. For now, the second shape just has two dots as opposed to the entire shape. When I have both of the withoutAntiAliasing and withAntiAliasing methods inside of the void render() method, only one shows. How exactly can I make it so that both of the methods described can be shown in the window? Code is below:

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

Solution

  • If you want to draw lines, you must use Line primitives instead of Point primitives.
    The first mesh is not displayed because you clear the framebuffer before drawing the second mesh.

    Move glClear and glFlush in the render function and use the primitive type 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();
    }