Yechiel babani
Yechiel babani

Reputation: 379

Triangle in OpenGL

I'm trying to create a triangle with OpenGL but I see nothing but a white board on my screen.

Here's my code:

#include <GL/glut.h>

extern float vertices[3][3] = 
{
    { -0.5f, -0.5f, 0.0f },
    {  0.5f, -0.5f, 0.0f },
    {  0.0f,  0.5f, 0.0f }
};


void display()
{
    glClearColor(0, 0, 0, 1);
    glBegin(GL_TRIANGLES);
    glColor3fv(1, 0, 0);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glEnd();
    glFlush();
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Hello Gult");
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    return 0;
}

What could it be and how can I solve this?

Upvotes: 1

Views: 357

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

You need to call glutSwapBuffers when using a double-buffered window (GLUT_DOUBLE). Also, you must clear the depth buffer when the Depth Test is enabled. Use glColor3f instead of glColor3fv when specifying a color with 3 separate arguments:

void display()
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1, 0, 0);
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glEnd();
    glutSwapBuffers();
}

Upvotes: 3

Related Questions