Textures appears white in opengl
c++, opengl, sdl-opengl
Solution
There are a number of things that might go wrong in your code.
My suspicion is, that you call `initGL()` prematurely, i.e without a context being available yet. You didn't show the code in question, so I can make only guesses about this.
Enabling the texturing unit is important for drawing only. You can upload texture data with the texture unit being disabled just fine. I strongly recommend you put that `glEnable(GL_TEXTURE_2D);` right before your quad drawing code and nowhere else (actually you should but glEnable and glDisable calls only into drawing code, for several reasons).
Loading the texture you fail to perform several sanity checks. Also you forget to properly set pixel store parameters, but that would make your texture look distorted. Also it would make sense to return the texture ID from your load function, instead of a boolean and putting the ID into a global variable. Texuture ID 0 is reserved, so you can use that to indicate an error.
You set the filter mode though, so this is not an issue with mipmap levels not being defined.
Problem
I can't load a `BMP texture` and show it on faces of a cube. I am working on `Windows 7`.My compiler is `Visual Studio 2010 Express`.I use `FreeImage` to load the bmp.My image bit depth is 24.My image size is 256 * 256.I am using `SDL` to create window and handle the events. When I compile and run the program my cube is still white and nothing is shown on its faces.I am loading the texture into a `GLuint` variable.When I print that variable it only prints "1". One of my friends told me that my graphic card is incompatible with this version of `OpenGL`.So I compiled and ran the program in an `Ubuntu` on a virtual machine and it worked fine.The cube was textured successfully.Can you please help me to run it on my `Windows` propely? And if you need my graphic card is `NVIDIA GT240 DDR5`. EDIT This is my initgl function: ``` //OpenGL initialization. int initGL(void) { glEnable(GL_TEXTURE_2D); //Enable texture mapping. if(!loadGLTextures("bmp_24.bmp")) return false; glShadeModel(GL_SMOOTH); glClearColor(0.0f , 0.0f , 0.0f , 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); //Nice perspective. glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST); return true; } ``` And this is the function that loads the image.GLuint texture[1] has been defined before: ``` //This function will load a bitmap image. bool loadGLTextures(const char* file) { FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; fif = FreeImage_GetFileType(file , 0); if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(file); if(fif == FIF_UNKNOWN) return false; FIBITMAP* dib = FreeImage_Load(fif , file); if(!dib) return false; //Create the texture. glGenTextures(1 , &texture[0]); //Typical texture generation using data from the bitmap. glBindTexture(GL_TEXTURE_2D , texture[0]); //Generate the texture. glTexImage2D(GL_TEXTURE_2D , 0 , GL_RGB , FreeImage_GetWidth(dib) , FreeImage_GetHeight(dib) , 0 , GL_BGR_EXT , GL_UNSIGNED_BYTE , FreeImage_GetBits(dib)); //Linear filtering. glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR); glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR); printf("%d" , texture[0]); FreeImage_Unload(dib); return true; } ``` And this is my render function: ``` //All of the drawing goes through this. int drawGLScene(void) { static float xrot = 0 , yrot = 0 , zrot = 0; //Clear screen and depth buffer. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f , 0.0f , -5.0f); glRotatef(xrot , 1.0f , 0.0f , 0.0f); glRotatef(yrot , 0.0f , 1.0f , 0.0f); glRotatef(zrot , 0.0f , 0.0f ,1.0f); //Select the texture. glBindTexture(GL_TEXTURE_2D , texture[0]); glBegin(GL_QUADS); //Front: //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , 1.0f); //Bottom right fo the texture and quad. glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , 1.0f); //Top right of the texture and quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f); //Top left of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f); //Back: //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f); //Bottom right of the texture and quad. glTexCoord2f(1.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f); //Top right of the texture and the quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f); //Top left of the texture and the quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f); //Top: //Top right of the texture and quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f); //Top left of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f); //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , 1.0f , 1.0f); //Bottom right of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f); //Bottom: //Top left of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , -1.0f , 1.0f); //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f); //Bottom right of the texture and quad. glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f); //Top right of the texture and quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , -1.0f , 1.0f); //Right: //Bottom right of the texture and quad. glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f); //Top right of the texture and quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f); //Top left of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f); //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(1.0f , -1.0f , 1.0f); //Left: //Bottom left of the texture and quad. glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f); //Bottom right of the texture and quad. glTexCoord2f(1.0f , 0.0f); glVertex3f(-1.0f , -1.0f , 1.0f); //Top right of the texture and quad. glTexCoord2f(1.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f); //Top left of the texture and quad. glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f); glEnd(); SDL_GL_SwapBuffers(); xrot += 0.1; yrot += 0.1; zrot += 0.1; return true; } ```