Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
have you tried setting the clear color to anything other than black to see if it works? –  dreta Jul 6 '12 at 11:26
    
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++. –  Darestium Jul 6 '12 at 11:26
    
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 :) –  Ray Dey Jul 6 '12 at 11:53
    
@RayDey Cool! Thanks for the tip ;) –  Darestium Jul 6 '12 at 12:15

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  bobobobo Jul 6 '12 at 15:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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