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

Author Topic: Problem using opengl in a different thread  (Read 1621 times)

0 Members and 1 Guest are viewing this topic.

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Problem using opengl in a different thread
« on: July 19, 2013, 07:50:33 am »
Hi,

I am trying to do a simple example that has a rendering thread and another thread for the events (as suggested in the SFML OpenGL tutorial).

I modified the opengl example (Opengl.cpp) that comes with SMFL2 and I am having one problem. It renders the background and the font but it doesn't render the OpenGL stuff. I am working with Visual Studio 2012, 64 bits compiler at Windows 7.

I send the example attached.

Any help and comments are welcome.

Thanks in advance,
Nicolas.
« Last Edit: July 20, 2013, 12:37:57 am by nicoraf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10842
    • View Profile
    • development blog
    • Email
Re: Problem using opengl in a different thread
« Reply #1 on: July 19, 2013, 09:56:53 am »
Why is mWindow.display() commented out?  ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem using opengl in a different thread
« Reply #2 on: July 19, 2013, 03:10:08 pm »
Sorry, I commented that before submiting the post to see what happens if I commented that. The code attached shouldn´t have that line commented. The problem still happens if it is uncommented.

Thanks!

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem using opengl in a different thread
« Reply #3 on: July 20, 2013, 12:38:49 am »
I uploaded the correct files with the corresponding display invocation. Any help will be appreciated!

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem using opengl in a different thread
« Reply #4 on: July 20, 2013, 01:17:22 am »
I simplified the example.

I put here a the attached code:

main.cpp
#include "Application.h"


int main()
{
        gg::Application *application = new(std::nothrow) gg::Application("");

        application->init();
        application->run();
        application->cleanup();

        delete application;
}

Application.c

#include <thread>
#include "Application.h"


namespace gg
{
        Application::Application(const std::string theTitle):
                mTitle(theTitle),
                mVideoMode(DEFAULT_VIDEO_WIDTH, DEFAULT_VIDEO_HEIGHT, DEFAULT_VIDEO_BPP),
                mWindow(),
                mContextSettings(),
                mWindowStyle(sf::Style::Close | sf::Style::Resize)
        {
        }

        void Application::init()
        {

                // Create the main window
                //sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
               
                mWindow.create(mVideoMode, mTitle, mWindowStyle, mContextSettings);
                mWindow.setVerticalSyncEnabled(true);
                mWindow.setActive(false);

                    // Set the color and depth clear values
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

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

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
        }

        void Application::cleanup()
        {
                // Don't forget to destroy our texture
                glDeleteTextures(1, &texture);
        }

        void Application::run()
        {
                // launch the rendering thread
                //sf::Thread thread(&Application::renderingThread, this);
                //thread.launch();
                std::thread renderingThread(&Application::renderingThread, this);

                // Start game loop
                while (mWindow.isOpen())
                {
                        // Process events
                        sf::Event event;
                        while (mWindow.pollEvent(event))
                        {
                                // Close window : exit
                                if (event.type == sf::Event::Closed)
                                        mWindow.close();

                                // Escape key : exit
                                if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                                        mWindow.close();

                                // Adjust the viewport when the window is resized
                                if (event.type == sf::Event::Resized)
                                        glViewport(0, 0, event.size.width, event.size.height);
                        }
                }

                renderingThread.join();
        }

        void Application::renderingThread()
        {
        // Create a clock for measuring the time elapsed
                sf::Clock clock;
       
                // Activate the window before using OpenGL commands.
        // This is useless here because we have only one window which is
        // always the active one, but don't forget it if you use multiple windows
        mWindow.setActive();

                // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(clock.getElapsedTime().asSeconds() * 50, 1.f, 0.f, 0.f);
        glRotatef(clock.getElapsedTime().asSeconds() * 30, 0.f, 1.f, 0.f);
        glRotatef(clock.getElapsedTime().asSeconds() * 90, 0.f, 0.f, 1.f);

        // Draw a cube
        glBegin(GL_QUADS);

            glColor3f(1.f, 0.f, 0.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f( 50.f,  50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);

            glColor3f(1.f, 0.f, 0.f);
            glVertex3f(-50.f, -50.f, 50.f);
            glVertex3f(-50.f,  50.f, 50.f);
            glVertex3f( 50.f,  50.f, 50.f);
            glVertex3f( 50.f, -50.f, 50.f);

            glColor3f(0.f, 1.f, 0.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f(-50.f,  50.f,  50.f);
            glVertex3f(-50.f, -50.f,  50.f);

            glColor3f(0.f, 1.f, 0.f);
            glVertex3f(50.f, -50.f, -50.f);
            glVertex3f(50.f,  50.f, -50.f);
            glVertex3f(50.f,  50.f,  50.f);
            glVertex3f(50.f, -50.f,  50.f);

            glColor3f(0.f, 0.f, 1.f);
            glVertex3f(-50.f, -50.f,  50.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f,  50.f);

            glColor3f(0.f, 0.f, 1.f);
            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);

        glEnd();

        // Finally, display the rendered frame on screen
        mWindow.display();
        }

        Application::~Application(void)
        {
        }
}
 


Application.h
#ifndef _GG_APPLICATION
#define _GG_APPLICATION

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

namespace gg
{
        class Application
        {
        public:

                /// Default Video Width to use if config file not found
                static const unsigned int DEFAULT_VIDEO_WIDTH = 800;
                /// Default Video Height to use if config file not found
                static const unsigned int DEFAULT_VIDEO_HEIGHT = 600;
                /// Default Video bits per pixel (color depth) if config file not found
                static const unsigned int DEFAULT_VIDEO_BPP = 32;

                /// Title to use for Window
                std::string               mTitle;
                /// Video Mode to use (width, height, bpp)
                sf::VideoMode             mVideoMode;
                /// Render window to draw to
                sf::RenderWindow          mWindow;
                /// Window settings to use when creating Render window
                sf::ContextSettings       mContextSettings;
                /// Window style to use when creating Render window
                unsigned long             mWindowStyle;


                sf::Texture backgroundTexture;
                sf::Font font;
                sf::Sprite background;
                GLuint texture;

                Application(const std::string theTitle);

                virtual void init();
                virtual void cleanup();
                virtual void run();

                virtual void renderingThread();

                virtual ~Application(void);
        };
}

#endif

 
« Last Edit: July 20, 2013, 01:19:07 am by nicoraf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10842
    • View Profile
    • development blog
    • Email
Re: Problem using opengl in a different thread
« Reply #5 on: July 20, 2013, 01:26:36 am »
You could even simplify it further and just use one main file and drop everything that is not important.
Unfortunately I can't help you, since I've no idea about OpenGL. :-[
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Problem using opengl in a different thread
« Reply #6 on: July 20, 2013, 02:25:47 am »
Thank you, I could fix the mistakes.

1)
At the rendering thread it had been missing a:
while (mWindow.isOpen())

2) For OpenGl I should use sf::Window instead of sf::RenderWindow that is for 2d graphics.

3) the window should be activated at the opengl initialization.


I send the modified files.

« Last Edit: July 20, 2013, 02:27:56 am by nicoraf »