Advertisement
Kojima0502

openGL_VBO

Dec 19th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. #include <math.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "glew.h"
  6. #include <GLUT/glut.h>
  7.  
  8.  
  9. //------- 頂点データ-----------//
  10. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  11. GLfloat vertexAry[6][4][3] =
  12. {
  13. //1つめの面(v0-v1-v2-v3)
  14. {
  15. {0.f,0.f,0.f},{1.f,0.f,0.f},
  16. {1.f,1.f,0.f},{0.f,1.f,0.f}
  17. },
  18. //2つめの面(v1-v5-v6-v2)
  19. {
  20. {1.f,0.f,0.f},{1.f,0.f,-1.f},
  21. {1.f,1.f,-1.f},{1.f,1.f,0.f}
  22. },
  23. //3つめの面(v4-v7-v6-v5)
  24. {
  25. {0.f,0.f,-1.f},{0.f,1.f,-1.f},
  26. {1.f,1.f,-1.f},{1.f,0.f,-1.f}
  27. },
  28. //4つめの面(v4-v0-v3-v7)
  29. {
  30. {0.f,0.f,-1.f},{0.f,0.f,0.f},
  31. {0.f,1.f,0.f},{0.f,1.f,-1.f}
  32. },
  33. //5つめの面(v3-v2-v6-v7)
  34. {
  35. {0.f,1.f,0.f},{1.f,1.f,0.f},
  36. {1.f,1.f,-1.f},{0.f,1.f,-1.f}
  37. },
  38. //6つめの面(v0-v4-v5-v1)
  39. {
  40. {0.f,0.f,0.f},{0.f,0.f,-1.f},
  41. {1.f,0.f,-1.f},{1.f,0.f,0.f}
  42. }
  43. };
  44.  
  45. //法線データ
  46. GLfloat normalAry[6][4][3] =
  47. {
  48. //1つめの面(v0-v1-v2-v3)
  49. {
  50. {0.f,0.f,1.f},{0.f,0.f,1.f},
  51. {0.f,0.f,1.f},{0.f,0.f,1.f}
  52. },
  53. //2つめの面(v1-v5-v6-v2)
  54. {
  55. {1.f,0.f,0.f},{1.f,0.f,0.f},
  56. {1.f,0.f,0.f},{1.f,0.f,0.f}
  57. },
  58. //3つめの面(v4-v7-v6-v5)
  59. {
  60. {0.f,0.f,-1.f},{0.f,0.f,-1.f},
  61. {0.f,0.f,-1.f},{0.f,0.f,-1.f}
  62. },
  63. //4つめの面(v4-v0-v3-v7)
  64. {
  65. {-1.f,0.f,0.f},{-1.f,0.f,0.f},
  66. {-1.f,0.f,0.f},{-1.f,0.f,0.f}
  67. },
  68. //5つめの面(v3-v2-v6-v7)
  69. {
  70. {0.f,1.f,0.f},{0.f,1.f,0.f},
  71. {0.f,1.f,0.f},{0.f,1.f,0.f}
  72. },
  73. //6つめの面(v0-v4-v5-v1)
  74. {
  75. {0.f,-1.f,0.f},{0.f,-1.f,0.f},
  76. {0.f,-1.f,0.f},{0.f,-1.f,0.f}
  77. }
  78. };
  79.  
  80. //色データ
  81. GLfloat colorAry[6][4][3] =
  82. {
  83. //1つめの面(v0-v1-v2-v3)
  84. {
  85. {0.f,0.f,1.f},{0.f,0.f,1.f},
  86. {0.f,0.f,1.f},{0.f,0.f,1.f}
  87. },
  88. //2つめの面(v1-v5-v6-v2)
  89. {
  90. {1.f,0.f,0.f},{1.f,0.f,0.f},
  91. {1.f,0.f,0.f},{1.f,0.f,0.f}
  92. },
  93. //3つめの面(v4-v7-v6-v5)
  94. {
  95. {0.f,1.f,1.f},{0.f,1.f,1.f},
  96. {0.f,1.f,1.f},{0.f,1.f,1.f}
  97. },
  98. //4つめの面(v4-v0-v3-v7)
  99. {
  100. {1.f,1.f,0.f},{1.f,1.f,0.f},
  101. {1.f,1.f,0.f},{1.f,1.f,0.f}
  102. },
  103. //5つめの面(v3-v2-v6-v7)
  104. {
  105. {0.f,1.f,0.f},{0.f,1.f,0.f},
  106. {0.f,1.f,0.f},{0.f,1.f,0.f}
  107. },
  108. //6つめの面(v0-v4-v5-v1)
  109. {
  110. {1.f,0.f,1.f},{1.f,0.f,1.f},
  111. {1.f,0.f,1.f},{1.f,0.f,1.f}
  112. }
  113. };
  114. //インデックス
  115. GLubyte indexAry[]=
  116. {
  117. 0,1,2,3,
  118. 4,5,6,7,
  119. 8,9,10,11,
  120. 12,13,14,15,
  121. 16,17,18,19,
  122. 20,21,22,23
  123. };
  124.  
  125. // π/180の値
  126. const float PI_OVER_180 = 0.0174532925f;
  127.  
  128. //VBO用ID
  129. GLuint VboId[3];//3つ分
  130. GLuint VboIndex;//インデックス
  131.  
  132. //描画関数
  133. void drawAry(void)
  134. {
  135. GLfloat *clientPtr;//クライアント側用
  136. GLfloat tmp[3];
  137. int idloop;
  138. int loop;
  139. static float angle = 2*PI_OVER_180;
  140.  
  141. //データの場所を知らせる
  142. //座標
  143. glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  144. glVertexPointer(3, GL_FLOAT, 0, 0);
  145. //法線
  146. glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  147. glNormalPointer(GL_FLOAT, 0, 0);
  148. //色
  149. glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  150. glColorPointer(3,GL_FLOAT, 0, 0);
  151. //インデックスもバインド
  152. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VboIndex);
  153.  
  154. glEnableClientState(GL_VERTEX_ARRAY);
  155. glEnableClientState(GL_NORMAL_ARRAY);
  156. glEnableClientState(GL_COLOR_ARRAY);
  157. //描画
  158. glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,0);
  159.  
  160. glDisableClientState(GL_COLOR_ARRAY);
  161. glDisableClientState(GL_NORMAL_ARRAY);
  162. glDisableClientState(GL_VERTEX_ARRAY);
  163.  
  164. //座標と法線を回転させる
  165. for(idloop = 0; idloop < 2;++idloop)
  166. {
  167. //idloop番目のバッファオブジェクトに注目
  168. glBindBuffer(GL_ARRAY_BUFFER,VboId[idloop]);
  169.  
  170. //対応付け
  171. clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,
  172. GL_READ_WRITE);
  173. if(clientPtr != NULL)
  174. {
  175. //24頂点*3座標
  176. for(loop = 0; loop < 24*3;loop += 3) {
  177. //読み出し(READ)
  178. tmp[0] = clientPtr[loop];
  179. tmp[1] = clientPtr[loop+1];
  180. tmp[2] = clientPtr[loop+2];
  181. //書き込み(WRITE)
  182. clientPtr[loop] = cos(angle)*tmp[0]
  183. + sin(angle)*tmp[2];
  184. clientPtr[loop+1] = tmp[1];
  185. clientPtr[loop+2] = -sin(angle)*tmp[0]
  186. + cos(angle)*tmp[2];
  187. }
  188. glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
  189. }
  190. }
  191. //クライアント側に戻す
  192. glBindBuffer(GL_ARRAY_BUFFER,0);
  193. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  194. }
  195. //------- 各種コールバック----------//
  196. void display(void)
  197. {
  198. static int angle = 0;
  199. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  200. glLoadIdentity();
  201. gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  202.  
  203. glPushMatrix();
  204. glRotatef(float(angle),1.f,0.f,0.f);//x軸回転
  205. drawAry();
  206. glPopMatrix();
  207.  
  208. glutSwapBuffers();
  209. if(++angle >= 360) angle = 0;
  210. }
  211.  
  212. void reshape(int w, int h)
  213. {
  214. glViewport(0,0,w,h);
  215. glMatrixMode(GL_PROJECTION);
  216. glLoadIdentity();
  217. gluPerspective(30.0, double(w)/h, 0.1, 200.0);
  218. glMatrixMode(GL_MODELVIEW);
  219. glLoadIdentity();
  220. }
  221.  
  222. void idle(void)
  223. {
  224. glutPostRedisplay();
  225. }
  226.  
  227. void keyboard(unsigned char key, int x, int y)
  228. {
  229. switch(key)
  230. {
  231. case 'q':
  232. case 'Q':
  233. case '\033':
  234. //終了処理
  235. glDeleteBuffers(3,&VboId[0]);
  236. glDeleteBuffers(1,&VboIndex);
  237. exit(0);
  238. break;
  239. }
  240. }
  241. //------ その他初期設定-------//
  242. void otherInit(void)
  243. {
  244. glClearColor(1.f, 1.f, 1.f, 1.f);
  245. glEnable(GL_DEPTH_TEST);
  246. glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  247. glEnable(GL_COLOR_MATERIAL);
  248. glEnable(GL_LIGHT0);
  249. glEnable(GL_LIGHTING);
  250. glEnable(GL_NORMALIZE);//法線の正規化
  251. }
  252.  
  253. //---- VBOの作成----//
  254. void buildVBO(void)
  255. {
  256. glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  257.  
  258. //頂点
  259. glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  260. glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry),
  261. vertexAry,GL_DYNAMIC_DRAW);//データ変更する
  262.  
  263. //法線
  264. glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  265. glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  266. normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  267.  
  268. //色
  269. glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  270. glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  271. colorAry,GL_STREAM_DRAW);//データ変更なし
  272.  
  273. //インデックス
  274. glGenBuffers(1,&VboIndex);
  275. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  276. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  277. indexAry,GL_STATIC_DRAW);//データ変更なし
  278. }
  279. //----------- メイン関数------------//
  280. int main(int argc, char *argv[])
  281. {
  282. //GLUTの初期設定
  283. glutInit(&argc, argv);
  284. glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  285. glutInitWindowSize(640,480);
  286. glutCreateWindow("VBO Sample");
  287. //GLEWの初期設定
  288. GLenum err = glewInit();
  289. if(err != GLEW_OK)
  290. {
  291. fprintf(stderr,"Err:%s\n",
  292. glewGetErrorString(err));
  293. return -1;
  294. }
  295. //拡張チェック
  296. if(!glewIsExtensionSupported(
  297. "GL_ARB_vertex_buffer_object")){
  298. puts("you Can't use VBO");
  299. return -1;
  300. }
  301.  
  302. //コールバック
  303. glutDisplayFunc(display);
  304. glutReshapeFunc(reshape);
  305. glutIdleFunc(idle);
  306. glutKeyboardFunc(keyboard);
  307.  
  308. otherInit();
  309.  
  310. buildVBO();//VBOの作成
  311. glutMainLoop();
  312.  
  313. return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement