2

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!

2
  • When you moved glBindTexture(GL_TEXTURE_2D, myTextureID);, did you delete it from loadPNG? Commented Apr 21, 2019 at 9:54
  • I did indeed, no dice unfortunately Commented Apr 21, 2019 at 10:07

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Just tried this, still no texture! I added glBindTexture(GL_TEXTURE_2D, testTexture2); before (and also tried it after) glEnable(GL_TEXTURE_2D); inside drawRect.
Should glTexImage2D be moved to drawRect?
@eddiewastaken What about loadPNG? Are you binding the texture in there? You need to bind the texture to initialise it.
@eddiewastaken Keep glTexImage2D in loadPNG
Yes, the only change I made from the original code is adding glBindTexture(GL_TEXTURE_2D, testTexture2); to drawRect
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.