openGL 紋理使用

2021-07-16 10:51:54 字數 3085 閱讀 2615

最近找了點資料學習了下opengl 紋理的使用

先有個 整體把握, 然後再去看大部頭中的細節講解, 感覺這樣的學習方式好些

總結下紋理使用總體流程:

1 啟用紋理

glenable(gl_texture_2d);

2 載入紋理

3 紋理 的顯示

載入紋理

1 讀取紋理影象高寬和畫素資料到記憶體中,老版本

opengl

需要考慮寬度和高度不是的整數次方

2 分配乙個新的紋理編號

glgentextures(1, &texture_id);

3 繫結紋理

glbindtexture(gl_texture_2d, texture_id);

表示當前所使用的紋理物件

4 設定紋理引數, 一堆函式呼叫

gltexparameter*

5 載入

glteximage2d

紋理的顯示

顯示某個紋理之前先繫結 glbindtexture(gl_texture_2d, texground);

指定繪製方法  glbegin(gl_quads);

指定紋理座標和對應頂點 gltexcoord2f(0.0f, 0.0f); glvertex3f(-8.0f, -8.0f, 0.0f);

#define windowwidth 400

#define windowheight 400

#define windowtitle "opengl 紋理測試"

#include #include #include /* 函式 grab

* 抓取視窗中的畫素

* 假設視窗寬度為 windowwidth,高度為 windowheight

*/#define bmp_header_length 54

void grab(void)

/* 函式 power_of_two

* 檢查乙個整數是否為 2 的整數次方,如果是,返回 1,否則返回 0

* 實際上只要檢視其二進位制位中有多少個,如果正好有 1 個,返回 1,否則返回 0

* 在「檢視其二進位制位中有多少個」時使用了乙個小技巧

* 使用 n &= (n-1)可以使得 n 中的減少乙個(具體原理大家可以自己思考)

*/int power_of_two(int n)

/* 函式 load_texture

* 讀取乙個 bmp 檔案作為紋理

* 如果失敗,返回 0,如果成功,返回紋理編號

*/gluint load_texture(const char* file_name)

// 根據總畫素位元組數分配記憶體

pixels = (glubyte*)malloc(total_bytes);

if( pixels == 0 )

// 讀取畫素資料

if( fread(pixels, total_bytes, 1, pfile) <= 0 )

// 在舊版本的 opengl 中

// 如果圖象的寬度和高度不是的整數次方,則需要進行縮放

// 這裡並沒有檢查 opengl 版本,出於對版本相容性的考慮,按舊版本處理

// 另外,無論是舊版本還是新版本,

// 當圖象的寬度和高度超過當前 opengl 實現所支援的最大值時,也要進行縮放

// 進行畫素縮放

gluscaleimage(gl_rgb,

width, height, gl_unsigned_byte, pixels,

new_width, new_height, gl_unsigned_byte, new_pixels);

// 釋放原來的畫素資料,把 pixels 指向新的畫素資料,並重新設定 width 和 height

free(pixels);

pixels = new_pixels;

width = new_width;

height = new_height;

} }// 分配乙個新的紋理編號

glgentextures(1, &texture_id);

if( texture_id == 0 )

// 繫結新的紋理,載入紋理並設定紋理引數

// 在繫結前,先獲得原來繫結的紋理編號,以便在最後進行恢復

glgetintegerv(gl_texture_binding_2d,(glint *) &last_texture_id);

glbindtexture(gl_texture_2d, texture_id);

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_repeat);

gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat);

gltexenvf(gl_texture_env, gl_texture_env_mode, gl_replace);

glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0,

gl_bgr_ext, gl_unsigned_byte, pixels);

glbindtexture(gl_texture_2d, last_texture_id);

// 之前為 pixels 分配的記憶體可在使用 glteximage2d 以後釋放

// 因為此時畫素資料已經被 opengl 另行儲存了乙份(可能被儲存到專門的圖形硬體中)

free(pixels);

return texture_id;

}/* 兩個紋理物件的編號

*/gluint texground;

gluint texwall;

void display(void)

int main(int argc, char* argv)

openGL 紋理使用

總結下紋理使用總體流程 1 啟用紋理 glenable gl texture 2d 2 載入紋理 3 紋理 的顯示 載入紋理 1 讀取紋理影象高寬和畫素資料到記憶體中,老版本opengl需要考慮寬度和高度不是的整數次方 2 分配乙個新的紋理編號 glgentextures 1,texture id ...

opengl紋理單元

可以這樣簡單的理解為 顯示卡中有n個紋理單元 具體數目依賴你的顯示卡能力 每個紋理單元 gl texture0 gl texture1等 都有gl texture 1d gl texture 2d等,如下 cpp view plain copy print struct textureunit te...

OPENGL紋理基礎

對於vbo道理是一樣的 1.紋理座標 在繪製一條線段時,我們設定其中乙個端點為紅色,另乙個端點為綠色,則opengl會自動計算線段中其它各畫素的顏色,如果是使用glshademode gl smooth 則最終會形成一種漸變的效果 例如線段中點,就是紅色和綠色的中間色 類似的,在繪製一條線段時,我們...