<涓?gt; 鍒濇帰opengl锛?鐢讳笁瑙掑舰

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了<涓?gt; 鍒濇帰opengl锛?鐢讳笁瑙掑舰相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/sign' title='sign'>sign   鍚﹀垯   鍍忕礌   signed   const   涔嬮棿   array   ade   澶氬皯   

銆€銆€鐜鎼缓濂斤紝鎴戜滑褰撶劧灏辨槸寮€濮嬪啓浠g爜锛岃繖閲屽氨寰楀厛浜嗚Вopengl鐨勪竴浜涘伐浣滄祦绋嬨€傞鍏堟垜浠緱浜嗚В涓変釜鍗曡瘝锛?/p>

銆€銆€銆€銆€椤剁偣鏁扮粍瀵硅薄锛圴AO锛?nbsp;

銆€銆€銆€銆€椤剁偣缂撳啿瀵硅薄锛圴BO锛?

銆€銆€銆€銆€绱㈠紩缂撳啿瀵硅薄锛圗BO锛?/strong>

銆€  姣旇緝绠€鍗曠殑姒傛嫭涓嬭繖鑺傜殑宸ヤ綔娴佺▼銆?/p>

銆€銆€銆€銆€1.瀹氫箟濂戒笁瑙掑舰鐨勪笁涓《鐐?/strong>

銆€銆€銆€銆€2.缁戝畾VBO锛屾妸涓夎褰㈡暟鎹紶鍏ヨ繘鍘?/strong>

銆€銆€銆€銆€3.鍋氶《鐐圭殑閾炬帴锛岃瀹氬睘鎬?/strong>

銆€銆€銆€銆€4.缂栧啓鏈€绠€鍗曠殑椤剁偣鐫€鑹插櫒鍜岀墖娈电潃鑹插櫒

銆€銆€銆€銆€5.缂栬瘧鐫€鑹插櫒锛屽苟鍔犺浇閾炬帴鍒伴」鐩噷

銆€銆€銆€銆€6.寮€鍚惊鐜覆鏌撴ā寮?/strong>

銆€

1.瀹氫箟椤剁偣

銆€銆€棣栧厛鎴戜滑瀹氫箟瑕佸厛瀹氫箟濂戒笁瑙掑舰鐨勪笁涓偣锛岀敱浜庢槸浼犲叆缁檕pengl浣跨敤锛宱pengl鍐呴儴鏄娇鐢?strong>鏍囧噯鍖栬澶囧潗鏍?/strong>锛屼篃灏辨槸-1鍒?鐨勫潗鏍囦綋绯汇€傚洜涓簅pengl骞朵笉浼氫篃涓嶆兂鐭ラ亾浣犺澶囨樉绀哄灏戝昂瀵稿暐鐨勶紝瀹冨彧瑕佸仛濂借嚜宸辩殑璁$畻锛屽悗闈㈡樉绀轰細閫氳繃杞崲涓哄睆骞曠┖闂村潗鏍囨潵鏄剧ず锛岃繖涓€姝ュ湪瑙嗗彛鍙樺寲鐨勬椂鍊欏畬鎴愩€?/p>

float vertices[] = {
        -0.5f, -0.5f, 0,
        0.5f, -0.5f, 0,
        0.0f, 0.5f, 0
};

杩欓噷灏辨湁3涓《鐐癸紝姣?涓竴缁勶紝鍒嗗埆鏄痻锛寉锛寊锛岃繖涓変釜鍧愭爣搴旇鏉ヨ澶у閮藉崄鍒嗙啛鎮夌殑鍚с€傚畾涔夊ソ鍚庯紝鎴戜滑闇€瑕佹妸浠栦紶鍏ュ埌鍥惧舰娓叉煋绠$嚎鐨勭涓€涓鐞嗛樁娈碉細椤剁偣鐫€鑹插櫒銆俹pengl閫氳繃VBO鏉ョ鐞嗚繖涓唴瀛橈紝瀹冧細鍦╣pu鍐呭瓨涓瓨鍌ㄦ墦閲忛《鐐广€備娇鐢ㄥ畠鎴戜滑灏卞彲浠ユ妸鏁版嵁涓€娆℃€т紶杈撳埌鏄惧崱閲岋紝鑰屼笉鐢ㄤ竴鐐逛竴鐐瑰彂锛屽姞蹇鍙栨晥鐜囥€?/p>

 

