简体   繁体   中英

Binding multiple textures OpenGL to different quads

I've managed to get my game to read in a PNG file, and successfully texture my objects. To be honest, I can't 100% nail down how it's actually working - and now I'd like to extend it to loading several textures, and using the one I specify.

Here's my PNG loading function:

//Loads PNG to texture
GLuint loadPNG(string name) {
    nv::Image img;
    GLuint myTextureID;

    if (img.loadImageFromFile(name.c_str())) {
        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_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
    }
    else {
        MessageBox(NULL, L"Failed to load texture", L"Sorry!", MB_OK | MB_ICONINFORMATION);
    }
    return myTextureID;
}

In my main function, I define the texture like this:

//Load in player texture
testTexture = loadPNG("test.png");

where testTexture is a global variable, of type GLuint . And drawing my rectangles in my main draw loop is done this way:

//Used to draw rectangles
void drawRect(gameObject &p) {
    glEnable(GL_TEXTURE_2D);

    //Sets PNG transparent background
    glEnable(GL_BLEND);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //glBindTexture(GL_TEXTURE_2D, myTexture);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex2f(p.x, p.y);
    glTexCoord2f(1.0, 0.0); glVertex2f(p.x + p.width, p.y);
    glTexCoord2f(1.0, 1.0); glVertex2f(p.x + p.width, p.y + p.height);
    glTexCoord2f(0.0, 1.0); glVertex2f(p.x, p.y + p.height);
    glEnd();

    glDisable(GL_TEXTURE_2D);
}

This works fine, texturing all my objects with the defined texture. However, I'd like to be able to define more textures, and use those. I tried moving:

glBindTexture(GL_TEXTURE_2D, myTextureID);

from the loadPNG function, into my drawRect , as:

glBindTexture(GL_TEXTURE_2D, testTexture);

However this doesn't apply any texture whatsoever. If anyone has any ideas, I'd really appreciate the help. Thanks!

You have to bind the texture in order to initialize it with glTexImage2D . Don't remove the call to glBindTexture from loadPNG . If you want to render with a different texture, simply bind the texture before rendering the quads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM