Bitmap OpenGL Texture

GL_RGBA is common.but 32bit bitmap format color order is BGR.
use GL_BGRA_EXT when glTexImage2D()

Initialization

void LoadTextureData(TCHAR* _filename){
    wprintf(L"%s",_filename);
    HANDLE handle;
    DWORD temp=0;
    BITMAPFILEHEADER bitmapFileHeader;
    BITMAPINFOHEADER bitmapInfoHeader;
    handle = CreateFile(_filename,GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL );
    //SetFilePointer(handle , 0 , NULL , FILE_BEGIN);
    // open Header and read
    if(handle==INVALID_HANDLE_VALUE || handle==NULL){assert(!"file does not exist");}
    ReadFile(handle,&bitmapFileHeader,sizeof(BITMAPFILEHEADER),&temp,NULL);
    for(int i=0;i<200;i++){    printf("off%d,type%d,size=%d\n",bitmapFileHeader.bfOffBits,bitmapFileHeader.bfType,bitmapFileHeader.bfSize);}
    if ( temp!=sizeof(BITMAPFILEHEADER) || bitmapFileHeader.bfType!=0x4D42 )
    {
       // CloseHandle(handle);
      //  assert(!"it is not bitmap format");
    }
    ReadFile(handle,&bitmapInfoHeader,sizeof(BITMAPINFOHEADER),&temp,NULL);
 
      ReadFile(handle,texturedatapointer,WINWIDTH*WINHEIGHT*4,&temp,NULL);
  //  printf("%dread\n",temp);
    printf("size=iwidth=%dheight=%d\n",bitmapInfoHeader.biHeight,bitmapInfoHeader.biWidth);
    for(int i=0;i<200;i++){    printf("%d\n",texturedatapointer[i]);}
    CloseHandle(handle);
}
void InitTexture(){
    GetGLError();
 
    glGenTextures ( 1, texId );
    glBindTexture ( GL_TEXTURE_2D, texId[0] );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, WINWIDTH, WINHEIGHT, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, texturedatapointer);
    GetGLError();
}

display()

void drawWall(){
    glColor4f(0.0,0.0,1.0,0.0);
    glEnable ( GL_TEXTURE_2D );
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
    glBindTexture ( GL_TEXTURE_2D, texId[0] );
    glBegin(GL_QUADS);
    glTexCoord2f(0,1);glVertex2i(0,0);
    glTexCoord2f(1,1);glVertex2i(WINWIDTH,0);
    glTexCoord2f(1,0);glVertex2i(WINWIDTH,WINHEIGHT);
    glTexCoord2f(0,0);glVertex2i(0,WINHEIGHT);
    glEnd();
    glFlush();
    glDisable(GL_TEXTURE_2D);
}
en.gif en
ja.gif ja


edit
サポートサイト Wikidot.com bitmapopengl