2.鍒涘缓骞剁粦瀹歏BO鏁版嵁

unsigned int VBO;
glGenBuffers(1, &VBO);
//鍒涘缓骞朵笖澹扮О涓€涓獀bo

opengl閲岀殑瀵硅薄閮芥槸閫氳繃涓€涓猧d鏉ョ储寮曪紝杩欐湁鐐瑰儚褰撴椂鐪嬬殑涓€涓彨Genius-X鐨勬父鎴忔鏋讹紝姝ゆ鏋跺氨鏄€氳繃id鏉ョ‘瀹氫竴涓父鎴忓璞″疄浣擄紝鍏朵粬娓告垙缁勫缓閫氳繃姝d渚濋檮鍒板璞′笂銆?/p>

姝ゅ锛宱pengl鎿嶄綔瀵硅薄閮芥槸闇€瑕佹垜浠厛瀵瑰叾缁戝畾鍒拌瀹氱殑缂撳啿瀵硅薄绫诲瀷涓婃潵杩涜鎿嶄綔锛岃€屼笉鐩存帴瀵规垜浠储寮曠洰琛ㄨ繘琛屾搷浣滐紝鎵€浠ユ垜浠渶瑕佹妸椤剁偣缁戝畾鍒皏bo鏃讹紝闇€瑕佽繖涔堝啓

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,GL_STATIC_DRAW);

鎶妚bo缁戝畾鍒癎L_ARRAY_BUFFER缂撳啿绫诲瀷涓婏紝opengl杩樻湁寰堝鍏朵粬鐨勫璞$被鍨嬶紝鍚庨潰鍙兘浼氭參鎱㈣閬撱€傞€氳繃缁戝畾鍚庯紝鎴戜滑瀵笹L_ARRAY_BUFFER鎿嶄綔灏卞彲浠ヤ簡锛岀劧鍚巊lBufferData灏辨槸杩涜鏁版嵁鐨勪紶鍏ワ紝鍥涗釜鍙傛暟鏄紦鍐插璞★紝椤剁偣鏁扮粍澶у皬锛岄《鐐规暟缁勶紝绠$悊椤剁偣鏂瑰紡銆傞《鐐圭鐞嗘柟寮忔湁3绉嶏細

  • GL_STATIC_DRAW 锛氭暟鎹笉浼氭垨鍑犱箮涓嶄細鏀瑰彉銆?/li>
  • GL_DYNAMIC_DRAW锛氭暟鎹細琚敼鍙樺緢澶氥€?/li>
  • GL_STREAM_DRAW 锛氭暟鎹瘡娆$粯鍒舵椂閮戒細鏀瑰彉銆?/li>

鎴戜滑鏆傛椂涓嶄細鏀规暟鎹紝鎵€浠ョ敤static灏卞彲浠ヤ簡銆?/p>

 

銆€銆€铏界劧杩欓噷宸茬粡杈惧埌浜嗘暟鎹粦瀹氱殑鏁堟灉锛屼絾濡傛灉鎴戜滑闇€瑕佺粯鍒跺涓墿鍝佺殑鏃跺€欙紝鎴戜滑闇€瑕侀噸澶嶅娆¢噸澶嶉《鐐瑰睘鎬х殑璁惧畾锛屾墍浠ュ嚭鐜颁簡VAO杩欎釜涓滆タ鏉ュ仛浼樺寲銆俈AO鍙互鐪嬭捣鏉ユ槸VBO鏁扮粍锛屽彧鏄叾杩橀檮鍔犱簡鍏朵粬鍙傛暟鍜屽睘鎬с€備娇鐢ㄥ畠涔熷緢绠€鍗曪紝鍜孷BO绫讳技銆?/p>

