1
votes

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.

PS : CODE UPDATED & PICTURE ADDED

Picture of my cube

1
No one can help me ? :(Lufen Martofilia

1 Answers

2
votes

I see two mistakes in the pastebin code:

Line 27:

glBindTexture(GL_TEXTURE_2D, 0);

You can not change the bound texture in between glBegin and glEnd calls. You will have to split rendering of the textured and not textured faces to their own begin-end blocks.

Line 74:

glTexImage2D(GL_TEXTURE_2D, 0, 4, m_image.getSize().x, m_image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, &m_image.getPixelsPtr);

The last parameter should be m_image.getPixelsPtr() not &m_image.getPixelsPtr. You are taking address of the method and using it as image data. The method getPixelsPtr will give you pointer to the image data if you call it.

Also it seems that you are creating new texture and loading image into it on every frame. If it is just in this example that renders once, then it is ok. But in actual program it would have big impact on performance and the program would soon run out of memory.

There may be more problems, it can't be told without seeing the whole code. If you can, upload somewhere a minimal complete example.