opengl打開本地bmp圖片繪製

 

注意bmp圖片的格式問題,32位ARGB  或者24位RGB。你所採用的素材必定要注意是多少位的就用多少位的。不然會顯示錯誤的圖片或者其餘什麼的錯誤。函數

代碼以下oop

32位版本spa

#include <GL/glut.h>

static GLint     ImageWidth;
static GLint     ImageHeight;
static GLint     PixelLength;
static GLubyte* PixelData;

#include <stdio.h>
#include <stdlib.h>

void display(void)
{
     // 清除屏幕並沒必要要
     // 每次繪製時,畫面都覆蓋整個屏幕
     // 所以不管是否清除屏幕,結果都同樣
     // glClear(GL_COLOR_BUFFER_BIT);

     // 繪製像素
//    glBitmap(ImageWidth,ImageHeight,0,0,0.0,0.0,PixelData);
     glDrawPixels(ImageWidth, ImageHeight,
         GL_ABGR_EXT, GL_UNSIGNED_BYTE, PixelData);

     // 完成繪製
     glutSwapBuffers();
}

int main(int argc, char* argv[])
{
     // 打開文件
     FILE* pFile = fopen("/home/aerk/bbb.bmp", "rb");
     if( pFile == 0 )
         exit(0);

     // 讀取圖象的大小信息
     fseek(pFile, 0x0012, SEEK_SET);
     fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
     fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);

     // 計算像素數據長度
     PixelLength = ImageWidth * 4;
     while( PixelLength % 4 != 0 )
         ++PixelLength;
     PixelLength *= ImageHeight;

     // 讀取像素數據
     PixelData = (GLubyte*)malloc(PixelLength);
     if( PixelData == 0 )
         exit(0);

     fseek(pFile, 54, SEEK_SET);
     fread(PixelData, PixelLength, 1, pFile);

     // 關閉文件
     fclose(pFile);

     // 初始化GLUT並運行
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowPosition(100, 100);
     glutInitWindowSize(ImageWidth, ImageHeight);
     glutCreateWindow("FileName");
     glutDisplayFunc(&display);
     glutMainLoop();

     // 釋放內存
     // 實際上,glutMainLoop函數永遠不會返回,這裏也永遠不會到達
     // 這裏寫釋放內存只是出於一種我的習慣
     // 不用擔憂內存沒法釋放。在程序結束時操做系統會自動回收全部內存
     free(PixelData);

     return 0;
}

24位版本操作系統

#include <GL/glut.h>

static GLint     ImageWidth;
static GLint     ImageHeight;
static GLint     PixelLength;
static GLubyte* PixelData;

#include <stdio.h>
#include <stdlib.h>

void display(void)
{
     // 清除屏幕並沒必要要
     // 每次繪製時,畫面都覆蓋整個屏幕
     // 所以不管是否清除屏幕,結果都同樣
     // glClear(GL_COLOR_BUFFER_BIT);

     // 繪製像素
//    glBitmap(ImageWidth,ImageHeight,0,0,0.0,0.0,PixelData);
     glDrawPixels(ImageWidth, ImageHeight,
         GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData);

     // 完成繪製
     glutSwapBuffers();
}

int main(int argc, char* argv[])
{
     // 打開文件
     FILE* pFile = fopen("/home/aerk/bbb.bmp", "rb");
     if( pFile == 0 )
         exit(0);

     // 讀取圖象的大小信息
     fseek(pFile, 0x0012, SEEK_SET);
     fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
     fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);

     // 計算像素數據長度
     PixelLength = ImageWidth * 3;
     while( PixelLength % 4 != 0 )
         ++PixelLength;
     PixelLength *= ImageHeight;

     // 讀取像素數據
     PixelData = (GLubyte*)malloc(PixelLength);
     if( PixelData == 0 )
         exit(0);

     fseek(pFile, 54, SEEK_SET);
     fread(PixelData, PixelLength, 1, pFile);

     // 關閉文件
     fclose(pFile);

     // 初始化GLUT並運行
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowPosition(100, 100);
     glutInitWindowSize(ImageWidth, ImageHeight);
     glutCreateWindow("FileName");
     glutDisplayFunc(&display);
     glutMainLoop();

     // 釋放內存
     // 實際上,glutMainLoop函數永遠不會返回,這裏也永遠不會到達
     // 這裏寫釋放內存只是出於一種我的習慣
     // 不用擔憂內存沒法釋放。在程序結束時操做系統會自動回收全部內存
     free(PixelData);

     return 0;
}
相關文章
相關標籤/搜索