c++sfmlopengl-compat

Triangle is not rendering


I wrote a simple program using the glFrustum function. All points of the triangle are within range, but it doesn't appear.

glewInit();
sf::Window window(sf::VideoMode(800, 800), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
// activate the window
window.setActive(true);
bool running = true;   
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

while (running)
{
    ........//event process
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glBegin(GL_TRIANGLES);
    glColor3f(0.1, 0.7, 0.2);
    glVertex3f(0, 0, -5);
    glVertex3f(2, 2, -5);
    glVertex3f(3, 3, -5);
    glEnd();

    window.display();
}

Solution

  • The problem is that these three vertices (0, 0, -5), (2, 2, -5) and (3, 3, -5) don't form a triangle. These are collinear, i.e. these three points lie on a line.

    You could, for example, change the middle vertex to (2, 0, -5) and a triangle will render:

    enter image description here