My textures are overriding one another.

I want the sky.png to be mapped onto the rectangle and the character.png to be mapped onto the square but the character.png is appearing for both because it is declared after the sky.png.

I have uploaded a screenshot of my problem.

Not sure how to fix this, do I need to put them in separate void init() functions?


#include <windows.h>        // Header File For Windows

/*    nvImage.h contains all of the function prototypes needed for image loading.
    nvImage.h includes glew.h and so we don't need to include gl and glu header files for OpenGL
*/
#include "Image_Loading/nvImage.h"

/**********************************************************************************************/

// This stores a handle to the texture
GLuint myTexture = 0;

GLuint loadPNG(char* name)
{
    // Texture loading object
    nv::Image img;

    GLuint myTextureID;

    // Return true on success
    if(img.loadImageFromFile(name))
    {
        glGenTextures(1, &myTextureID);
        glBindTexture(GL_TEXTURE_2D, myTextureID);
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
        glTexImage2D(GL_TEXTURE_2D, 0, img.getInternalFormat(), img.getWidth(), img.getHeight(), 0, img.getFormat(), img.getType(), img.getLevel(0));
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
    }

    else
        MessageBox(NULL, "Failed to load texture", "End of the world", MB_OK | MB_ICONINFORMATION);

    return myTextureID;
}




void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);

    /**********************************************************************************************/

    myTexture = loadPNG("sky.png");

    myTexture = loadPNG("character.png");
    /**********************************************************************************************/
}


void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    /**********************************************************************************************/
    
    glEnable(GL_TEXTURE_2D);
        glColor3f(1,1,1);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0);    glVertex2f(-1,0);
        glTexCoord2f(0.0, 1.0);    glVertex2f(-1,1);
        glTexCoord2f(1.0, 1.0);    glVertex2f(1,1);
        glTexCoord2f(1.0, 0.0);    glVertex2f(1,0);
        glEnd();
    
    glDisable(GL_TEXTURE_2D);

    
    
    glEnable(GL_TEXTURE_2D);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0); glVertex2f(-0.5,-0.5);
        glTexCoord2f(0.0, 4.0); glVertex2f(-0.5,0.5);
        glTexCoord2f(4.0, 4.0); glVertex2f(0.5,0.5);
        glTexCoord2f(4.0, 0.0); glVertex2f(0.5,-0.5);


    glEnd();

    glDisable(GL_TEXTURE_2D);

    
}

Hey!

the myTexture-variable here:


// This stores a handle to the texture
GLuint myTexture = 0;

must be declared as array to store two handles, like this:


GLuint myTexture[2];

Later you call:


myTexture[0] = loadPNG("sky.png");

myTexture[1] = loadPNG("character.png");

Somewhere around here:


glEnable(GL_TEXTURE_2D);

you have to bind the handle to be used again, like this:


glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, myTexture[0]);

[DRAW FIRST QUAD HERE]

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, myTexture[1]);

[NOW DRAW THE SECOND QUAD]

Hope that helps…