Ninja_Panda
Ninja_Panda

Reputation: 25

C++ OpenGl/Glut failing to draw

I am trying to simply draw a triangle in a window. I've drawn shapes before in previous code, and have looked up common issues such as failure to flush or not clearing the color buffer.

No matter what I seem to try though, I can't get anything to draw on screen, even after I've simplified my code to basically look exactly like my previous (working!) code. All I have is a main and a render:

// Declarations //
void Render(void); //Call the drawing functions

int main(int argc, char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(20,20);
    glutCreateWindow("Triangle Test");

    //prepare for drawing
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    //now draw
    glutDisplayFunc(Render);

    glutMainLoop();
}


// ---- Render Function ----
void Render(void)
{

    // Draw a triangle
    glColor3f(1.0f, 1.0f, 1.0f);

    glBegin(GL_LINE_STRIP);

    glVertex2f(100.0f, 20.0f);
    glVertex2f(0.0f, 20.0f);
    glVertex2f(20.0f, 50.0f);

    glEnd();
    glFlush();
}

On run, it draws a window with the background color I set (in this case black) and nothing else. I'm completely stumped. All of the other questions on stack seem to be resolved by things I have in here (i.e. glFlush) and its virtually identical to my old code, which draws fine. Any ideas?

Upvotes: 1

Views: 785

Answers (1)

user1118321
user1118321

Reputation: 26355

You're drawing a line strip that's bigger than your window. You need to either set your matrices so you see a larger area, draw a smaller polygon, or draw a filled polygon by drawing a triangle instead of a line strip.

Upvotes: 1

Related Questions