Advertisement
NEGI_RUS

OpenGL ES 2.0 - Cube

Apr 13th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. // init:
  2. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  3.  
  4. Mesh cube = new Mesh(false, 8, 36,
  5.     new VertexAttribute(VertexAttribute.POSITION, 3, ShaderProgram.POSITION_ATTRIBUTE),
  6.     new VertexAttribute(VertexAttribute.COLOR_PACKED, 4, ShaderProgram.COLOR_ATTRIBUTE)
  7. );
  8.  
  9. float x = -100;
  10. float y = -50;
  11. float z = 250;
  12.  
  13. float w = 50;
  14. float h = 50;
  15. float l = 50;
  16.  
  17. float r = Color.RED.toFloatBits();
  18. float g = Color.GREEN.toFloatBits();
  19. float b = Color.BLUE.toFloatBits();
  20.  
  21. float[] vertices = {
  22.     -w / 2f + x, y, l / 2f + z,  // 0
  23.     r,
  24.     -w / 2f + x, h + y, l / 2f + z, // 1
  25.     r,
  26.     w / 2f + x, h + y, l / 2f + z, // 2
  27.     b,
  28.     w / 2f + x, y, l / 2f + z, // 3
  29.     b,
  30.  
  31.     w / 2f + x, y, -l / 2f + z, // 4
  32.     g,
  33.     w / 2f + x, y + h, -l / 2f + z, // 5
  34.     g,
  35.     -w / 2f + x, y + h, -l / 2f + z, // 6
  36.     r,
  37.     -w / 2f + x, y, -l / 2f + z, // 7
  38.     r
  39. };
  40.  
  41. short[] indices = {
  42.     0, 1, 2, 2, 3, 0,
  43.     3, 2, 5, 5, 4, 3,
  44.     4, 5, 6, 6, 7, 4,
  45.     7, 6, 1, 1, 0, 7,
  46.     0, 3, 4, 4, 7, 0,
  47.     1, 6, 5, 5, 2, 1
  48. };
  49.  
  50. cube.setIndices(indices);
  51. cube.setVertices(vertices);
  52.  
  53. // render:
  54. GLES20.glClearColor(0f, 0f, 0f, 1f);
  55. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  56.  
  57. shaderProgram.begin();
  58. cube.render(shaderProgram, GLES20.GL_TRIANGLES, 0, 36);
  59. shaderProgram.end();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement