Texture mapping - doesn't work

Hello, everyone.
First of all, I did a lot of reading and coding myself before posting this one.

I’m writing an OpenGL-course final work, and the texture just doesn’t appear on the object. Can someone please explain to me what exactly is wrong with the functions?

/* Load a bitmap from file and "binding" the texture */
int myLoadBitmap(char *filename)
{
    int i, j=0;
    int num_texture = -1;
    FILE *l_file;
    unsigned char *l_texture;
    BITMAPFILEHEADER fileheader;
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;
    num_texture++;
    if((l_file = fopen(filename,"rb"))==NULL)   
        return (-1);
    fread(&fileheader, sizeof(fileheader), 1, l_file);
    fseek(l_file, sizeof(fileheader), SEEK_SET);
    fread(&infoheader, sizeof(infoheader), 1, l_file);
    l_texture = (byte *)malloc(infoheader.biWidth*infoheader.biHeight*4);
    memset(l_texture, 0, infoheader.biWidth*infoheader.biHeight*4);
    for(i=0; i<infoheader.biWidth*infoheader.biHeight; i++)
    {
        fread(&rgb, sizeof(rgb), 1, l_file);
        l_texture[j+0] = rgb.rgbtRed;
        l_texture[j+1] = rgb.rgbtGreen;
        l_texture[j+2] = rgb.rgbtBlue;
        l_texture[j+3] = 255;
        j+=4;
    }
    fclose(l_file);
    glBindTexture(GL_TEXTURE_2D, num_texture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
    free(l_texture);
    return (num_texture);
}

/* Use the previous function to cylinder (which is barrel-"baraban" with a bullets in the revolver model) */
void baraban()
{
    int texture = myLoadBitmap("..\	extures\\baraban.bmp");
    if (!texture)
    {
        printf ("Texture did not load
");
        glutDestroyWindow(win);
        exit(0);
    }
    /*--------------------------------------------------------*/
    glPushMatrix();
    glRotatef(90.0, 0.0, 1.0, 0.0);
    /*----------------------------------------------------*/
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D); /* activate */
    gluCylinder(b, 1.0, 1.0, 2.0, 10, 10);
    glDisable(GL_TEXTURE_2D); /* d-activate */
    /*---------------------------------------------------*/
    gluDisk(d, 0.0, 1.0, 10, 2);
    glTranslatef(0.0, 0.0, 2.0);
    gluDisk(d, 0.0, 1.0, 10, 2);
    glPopMatrix();
}

Full source code -> HERE
:confused:
P.S. I’m using free Borland C Compiler 5.5

remove these:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);

Removing those took no effect…

Do not free l_texture. Within your myLoadBitmap() , you are generating image and then destroying. Free your image in main() after disabling texture or after mapping.

I did changes, as you mentioned, and it didn’t work too…

int myLoadBitmap(char *filename, unsigned char *l_texture) 
{ 
    int i, j=0;
    int num_texture = -1;
    FILE *l_file; 
    BITMAPFILEHEADER fileheader; 
    BITMAPINFOHEADER infoheader;
    RGBTRIPLE rgb;
    num_texture++;
    if((l_file = fopen(filename,"rb"))==NULL)   
        return (-1);
    fread(&fileheader, sizeof(fileheader), 1, l_file);
    fseek(l_file, sizeof(fileheader), SEEK_SET);
    fread(&infoheader, sizeof(infoheader), 1, l_file);
    l_texture = (byte *)malloc(infoheader.biWidth*infoheader.biHeight*4);
    memset(l_texture, 0, infoheader.biWidth*infoheader.biHeight*4);
    for(i=0; i<infoheader.biWidth*infoheader.biHeight; i++)
    {
        fread(&rgb, sizeof(rgb), 1, l_file);
        l_texture[j+0] = rgb.rgbtRed;
        l_texture[j+1] = rgb.rgbtGreen;
        l_texture[j+2] = rgb.rgbtBlue;
        l_texture[j+3] = 255;
        j+=4;
    } 
    fclose(l_file);
    glBindTexture(GL_TEXTURE_2D, num_texture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    //glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
    //free(l_texture);
    return (num_texture); 
}

void baraban()
{
    unsigned char *l_tex = NULL;
    int texture = myLoadBitmap("..\	extures\\baraban.bmp", l_tex);
    if (!texture) 
    {
        printf ("Texture did not load
");
        glutDestroyWindow(win);
        exit(0);
    }
    
    glPushMatrix();
    glRotatef(90.0, 0.0, 1.0, 0.0);
    
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D);
    gluCylinder(b, 1.0, 1.0, 2.0, 10, 10); 
    glDisable(GL_TEXTURE_2D);
    free(l_tex);

    gluDisk(d, 0.0, 1.0, 10, 2); 
    glTranslatef(0.0, 0.0, 2.0);
    gluDisk(d, 0.0, 1.0, 10, 2);
    glPopMatrix();
}

Try using
glGenTextures(1,&num_texture)
instead of assigning 0…N names.

deleting l_texture is of no consequence, as the data is already uploaded.

instead of assigning 0…N names

Which lines exactly? :stuck_out_tongue:

i m not sure but

what is significance of
int num_texture = -1;

as perhaps it
always give -1(failed in my means(i m not sure about this as it may/or not(local scope)) modified)

He is incrementing num_Texture after few lines so its value becomes 0.


int texture = myLoadBitmap("..\	extures\\baraban.bmp", l_tex);
    if (!texture) 
    {
        printf ("Texture did not load
");
        glutDestroyWindow(win);
        exit(0);
    }


The return value is 0.Inside myLoadBitmap(), the num_texture is incremented to 0.


 /*----------------------------------------------------*/
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D); /* activate */
    gluCylinder(b, 1.0, 1.0, 2.0, 10, 10);
    glDisable(GL_TEXTURE_2D); /* d-activate */
    /*---------------------------------------------------*/


Where are you mapping your texture to the cylinder??
Something like for a drawQuad() we supply texel values.

glTextureCoord2f();
glVertex()…

??

Where are you mapping your texture to the cylinder??
Something like for a drawQuad() we supply texel values.

glTextureCoord2f();
glVertex()…

??

I guess, this is the exact problem :eek:
I simply didnt know I have to. Can you assist me? Where should I em… map the texture?

Hi!!!
View documentation for GLU, on Quadrics, and drew attention to gluQuadricNormals, gluQuadricTexture …

Rightly said by Rigon. Visit this http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18

You need to let quadratic generate texture coordinates.

Other way is to approximate your cylinder as a faceted object, then at each plane (taken as a quad) , map your texture( if you do not want to automatically generate texels).