Results 1 to 5 of 5

Thread: Unable to apply texture on my cube using opengl & sfml

  1. #1
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    3

    Unable to apply texture on my cube using opengl & sfml

    I want to load a texture on one of my faces on a cube. All the other faces have a specific color that I put on them. The problem is that, when I try to apply the texture, it will not display it. Instead, the last texture used on the cube is being applied.

    Here is a code sample :

    Code :
    Engine::Engine() : m_isMovingUp(false), m_isMovingDown(false), m_isMovingLeft(false), m_isMovingRight(false), m_context(24, 8, 4, 3, 3), m_angleX(0), m_angleZ(0), m_mov(3.0f, 3.0f, 3.0f)
    {
        //Window settings
        m_win.create(sf::VideoMode(800, 600), "SFML Event and with some OpenGL graphics", sf::Style::Default, m_context);
        m_win.setFramerateLimit(60);
     
        //Opengl settings
        glEnable(GL_TEXTURE_2D);
        m_textureID = loadTexture("/Data/test.jpg");
     }
     
    void Engine::run()
    {
        bool running = true;
        while (running)
        {
            processEvents();
            update();
            render();
        }
    }
     
     
    void Engine::cube()
    {
        //glRotated(m_angleX, 1, 0, 0);
        //glRotated(m_angleZ, 0, 0, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glBindTexture(GL_TEXTURE_2D, m_textureID);
        glBegin(GL_QUADS);
        //face Rouge
        //glColor3ub(255, 0, 0);
        glTexCoord2d(0, 1); glVertex3d(1, 1, 1);
        glTexCoord2d(0, 0); glVertex3d(1, 1, -1);
        glTexCoord2d(1, 0); glVertex3d(-1, 1, -1);
        glTexCoord2d(1, 1); glVertex3d(-1, 1, 1);
        glEnd();
        glBegin(GL_QUADS);
        //face verte
        glColor3ub(0, 255, 0); 
        glVertex3d(1, -1, 1);
        glVertex3d(1, -1, -1);
        glVertex3d(1, 1, -1);
        glVertex3d(1, 1, 1);
     
        //face bleu
        glColor3ub(0, 0, 255);
        glVertex3d(1, -1, 1);
        glVertex3d(-1, -1, 1);
        glVertex3d(-1, -1, -1);
        glVertex3d(1, -1, -1);
     
        //face jaune
        glColor3ub(255, 255, 0); 
        glVertex3d(-1, 1, 1);
        glVertex3d(-1, 1, -1);
        glVertex3d(-1, -1, -1);
        glVertex3d(-1, -1, 1);
     
        //face cyan
        glColor3ub(0, 255, 255); 
        glVertex3d(1, 1, -1);
        glVertex3d(1, -1, -1);
        glVertex3d(-1, -1, -1);
        glVertex3d(-1, 1, -1);
     
       //face magenta
       glColor3ub(255, 0, 255); 
       glVertex3d(1, -1, 1);
       glVertex3d(1, 1, 1);
       glVertex3d(-1, 1, 1);
       glVertex3d(-1, -1, 1);
     
       glEnd();
       glFlush();
    }
     
    GLuint Engine::loadTexture(string file)
    {
        GLuint textureID;
        if (!m_image.loadFromFile(file))
            EXIT_FAILURE;
        glGenTextures(1, &textureID);
        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, 4, m_image.getSize().x, m_image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_image.getPixelsPtr());
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
        return textureID;
    }
    This is where I'm at after fixing as much as I could.

    1472480643-sans-titre.jpg

    thanks for your help !

    PS: Since he is not able to apply the texture, the face is automaticly painting in pink since it is the last call with Glcolor

  2. #2
    Member Contributor
    Join Date
    Nov 2016
    Posts
    67
    Why is glBindTexture(GL_TEXTURE_2D, textureID); commented out in loadTexture ? The subsequent glTexImage and glTexParameter calls won't work if the texture is not bound.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Also, I would suggest your to unbind your texture after the face that has to be texturized. Otherwise, the last texture coordinate will be applied and you'll then have the same color for all the other faces (which will be the color of the image at this texcoord).
    Also, use GL_REPLACE as the texture environment, except if you expect it to be modulated with the color of the last face (magenta).

  4. #4
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    3

    answear

    Quote Originally Posted by Spoops View Post
    Why is glBindTexture(GL_TEXTURE_2D, textureID); commented out in loadTexture ? The subsequent glTexImage and glTexParameter calls won't work if the texture is not bound.
    Oups i forgot to unapply this i was testing something ! i update it right now !

  5. #5
    Newbie Newbie
    Join Date
    Aug 2016
    Posts
    3

    answear

    Quote Originally Posted by Silence View Post
    Also, I would suggest your to unbind your texture after the face that has to be texturized. Otherwise, the last texture coordinate will be applied and you'll then have the same color for all the other faces (which will be the color of the image at this texcoord).
    Also, use GL_REPLACE as the texture environment, except if you expect it to be modulated with the color of the last face (magenta).
    Thanks for your answear !

Similar Threads

  1. SFML OpenGL implementation
    By znyk11 in forum OpenGL: User Software
    Replies: 1
    Last Post: 02-22-2018, 04:43 AM
  2. Generate 2D square texture to apply to a 3D cube
    By nenekira in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 05-28-2016, 02:00 AM
  3. How do I apply a texture over a material in OpenGL ES?
    By ldantona in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-13-2014, 07:51 AM
  4. C++ opengl sfml obj loader help
    By Enlighten in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 04-22-2012, 06:59 AM
  5. How to apply reflection effect to a simple cube?
    By powervr in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 11-19-2004, 08:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean