OpenGL加载位图例程错误

时间:2015-12-08 14:05:21

标签: c++ winapi opengl bitmap

我加载位图的例程正在做错。当我进行纹理贴图时,红色像素将转换为绿色,反之亦然。 'LOAD'是保存位图数据的纹理类的变量,'tsLogSystemMessage'只是我的错误处理例程。  这是代码:

BMT.BMPwidth = -1; DWORD Bitread,SFP;
HANDLE BitFile = CreateFileA(imagepath,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(BitFile == INVALID_HANDLE_VALUE) 
{ 
    tsLogSystemMessage(LOG_MSG_ERROR,"File %s cannot be opened or does not exist\n",imagepath);
    return(BMT); //continue with error operation
}
ReadFile(BitFile,&LOAD.BMPheader,54,&Bitread,NULL);
if((Bitread == 0) || (Bitread != 54))
{
    tsLogSystemMessage(LOG_MSG_ERROR,"ERROR: UNEXPECTED END OF FILE FOUND!!!%s\n",imagepath);
    return(BMT); //continue with error operation
}
if (LOAD.BMPheader[0]!='B' || LOAD.BMPheader[1]!='M' )//The first 2 bytes should be B and M
{    
    tsLogSystemMessage(LOG_MSG_ERROR,"ERROR: Incorrect Bitmap Format in file: %s\n",imagepath);
    return(BMT); //continue with error operation
} 
// Read ints from the byte array
LOAD.BMPdataPos    = *(int*)&(LOAD.BMPheader[0x0A]);
LOAD.BMPSize  = *(int*)&(LOAD.BMPheader[0x22]);
LOAD.BMPwidth      = *(int*)&(LOAD.BMPheader[0x12]);
LOAD.BMPheight     = *(int*)&(LOAD.BMPheader[0x16]);
// Some BMP files are misformatted, guess missing information
if (LOAD.BMPSize==0)    LOAD.BMPSize = LOAD.BMPwidth * LOAD.BMPheight *3; // 3 : one byte for each Red, Green and Blue component
if (LOAD.BMPdataPos==0)      LOAD.BMPdataPos=54;
// Create a buffer
LOAD.data = new unsigned char [LOAD.BMPSize]; // Read the actual data from the file into the buffer
SFP = SetFilePointer(BitFile,1,NULL,FILE_BEGIN); //Reading all data from the beginning of file
if(!SFP)//Can't read from beginning? Problem.
{
    tsLogSystemMessage(LOG_MSG_ERROR,"ERROR: Unable to complete read operation in file: %s\n",imagepath);
    return(BMT); //continue with error operation
}
else
{
    ReadFile(BitFile,LOAD.data,LOAD.BMPSize,&Bitread,NULL); //Read all data
    if((Bitread == 0) || (Bitread != LOAD.BMPSize))
    {
        tsLogSystemMessage(LOG_MSG_ERROR,"ERROR: UNEXPECTED END OF FILE FOUND!!!%s\n",imagepath);
        return(BMT); //continue with error operation
    }
} 
tsLogSystemMessage(LOG_MSG_REPORT,"Texture %s Opened Successfully....\n",imagepath);
CloseHandle(BitFile);
return(LOAD);

任何帮助都将非常感谢!!!!!!!

好的,我的glTexImage2d例程:  PARTTEXDATA是上述例程中返回类的类变量,'count'是传递了多少纹理将被加载的参数。

    glGenTextures(count,TEXTURE_IDS);
    for(XP = 0;XP < count;XP++)
   {
    PARTTEXDATA = tsLoadBitmapFile(files[XP]);
    if(PARTTEXDATA.BMPdataPos != -1)
    {
        glBindTexture(GL_TEXTURE_2D,TEXTURE_IDS[XP]);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,PARTTEXDATA.BMPwidth,PARTTEXDATA.BMPheight, 0,GL_RGB, GL_UNSIGNED_BYTE,
            PARTTEXDATA.data );
        free(PARTTEXDATA.data);
    }

    }
    for(tt = 0;tt < count; tt++) //This loop allows the ID's for the textures to be available for all other BITMAP_OPERATION classes in this project
    {
        TEXES[tt] = TEXTURE_IDS[tt];
    }
    return(1);

0 个答案:

没有答案