1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Texture Map shows all white in the cube not the pictures

Discussion in 'C++' started by christina123y, Aug 31, 2010.

  1. christina123y

    christina123y New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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"
    
    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; ibmiHeader.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 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; 
    }
     

Share This Page