Advertisement
Guest User

sphere renderer android

a guest
Jan 20th, 2013
1,816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.00 KB | None | 0 0
  1. package com.pixdip.android.glsphere;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import java.nio.ShortBuffer;
  7. import javax.microedition.khronos.egl.EGLConfig;
  8. import javax.microedition.khronos.opengles.GL10;
  9. import android.opengl.GLES20;
  10. import android.opengl.GLSurfaceView.Renderer;
  11. import android.opengl.Matrix;
  12.  
  13. public class myGLES20Renderer implements Renderer {
  14. public float xAngle;
  15. public float yAngle;
  16. private FloatBuffer cubeBuffer;
  17. private FloatBuffer colorBuffer;
  18. private ShortBuffer indexBuffer;
  19. private float[] m_fViewMatrix = new float[16];
  20. private float[] m_fProjMatrix = new float[16];
  21. private float[] m_fVPMatrix = new float[16];
  22. private float[] m_fIdentity = new float[16];
  23. private int myProgram;
  24. private int myAVertexLocation;
  25. private int myAColorLocation;
  26. private int myu_VPMatrix;
  27.  
  28. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  29. gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
  30. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  31. GLES20.glDepthFunc(GLES20.GL_LEQUAL);
  32. // GLES20.glFrontFace(GLES20.GL_CW);
  33. initShapes();
  34. int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  35. int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
  36. myProgram = GLES20.glCreateProgram();
  37. GLES20.glAttachShader(myProgram, vertexShader);
  38. GLES20.glAttachShader(myProgram, fragmentShader);
  39. GLES20.glLinkProgram(myProgram);
  40. }
  41. public void onSurfaceChanged(GL10 gl, int width, int height) {
  42. float ratio = (float) width / height;
  43. GLES20.glViewport(0, 0, width, height);
  44. Matrix.setLookAtM(m_fViewMatrix, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0);
  45. Matrix.frustumM(m_fProjMatrix, 0, -ratio, ratio, -1, 1, 2, 5);
  46. myAVertexLocation = GLES20.glGetAttribLocation(myProgram, "vPosition");
  47. myAColorLocation = GLES20.glGetAttribLocation(myProgram, "vColor");
  48. myu_VPMatrix = GLES20.glGetUniformLocation(myProgram, "u_VPMatrix");
  49. xAngle = 0;
  50. yAngle = 0;
  51. }
  52. public void onDrawFrame(GL10 gl) {
  53. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  54. GLES20.glUseProgram(myProgram);
  55. GLES20.glVertexAttribPointer(myAVertexLocation, 3, GLES20.GL_FLOAT, false, 12, cubeBuffer);
  56. GLES20.glEnableVertexAttribArray(myAVertexLocation);
  57. GLES20.glVertexAttribPointer(myAColorLocation, 4, GLES20.GL_FLOAT, false, 16, colorBuffer);
  58. GLES20.glEnableVertexAttribArray(myAColorLocation);
  59. Matrix.setIdentityM(m_fIdentity, 0);
  60. Matrix.rotateM(m_fIdentity, 0, -xAngle, 0, 1, 0);
  61. Matrix.rotateM(m_fIdentity, 0, -yAngle, 1, 0, 0);
  62. Matrix.multiplyMM(m_fVPMatrix, 0, m_fViewMatrix, 0, m_fIdentity, 0);
  63. Matrix.multiplyMM(m_fVPMatrix, 0, m_fProjMatrix, 0, m_fVPMatrix, 0);
  64. GLES20.glUniformMatrix4fv(myu_VPMatrix, 1, false, m_fVPMatrix, 0);
  65. GLES20.glDrawElements(GLES20.GL_TRIANGLES, 192*3, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
  66. }
  67. private void initShapes() {
  68. //cube co-ordinated
  69. float[] cube = {0.000000f,-0.878472f,-0.000000f,
  70. 0.350694f,-0.819444f,-0.000000f,
  71. -0.000000f,-0.819444f,0.350694f,
  72. -0.350694f,-0.819444f,-0.000000f,
  73. 0.000000f,-0.819444f,-0.350695f,
  74. 0.326389f,-0.763889f,-0.326389f,
  75. 0.326389f,-0.763889f,0.326389f,
  76. -0.326389f,-0.763889f,0.326389f,
  77. -0.326389f,-0.763889f,-0.326389f,
  78. -0.000000f,0.878472f,0.000000f,
  79. 0.000000f,0.819444f,-0.350694f,
  80. -0.350695f,0.819444f,0.000000f,
  81. -0.000000f,0.819444f,0.350695f,
  82. 0.350694f,0.819444f,0.000000f,
  83. 0.326389f,0.763889f,-0.326389f,
  84. -0.326389f,0.763889f,-0.326389f,
  85. -0.326389f,0.763889f,0.326389f,
  86. 0.326389f,0.763889f,0.326389f,
  87. 0.878472f,0.000000f,0.000000f,
  88. 0.819445f,0.000000f,-0.350694f,
  89. 0.819444f,0.350694f,0.000000f,
  90. 0.819444f,-0.000000f,0.350695f,
  91. 0.819444f,-0.350694f,0.000000f,
  92. 0.763889f,-0.326389f,-0.326389f,
  93. 0.763889f,0.326389f,-0.326389f,
  94. 0.763889f,0.326389f,0.326389f,
  95. 0.763889f,-0.326389f,0.326389f,
  96. -0.000000f,-0.000000f,0.878472f,
  97. 0.350694f,0.000000f,0.819445f,
  98. -0.000000f,0.350694f,0.819444f,
  99. -0.350695f,0.000000f,0.819444f,
  100. -0.000000f,-0.350694f,0.819444f,
  101. 0.326389f,-0.326389f,0.763889f,
  102. 0.326389f,0.326389f,0.763889f,
  103. -0.326389f,0.326389f,0.763889f,
  104. -0.326389f,-0.326389f,0.763889f,
  105. -0.878472f,0.000000f,-0.000000f,
  106. -0.819445f,0.000000f,0.350694f,
  107. -0.819444f,0.350694f,-0.000000f,
  108. -0.819444f,0.000000f,-0.350695f,
  109. -0.819444f,-0.350694f,-0.000000f,
  110. -0.763889f,-0.326389f,0.326389f,
  111. -0.763889f,0.326389f,0.326389f,
  112. -0.763889f,0.326389f,-0.326389f,
  113. -0.763889f,-0.326389f,-0.326389f,
  114. 0.000000f,0.000000f,-0.878472f,
  115. 0.350695f,0.000000f,-0.819444f,
  116. 0.000000f,-0.350694f,-0.819444f,
  117. -0.350694f,0.000000f,-0.819444f,
  118. 0.000000f,0.350694f,-0.819444f,
  119. 0.326389f,0.326389f,-0.763889f,
  120. 0.326389f,-0.326389f,-0.763889f,
  121. -0.326389f,-0.326389f,-0.763889f,
  122. -0.326389f,0.326389f,-0.763889f,
  123. 0.598958f,-0.598958f,-0.302083f,
  124. 0.643229f,-0.643229f,0.000000f,
  125. 0.598958f,-0.598958f,0.302083f,
  126. 0.302083f,-0.598958f,-0.598958f,
  127. 0.000000f,-0.643229f,-0.643229f,
  128. -0.302083f,-0.598958f,-0.598958f,
  129. 0.598958f,-0.302083f,-0.598958f,
  130. 0.643229f,0.000000f,-0.643229f,
  131. 0.598958f,0.302083f,-0.598958f,
  132. 0.302083f,-0.598958f,0.598958f,
  133. -0.000000f,-0.643229f,0.643229f,
  134. -0.302083f,-0.598958f,0.598958f,
  135. 0.598958f,-0.302083f,0.598958f,
  136. 0.643229f,0.000000f,0.643229f,
  137. 0.598958f,0.302083f,0.598959f,
  138. -0.598958f,-0.598958f,0.302083f,
  139. -0.643229f,-0.643229f,-0.000000f,
  140. -0.598958f,-0.598958f,-0.302084f,
  141. -0.598958f,-0.302083f,0.598958f,
  142. -0.643229f,0.000000f,0.643229f,
  143. -0.598959f,0.302083f,0.598958f,
  144. -0.598958f,-0.302083f,-0.598958f,
  145. -0.643229f,0.000000f,-0.643229f,
  146. -0.598958f,0.302083f,-0.598958f,
  147. 0.598958f,0.598958f,-0.302083f,
  148. 0.643229f,0.643229f,0.000000f,
  149. 0.598958f,0.598958f,0.302084f,
  150. 0.302083f,0.598958f,-0.598958f,
  151. 0.000000f,0.643229f,-0.643229f,
  152. -0.302083f,0.598958f,-0.598958f,
  153. 0.302083f,0.598958f,0.598959f,
  154. -0.000000f,0.643229f,0.643229f,
  155. -0.302084f,0.598958f,0.598958f,
  156. -0.598958f,0.598958f,0.302083f,
  157. -0.643229f,0.643229f,-0.000000f,
  158. -0.598958f,0.598958f,-0.302083f,
  159. 0.509259f,-0.509259f,-0.509259f,
  160. 0.509259f,-0.509259f,0.509259f,
  161. -0.509259f,-0.509259f,0.509259f,
  162. -0.509259f,-0.509259f,-0.509259f,
  163. 0.509259f,0.509259f,-0.509259f,
  164. 0.509259f,0.509259f,0.509260f,
  165. -0.509260f,0.509259f,0.509259f,
  166. -0.509259f,0.509259f,-0.509259f};
  167. //colors for each vertices
  168. float[] colors = {
  169. 1,0,0,1, 1,0,0,1,
  170. 1,0,0,1, 1,0,0,1,
  171. 1,0,0,1, 1,0,0,1,
  172. 1,0,0,1, 1,0,0,1,
  173. 1,0,0,1, 1,0,0,1,
  174. 1,0,0,1, 1,0,0,1,
  175. 1,0,0,1, 1,0,0,1,
  176. 1,0,0,1, 1,0,0,1,
  177. 1,0,0,1, 1,0,0,1,
  178. 1,0,0,1, 1,0,0,1,
  179. 1,0,0,1, 1,0,0,1,
  180. 1,0,0,1, 1,0,0,1,
  181. 1,0,0,1, 1,0,0,1,
  182. 1,0,0,1, 1,0,0,1,
  183. 1,0,0,1, 1,0,0,1,
  184. 1,0,0,1, 1,0,0,1,
  185. 1,0,0,1, 1,0,0,1,
  186. 1,0,0,1, 1,0,0,1,
  187. 1,0,0,1, 1,0,0,1,
  188. 1,0,0,1, 1,0,0,1,
  189. 1,0,0,1, 1,0,0,1,
  190. 1,0,0,1, 1,0,0,1,
  191. 1,0,0,1, 1,0,0,1,
  192. 1,0,0,1, 1,0,0,1,
  193. 1,0,0,1, 1,0,0,1,
  194. 1,0,0,1, 1,0,0,1,
  195. 1,0,0,1, 1,0,0,1,
  196. 1,0,0,1, 1,0,0,1,
  197. 1,0,0,1, 1,0,0,1,
  198. 1,0,0,1, 1,0,0,1,
  199. 1,0,0,1, 1,0,0,1,
  200. 1,0,0,1, 1,0,0,1,
  201. 1,0,0,1, 1,0,0,1,
  202. 1,0,0,1, 1,0,0,1,
  203. 1,0,0,1, 1,0,0,1,
  204. 1,0,0,1, 1,0,0,1,
  205. 1,0,0,1, 1,0,0,1,
  206. 1,0,0,1, 1,0,0,1,
  207. 1,0,0,1, 1,0,0,1,
  208. 1,0,0,1, 1,0,0,1,
  209. 1,0,0,1, 1,0,0,1,
  210. 1,0,0,1, 1,0,0,1,
  211. 1,0,0,1, 1,0,0,1,
  212. 1,0,0,1, 1,0,0,1,
  213. 1,0,0,1, 1,0,0,1,
  214. 1,0,0,1, 1,0,0,1,
  215. 1,0,0,1, 1,0,0,1,
  216. 1,0,0,1, 1,0,0,1,
  217. 1,0,0,1, 1,0,0,1,
  218. 1,0,0,1, 1,0,0,1
  219. };
  220. //indeces for drawing the vertices in specified order
  221. short[] indeces = {0,4,5,
  222. 1,5,54,
  223. 4,58,57,
  224. 5,57,90,
  225. 0,1,6,
  226. 2,6,63,
  227. 1,55,56,
  228. 6,56,91,
  229. 0,2,7,
  230. 3,7,69,
  231. 2,64,65,
  232. 7,65,92,
  233. 0,3,8,
  234. 4,8,59,
  235. 3,70,71,
  236. 8,71,93,
  237. 9,13,14,
  238. 10,14,81,
  239. 13,79,78,
  240. 14,78,94,
  241. 9,10,15,
  242. 11,15,89,
  243. 10,82,83,
  244. 15,83,97,
  245. 9,11,16,
  246. 12,16,86,
  247. 11,88,87,
  248. 16,87,96,
  249. 9,12,17,
  250. 13,17,80,
  251. 12,85,84,
  252. 17,84,95,
  253. 18,22,23,
  254. 19,23,60,
  255. 22,55,54,
  256. 23,54,90,
  257. 18,19,24,
  258. 20,24,78,
  259. 19,61,62,
  260. 24,62,94,
  261. 18,20,25,
  262. 21,25,68,
  263. 20,79,80,
  264. 25,80,95,
  265. 18,21,26,
  266. 22,26,56,
  267. 21,67,66,
  268. 26,66,91,
  269. 27,31,32,
  270. 28,32,66,
  271. 31,64,63,
  272. 32,63,91,
  273. 27,28,33,
  274. 29,33,84,
  275. 28,67,68,
  276. 33,68,95,
  277. 27,29,34,
  278. 30,34,74,
  279. 29,85,86,
  280. 34,86,96,
  281. 27,30,35,
  282. 31,35,65,
  283. 30,73,72,
  284. 35,72,92,
  285. 36,40,41,
  286. 37,41,72,
  287. 40,70,69,
  288. 41,69,92,
  289. 36,37,42,
  290. 38,42,87,
  291. 37,73,74,
  292. 42,74,96,
  293. 36,38,43,
  294. 39,43,77,
  295. 38,88,89,
  296. 43,89,97,
  297. 36,39,44,
  298. 40,44,71,
  299. 39,76,75,
  300. 44,75,93,
  301. 45,49,50,
  302. 46,50,62,
  303. 49,82,81,
  304. 50,81,94,
  305. 45,46,51,
  306. 47,51,57,
  307. 46,61,60,
  308. 51,60,90,
  309. 45,47,52,
  310. 48,52,75,
  311. 47,58,59,
  312. 52,59,93,
  313. 45,48,53,
  314. 49,53,83,
  315. 48,76,77,
  316. 53,77,97,
  317. 1,0,5,
  318. 55,1,54,
  319. 5,4,57,
  320. 54,5,90,
  321. 2,0,6,
  322. 64,2,63,
  323. 6,1,56,
  324. 63,6,91,
  325. 3,0,7,
  326. 70,3,69,
  327. 7,2,65,
  328. 69,7,92,
  329. 4,0,8,
  330. 58,4,59,
  331. 8,3,71,
  332. 59,8,93,
  333. 10,9,14,
  334. 82,10,81,
  335. 14,13,78,
  336. 81,14,94,
  337. 11,9,15,
  338. 88,11,89,
  339. 15,10,83,
  340. 89,15,97,
  341. 12,9,16,
  342. 85,12,86,
  343. 16,11,87,
  344. 86,16,96,
  345. 13,9,17,
  346. 79,13,80,
  347. 17,12,84,
  348. 80,17,95,
  349. 19,18,23,
  350. 61,19,60,
  351. 23,22,54,
  352. 60,23,90,
  353. 20,18,24,
  354. 79,20,78,
  355. 24,19,62,
  356. 78,24,94,
  357. 21,18,25,
  358. 67,21,68,
  359. 25,20,80,
  360. 68,25,95,
  361. 22,18,26,
  362. 55,22,56,
  363. 26,21,66,
  364. 56,26,91,
  365. 28,27,32,
  366. 67,28,66,
  367. 32,31,63,
  368. 66,32,91,
  369. 29,27,33,
  370. 85,29,84,
  371. 33,28,68,
  372. 84,33,95,
  373. 30,27,34,
  374. 73,30,74,
  375. 34,29,86,
  376. 74,34,96,
  377. 31,27,35,
  378. 64,31,65,
  379. 35,30,72,
  380. 65,35,92,
  381. 37,36,41,
  382. 73,37,72,
  383. 41,40,69,
  384. 72,41,92,
  385. 38,36,42,
  386. 88,38,87,
  387. 42,37,74,
  388. 87,42,96,
  389. 39,36,43,
  390. 76,39,77,
  391. 43,38,89,
  392. 77,43,97,
  393. 40,36,44,
  394. 70,40,71,
  395. 44,39,75,
  396. 71,44,93,
  397. 46,45,50,
  398. 61,46,62,
  399. 50,49,81,
  400. 62,50,94,
  401. 47,45,51,
  402. 58,47,57,
  403. 51,46,60,
  404. 57,51,90,
  405. 48,45,52,
  406. 76,48,75,
  407. 52,47,59,
  408. 75,52,93,
  409. 49,45,53,
  410. 82,49,83,
  411. 53,48,77,
  412. 83,53,97
  413. };
  414. cubeBuffer = null;
  415. colorBuffer = null;
  416. indexBuffer = null;
  417.  
  418. cubeBuffer = ByteBuffer.allocateDirect(cube.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  419. cubeBuffer.put(cube);
  420. cubeBuffer.position(0);
  421.  
  422. colorBuffer = ByteBuffer.allocateDirect(colors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  423. colorBuffer.put(cube);
  424. colorBuffer.position(0);
  425.  
  426. indexBuffer = ByteBuffer.allocateDirect(indeces.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
  427. indexBuffer.put(indeces);
  428. indexBuffer.position(0);
  429. }
  430. private int loadShader(int type, String source) {
  431. int shader = GLES20.glCreateShader(type);
  432. GLES20.glShaderSource(shader, source);
  433. GLES20.glCompileShader(shader);
  434. return shader;
  435. }
  436. private String vertexShaderCode =
  437. "attribute vec4 vPosition; \n"
  438. + "attribute vec4 vColor; \n"
  439. + "varying vec4 vFinalColor; \n"
  440. + "uniform mat4 u_VPMatrix; \n"
  441. + "void main() \n"
  442. + "{ \n"
  443. + " gl_Position = u_VPMatrix * vPosition; \n"
  444. + " vFinalColor = vColor; \n"
  445. + "} \n";
  446. private String fragmentShaderCode =
  447. "#ifdef GL_FRAGMENT_PRECISION_HIGH \n"
  448. + "precision highp float; \n"
  449. + "#else \n"
  450. + "precision mediump float; \n"
  451. + "#endif \n"
  452. + "varying vec4 vFinalColor; \n"
  453. + "void main() \n"
  454. + "{ \n"
  455. + " gl_FragColor = vFinalColor; \n"
  456. + "} \n";
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement