OpenGL不会改变纹理

时间:2015-01-10 15:37:21

标签: c++ opengl

我的纹理不会改变,我不知道为什么。这是我的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);
};

这是CPP。

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传递纹理句柄的指针。句柄在运行期间确实会改变到不同的句柄,但它会绘制我加载的最后一个纹理,而不管纹理句柄。

1 个答案:

答案 0 :(得分:2)

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

glBindTexture (GL_TEXTURE_CUBE_MAP, *Texture);

将其更改为

glBindTexture(GL_TEXTURE_2D, *Texture);

我还建议使用GLuint代替GLuint*并将其存储到地图中。 glGenTextures获取指针的唯一原因是因为它也可以输出到GLuint的数组。