SFML community forums

Help => Graphics => Topic started by: Asteroid on August 06, 2012, 07:20:09 am

Title: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 07:20:09 am
Hi there,
I am really new to OpenGL Programming and SFML.
I was trying to Draw a polygon in the Window.
I tried this code.
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include<SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // create the window
    sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    // load resources, initialize the OpenGL states, ...

    // run the main loop
    bool running = true;
    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }
        }

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        glTranslatef(-1.5f,0.0f,-6.0f);

        // draw...
        glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
           glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
           glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
           glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
        glEnd();  

        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}

I know something is missing but what is it?
Whenever i try to debug it.
Only a Blank Window Shows Up.
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 07:57:23 am
You don't define the clear color (glClearColor) nor the triangle color (glColor), so they might both be white.
Title: Re: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 08:21:53 am
That's the new code.
As you said about glClearColor and glColor they both added in it now.
and its still blank.

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include<SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // create the window
    sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // load resources, initialize the OpenGL states, ...


    // run the main loop
    bool running = true;
    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }
        }

        // clear the buffers

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        // draw...
       glTranslatef(3.0f,0.0f,0.0f);  
       glColor3f(0.5f,0.5f,1.0f);              // Set The Color To Blue One Time Only
       glBegin(GL_QUADS);                  // Start Drawing Quads
          glVertex3f(-1.0f, 1.0f, 0.0f);          // Left And Up 1 Unit (Top Left)
          glVertex3f( 1.0f, 1.0f, 0.0f);          // Right And Up 1 Unit (Top Right)
          glVertex3f( 1.0f,-1.0f, 0.0f);          // Right And Down One Unit (Bottom Right)
          glVertex3f(-1.0f,-1.0f, 0.0f);          // Left And Down One Unit (Bottom Left)
        glEnd();  

        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return TRUE;
}
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 08:45:29 am
Your depth-buffer handling is incomplete. Since this example doesn't use it, you should try to disable it with glDisable(GL_DEPTH_TEST).
Title: Re: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 09:04:17 am
I tried Disabling it using glDisable(GL_DEPTH_TEST).
the Window is still Blank.
What can i do?
Is not there any good Tutorial for this?

I have seen a tutorial for the Cube in SFML 1.2.
But as i said I'm really new to OpenGL.
I can't convert this 1.2 code to the 2.0 version code.
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 09:07:37 am
The SFML code is ok, now you need to find a pure OpenGL tutorial, which the SFML website doesn't provide.
Title: Re: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 09:20:18 am
Well What's wrong with the code i written above?
What should i do with it?
Why is it not working?
Why I always see a black screen?
That's all the questions on my mind rite now.

It will be great if I get an Answer for them.
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 09:24:38 am
The problem is that you don't define any modelview or projection matrix, so the display range is [-1, 1] on all axes. But your glTranslatef moves everything at x=3, so your quad is out of the visible area. If you remove it, you should see your blue quad covering the entire window.

A bigger problem is that you didn't read any OpenGL tutorial before trying to use OpenGL... What did you expect?
Title: Re: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 10:00:36 am
Well I'm keep getting this error while trying to set the glMatrixMode.

1>main.obj : error LNK2019: unresolved external symbol _gluPerspective@32 referenced in function _main
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 10:03:17 am
From the tutorial ("Using OpenGL in a SFML window"):
Quote
You will then need to link your program to the OpenGL library. Unlike what it does with the headers, SFML can't provide a unified way of linking OpenGL. Therefore, you need to know which library to link to according to what OS you're using ("opengl32" on Windows, "GL" on Linux, etc.). Same thing for GLU, in case you use it too: "glu32" on Windows, "GLU" on Linux, etc.

OpenGL functions start with the "gl" prefix, GLU functions start with the "glu" prefix. Remember this when you get linker errors, this will help you to find which library you forgot to link.
Title: Re: Windows Shows up Blank.
Post by: Asteroid on August 06, 2012, 10:15:11 am
Ehm well now i figured out everything.
Everything is working fine now.

Thanks for the help.
And sorry for being annoying. :P
Title: Re: Windows Shows up Blank.
Post by: Laurent on August 06, 2012, 10:31:00 am
You're not annoying, that's why forums exist ;)

But don't rush, take the time to read the SFML tutorials carefully, and find a good OpenGL reference (book or tutorials) :)