ThreatIndicatorGLRenderer.java 7.0 KB

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