0
\$\begingroup\$

I am working on a graphics project I want to make a city using opengl with c++ anyway in the last few days I have been trying to load a texture but it didn't work with me in any way I have tried many codes and tried to follow some tutorials but it didn't went ok

here is my load texture function:

int LoadTexture(char *filename,int alpha) 
{
    int i, j=0; //Index variables
    FILE *l_file; //File pointer
    unsigned char *l_texture; //The pointer to the memory zone in which we will load the texture

    // windows.h gives us these types to work with the Bitmap files
    BITMAPFILEHEADER fileheader; 
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;

    num_texture++; // The counter of the current texture is increased

    if( (l_file = fopen(filename, "rb"))==NULL) return (-1); // Open the file for reading

    fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader

    fseek(l_file, sizeof(fileheader), SEEK_SET); // Jump the fileheader
    fread(&infoheader, sizeof(infoheader), 1, l_file); // and read the infoheader

    // Now we need to allocate the memory for our image (width * height * color deep)
    l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
    // And fill it with zeros
    memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);

    // At this point we can read every pixel of the image
    for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
    {            
            // We load an RGB value from the file
            fread(&rgb, sizeof(rgb), 1, l_file); 

            // And store it
            l_texture[j+0] = rgb.rgbtRed; // Red component
            l_texture[j+1] = rgb.rgbtGreen; // Green component
            l_texture[j+2] = rgb.rgbtBlue; // Blue component
            l_texture[j+3] = alpha; // Alpha value
            j += 4; // Go to the next position
    }

    fclose(l_file); // Closes the file stream

    glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

    // The next commands sets the texture parameters
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the u,v coordinates overflow the range 0,1 the image is repeated
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // The magnification function ("linear" produces better results)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //The minifying function

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECR );

    // Finally we define the 2d texture
    glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

    // And create 2d mipmaps for the minifying function
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

    free(l_texture); // Free the memory we used to load the texture

    return (num_texture); // Returns the current texture OpenGL ID
}

I call the function in init section

textureID = LoadTexture("Building.bmp",255);

and in the display section I wrote :

glBindTexture(GL_TEXTURE_2D,textureID);
    glBegin(GL_QUADS);  
            //front
            glTexCoord2f(1,0) ;         glVertex3f(1,-1,0); 
            glTexCoord2f(1,1) ;         glVertex3f(1,1,0);
            glTexCoord2f(0,1) ;         glVertex3f(-1,1,0);
            glTexCoord2f(0,0) ;         glVertex3f(-1,-1,0);
    glEnd();
\$\endgroup\$
  • \$\begingroup\$ Are you calling glGenTextures before glBindTexture? \$\endgroup\$ – SurvivalMachine May 8 '15 at 6:59
  • \$\begingroup\$ @survivalMachine no I didn't call glGenTexture \$\endgroup\$ – Dania May 8 '15 at 8:22
0
\$\begingroup\$

With open gl you can't just bind a texture before creating it.

Instead of keeping a counter called num_textures. Each time you want to create a new texture, retrieve a handle from OpenGL's glGenTextures function.

int id;
glGenTextures(1, &id);

So you would get rid of num_texture and all its usages.

And instead of this:

glBindTexture(GL_TEXTURE_2D, num_texture); // Bind the ID texture specified by the 2nd parameter

You write this:

int id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);

In other words you must use a handle OpenGL provides you, not just some random number you have decided.

In the future investigate using the glGetError function to see what went wrong as attempting to bind a texture that doesn't exist should give you a nice juicy error message.

\$\endgroup\$
  • \$\begingroup\$ you mean that I have to put this function in the initialize section ? and then use the id in loadtexture function ? \$\endgroup\$ – Dania May 8 '15 at 8:47
  • \$\begingroup\$ No sorry, I probably wasn't very clear. I've updated the answer now. \$\endgroup\$ – neon64 May 8 '15 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.