Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: GL Clear Color does not work, Renderwindow won't draw  (Read 4915 times)

0 Members and 1 Guest are viewing this topic.

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
GL Clear Color does not work, Renderwindow won't draw
« on: July 07, 2014, 02:25:46 am »
I'm having an issue using multiple windows with SFML. At the moment all of my graphics in the program are rendered with modern OpenGL, but I want to create a separate window that I use as sort of a console for my main window. Because of this my main window is a
sf::Window
and my addition window I create is a
sf::RenderWindow

The problem however is that when I attempt to display both windows, my main window no longer clears the background color to blue, and my renderwindow also does not draw a simple circle. I attempted messing around with
setActive()
but nothing seems to be working. Here's what I'm talking about:

What the program normally looks like


black background when console window drawn, circle also does not render to console window


What I'm doing:

console window declaration: (Exact same context settings, but this is a renderwindow as opposed to window
    ConsoleWindow.create(sf::VideoMode(400,200),
        "Console",
        sf::Style::Default,
        sf::ContextSettings(32, 8, 0, 3, 3)
    );

console window render
    ParentWindow->setActive(false);
    ConsoleWindow.draw(circle);
    ConsoleWindow.display();
    ParentWindow->setActive(true);

Just before the main loop starts, I call
window.setActive(true)

During the main loop(very simplified form):
       
        //handle events
            //...
        //call GL clear color to dark blue
        //compute matricies
        //enable vertex attrib arrays
        //draw arrays instanced
        //disable vertex attrib arrays
        window.display(); //window to draw OpenGL to
        console_window.render(); //calls console render code as seen above

Any ideas as to how I can get these two windows to render in harmony?
« Last Edit: July 07, 2014, 02:27:56 am by Syntactic Fructose »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: GL Clear Color does not work, Renderwindow won't draw
« Reply #1 on: July 07, 2014, 03:07:21 am »
I just had a look into your repository, because the information you provided here wasn't enough to provide a proper diagnosis. The first thing I noticed is that your main() is waaaaaay too long. You really need to section the code into sub-procedures. That way you know which window your GL operations currently have an effect on.

When you start mixing both SFML rendering and your own GL rendering, the thing beginners to SFML often get wrong is that they assume that SFML rendering will go out of its way to not interfere with your own GL rendering. SFML makes no guarantees that any GL state is preserved within a call. You will have to make sure that your required GL states are restored after accessing anything related to the SFML graphics module. The inverse is true as well. You will just as well have to make sure that any of SFML's GL state is restored before you access anything related to the graphics module.

This is the reason why I recommended splitting your rendering up into an SFML sub-procedure and a GL sub-procedure. If each is performed once per frame, you will only have to do state housekeeping twice, each time you switch from one to the other. There is a section about this in the Window module tutorial, but the solution that is offered only works properly with legacy GL, so you will have to do everything by hand since you are using modern GL.

Your glClearColor(0.0f, 0.0f, 0.1f, 0.0f); call is probably also overridden by the glClearColor in RenderWindow (which defaults to black). That is also state that has to be preserved.

You might also want to clear the RenderWindow at the start of each frame...
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: GL Clear Color does not work, Renderwindow won't draw
« Reply #2 on: July 07, 2014, 05:38:06 pm »
I really appreciate the advice. I knew deep down my main.cpp was horrendous and badly needed restructuring, I think it's time I spend some time of that before adding more code to the project.

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: GL Clear Color does not work, Renderwindow won't draw
« Reply #3 on: July 08, 2014, 09:15:19 pm »
So update to my problem, I managed to fix the issue; however I am not too sure if this is an actual solution or a dirty workaround.

I moved the creation of the console window to before I set the initial viewport, and moved the render call to before the actual openGL draw. This works fine:

sf::Window window;
        window.create(sf::VideoMode(1000,750),          //declare window
                "Particle Simulation",                                  //window title
                sf::Style::Default,
                sf::ContextSettings(32, 8, 0, 3, 3)
                );                                                                              //default context settings, my custom ones were screwing with the program so I let SFML decide
       
    ConsoleManager console_window(&window);
   
    glViewport(0,0,window.getSize().x,window.getSize().y);

then calling each:

        console_window.render();
        window.display();

my console render function did not change at all, since the main window I draw to is a sf::window I don't have any options to save and push GL states, could this be why it seems the render window is not interfering with the normal window?

I was wondering if this was a valid solution to my issue if anyone is reading....


binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: GL Clear Color does not work, Renderwindow won't draw
« Reply #4 on: July 08, 2014, 10:11:17 pm »
Like I said in my previous post, the most important thing to do is keep the overview over which GL states you are modifying with your GL operations. It is not always apparent which context (window) is currently the active one, and even subtle things might cause it to change.

In this case, you are still affecting the RenderWindow with the glViewport call because it becomes active when you create it. I can't tell why this works without further investigation of your code, but it is really more of a trial and error solution than one that is robust.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).