简体   繁体   English

将多个纹理OpenGL绑定到不同的四边形

[英]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. 我设法使我的游戏读取到PNG文件中,并成功构造了我的对象。 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. 老实说,我无法100%确定其实际工作方式-现在我想将其扩展为加载多个纹理,并使用我指定的纹理。

Here's my PNG loading function: 这是我的PNG加载功能:

//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 . 其中testTextureGLuint类型的全局变量。 And drawing my rectangles in my main draw loop is done this way: 并在主draw循环中绘制矩形是这样的:

//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: loadPNG函数进入我的drawRect ,如下所示:

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 . 您必须绑定纹理才能使用glTexImage2D对其进行初始化。 Don't remove the call to glBindTexture from loadPNG . 不要从loadPNG删除对glBindTexture的调用。 If you want to render with a different texture, simply bind the texture before rendering the quads. 如果要使用其他纹理进行渲染,只需在渲染四边形之前绑定纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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