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

Author Topic: Unable to apply texture on my cube using opengl & sfml  (Read 1933 times)

0 Members and 1 Guest are viewing this topic.

lufen34

  • Newbie
  • *
  • Posts: 4
    • View Profile
Unable to apply texture on my cube using opengl & sfml
« on: August 29, 2016, 04:24:20 pm »
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 :

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.



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
« Last Edit: August 29, 2016, 04:30:41 pm by lufen34 »

lufen34

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Unable to apply texture on my cube using opengl & sfml
« Reply #1 on: August 30, 2016, 05:12:52 pm »
bump :-\

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Unable to apply texture on my cube using opengl & sfml
« Reply #2 on: August 31, 2016, 01:56:36 am »
i don't program in raw openGL but i'm curious about the "loadTexture" function.

 m_textureID = loadTexture("/Data/test.jpg");

GLuint Engine::loadTexture(string file)
{
  ...
}

 

If you haven't done it, check that the function works well and the path is the correct.

I think that you already looked at the SFML openGL example.

Probably binary1248 can help you.

And please, don't bump up threads, its a bad practice and even worst on programming forums IMO.
I would like a spanish/latin community...
Problems building for Android? Look here

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Unable to apply texture on my cube using opengl & sfml
« Reply #3 on: August 31, 2016, 02:08:49 am »
Seems that you also don't bind texture. You need to uncomment bind-comment because after texture is "generated" it doesn't bund itself to comtext.
« Last Edit: August 31, 2016, 02:20:12 am by Mr_Blame »