Alamin
Alamin

Reputation: 2165

Why multiple function call not working in Opengl?

I'm try to use different function to make various object and call by one function like display() in my Opengl project. But It's not working, when I called two function then show me a function another one is not showing, I don't know why,

I have tried this way:

#include <windows.h>  // for MS Windows
#include <GL/glut.h>  // GLUT, include glu.h and gl.h
#include <math.h>


void box() {

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer (background)
    // Draw a Red 1x1 Square centered at origin
    glBegin(GL_POLYGON);              // Each set of 4 vertices form a quad
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.9f, 0.7f);
    glVertex2f(0.9f, 0.7f);
    glVertex2f(0.9f, -0.7f);    // x, y

    glEnd();


    glFlush();  // Render now


}

void triangle()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);              // Each set of 4 vertices form a quad
    glColor3f(0.0f, 1.0f, 0.0f); // green

    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.4f, 0.7f);
    glVertex2f(0.1f, -0.7f);    // x, y

    glEnd();


    glFlush();  // Render now
}
void display()
{
    box();
    triangle();
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(1000, 600);// Set the window's initial width & height
    glutCreateWindow("OpenGL Setup Test");
    //gluOrtho2D(-0.1,0.7,-0.1,0.3); // Create a window with the given title
    //glutInitWindowSize(500, 500);// Set the window's initial width & height
    glutDisplayFunc(display);// Register display callback handler for window re-paint
    glutMainLoop();           // Enter the event-processing loop
    return 0;
}

Any suggestion please.

Upvotes: 3

Views: 468

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72479

You are clearing the buffer before rendering each of the shapes, thus erasing the previous shape you just drew. Instead you should clear it only once per display. Same goes with glFlush:

void box() {
    // Draw a Red 1x1 Square centered at origin
    glBegin(GL_POLYGON);
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.9f, 0.7f);
    glVertex2f(0.9f, 0.7f);
    glVertex2f(0.9f, -0.7f);    // x, y
    glEnd();
}

void triangle()
{
    glBegin(GL_POLYGON);
    glColor3f(0.0f, 1.0f, 0.0f); // green
    glVertex2f(-0.9f, -0.7f);
    glVertex2f(-0.4f, 0.7f);
    glVertex2f(0.1f, -0.7f);    // x, y
    glEnd();
}

void display()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer (background)
    box();
    triangle();
    glFlush();  // Render now
}

Upvotes: 2

Related Questions