Thread: texture is all white in opengl!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    texture is all white in opengl!

    when I bind my texture in opengl to a square, it just displays as a white square. Here is the code, what are the possible causes of this?
    Code:
    void Draw (void)
    {
    	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer
    	glLoadIdentity();											// Reset The View	
        glEnable(GL_TEXTURE_2D);
        glTranslatef(0.0f, 0.0f, -6.0f);
    
    	//glColor3f(1.0f,1.0f,1.0f);
        glBindTexture(GL_TEXTURE_2D, texture[1]);
    
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
            glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f,-1.0f);
            glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f,-1.0f);
            glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 0.0f);
        glEnd();
    
    	glFlush ();													// Flush The GL Rendering Pipeline
    }
    I know texutre[1] has loaded correctly, so what else can it be? Thanks for your help!

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    uncoment the glColor3f and try changing the color to

    glColor3f(0.5f,0.0f,0.0f);

    and see what you get.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    31

    Wink Its all wrong buddy.....

    First of all you didnt load the Bitmap file if it is a Bitmap your loading. So at the begining of your program copy/paste this funtion.
    //Loads Bitmap into memory and Loads it
    ////// Texture Information
    BITMAPINFOHEADER bitmapInfoHeader; // bitmap info header
    unsigned char* bitmapData; // the texture data
    unsigned int texture; // the texture object

    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
    {
    FILE *filePtr; // the file pointer
    BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
    unsigned char *bitmapImage; // bitmap image data
    int imageIdx = 0; // image index counter
    unsigned char tempRGB; // swap variable

    // open filename in "read binary" mode
    filePtr = fopen(filename, "rb");
    if (filePtr == NULL)
    return NULL;

    // read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

    // verify that this is a bitmap by checking for the universal bitmap id
    if (bitmapFileHeader.bfType != BITMAP_ID)
    {
    fclose(filePtr);
    return NULL;
    }

    // read the bitmap information header
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

    // move file pointer to beginning of bitmap data
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

    // allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    // verify memory allocation
    if (!bitmapImage)
    {
    free(bitmapImage);
    fclose(filePtr);
    return NULL;
    }

    // read in the bitmap image data
    fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

    // make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
    fclose(filePtr);
    return NULL;
    }

    // swap the R and B values to get RGB since the bitmap color format is in BGR
    for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
    {
    tempRGB = bitmapImage[imageIdx];
    bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
    bitmapImage[imageIdx + 2] = tempRGB;
    }

    // close the file and return the bitmap image data
    fclose(filePtr);
    return bitmapImage;
    }

    Basically this code sets up a funtion which will read the bitmap, swap the BGR to RGB and make OpenGL be able to read the bitmap.

    Now in your Initialize funtion include this.....but after your Cube creation

    void Init()
    {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // clear to black

    glShadeModel(GL_SMOOTH); // use smooth shading
    glEnable(GL_DEPTH_TEST); // hidden surface removal
    glEnable(GL_CULL_FACE); // do not calculate inside of poly's
    glFrontFace(GL_CCW); // counter clock-wise polygons are out

    glEnable(GL_TEXTURE_2D); // enable 2D texturing
    //change wood.bmp to the name of your bitmap
    // load our bitmap file
    bitmapData = LoadBitmapFile("wood.bmp", &bitmapInfoHeader);

    glGenTextures(1, &texture); // generate texture object
    glBindTexture(GL_TEXTURE_2D, texture); // enable our texture object

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    // generate the texture image
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
    bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
    }

    Now basically this code Generates the texture object, Loads it, Bind it, and enable Texture 2D.

    Now last and not least is the was you texture your cube. You textured your cube all wrong my man, its suppose to be in this order of texturing it

    glBegin(GL_QUADS); // front face
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
    glEnd();

    noticed the order of the way you texture it, from 0 to 0, 1 to 0, 1 to 1, 0 to 1. thats all and post me if it works.
    - Read to Learn, and Learn to Read -

  4. #4
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    opengl15 - I think he only posted the drawing function as he said the texture is loaded correctly.

    if you haven't got this working yet i think there is a command you must use to enable texture mapping that could be the problem.

    uncommenting the glcolour3f will tint the bitmap. i think passing 1.0f, 1.0f, 1.0f makes it normal.
    VC++ 6

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    VBProgrammer, you were right. It was the texture mapping function. I was not entering the right arguments, but it is fixed now, so thanks to all three of you.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    31

    Red face Good

    Glad we can help you man thats what where here for
    - Read to Learn, and Learn to Read -

  7. #7
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175

    Question

    who...what...where...why....when....I got something right...There's a first!
    VC++ 6

  8. #8
    Unregistered
    Guest
    congratulations!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Texture Binding with OpenGL
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 07-01-2008, 11:02 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM