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

https://bitbucket.org/DeveloperUX/behaviortree · Java · 252 lines · 182 code · 31 blank · 39 comment · 2 complexity · 2c9c2e8d1643d4b52cf2da78754f0557 MD5 · raw file

  1. package com.example.atogl;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.nio.IntBuffer;
  6. import javax.microedition.khronos.egl.EGLConfig;
  7. import javax.microedition.khronos.opengles.GL10;
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.opengl.GLU;
  12. import android.opengl.GLSurfaceView;
  13. import android.os.Handler;
  14. public abstract class GLTutorialBase extends GLSurfaceView implements GLSurfaceView.Renderer {
  15. protected Context context;
  16. protected Handler handler;
  17. int width;
  18. int height;
  19. int fps;
  20. float box[] = new float[] {
  21. // FRONT
  22. 0.5f, -0.5f, 0.5f,
  23. 0.5f, 0.5f, 0.5f,
  24. -0.5f, -0.5f, 0.5f,
  25. -0.5f, 0.5f, 0.5f,
  26. // BACK
  27. -0.5f, -0.5f, -0.5f,
  28. -0.5f, 0.5f, -0.5f,
  29. 0.5f, -0.5f, -0.5f,
  30. 0.5f, 0.5f, -0.5f,
  31. // LEFT
  32. -0.5f, -0.5f, 0.5f,
  33. -0.5f, 0.5f, 0.5f,
  34. -0.5f, -0.5f, -0.5f,
  35. -0.5f, 0.5f, -0.5f,
  36. // RIGHT
  37. 0.5f, -0.5f, -0.5f,
  38. 0.5f, 0.5f, -0.5f,
  39. 0.5f, -0.5f, 0.5f,
  40. 0.5f, 0.5f, 0.5f,
  41. // TOP
  42. -0.5f, 0.5f, 0.5f,
  43. 0.5f, 0.5f, 0.5f,
  44. -0.5f, 0.5f, -0.5f,
  45. 0.5f, 0.5f, -0.5f,
  46. // BOTTOM
  47. -0.5f, -0.5f, 0.5f,
  48. -0.5f, -0.5f, -0.5f,
  49. 0.5f, -0.5f, 0.5f,
  50. 0.5f, -0.5f, -0.5f,
  51. };
  52. float texCoords[] = new float[] {
  53. // FRONT
  54. 0.0f, 1.0f,
  55. 0.0f, 0.0f,
  56. 1.0f, 1.0f,
  57. 1.0f, 0.0f,
  58. // BACK
  59. 1.0f, 0.0f,
  60. 1.0f, 1.0f,
  61. 0.0f, 0.0f,
  62. 0.0f, 1.0f,
  63. // LEFT
  64. 1.0f, 0.0f,
  65. 1.0f, 1.0f,
  66. 0.0f, 0.0f,
  67. 0.0f, 1.0f,
  68. // RIGHT
  69. 1.0f, 0.0f,
  70. 1.0f, 1.0f,
  71. 0.0f, 0.0f,
  72. 0.0f, 1.0f,
  73. // TOP
  74. 0.0f, 0.0f,
  75. 1.0f, 0.0f,
  76. 0.0f, 1.0f,
  77. 1.0f, 1.0f,
  78. // BOTTOM
  79. 1.0f, 0.0f,
  80. 1.0f, 1.0f,
  81. 0.0f, 0.0f,
  82. 0.0f, 1.0f
  83. };
  84. FloatBuffer cubeBuff;
  85. FloatBuffer texBuff;
  86. public void setHandler(Handler han)
  87. {
  88. handler = han;
  89. }
  90. /**
  91. * Make a direct NIO FloatBuffer from an array of floats
  92. * @param arr The array
  93. * @return The newly created FloatBuffer
  94. */
  95. protected static FloatBuffer makeFloatBuffer(float[] arr) {
  96. ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
  97. bb.order(ByteOrder.nativeOrder());
  98. FloatBuffer fb = bb.asFloatBuffer();
  99. fb.put(arr);
  100. fb.position(0);
  101. return fb;
  102. }
  103. /**
  104. * Make a direct NIO IntBuffer from an array of ints
  105. * @param arr The array
  106. * @return The newly created IntBuffer
  107. */
  108. protected static IntBuffer makeFloatBuffer(int[] arr) {
  109. ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
  110. bb.order(ByteOrder.nativeOrder());
  111. IntBuffer ib = bb.asIntBuffer();
  112. ib.put(arr);
  113. ib.position(0);
  114. return ib;
  115. }
  116. protected static ByteBuffer makeByteBuffer(Bitmap bmp) {
  117. ByteBuffer bb = ByteBuffer.allocateDirect(bmp.getHeight()*bmp.getWidth()*4);
  118. bb.order(ByteOrder.BIG_ENDIAN);
  119. IntBuffer ib = bb.asIntBuffer();
  120. for (int y = 0; y < bmp.getHeight(); y++)
  121. for (int x=0;x<bmp.getWidth();x++) {
  122. int pix = bmp.getPixel(x, bmp.getHeight()-y-1);
  123. // Convert ARGB -> RGBA
  124. byte alpha = (byte)((pix >> 24)&0xFF);
  125. byte red = (byte)((pix >> 16)&0xFF);
  126. byte green = (byte)((pix >> 8)&0xFF);
  127. byte blue = (byte)((pix)&0xFF);
  128. ib.put(((red&0xFF) << 24) |
  129. ((green&0xFF) << 16) |
  130. ((blue&0xFF) << 8) |
  131. ((alpha&0xFF)));
  132. }
  133. ib.position(0);
  134. bb.position(0);
  135. return bb;
  136. }
  137. protected int loadTexture(GL10 gl, int resource) {
  138. return loadTexture(gl, BitmapFactory.decodeResource(context.getResources(), resource));
  139. }
  140. /**
  141. * Create a texture and send it to the graphics system
  142. * @param gl The GL object
  143. * @param bmp The bitmap of the texture
  144. * @param reverseRGB Should the RGB values be reversed? (necessary workaround for loading .pngs...)
  145. * @return The newly created identifier for the texture.
  146. */
  147. protected static int loadTexture(GL10 gl, Bitmap bmp) {
  148. int[] tmp_tex = new int[1];
  149. gl.glGenTextures(1, tmp_tex, 0);
  150. int tex = tmp_tex[0];
  151. loadTexture(tex, GL10.GL_TEXTURE_2D, bmp, gl);
  152. return tex;
  153. }
  154. public void loadTexture(int texture, int type, int resource, GL10 gl) {
  155. loadTexture(texture, type, BitmapFactory.decodeResource(context.getResources(), resource), gl);
  156. }
  157. static public void loadTexture(int texture, int type, Bitmap bmp, GL10 gl) {
  158. loadTexture(texture, type, bmp.getWidth(), bmp.getHeight(), makeByteBuffer(bmp), gl);
  159. }
  160. static public void loadTexture(int texture, int type, int width, int height, ByteBuffer bb, GL10 gl) {
  161. gl.glBindTexture(type, texture);
  162. gl.glTexImage2D(type, 0, GL10.GL_RGBA, width, height, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, null);
  163. gl.glTexSubImage2D(type, 0, 0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
  164. gl.glTexParameterf(type, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  165. gl.glTexParameterf(type, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  166. }
  167. /**
  168. * Constructor
  169. */
  170. public GLTutorialBase(Context context) {
  171. this(context, -1);
  172. }
  173. /**
  174. * Constructor for animated views
  175. * @param c The View's context
  176. * @param fps The frames per second for the animation.
  177. */
  178. public GLTutorialBase(Context context, int fps) {
  179. super(context);
  180. this.context = context;
  181. this.fps = fps;
  182. cubeBuff = makeFloatBuffer(box);
  183. texBuff = makeFloatBuffer(texCoords);
  184. }
  185. public void setupCube(GL10 gl) {
  186. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
  187. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  188. gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
  189. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  190. }
  191. public void drawCube(GL10 gl) {
  192. gl.glColor4f(1.0f, 1, 1, 1.0f);
  193. gl.glNormal3f(0,0,1);
  194. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  195. gl.glNormal3f(0,0,-1);
  196. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
  197. gl.glColor4f(1, 1.0f, 1, 1.0f);
  198. gl.glNormal3f(-1,0,0);
  199. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
  200. gl.glNormal3f(1,0,0);
  201. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);
  202. gl.glColor4f(1, 1, 1.0f, 1.0f);
  203. gl.glNormal3f(0,1,0);
  204. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
  205. gl.glNormal3f(0,-1,0);
  206. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
  207. }
  208. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  209. }
  210. public void onSurfaceChanged(GL10 gl, int w, int h) {
  211. this.width = w;
  212. this.height = h;
  213. gl.glMatrixMode(GL10.GL_PROJECTION);
  214. gl.glLoadIdentity();
  215. gl.glViewport(0,0,w,h);
  216. GLU.gluOrtho2D(gl, 0, w, h, 0);
  217. //GLU.gluPerspective(gl, 45.0f, ((float)w)/h, 1f, 100f);
  218. init(gl);
  219. }
  220. protected void init(GL10 gl) {}
  221. }