Thread: Texture Map shows all white in the cube not the pictures

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Smile Texture Map shows all white in the cube not the pictures

    Hi, everyone! I'm a newer to OpenGL. I need to paint BMP format pictures to a cube with texture map. I read as BMP format, and the texture map process was correct. I have use VS debugger and seems that the value were all right, however, the whole cube was all white. I have asked others to help me, they said that the process was right, but still couldn't found the resolution. Wish you can help me!
    Thank for any help! Wish your reply soon!
    Here is my code.
    Code:
    #include "stdafx.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>
    #include <GL/GLAux.h>
    
    GLuint textureid[6];
    static GLfloat theta[] = {0.0,0.0,0.0};
    static GLint axis = 2;
    
    // 'LoadDIBitmap()' - Load a DIB/BMP file from disk.
    // Returns a pointer to the bitmap if successful, NULL otherwise...
    GLubyte *LoadDIBitmap(const char *filename, BITMAPINFO **info)
    {
    	FILE *fp;                    // Open file pointer
    	BITMAPFILEHEADER header;    
    	DWORD infosize;              // Size of header information
    	DWORD bitsize;               // Size of bitmap
    	GLubyte *bits;               // Bitmap pixel bits
    	GLubyte *ptr;                // Pointer into bitmap 
    	GLubyte temp;                // Temporary variable to swap red and blue
    	int x, y;                    // X and Y position in image
    	int length;               
    
    	// Try opening the file; use "rb" mode to read this *binary* file.
    	if ((fp = fopen(filename, "rb")) == NULL)
    	{   
    		// Couldn't read the file header
    		printf("Couldn't read the file header!\n");
    		return NULL;
    	}
    
    	// Read the file header and any following bitmap information
    	if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
    	{
    		// Couldn't read the file header
    		printf("Couldn't read the file header!\n");
    		fclose(fp);
    		return NULL;
    	}
    
    	// Check for BM reversed
    	if (header.bfType != 'MB')
    	{
    		printf("Not a bitmap file!\n");
    		fclose(fp);
    		return NULL;
    	}
    
    		infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);  
    	if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
    	{
    		// Couldn't allocate memory for bitmap info
    		printf("Couldn't allocate memory for bitmap info!\n");
    		fclose(fp);
    		return NULL;
    	}
    
    	if (fread(*info, 1, infosize, fp) < infosize)
    	{
    		// Couldn't read the bitmap header
    		printf("Couldn't read the bitmap header!\n");
    		free(*info);
    		fclose(fp);
    		return NULL;
    	}
    
    	// Now that we have all the header info read in, allocate memory for the bitmap and read *it* in
    	if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
    	{
    		bitsize = ((*info)->bmiHeader.biWidth * abs((*info)->bmiHeader.biHeight) *
    			(*info)->bmiHeader.biBitCount) / 8;
    	}
    
    	if ((bits = (GLubyte *)malloc(bitsize)) == NULL)
    	{
    		// Couldn't allocate memory 
    		printf("Couldn't allocate memory !\n");
    		free(*info);
    		fclose(fp);
    		return NULL;
    	}
    
    	if (fread(bits, 1, bitsize, fp) < bitsize)
    	{
    		// Couldn't read bitmap - free memory and return NULL!
    		printf("Couldn't read bitmap\n");
    		free(*info);
    		free(bits);
    		fclose(fp);
    		return NULL;
    	}
    
    
    	length = ((*info)->bmiHeader.biWidth * 3 + 3) & ~3;
    	for (y = 0; y < (*info)->bmiHeader.biHeight; y++)
    		for (x = 0, ptr = bits + y * length; x < (*info)->bmiHeader.biWidth; x++, ptr += 3)
    		{
    			temp = ptr[0];
    			ptr[0] = ptr[2];
    			ptr[2] = temp;
    		}
    
    		// OK, everything went fine - return the allocated bitmap.
    		fclose(fp);
    		return bits;
    }
    
    
    
    int LoadGLTextures()   
    {
    	int Status = 0;        
    
    	GLubyte *TextureImage[6];     
                    TextureImage[0] = (GLubyte*)malloc(sizeof(void*));
    	TextureImage[1] = (GLubyte*)malloc(sizeof(void*));
    	TextureImage[2] = (GLubyte*)malloc(sizeof(void*));
    	TextureImage[3] = (GLubyte*)malloc(sizeof(void*));
    	TextureImage[4] = (GLubyte*)malloc(sizeof(void*));
    	TextureImage[5] = (GLubyte*)malloc(sizeof(void*));
    	memset(TextureImage,0,sizeof(void *)*6);   
    	char *pictures[] = 
                    { 		
                         "E:/TestProject/MyTexture/rec/1.bmp", 
    		"E:/TestProject/MyTexture/rec/2.bmp", 
    		"E:/TestProject/MyTexture/rec/3.bmp",  
    		"E:/TestProject/MyTexture/rec/4.bmp", 
    		"E:/TestProject/MyTexture/rec/5.bmp", 
    		"E:/TestProject/MyTexture/rec/6.bmp"
    	};
    	glGenTextures(6, &textureid[0]);    
    	for(int i=0; i<6; i++) 
    	{
    		BITMAPINFO *BitmapInfo;
    		if (TextureImage[i] = LoadDIBitmap(pictures[i], &BitmapInfo)) 
    		{
    			Status = 1;        
    
    			glBindTexture(GL_TEXTURE_2D, textureid[i]); 
    			glTexImage2D(GL_TEXTURE_2D, 0, 3, BitmapInfo->bmiHeader.biWidth, BitmapInfo->bmiHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]);
    			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    		}
    	}
    	
    	for(int i=0;i<6;i++)
    	{
    		if(TextureImage[i])
    		{
    			free(TextureImage[i]);    
    		}
    	}
    	return Status;          
    }
    
    void DrawGLScene(GLvoid)         
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_TEXTURE_2D);
       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    
    	glLoadIdentity();         
    	glTranslatef(0.0f,0.0f,-5.0f);
    
    	glRotatef(theta[0],1.0f,0.0f,0.0f);
    	glRotatef(theta[1],0.0f,1.0f,0.0f);
    	glRotatef(theta[2],0.0f,0.0f,1.0f);
    
    	// Front Face
    	glBindTexture(GL_TEXTURE_2D, textureid[0]); 
    	glBegin(GL_QUADS);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    	glEnd();
    
    	// Back Face
    	glBindTexture(GL_TEXTURE_2D, textureid[1]); 
    	glBegin(GL_QUADS);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    	glEnd();
    
    	// Top Face
    	glBindTexture(GL_TEXTURE_2D, textureid[2]); 
    	glBegin(GL_QUADS);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    	glEnd();
    
    	// Bottom Face
    	glBindTexture(GL_TEXTURE_2D, textureid[3]); 
    	glBegin(GL_QUADS);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    	glEnd();
    
    	// Right face
    	glBindTexture(GL_TEXTURE_2D, textureid[4]);
    	glBegin(GL_QUADS);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
    	glEnd();
    
    	// Left Face
    	glBindTexture(GL_TEXTURE_2D, textureid[5]); 
    	glBegin(GL_QUADS);
    	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    	glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
    	glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
    	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
    	glEnd();
    	//glFlush();
    	glutSwapBuffers();
    	glDisable(GL_TEXTURE_2D); 
    
    }
    
    void spinCube()
    {
    	theta[axis] += 0.1;
    	if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
    
    	glutPostRedisplay();
    }
    
    
    void init(void)
    {    
    	LoadGLTextures();
    	glClearColor (0.0, 0.0, 0.0, 0.0);
    	glClearDepth(1.0);
    
    	glShadeModel(GL_FLAT);
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_TEXTURE_2D);
    }
    
    void mouse(int btn, int state, int x, int y)
    {
    	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
    	if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
    	if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
    }
    
    void reshape(int w, int h)
    {
    	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	glTranslatef(0.0, 0.0, -3.6);
    }
    
    void key(unsigned char k, int x, int y)
    {
    	if(k == '1') glutIdleFunc(spinCube);
    	if(k == '2') glutIdleFunc(NULL);
    }
    
    int main(int argc, char** argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(800, 600);
    	glutInitWindowPosition(100, 100);
    	glutCreateWindow(argv[0]);
    	init();
    	glutDisplayFunc(DrawGLScene);
    	glutIdleFunc(spinCube);
    	glutMouseFunc(mouse);
    	glutKeyboardFunc(key);
    
    	glutReshapeFunc(reshape);
    	glutMainLoop();
    	return 0; 
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    A few things to check first:

    * Make sure that the bitmaps you are loading are not all-white?
    * You are assuming that your bitmap data consists of 3 component 24-bit RGB data, is this indeed the case?
    * Make sure that glGetError() does not report an error.

    These lines are fishy (actually, they are not doing what you intend them to do, go through them line by line with pen and paper and figure out why yourself):

    Code:
            length = ((*info)->bmiHeader.biWidth * 3 + 3) & ~3;
            for (y = 0; y < (*info)->bmiHeader.biHeight; y++)
                    for (x = 0, ptr = bits + y * length; x < (*info)->bmiHeader.biWidth; x++, ptr += 3)
                    {
                            temp = ptr[0];
                            ptr[0] = ptr[2];
                            ptr[2] = temp;
                    }
    If you want to loop through all pixels in your buffer, why not just do so, instead of using unnecessarily fancy code:

    Code:
    ptr = bits;
    for(i = 0; i < width * height; ++i) 
    { 
         swap_rgb(ptr); 
         ptr+= 3;
    }
    Upon further inspection the following is strange too:

    Code:
    GLubyte *TextureImage[6];     
                    TextureImage[0] = (GLubyte*)malloc(sizeof(void*));
            TextureImage[1] = (GLubyte*)malloc(sizeof(void*));
            TextureImage[2] = (GLubyte*)malloc(sizeof(void*));
            TextureImage[3] = (GLubyte*)malloc(sizeof(void*));
            TextureImage[4] = (GLubyte*)malloc(sizeof(void*));
            TextureImage[5] = (GLubyte*)malloc(sizeof(void*));
            memset(TextureImage,0,sizeof(void *)*6);
    So you allocate space for 6 pointers, then immediately proceed to allocate space for exactly one pointer at the locations of these pointers, why? Further on, you then overwrite the allocated pointer:

    Code:
    if (TextureImage[i] = LoadDIBitmap(pictures[i], &BitmapInfo))
    There you have something called a memory leak.

    In attempting to fix your code do the following, which you should do whenever you encounter a problem (it's the application of what you could call a systematic approach to problem solving):
    1. Make sure that your bitmap loading code actually does what it is supposed to do, i.e., load the bitmap, then write the data you just read back to a file and see if what you get back is the original image. If not, your code is obviously broken (which it is by the way).
    2. Make sure that your renderer is actually working. Generate a simple texture (e.g. a 2 x 2 checkerboard pattern) and draw it onto a single quad. Then show the quad. If the textured quad is rendered correctly, your rendering code is OK.
    3. Once you ascertained that you can both read the bitmap correctly, and draw a textured quad correctly, you can do the more fancy stuff you've been trying to do (load a fancy bitmap, use it to texture a rotating cube, ...)
    Last edited by MWAAAHAAA; 08-25-2010 at 02:49 PM.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cube map is upside down.
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 07-28-2010, 10:20 PM
  2. Replies: 47
    Last Post: 07-13-2010, 07:22 PM
  3. d3d9 c++ texture wrapper... again...
    By yaya in forum Game Programming
    Replies: 0
    Last Post: 04-01-2009, 01:08 PM
  4. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  5. Replies: 4
    Last Post: 02-27-2008, 03:10 PM