OpenGL 不会改变纹理

OpenGL Won't Change Texture

我的纹理不会改变,我不确定为什么。这是我的纹理 Loader.h

    class TextureLoader
{
private:
    GLuint* Texture;
    std::map<std::string, GLuint*> TextureMap;
public:
    TextureLoader(){};
    ~TextureLoader()
    {
        delete Texture;
    }
    bool LoadTexture(std::string Source);
    GLuint* GetImage(std::string TextureID);    
    bool CheckTextureExsist(std::string TextureID);
};

这是菲律宾共产党。

bool TextureLoader::LoadTexture(std::string Source)
{
    //Bind the texture to load in.
    Texture = new GLuint;
    glGenTextures (1, Texture);
    glBindTexture (GL_TEXTURE_CUBE_MAP, *Texture);

    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_WRAP_R,GL_CLAMP_TO_EDGE);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);

    //Width, Height and Components of the image.
    int x, y, n;

    //Set pixel format
    int force_channels = 4;

    //Load data into the char.
    unsigned char*  image_data = stbi_load (
        Source.c_str(), &x, &y, &n, force_channels);

    //Check too see if the image loaded.
    if (!image_data) {
        fprintf (stderr, "ERROR: could not load %s\n", Source);
        return false;
    }

    //Copy the image data to the selected target.
    glTexImage2D (
        GL_TEXTURE_2D,
        0,
        GL_RGBA,
        x,
        y,
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        image_data
        );
    free (image_data);

    TextureMap.insert(std::pair<std::string, GLuint*>(Source,Texture));
    return true;
}

GLuint* TextureLoader::GetImage(std::string TextureID)
{
    return TextureMap.find(TextureID)->second;
}

bool TextureLoader::CheckTextureExsist(std::string TextureID)
{

    if(TextureMap.find(TextureID) == TextureMap.end())
    {
        return false;
    }
    else 
        return true;
}

我就是这样画的。

glBindTexture(GL_TEXTURE_2D, *TextureID);
    glMaterialfv(GL_FRONT , GL_AMBIENT, Ambient);
    glMaterialfv(GL_FRONT , GL_DIFFUSE, Diffuse);
    glMaterialfv(GL_FRONT , GL_SPECULAR, Specular);
    glMaterialf(GL_FRONT , GL_SHININESS, Shininess);

    glVertexPointer(3,GL_FLOAT,0,&Vertices[0]);
    glNormalPointer(GL_FLOAT,0,&Normals[0]);
    glTexCoordPointer(2,GL_FLOAT,0,&TextureCoords[0]);

    glPushMatrix();
    glScalef(Scale[0],Scale[1],Scale[2]);
    glTranslatef(Translate[0],Translate[1],Translate[2]);
    glRotatef(Rotate[0],Rotate[1],Rotate[2],Rotate[3]);     
    glDrawArrays(GL_TRIANGLES, 0,(GLsizei)(Vertices.size()/3));
    glPopMatrix();
    glBindTexture(GL_TEXTURE_2D, NULL);

TextureID 被传递给纹理句柄的指针。句柄在 运行 时间内确实更改为不同的句柄,但它确实绘制了我加载的最后一个纹理,而不管纹理句柄如何。

问题是绑定到 LoadTexture 函数中的 GL_TEXTURE_CUBE_MAP

glBindTexture (GL_TEXTURE_CUBE_MAP, *Texture);

将其更改为

glBindTexture(GL_TEXTURE_2D, *Texture);

我还建议使用 GLuint 而不是 GLuint* 并将其存储到地图中。 glGenTextures 接受指针的唯一原因是因为它也可以输出到 GLuint.

的数组