unsigned int g_VAO;
glGenVertexArrays(1, &g_VAO);
glBindVertexArray(g_VAO);

涔熸槸鍏堝垱寤篿d锛屽啀缁戝畾鍒伴《鐐规暟缁勪笂銆傚叾鍚庯紝鎴戜滑鐓у父鍋氬悗闈㈢殑VBO鎿嶄綔鍜岄《鐐瑰睘鎬ч摼鎺ュ嵆鍙紝vbo浼氳嚜鍔ㄥ瓨鍏ュ埌vao涓紝椤剁偣灞炴€ч摼鎺ュ彧闇€瑕佹搷浣滀竴娆″嵆鍙鎵€鏈塿bo鐢熸晥銆?/p>

 

3.椤剁偣灞炴€ч摼鎺?/strong>

銆€銆€浼犲叆浜嗛《鐐规暟鎹悗锛屾垜浠鍛婄煡opengl鎬庝箞瑙f瀽杩欎簺鏁版嵁锛屽叾鏈韩鏄笉鐭ラ亾濡備綍澶勭悊杩欎釜鏁扮粍鐨?/p>

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer鐨勫弬鏁颁竴涓竴涓В鏋?/p>

銆€銆€绗竴涓弬鏁版槸鎸囧畾鎴戜滑鍦ㄩ《鐐圭潃鑹插櫒涓娇鐢ㄧ殑浣嶇疆灞炴€э紝涓€鑸粯璁や负0锛屾垜浠厛浼犲叆0锛岃繖涓睘鎬т細鍜岄《鐐圭潃鑹插櫒閲屾暟鎹鍙栭厤鍚堜娇鐢?/p>

銆€銆€绗簩涓弬鏁版槸鎸囧畾椤剁偣灞炴€х殑澶у皬锛屾垜浠敱3涓€肩粍鎴?/p>

銆€銆€绗笁涓槸椤剁偣鐨勬暟鎹被鍨嬶紝鎴戜滑鐢ㄤ簡GL_FLOAT鐨勬诞鐐圭被鍨?/p>

銆€銆€绗洓涓槸鏄惁甯屾湜鏁版嵁琚爣鍑嗗寲锛屽鏋滄垜浠缃负GL_TRUE灏变細琚槧灏勫埌-1 鍜?1涔嬮棿锛屾垜浠涓篺alse锛屽洜涓烘垜浠殑鏁版嵁宸茬粡鏍囧噯鍖栦簡

銆€銆€绗簲涓暟鎹槸姝ラ暱锛屼篃灏辨槸璇诲彇涓嬩竴涓《鐐归渶瑕佽烦澶氬皯鍐呭瓨锛屾垜浠繖閲屾槸3*sizeof(float)澶у皬锛屽洜涓烘垜浠《鐐圭揣瀵嗘帓鍒楃殑

銆€銆€绗叚涓槸涓€涓己鍒惰浆鎹紝琛ㄧず鏁版嵁鍦ㄧ紦鍐蹭腑浣嶇疆鍋忕Щ閲忥紝鍥犱负鏁版嵁鍦ㄦ暟缁勫紑澶达紝鎵€浠ユ槸0

鑰実lEnableVertexAttribArray鍒欐槸鍚敤椤剁偣灞炴€э紝鍥犱负杩欎釜灞炴€ч粯璁ゆ槸绂佹鐨勶紝0璺熶笂闈㈣鐨勭涓€涓弬鏁版剰涔変竴鏍枫€?/p>

 

4.缂栧啓绠€鍗曠殑椤剁偣鐫€鑹插櫒

銆€銆€褰撴垜浠緭鍏ュソ浜嗛《鐐癸紝鎴戜滑灏卞紑濮嬬紪鍐欐覆鏌撹繃绋嬩腑鍙紪绋嬬殑涓や釜閮ㄥ垎锛岄《鐐圭潃鑹插櫒鍜岀墖娈电潃鑹插櫒銆傚湪姝や箣鍓嶆垜浠厛浜嗚В涓嬪浘褰㈡覆鏌撶绾跨殑鍑犱釜闃舵銆?/p>

