• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            用 Targa(TGA) 圖像文件,因為它簡單并且支持 alpha 通道,它可以使你更容易的創建酷的效果。
            TGA 文件與 BMP 文件一樣,圖像數據都是按 BGR 存儲,而 OpenGL 的圖像是按 RGB 來使用的。
            這里用了一個很好的方法,一次性讀入所有的圖像數據,然后再進行轉換 R 與 B,而不是每次讀一個 R,G,B,A,這種讀法很省時間。fread(buffer, size, number, file);按塊讀取文件內容
            size 為每次讀取幾個 byte,如按字節讀取,這里使用的一樣,size == 1;
            number 為一共讀取多少個 size 的內容,number = width * height * bytesPerPixel;(整個圖像的數據內容)
            這個方法只能讀取無壓縮的 TGA 文件
            TGA 開始為 12 字節的內容為描述文件的一般信息,接下來的 6 字節為最重要的信息,文件寬,高,每個像素的位數, 從第 18 個字節開始,就是圖像的數據內容。
            更重要的一點,這個程序進行了各種出錯處理。
             
            #ifndef _TGALOAD_H_
            #define _TGALOAD_H_
            #include <stdio.h>
             
            typedef struct {
                   GLubyte* data;
                    GLuint width;
                    GLuint height;
                    GLuint rgbType;
            } TextureImage;
             
            GLboolean loadTGA(char* fileName, TextureImage* textureImage) {
                    GLubyte TGAheader[12]= { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // 無壓縮的TGA文件頭
                    GLubyte TGAcompare[12]; // 保存讀入的文件頭信息
                    GLubyte header[6]; // 保存最有用的圖像信息,寬,高,位深
                    GLuint bytesPerPixel; // 記錄每個顏色所占用的字節數
                    GLuint imageSize; // 記錄文件大小
                    GLuint temp; 
            // 臨時變量
             
                    FILE *file = fopen(fileName, "rb"); // 打開一個TGA文件
                    if (file==NULL || // 文件存在么?
                                  fread(TGAcompare, 1, sizeof(TGAcompare), file)!= sizeof(TGAcompare) ||
                                  // 是否包含12個字節的文件頭?
                                  memcmp(TGAheader, TGAcompare, sizeof(TGAheader))!= 0 ||
                                  // 是否是我們需要的格式?
                                  fread(header, 1, sizeof(header), file)!= sizeof(header)) {
                                  // 如果是讀取下面六個圖像信息
                            if (file == NULL) // 文件不存在返回錯誤
                                    return false;
                            else {
                                    fclose(file); // 關閉文件返回錯誤
                                    return false;
                            }
                    }
             
                    textureImage->width = header[1] * 256 + header[0]; // 記錄文件寬度
                    textureImage->height = header[3] * 256 + header[2]; 
            // 記錄文件高度
                    if (textureImage->width <= 0 || // 寬度是否小于0
                                  textureImage->height <= 0 || // 高度是否小于0
                                  (header[4] != 24 && header[4] != 32)) { // TGA文件是24/32位?
                            fclose(file); // 如果失敗關閉文件,返回錯誤
                            return false;
                    }
                    bytesPerPixel = header[4] >> 3; // 記錄每個象素所占的字節數
                    textureImage->rgbType = bytesPerPixel; // 記錄每個象素所占的字節數
             
                    imageSize = textureImage->width *textureImage->height * bytesPerPixel;
                    // 計算TGA文件加載所需要的內存大小
                    textureImage->data = (GLubyte*)malloc(imageSize); // 分配內存去保存TGA數據
                    if (textureImage->data == NULL || // 系統是否分配了足夠的內存?
                                  fread(textureImage->data, 1, imageSize, file)!= imageSize) {
                                  // 是否成功讀入內存?
                            if (textureImage->data != NULL) { // 是否有數據被加載
                                    free(textureImage->data); // 如果是,則釋放載入的數據
                            }
                            fclose(file); // 關閉文件
                            return false; // 返回錯誤
                    }
             
                    for (GLuint i=0; i<GLuint(imageSize); i+=bytesPerPixel) {
                            // 循環所有的像素
                            // 交換R和B的值
                            temp = textureImage->data[i];
                            textureImage->data[i] = textureImage->data[i + 2];
                            textureImage->data[i + 2] = temp;
                    }
             
                    fclose(file); // 關閉文件
                    
            return true;
            }
             
            #endif
             
            posted on 2010-12-17 17:32 逛奔的蝸牛 閱讀(609) 評論(0)  編輯 收藏 引用 所屬分類: OpenGL
            91久久福利国产成人精品| 久久久久久精品成人免费图片| 久久青青草原亚洲av无码app| 青青草原精品99久久精品66| 久久精品国产亚洲AV嫖农村妇女| 麻豆精品久久久一区二区| 久久996热精品xxxx| 久久精品国产亚洲av麻豆图片| 91久久婷婷国产综合精品青草| 久久久无码精品午夜| 久久人爽人人爽人人片AV| 国产香蕉97碰碰久久人人| 亚洲国产精品无码久久| 久久亚洲2019中文字幕| 国产情侣久久久久aⅴ免费| 久久伊人色| 亚洲午夜精品久久久久久人妖| 久久国产劲爆AV内射—百度| 狠狠色丁香久久婷婷综| 久久乐国产综合亚洲精品| 久久本道伊人久久| 久久久久波多野结衣高潮| 久久久久亚洲AV成人网| 久久这里只精品国产99热| 久久亚洲AV成人无码国产| 色偷偷88欧美精品久久久 | 国产精品天天影视久久综合网| 一本大道久久香蕉成人网| 99久久综合狠狠综合久久| 69久久精品无码一区二区| 亚洲av日韩精品久久久久久a | 久久无码AV一区二区三区| 久久久国产乱子伦精品作者| 一级做a爰片久久毛片毛片| 久久国产精品二国产精品| 热久久这里只有精品| 久久99毛片免费观看不卡| 色综合久久最新中文字幕| 久久久久久久99精品免费观看| 97久久精品人妻人人搡人人玩| 久久99国产综合精品|