/Research/Example projects/android2.1_TestOGL/src/com/example/atogl/GLTutorialFour.java

https://bitbucket.org/DeveloperUX/behaviortree · Java · 89 lines · 54 code · 22 blank · 13 comment · 0 complexity · 95e677f179105b29ac712304578e778f MD5 · raw file

  1. package com.example.atogl;
  2. import java.nio.FloatBuffer;
  3. import javax.microedition.khronos.opengles.GL10;
  4. import android.content.Context;
  5. import android.opengl.GLU;
  6. /**
  7. * http://www.zeuscmd.com/tutorials/opengles/10-Transformations.php
  8. * @author bburns
  9. */
  10. public class GLTutorialFour extends GLTutorialBase {
  11. float[] triangle = new float[] { -0.25f, -0.25f, 0.0f,
  12. 0.25f, -0.25f, 0.0f,
  13. -0.25f, 0.25f, 0.0f };
  14. float[] square = new float[] { -0.25f, -0.25f, 0.0f,
  15. 0.25f, -0.25f, 0.0f,
  16. -0.25f, 0.25f, 0.0f,
  17. 0.25f, 0.25f, 0.0f };
  18. float[] colors = new float[] { 1, 0, 0, 1,
  19. 0, 1, 0, 1,
  20. 0, 0, 1, 1 };
  21. FloatBuffer squareBuff;
  22. FloatBuffer triangleBuff;
  23. FloatBuffer colorBuff;
  24. float xrot = 0.0f;
  25. float yrot = 0.0f;
  26. public GLTutorialFour(Context c) {
  27. super(c, 20);
  28. squareBuff = makeFloatBuffer(square);
  29. triangleBuff = makeFloatBuffer(triangle);
  30. colorBuff = makeFloatBuffer(colors);
  31. }
  32. protected void init(GL10 gl) {
  33. // Setup background color
  34. gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  35. // Smooth shading
  36. gl.glShadeModel(GL10.GL_SMOOTH);
  37. }
  38. public void onDrawFrame(GL10 gl) {
  39. xrot += 1f;
  40. yrot += 1f;
  41. // Clear screen
  42. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  43. // Scene view matrix
  44. gl.glMatrixMode(GL10.GL_MODELVIEW);
  45. gl.glLoadIdentity();
  46. gl.glPushMatrix();
  47. gl.glTranslatef(-0.25f,0.0f,-2f);
  48. gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
  49. // Setup triangle data and draw it.
  50. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuff);
  51. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  52. gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
  53. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  54. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
  55. // Reset to identity
  56. gl.glPopMatrix();
  57. // Clear the color array
  58. gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
  59. // Translation to square location.
  60. gl.glTranslatef(0.25f, 0.0f, -2f);
  61. gl.glRotatef(yrot, 0, 1, 0);
  62. // Setup square data & draw it
  63. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuff);
  64. gl.glColor4f(0.25f, 0.25f, 0.75f, 1.0f);
  65. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  66. }
  67. }