OpenGL加载将最后加载的纹理绑定到所有textureID

时间:2016-12-13 09:01:14

标签: c++ opengl loading texture-mapping texture2d

所以我的问题是我尝试在启动时加载多个纹理,然后存储所有ID,以便我可以绑定它们来使用它们。现在我知道ID正在被正确存储,因为我可以调试它并在分配和使用中看到ID是相同的。只为每个绑定它使用我加载的最后一个纹理。这是我的代码:

GLuint TextureLoader::LoadTexture (const char* fileName,Material& material,int width,int height) {
    GLuint textureImage;

    FILE* textureFile;
    textureFile = fopen(fileName, "rb");
    unsigned char* imageData;

    if (textureFile == NULL) {
        return 0;
    }
    imageData = (unsigned char*)malloc(width * height * 3);
    char header[54];
    fread(header,1,54,textureFile);
    fread(imageData, width * height * 3, 1, textureFile);

    fclose(textureFile);

    for (int i = 0; i < width * height; ++i) {
        int nextIndex = i * 3;
        unsigned char a = imageData[nextIndex];
        unsigned char b = imageData[nextIndex+2];

        imageData[nextIndex] = b;
        imageData[nextIndex+2] = a;
    }

    glEnable(GL_TEXTURE_2D);
    glGenTextures( 1, &textureImage );
    glBindTexture( GL_TEXTURE_2D, textureImage );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


    /*glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, imageData );*/
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, imageData);
    glGenerateMipmap(GL_TEXTURE_2D);
    free(imageData);

    return textureImage;
}

以下是我对此代码的使用:

if (showTexture) {
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, material.texture);
}

这是我的加载电话:

green1.texture = TextureLoader::LoadTexture("/Users/hewitt/Desktop/OpenGLImages/face6.bmp",green1, 256, 256);

然后我只使用:

glTexCoord2f(1.0f,0.0f);

根据角落的变化进行绘制。这样就可以正确地绘制一个纹理,即使我使用不同的数字也可以重复纹理。

非常感谢任何帮助。

编辑----------

glBegin(mode);
            Spatial::Vector3 normal = Mesh::calculateNormal (pointGroup);
            glNormal3f(normal.x, normal.y, normal.z);

            Material material = mesh.getMaterial();

            if (showTexture) {
                glEnable(GL_TEXTURE_2D);
                glBindTexture(GL_TEXTURE_2D, material.texture);
            }

            int counter = 0;
            for (Spatial::Vector3 point : pointGroup) {
                if (showTexture == false) {
                    Material::Colour colour = material.getColour();
                    glColor3f(colour.red, colour.green, colour.blue);
                } else {

                    if (counter == 0) {
                        glTexCoord2f(1.0f,0.0f);
                    } else if (counter == 1) {
                        glTexCoord2f(1.0f,1.0f);
                    } else if (counter == 2) {
                        glTexCoord2f(0.0f,1.0f);
                    } else if (counter == 3) {
                        glTexCoord2f(0.0f,0.0f);
                    }
                }

                glVertex3f(point.x,point.y,point.z);
                counter ++;
            }


            glEnd();

1 个答案:

答案 0 :(得分:4)

glEnableglBindTexture之间,您无法致电glBeginglEnd。您必须在glBegin之前绑定纹理。实际上,您可以在glBeginglEnd之间调用的OpenGL函数集仅限于传输顶点属性的glVertexglColorglTexCoord等。 ..)。

请注意,即时模式API(即glBegin / glEnd和朋友)是遗留的,已被弃用超过10年。我建议你切换到现代的OpenGL管道,它会为你排序很多东西。