OpenGL loading associates the last loaded texture with all texture parameters

So my problem is that I try to load several textures at startup and then save all IDs so that I can bind them to their use. Now I know that the identifier is stored correctly, because I can debug it and see, both in assignment and in use, that the identifier is the same. For each binding, the last texture that I load is used. Here is my code:

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;
}

Here is my use of this code:

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

Here is my download request:

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

And then I just use:

glTexCoord2f(1.0f,0.0f);

for drawing depending on the change in angle. And it draws one texture correctly, it just repeats the texture even when I use a different number.

Any help is greatly appreciated.

Edit ----------

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();
+4
1

glEnable glBindTexture, glBegin glEnd. glBegin. OpenGL, glBegin glEnd, , (glVertex, glColor, glTexCoord ..).

, API ( glBegin/glEnd ) 10 . OpenGL, .

+4

Source: https://habr.com/ru/post/1663596/


All Articles