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;
}