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

Author Topic: [SOLVED] Binding sfml texture to OpenGL object in SFML 2  (Read 7350 times)

0 Members and 1 Guest are viewing this topic.

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] Binding sfml texture to OpenGL object in SFML 2
« on: June 24, 2013, 02:11:12 am »
I tried to edit the opengl.cpp example file to bind an sfml texture to the spinning cube rather than using a GLUint.  The file even says "We could directly use a sf::Texture as an OpenGL texture (with its Bind() member function)" but I have not been able to do so.  The result is a spinning white cube.  Can someone tell me what I am missing please?

////////////////////////////////////////////////////////////
// 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);

    // Load an OpenGL texture.
    // We could directly use a sf::Texture as an OpenGL texture (with its Bind() member function),
    // but here we want more control on it (generate mipmaps, ...) so we create a new one from an image
    //GLuint texture = 0;
    //{
    //    sf::Image image;
    //    if (!image.loadFromFile("resources/texture.jpg"))
    //        return EXIT_FAILURE;
    //    glGenTextures(1, &texture);
    //    glBindTexture(GL_TEXTURE_2D, texture);
    //    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
    //    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    //}

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

    // Enable Z-buffer read and write
    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);

        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, 01:00:01 am by Champion_Konrad_von_Limburg »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Binding sfml texture to OpenGL object in SFML 2
« Reply #1 on: June 24, 2013, 08:08:24 am »
Is loadFromFile successful?
Laurent Gomila - SFML developer

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Binding sfml texture to OpenGL object in SFML 2
« Reply #2 on: June 24, 2013, 08:42:11 pm »
Yes load from file is successful.  I tested this by changing to a non existent filename and I get a run-time error that the filename doesn't exist. As the code is written now I get no runtime errors.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Binding sfml texture to OpenGL object in SFML 2
« Reply #3 on: June 24, 2013, 09:04:04 pm »
You're right, it fails. I'll investigate it as soon as possible.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Binding sfml texture to OpenGL object in SFML 2
« Reply #4 on: June 24, 2013, 09:46:21 pm »
Sorry, my bad, I made an error in my test code. It works fine.

What's missing in your code is a call to glEnable(GL_TEXTURE_2D).
Laurent Gomila - SFML developer

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Binding sfml texture to OpenGL object in SFML 2
« Reply #5 on: June 25, 2013, 12:59:34 am »
That was it, thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] Binding sfml texture to OpenGL object in SFML 2
« Reply #6 on: June 25, 2013, 07:56:02 am »
This is out of topic, but did you read my PM about your name?
Laurent Gomila - SFML developer