鎶€鏈垎浜浘鐗? src=

銆€銆€棣栧厛灏辨槸鎴戜滑鐨勯《鐐癸紝杈撳叆鍚庯紝椤剁偣鐫€鑹插櫒浼氱‘瀹氶《鐐瑰ぇ姒傜殑浣嶇疆锛屽苟杩涜涓€浜涘熀鏈鐞嗐€備箣鍚庯紝鍥惧厓瑁呴厤浼氭妸 椤剁偣瑁呴厤鎴愭寚瀹氱殑鍦熷厓褰㈢姸锛岃繖閲屾垜鐢ㄦ槸闇€瑕佷笁瑙掑舰銆?鐒跺悗灏辨槸鍑犱綍鐫€鑹插櫒锛岃繖涓潃鑹插櫒杩樻病浜嗚В鏄共鍢涳紝鍙煡閬撲篃鏄竴涓彲缂栫▼鐨勶紝澶т綋灏辨槸鐢ㄤ簬鐢熸垚鍏朵粬褰㈢姸銆傜劧鍚庤緭鍑虹粰鍏夋爡鍖栭樁娈?/strong>锛岃繖涓樁娈靛彧鏄妸鍥惧厓鏄犲皠鎴愬睆骞曚笂瀵瑰簲鐨勫儚绱犱綅锛屼緵鐗囨鐫€鑹插櫒浣跨敤鐨勭墖娈点€傜劧鍚庝細杩涜涓€娆¤鍓紝鎶婅鍥剧湅涓嶅埌鐨勫尯鍩熺粰瑁佹帀锛屾彁鍗囨墽琛屾晥鐜囥€?strong>涓€涓墖娈碉紝灏辨槸娓叉煋涓€涓儚绱犳墍闇€瑕佺殑鎵€鏈夋暟鎹?/strong>銆?strong>鐗囨鐫€鑹插櫒鐩殑灏辨槸璁$畻涓€涓儚绱犵殑鏈€缁堥鑹?/strong>锛岃繖閲岀敤浜庡鐞嗗厜鐓у晩锛岄槾褰卞晩浠€涔堢殑銆?/p>

銆€銆€鏈€鍚庣殑鏁版嵁浼氳繘琛屽绉嶆祴璇曪紝杩欎釜鍦╟ocos2dx璇讳功绗旇閲屾湁鎻愯繃锛岃繖閲屾殏涓斾笉鍐欎簡銆傦紙鍙傝€?杩欓噷锛?/p>

 

1.椤剁偣鐫€鑹插櫒

銆€銆€浜嗚В鍒拌繖閲岋紝鎴戜滑鐜板湪寮€濮嬬紪鍐欓《鐐圭潃鑹插櫒锛屼粬灏辨槸澶勭悊浣嶇疆涓轰富鐨勪竴涓紪绋嬪潡銆?/p>

#version 330 core //璁惧畾浣跨敤鐨刼pengl鐗堟湰锛屾垜浠敤3.3鐨勬牳蹇冩ā寮?/span>
layout (location = 0) in vec3 aPos; //瀹氫綅杈撳叆椤剁偣鐨勪綅缃紝杩欓噷鍜屼笂闈㈣鐨刧lVertexAttribPointer绗竴涓弬鏁板搴旓紝鏄竴涓笁鍒嗛噺鐨勫璞?/span>

void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); //鍘熷皝涓嶅姩浼犲嚭鍘伙紝绗洓涓槸榻愭鍒嗛噺锛岀敤浜?d浣跨敤锛?d鎴戜滑浣跨敤1.0
}

