ThreatIndicatorGLRenderer.java 7.1 KB

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