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

Author Topic: [SOLVED] Passing RenderTexture to sf::Texture::bind for OpenGL cube  (Read 3141 times)

0 Members and 1 Guest are viewing this topic.

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
I am using a modified version of the opengl.cpp example file.  I am now able (due to some previous help from Laurent) to bind an sfml texture directly to an OpenGL object.  I am trying to bind a very simple RenderTexture to a OpenGL cube using getTexture().  Eventually I intend to draw sprites to the texture but for now all I am doing is clearing it with red.  The result is that the cube is invisible.  Can someone please tell me what I am doing wrong?

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


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    sf::RenderTexture texture;
    texture.create(500, 500);
    texture.clear(sf::Color::Red);
    sf::Texture::bind( & texture.getTexture() );

    // Enable Z-buffer read and write
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glClearDepth(1.f);

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

    // Create a clock for measuring the time elapsed
    sf::Clock clock;

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

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

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

        // 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
        window.setActive();

        // Clear the depth buffer
        glClear(GL_DEPTH_BUFFER_BIT);
        window.clear(sf::Color::Black);
        texture.clear(sf::Color::Red);

        //sf::Texture * t1;
        //t1.loadFromFile( "./resources/1.png" );
        //t1 = texture.getTexture();
        sf::Texture::bind( & texture.getTexture() );

        float x = 0;
        float y = 0;

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

        // Draw a cube
        float size = 20.f;
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);

            glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(size, -size,  size);

            glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, -size,  size);

            glTexCoord2f(0, 1); glVertex3f(-size, size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, size,  size);

        glEnd();

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

    return EXIT_SUCCESS;
}
 
« Last Edit: June 25, 2013, 08:42:07 am by Champion_Konrad_von_Limburg »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Passing RenderTexture to sf::Texture::bind for OpenGL cube
« Reply #1 on: June 25, 2013, 08:04:20 am »
Since you now have two render targets (the window and the render-texture), every time you make OpenGL calls you first need to set the active target. Every time you use the render-texture, you must call window.setActive() so that subsequent OpenGL calls will apply to the window, not to the render-texture.

Later, if you add SFML draw calls, don't forget to call push/popGLStates as well.

Everything is clearly explained in the tutorials, you should read them carefully.
Laurent Gomila - SFML developer

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Passing RenderTexture to sf::Texture::bind for OpenGL cube
« Reply #2 on: June 25, 2013, 08:38:29 am »
Thank you again Laurent, I read the tutorials 'Drawing 2d stuff' and 'Using OpenGL in a SFML window' but did not realize that the RenderTexture had to be set active in the same way that it describes for windows.