杩欐槸鏈€绠€鍗曠殑涓€涓《鐐圭潃鑹插櫒锛屾妸椤剁偣鍘熷皝涓嶅姩浼犲嚭鍘伙紝鎴戜滑鍙互鍋氬悇绉嶅彉鎹㈣揪鍒版垜浠兂瑕佺殑鏁堟灉锛屼緥濡備綅绉伙紝缂╂斁銆傝繖娈典唬鐮佸彲浠ュ紑涓€涓猼xt鏂囨湰鏉ュ啓锛屽啓濂藉悗鏀惧埌椤圭洰宸ョ▼涓€涓柊鐩綍閲岋紝渚嬪shader鐩綍鍗冲彲锛岃繖閲屾垜鏀瑰悕涓簐er.vs锛岃€屼笉鐢ㄦ斁鍏ユ簮鐮佺洰褰曢噷锛屽惁鍒欓」鐩紪璇戞椂鍊欎細鎶ラ敊锛屾垜灏辨槸韪╀簡杩欎釜鍧戙€?/p>

 

銆€鍐欏ソ鍚庯紝鎴戜滑闇€瑕佺紪璇戣繖涓潃鑹插櫒銆?/p>

// 缂栬瘧椤剁偣鐫€鑹插櫒
unsigned int vertexShader;
string vertexShaderSource = readFile("ver.vs"); //璇诲彇鐫€鑹插櫒浠g爜瀛楃涓?
const char* verCode = vertexShaderSource.c_str();
vertexShader = glCreateShader(GL_VERTEX_SHADER); //鍒涘缓椤剁偣鐫€鑹插櫒锛屽垱寤虹墖娈靛垯鐢℅L_FRAGMENT_SHADER
glShaderSource(vertexShader, 1, &verCode, NULL); //缁戝畾鐫€鑹插櫒婧愮爜锛?浠h〃鍙湁涓€娈垫簮鐮侊紝鍙紶鍏ュ娈碉紝绗笁涓氨鏄簮鐮佸湴鍧€
glCompileShader(vertexShader); //缂栬瘧婧愮爜

濡傛灉鎯崇煡閬撴槸鍚︾紪璇戞垚鍔燂紝鍙互鐢╣lCompileShader鏉ユ煡鐪?/p>

int  success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); //infoLog閲屽氨鏈夊搴旂殑閿欒鏃ュ織

 

2.鐗囨鐫€鑹插櫒

 銆€缂栧啓鐗囨鐫€鑹插櫒

#version 330 core
out vec4 FragColor; //杈撳嚭鐨勭墖娈靛彉閲?/span>

void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); //璁惧畾浜嗘煇涓鑹?/span>
} 
//缁戝畾鐗囨鐫€鑹插櫒
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

 

3.鐫€鑹插櫒绋嬪簭

銆€銆€鍐欏畬2涓潃鑹插櫒鍚庯紝鎴戜滑闇€瑕佹妸浠栬繛鎺ュ悎骞跺湪涓€璧凤紝渚涚粰绯荤粺浣跨敤銆?/p>

unsigned int g_ShaderID;
g_ShaderID = glCreateProgram(); //鍒涘缓鐫€鑹插櫒绋嬪簭
glAttachShader(g_ShaderID, vertexShader); //缁戝畾椤剁偣鐫€鑹插櫒
glAttachShader(g_ShaderID, fragmentShader); //缁戝畾鐗囨鐫€鑹插櫒
glLinkProgram(g_ShaderID); //閾炬帴绋嬪簭
//閾炬帴鍚庢垜浠彲浠ュ垹闄?涓潃鑹插櫒浜?/span>
glDeleteShader(vertexShader); 
glDeleteShader(fragmentShader);
//涔嬪悗鎴戜滑鍙互閫氳繃glUseProgram(g_ShaderID)鏉ヤ娇鐢ㄨ繖涓▼搴?/span>

 

寮€鍚惊鐜覆鏌撴ā寮?/strong>

銆€銆€鍒拌繖閲岋紝鎴戜滑绂绘覆鏌撳嚭涓夎褰㈣繕宸竴姝ワ紝灏辨槸璋冪敤鐢诲浘鍑芥暟

glUseProgram(g_ShaderID); //浣跨敤杩欎釜鐫€鑹插櫒绋嬪簭
glDrawArrays(GL_TRIANGLES, 0, 3); //鐢诲浘

