0
\$\begingroup\$

I have tried to follow nehe's opengl tutorial lesson 2. I use sfml for my window creation. The problem I have is that both the triangle and the quad don't show up on the screen:

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>

void processEvents(sf::Window *app);
void processInput(sf::Window *app, const sf::Input &input);

void renderCube(sf::Window *app, sf::Clock *clock);
void renderGlScene(sf::Window *app);

void init();

int main()  {
    sf::Window app(sf::VideoMode(800, 600, 32), "Nehe Lesson 2");

    app.UseVerticalSync(false);

    init();

    while (app.IsOpened())  {
        processEvents(&app);

        renderGlScene(&app);

        app.Display();
    }

    return EXIT_SUCCESS;
}

void init()  {
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable z-buffer and read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perpective projection
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluPerspective(45.f, 1.f, 1.f, 500.f);

    glShadeModel(GL_SMOOTH);
}

void processEvents(sf::Window *app)  {
    sf::Event event;

    while (app->GetEvent(event))  {
        if (event.Type == sf::Event::Closed)  {
            app->Close();
        }

        if (event.Type == sf::Event::KeyPressed &&
            event.Key.Code == sf::Key::Escape)  {
            app->Close();
        }
    }
}

void renderGlScene(sf::Window *app)  {
    app->SetActive();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear the screen and the depth buffer
    glLoadIdentity();                   // Reset the view

    glTranslatef(-1.5f, 0.0f, -6.0f);   // Move Left 1.5 units and into the screen 6.0

    glBegin(GL_TRIANGLES);
        glVertex3f( 0.0f, 1.0f, 0.0f);  // Top
        glVertex3f(-1.0,-1.0f, 0.0f);  // Bottom Left
        glVertex3f( 1.0f,-1.0f, 0.0f);  // Bottom Right
    glEnd();

    glTranslatef(3.0f, 0.0f, 0.0f);

    glBegin(GL_QUADS);  // Draw a quad
        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f);
        glVertex3f(-1.0f,-1.0f, 0.0f);
    glEnd();
}

I would greatly appreciate it if someone could help me resolve my issue.

\$\endgroup\$
3
  • \$\begingroup\$ have you tried setting the clear color to anything other than black to see if it works? \$\endgroup\$
    – dreta
    Jul 6 '12 at 11:26
  • \$\begingroup\$ Okay, I fixed it, I just added glMatrixMode(GL_MODELVIEW); directly after I call glClear() in the rendering function. Thanks for trying to help, I really appreciate it! :) And this is old OpenGL? I had no idea, I'm very new to the world of C++. \$\endgroup\$
    – Darestium
    Jul 6 '12 at 11:26
  • \$\begingroup\$ I think you might want to change the first glMatrixMode call in your init function to be glMatrixMode(GL_PROJECTION). Currently, you're creating a projection matrix in the modelview space, which I don't think is what you're after. You solved your issue, but my note is only to save you trouble later on :) \$\endgroup\$
    – Ray Dey
    Jul 6 '12 at 11:53
1
\$\begingroup\$

Changed init's from:

glMatrixMode(GL_MODELVIEW);

to

glMatrixMode(GL_PROJECTION);

and added

glMatrixMode(GL_MODELVIEW);

to "renderGlScene(sf::Window *app)" directly after "glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)" is called.

\$\endgroup\$
1
  • \$\begingroup\$ That's right. OpenGL is state based, so your glMatrix calls always operate on the last matrix you selected. Here you were always operating on the MODELVIEW matrix and had no projection matrix. If you don't plan on changing the projection matrix again, you could put glMatrixMode( GL_MODELVIEW ); right after your gluPerspective(45.f, 1.f, 1.f, 500.f); call \$\endgroup\$
    – bobobobo
    Jul 6 '12 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.