ThreatIndicatorGLRenderer.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Scanner;
  5. import javax.microedition.khronos.egl.EGLConfig;
  6. import javax.microedition.khronos.opengles.GL10;
  7. import android.content.res.AssetManager;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.opengl.GLES20;
  11. import android.opengl.GLSurfaceView.Renderer;
  12. import android.opengl.GLUtils;
  13. import android.opengl.Matrix;
  14. import android.util.Log;
  15. public class ThreatIndicatorGLRenderer implements Renderer {
  16. private static int threatLevel = 0;
  17. public static void setThreatLevel(int level) {
  18. threatLevel = level;
  19. }
  20. public static AssetManager assets;
  21. private int width;
  22. private int height;
  23. private float aspectRatio;
  24. private int program;
  25. private float [] modelview;
  26. private float [] projection;
  27. private float [] mvp;
  28. private AnimatedMesh androidMesh = null;
  29. private AnimatedMesh beeMesh = null;
  30. private int androidTexture;
  31. private int beeTexture;
  32. public ThreatIndicatorGLRenderer() {}
  33. public int loadTexture(String filePath) {
  34. Bitmap bitmap = null;
  35. try {
  36. bitmap = BitmapFactory.decodeStream(assets.open(filePath));
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. return 0;
  40. }
  41. int[] names = {0};
  42. GLES20.glGenTextures(1, names, 0);
  43. int tex = names[0];
  44. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  45. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  46. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  47. GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  48. bitmap.recycle(); // memory is now gpu -> free it
  49. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  50. return tex;
  51. }
  52. public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
  53. GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  54. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  55. GLES20.glEnable(GLES20.GL_CULL_FACE);
  56. GLES20.glEnable(GLES20.GL_TEXTURE_2D);
  57. try {
  58. InputStream is = assets.open("meshes/android.amh");
  59. androidMesh = new AnimatedMesh(is);
  60. } catch (IOException e) {
  61. Log.e("gl", "Couldn't open android mesh");
  62. }
  63. androidTexture = loadTexture("textures/android-tex.png");
  64. try {
  65. InputStream is = assets.open("meshes/bee.amh");
  66. beeMesh = new AnimatedMesh(is);
  67. } catch (IOException e) {
  68. Log.e("gl", "Couldn't open bee mesh");
  69. }
  70. beeTexture = loadTexture("textures/bee-tex.png");
  71. modelview = new float[16];
  72. Matrix.setIdentityM(modelview, 0);
  73. projection = new float[16];
  74. mvp = new float[16];
  75. String vertexSource = "attribute vec3 position; void main() {gl_Position = vec4(position, 1.0);}";
  76. String fragmentSource = "void main() {gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);}";
  77. try {
  78. vertexSource = inputStreamToString(assets.open("shaders/skinned.vert"));
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. try {fragmentSource = inputStreamToString(assets.open("shaders/skinned.frag"));
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. program = loadProgram(vertexSource, fragmentSource);
  87. }
  88. static float beeAnimation = 0.0f;
  89. private float[] mixColor(float alpha, float[] color1, float[] color2) {
  90. float[] color3 = new float[4];
  91. color3[0] = (1.0f - alpha) * color1[0] + alpha * color2[0];
  92. color3[1] = (1.0f - alpha) * color1[1] + alpha * color2[1];
  93. color3[2] = (1.0f - alpha) * color1[2] + alpha * color2[2];
  94. color3[3] = (1.0f - alpha) * color1[3] + alpha * color2[3];
  95. return color3;
  96. }
  97. public void onDrawFrame(GL10 arg0) {
  98. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  99. GLES20.glUseProgram(program);
  100. int colorUniformLoc = GLES20.glGetUniformLocation(program, "color");
  101. int textureUniformLoc = GLES20.glGetUniformLocation(program, "texture");
  102. int mvpUniformLoc = GLES20.glGetUniformLocation(program, "mvp");
  103. //final float[] androidColor = {165.0f / 255.0f, 202.0f / 255.0f, 57.0f / 255.0f, 1.0f}; // ok
  104. final float[] whiteColor = {1.0f, 1.0f, 1.0f, 1.0f};
  105. final float[] redColor = {2.0f, 0.4f, 0.2f, 1.0f};
  106. final float[] yellowColor = {1.0f, 1.0f, 0.0f, 1.0f};
  107. float[] color = mixColor(0.5f + 0.5f * (float)Math.sin(0.2f * beeAnimation), whiteColor, redColor);
  108. GLES20.glUniform4fv(colorUniformLoc, 1, color, 0);
  109. Log.i("gl", color[0]+" "+color[1]+" "+color[2]+" "+color[3]);
  110. GLES20.glUniform1i(textureUniformLoc, 0);
  111. Matrix.setIdentityM(modelview, 0);
  112. Matrix.translateM(modelview, 0, 0.0f, -0.5f, -2.0f);
  113. Matrix.rotateM(modelview, 0, -80.0f, 1.0f, 0.0f, 0.0f);
  114. Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0);
  115. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0);
  116. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, androidTexture);
  117. androidMesh.tick();
  118. androidMesh.draw(program);
  119. GLES20.glUniform4fv(colorUniformLoc, 1, whiteColor, 0);
  120. beeAnimation += 1.0f;
  121. float beeSize = 0.2f;
  122. Matrix.rotateM(modelview, 0, -4.0f * beeAnimation, 0.0f, 0.0f, 1.0f);
  123. Matrix.translateM(modelview, 0, 0.6f, 0.0f, 0.7f + 0.1f * (float)Math.sin(0.2f * beeAnimation));
  124. Matrix.rotateM(modelview, 0, 20.0f * (float)Math.cos(0.2f * beeAnimation), 1.0f, 0.0f, 0.0f);
  125. Matrix.scaleM(modelview, 0, beeSize, beeSize, beeSize);
  126. Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0);
  127. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0);
  128. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, beeTexture);
  129. beeMesh.tick();
  130. beeMesh.draw(program);
  131. }
  132. public void onSurfaceChanged(GL10 arg0, int w, int h) {
  133. width = w;
  134. height = h;
  135. aspectRatio = (float)w / (float)h;
  136. Matrix.orthoM(projection, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
  137. float near = 0.1f;
  138. float fov = 2.0f;
  139. Matrix.frustumM(projection, 0, near * -aspectRatio, near * aspectRatio, -near, near, fov * near, 100.0f);
  140. GLES20.glViewport(0, 0, width, height);
  141. }
  142. // see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
  143. private static String inputStreamToString(InputStream is) {
  144. Scanner scanner = new Scanner(is);
  145. Scanner s = scanner.useDelimiter("\\A");
  146. String result = s.hasNext() ? s.next() : "";
  147. scanner.close();
  148. return result;
  149. }
  150. public static int loadShader(int type, String source) {
  151. int shader = GLES20.glCreateShader(type);
  152. GLES20.glShaderSource(shader, source);
  153. GLES20.glCompileShader(shader);
  154. Log.i("gl", GLES20.glGetShaderInfoLog(shader));
  155. return shader;
  156. }
  157. public static int loadProgram(String vertexSource, String fragmentSource) {
  158. int program = GLES20.glCreateProgram();
  159. GLES20.glAttachShader(program, loadShader(GLES20.GL_VERTEX_SHADER, vertexSource));
  160. GLES20.glAttachShader(program, loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource));
  161. GLES20.glLinkProgram(program);
  162. return program;
  163. }
  164. }