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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nicoraf

Pages: 1 [2]
16
Graphics / Re: Problem using opengl in a different thread
« 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

 

17
Graphics / Re: Problem using opengl in a different thread
« on: July 20, 2013, 12:38:49 am »
I uploaded the correct files with the corresponding display invocation. Any help will be appreciated!

18
Graphics / Re: Problem using opengl in a different thread
« 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!

19
Graphics / 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.

Pages: 1 [2]