glDrawArrays绗竴涓弬鏁版槸鎴戜滑闇€瑕佺敾鐨勫浘褰紝鎴戜滑璁惧畾涓轰笁瑙掑舰锛屾垜浼拌杩欓噷鏄粰鍥惧舰瑁呴厤浣跨敤鐨勩€傜浜屼釜鍙傛暟鏄《鐐规暟缁勮捣濮嬬储寮曪紝杩欓噷濉?锛岀涓変釜鏄粯鍒跺灏戜釜椤剁偣锛岃繖閲屾垜浠槸3涓紝姝ゆ椂涓€杩愯锛屼笁瑙掑舰鍑烘潵浜嗐€?/p>

鎶€鏈垎浜浘鐗? src=鎴戜滑灏濊瘯娉ㄩ噴glUseProgram锛屽彲浠ュ彂鐜板浘褰㈠彉鐧斤紝鍙浠栨湁榛樿鐨勭潃鑹插櫒鍦ㄨ繍琛屻€?/p>

 

EBO鐨勪娇鐢?/h1>

銆€銆€褰撴垜浠兂瑕佺敾2涓笁瑙掑舰鐨勬椂鍊欙紝鎴戜滑鍙兘闇€瑕佷紶鍏?涓偣锛屼絾濡傛灉椤剁偣鏈夐噸澶嶇殑鏃跺€欙紝灏变細鍑虹幇鍐呭瓨鐨勬氮璐癸紝鑰?d妯″瀷涓氨鏄€氳繃寰堝寰皬鐨勪笁瑙掑舰杩炴帴鑰屾垚锛屾墍浠ュ嚭鐜颁簡EBO銆傜畝鍗曟潵璇达紝ebo灏辨槸瀵归《鐐规暟鎹繘琛屼竴涓储寮?/p>

float vertices[] = {
    0.5f, 0.5f, 0,
    0.5f, -0.5f, 0,
    -0.5f, -0.5f, 0,
    -0.5f, 0.5f, 0.0f
};
unsigned int indices[] = {
    0, 1, 3,
    1, 2, 3
};

鍙互鐪嬪埌鎴戜滑2涓笁瑙掑舰锛屾槸鐢╲ertices鐨?锛?锛?涓変釜鐐规瀯鎴愶紝1锛?锛?鏋勬垚鍙︿竴涓笁瑙掑舰銆?/p>

glGenBuffers(1, &g_EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

鍙互鐪嬪嚭鍜寁bo鍔犺浇宸笉澶氾紝鍙槸浠栫粦瀹氬埌GL_ELEMENT_ARRAY_BUFFER缂撳啿鍖恒€?/p>

鐒跺悗鎴戜滑閫氳繃鏇挎崲鐢诲浘鍑芥暟锛屽嵆鍙疄鐜扮敾鍥?/p>

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glDrawElements灏辨槸浣跨敤绱㈠紩鏉ョ敾鍥撅紝绗竴涓弬鏁版寚瀹氬浘褰紝绗簩涓槸闇€瑕侀《鐐逛釜鏁帮紝绗笁涓槸绱㈠紩鏁版嵁绫诲瀷锛岀鍥涗釜鏄痚bo鏁扮粍涓殑璧峰鍋忕Щ閲忥紝鐜板湪灏卞ぇ姒傝繃浜嗕竴閬嶇畝鍗曠殑娓叉煋杩囩▼銆?/p>

 

以上是关于<涓?gt; 鍒濇帰opengl锛?鐢讳笁瑙掑舰的主要内容,如果未能解决你的问题,请参考以下文章

鍑芥暟寮忕紪绋婩P 鍒濇帰

銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 03锛氱敾涓€涓笁瑙掑舰锛堝悜閲忓弶涔樼畻娉?& 閲嶅績鍧愭爣绠楁硶锛?/a>

JDK婧愮爜鍒嗘瀽鍒濇鏁寸悊

2020鍒濇璁″垝

鏍戝舰DP鍒濇-鐪熸爲1662