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

Author Topic: SFML 2.0 and OpenGL texture mapping problem  (Read 3799 times)

0 Members and 1 Guest are viewing this topic.

pufixas

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML 2.0 and OpenGL texture mapping problem
« on: November 27, 2012, 05:07:55 pm »
Hello, I have a problem which I seem can't fix. I tried using everything in the past few hours, nothing worked. Essentially what I'm trying to do just bind some 128x128 .png format texture on to a square in OpenGL with SFML 2.0. I got no errors, but all I can see is white square and thats all. Here's the simplified code I'm trying to get to work:

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

using namespace std;

int main()
{
        sf::RenderWindow window(sf::VideoMode(800,600,32),"Title");

        glViewport(0,0,800,600);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        glOrtho(0,800,0,600,1,-1);

        sf::Image texture;
        texture.loadFromFile("Resources/texture.png");

        GLuint id;
        glGenTextures(1,&id);
        glBindTexture(GL_TEXTURE_2D,id);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,128,128,0,GL_RGBA,GL_UNSIGNED_BYTE,texture.getPixelsPtr());

        unsigned short indices[6] = {0,1,2,2,3,0};
        float vertices[8] = {100,100,
                                                200,100,
                                                200,200,
                                                100,200};

        float textureCoords[8] = {0.0f, 1.0f,
                             1.0f, 1.0f,
                             1.0f, 0.0f,
                             0.0f, 0.0f};
       
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(2,GL_FLOAT,0,vertices);
        glTexCoordPointer(2,GL_FLOAT,0,textureCoords);

       

        while(window.isOpen())
        {
                sf::Event event;
        while (window.pollEvent(event))
        {

        }
                glClear(GL_COLOR_BUFFER_BIT);
                glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT, indices);
                window.display();
        }

        return 0;
}

I also tried using gluBuild2DMipmaps() instead of glTexImage2D() but I got some very strange artifacts in my texture, it doesn't even look like my texture anymore, it just KIND OF has same colors but it is very distorted and looks random.
Maybe i'm doing something wrong? How do you bind textures to your OpenGL games/applications?
I ported this code from Android OpenGL example from a book which i'm reading, and there it works fine.

Thanks for your help in advance.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: SFML 2.0 and OpenGL texture mapping problem
« Reply #1 on: November 27, 2012, 10:18:55 pm »
If it's not imperative that you use OpenGL calls to set the texture up, then you can use sf::Texture and its bind() member function.

Otherwise, my next step would be to create a small pure SFML sample to demonstrate thatn the png is loading correctly, then step-by-step replace the sfml calls with the opengl equivalents.

pufixas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML 2.0 and OpenGL texture mapping problem
« Reply #2 on: November 28, 2012, 06:43:22 am »
Thank you. I replaced all the OpenGL texture binding calls to only one sf::Texture::bind() call and everything worked fine. Not only easier, but saves much of typing. But I still don't understand why it didn't work previously.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: SFML 2.0 and OpenGL texture mapping problem
« Reply #3 on: November 28, 2012, 06:53:30 am »
No probs, in future you can use glGetError() in between opengl calls to see if anything went wrong, but SFML can do a lot without needing to write any OpenGL.

 

anything