ThreatIndicatorGLRenderer.java 6.9 KB

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