<涓?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>
銆€銆€棣栧厛灏辨槸鎴戜滑鐨勯《鐐癸紝杈撳叆鍚庯紝椤剁偣鐫€鑹插櫒浼氱‘瀹氶《鐐瑰ぇ姒傜殑浣嶇疆锛屽苟杩涜涓€浜涘熀鏈鐞嗐€備箣鍚庯紝鍥惧厓瑁呴厤浼氭妸 椤剁偣瑁呴厤鎴愭寚瀹氱殑鍦熷厓褰㈢姸锛岃繖閲屾垜鐢ㄦ槸闇€瑕佷笁瑙掑舰銆?鐒跺悗灏辨槸鍑犱綍鐫€鑹插櫒锛岃繖涓潃鑹插櫒杩樻病浜嗚В鏄共鍢涳紝鍙煡閬撲篃鏄竴涓彲缂栫▼鐨勶紝澶т綋灏辨槸鐢ㄤ簬鐢熸垚鍏朵粬褰㈢姸銆傜劧鍚庤緭鍑虹粰鍏夋爡鍖栭樁娈?/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>
鎴戜滑灏濊瘯娉ㄩ噴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锛?鐢讳笁瑙掑舰的主要内容,如果未能解决你的问题,请参考以下文章