我的OpenGL程序只使用最后加载的纹理(C ++)

时间:2014-05-27 03:25:38

标签: c++ opengl texture-mapping texture2d

我的装载机出现了这个问题:

int loadTexture(char *file)
{
    // Load the image
    SDL_Surface *tex = IMG_Load(file);
    GLuint t;

    cout << "Loading image: " << string(file) << "\n";
    if (tex) {
        glGenTextures(1, &t); // Generating 1 texture
        glBindTexture(GL_TEXTURE_2D, t); // Bind the texture
        glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tex->pixels); // Map texture
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Set minifying parameter to linear
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Set magnifying parameter to linear
        SDL_FreeSurface(tex); // Free the surface from memory
        cout << "   > Image loaded: " << string(file) << "\n\n";
        return t; // return the texture index
    }
    else {
        cout << "   > Failed to load image: " << IMG_GetError() << "\n\n";
        SDL_FreeSurface(tex); // Free the surface from memory
        return -1; // return -1 in case the image failed to load
    }

}

它可以很好地加载图像,但在绘制对象时只使用最后加载的图像:

textTest = loadTexture("assets/test_texture_64.png");
textTest2 = loadTexture("assets/test_texture2_64.png");
textTest3 = loadTexture("assets/test_texture3_64.png");
textTest4 = loadTexture("assets/test_texture4_64.png");

纹理文件: http://i.imgur.com/2K9NsZF.png

程序运行: http://i.imgur.com/5FMrA1b.png

在绘制对象之前,我使用glBindTexture(GL_TEXTURE_2D,t),其中t是我想要使用的纹理的名称。我是OpenGL和C ++的新手,所以我在这里无法理解这个问题。

1 个答案:

答案 0 :(得分:0)

加载纹理时,应检查loadTexture是否返回不同的纹理ID。然后你需要确保使用glBindTexture(...)将正确的纹理绑定到对象上,你说你已经在做了。

你现在如何画出你的物品?是否涉及多个纹理?在绘制对象之前和之后,请务必进行正确的glPushMatrix / glPopMatrix调用。

通过查看您的装载机,虽然您没有glEnableglDisable GL_TEXTURE_2D,但我认为这是正确的,但这不重要。