View Full Version : texture is all white in opengl!
Crossbow
03-26-2002, 08:50 PM
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?
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!
no-one
03-27-2002, 12:02 PM
uncoment the glColor3f and try changing the color to
glColor3f(0.5f,0.0f,0.0f);
and see what you get.
opengl15
03-27-2002, 03:51 PM
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. :D
//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 :D
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.
VBprogrammer
03-27-2002, 05:34 PM
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.
Crossbow
03-27-2002, 08:29 PM
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.
opengl15
03-27-2002, 08:31 PM
Glad we can help you man thats what where here for ;)
VBprogrammer
03-30-2002, 10:58 AM
who...what...where...why....when....I got something right...There's a first! ;)
Unregistered
03-31-2002, 11:54 AM
congratulations!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.