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

Author Topic: [SOLVED] sf::Image -> GLuint  (Read 2151 times)

0 Members and 1 Guest are viewing this topic.

CarbonAsh

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] sf::Image -> GLuint
« on: January 11, 2010, 07:36:52 pm »
I have a codebase that uses GLuints to hold textures and I'm trying to convert it from using SDL for windowing/event/audio/etc to SFML.  However, I cannot seem to get my textures to display correctly.  I've whipped up some code showing how I think you should get a GLuint handle to a texture from an sf::Image.

Right now all it does it fill the entire window with a white square.  If I change the code to use a RenderWindow and sf::Sprite the image draws
fine.

Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    sf::Window app(sf::VideoMode(1280,800), "SFML Test");

    app.EnableKeyRepeat(false);
    app.SetActive();

    // opengl initialization
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glViewport( 0, 0, 1280, 800 );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, 1280, 800, 0, -1, 1 );


    sf::Image pic;
    pic.LoadFromFile("bg01.jpg");

    GLuint bgtext = 0;
    glGenTextures(1, &bgtext);
    glBindTexture(GL_TEXTURE_2D, bgtext);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, pic.GetWidth(), pic.GetHeight(),
        0, GL_RGBA, GL_UNSIGNED_BYTE, pic.GetPixelsPtr());

    bool loop = true;
    while (loop)
    {
        sf::Event e;
        while (app.GetEvent(e))
        {
            if (e.Type == sf::Event::Closed ||
                e.Type == sf::Event::KeyPressed)
                loop = false;
        }

        // Do the drawing
        glClear( GL_COLOR_BUFFER_BIT );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        glScalef(1280, 800, 0);
        glBindTexture(GL_TEXTURE_2D, bgtext);

        glBegin( GL_QUADS );
            // Top-left vertex (corner)
            glTexCoord2i( 0, 0 );
            glVertex3f( 0, 0, 0 );

            // Bottom-left vertex (corner)
            glTexCoord2i( 0, 1 );
            glVertex3f( 0, 1.0f, 0 );

            // Bottom-right vertex (corner)
            glTexCoord2i( 1, 1 );
            glVertex3f( 1.0f, 1.0f, 0 );

            // Top-right vertex (corner)
            glTexCoord2i( 1, 0 );
            glVertex3f( 1.0f, 0, 0 );
        glEnd();

        app.Display();
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] sf::Image -> GLuint
« Reply #1 on: January 11, 2010, 11:51:21 pm »
Take a look at the OpenGL sample from the SDK ;)
Laurent Gomila - SFML developer

CarbonAsh

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] sf::Image -> GLuint
« Reply #2 on: January 12, 2010, 06:14:24 am »
I fixed it.  In case anyone else runs into a similar problem I just had to add these lines after I created the texture and still had it bound:

Code: [Select]

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);