ThreatIndicatorGLRenderer.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /**
  17. * @author Fabio Arnold
  18. *
  19. * ThreatIndicatorGLRenderer
  20. * This class is responsible for drawing an animation representing the current threat level.
  21. * Use the method setThreatLevel to set the state (0 to 3).
  22. */
  23. public class ThreatIndicatorGLRenderer implements Renderer {
  24. public enum ThreatLevel {
  25. NOT_MONITORING,
  26. NO_THREAT,
  27. PAST_THREAT,
  28. LIVE_THREAT
  29. }
  30. /**
  31. * Set the threat level which should be indicated
  32. * @param threatLevel
  33. */
  34. public static void setThreatLevel(ThreatLevel threatLevel) {
  35. mNextThreatLevel = threatLevel;
  36. }
  37. /**
  38. * Match the background color of the view holding this renderer
  39. * @param color 32 bit integer encoding the color
  40. */
  41. public static void setBackgroundColor(int color) {
  42. mBackgroundColor[0] = (float)Color.red(color) / 255.0f;
  43. mBackgroundColor[1] = (float)Color.green(color) / 255.0f;
  44. mBackgroundColor[2] = (float)Color.blue(color) / 255.0f;
  45. }
  46. private static float[] mBackgroundColor = new float[3];
  47. public static AssetManager assets = null; // needs to be set by the Activity using this class
  48. // OpenGL data
  49. private int mProgram;
  50. private float [] mModelview;
  51. private float [] mProjection;
  52. private float [] mMVP;
  53. private AnimatedMesh androidMesh = null;
  54. private AnimatedMesh beeMesh = null;
  55. private int androidTexture;
  56. private int beeTexture;
  57. // threat state
  58. private static ThreatLevel mNextThreatLevel = ThreatLevel.NO_THREAT;
  59. private ThreatLevel mCurrentThreatLevel = ThreatLevel.NO_THREAT;
  60. private ThreatLevel mTargetThreatLevel = ThreatLevel.NO_THREAT;
  61. private float mThreatLevelTransition = 1.0f; // 1.0 means transition is complete
  62. private static boolean sPlayGreetingAnimation = true; // greet the first time
  63. private long mStartTimeMillis; // for animation
  64. public ThreatIndicatorGLRenderer() {
  65. mStartTimeMillis = System.currentTimeMillis();
  66. }
  67. /**
  68. * Initialization will be called after GL context is created and is current
  69. */
  70. public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
  71. GLES20.glClearColor(mBackgroundColor[0], mBackgroundColor[1], mBackgroundColor[2], 1.0f);
  72. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  73. GLES20.glEnable(GLES20.GL_CULL_FACE);
  74. GLES20.glEnable(GLES20.GL_TEXTURE_2D);
  75. try {
  76. InputStream is = assets.open("meshes/android.amh");
  77. androidMesh = new AnimatedMesh(is);
  78. /* play the greeting animation the first time the gets opened
  79. not each time the threatindicator gets created */
  80. if (sPlayGreetingAnimation) {
  81. androidMesh.startAction("greet", false, false);
  82. sPlayGreetingAnimation = false;
  83. } else if (mCurrentThreatLevel == ThreatLevel.NO_THREAT) { // default state
  84. androidMesh.startAction("happy", true, false); // play NO_THREAT animation
  85. }
  86. } catch (IOException e) {
  87. Log.e("gl", "Couldn't open android mesh");
  88. }
  89. androidTexture = loadTexture("textures/android-tex.png");
  90. try {
  91. InputStream is = assets.open("meshes/bee.amh");
  92. beeMesh = new AnimatedMesh(is);
  93. beeMesh.startAction("bee_armatureAct", true, false);
  94. } catch (IOException e) {
  95. Log.e("gl", "Couldn't open bee mesh");
  96. }
  97. beeTexture = loadTexture("textures/bee-tex.png");
  98. mModelview = new float[16];
  99. Matrix.setIdentityM(mModelview, 0);
  100. mProjection = new float[16];
  101. mMVP = new float[16];
  102. // default shader
  103. String vertexSource = "attribute vec3 position; void main() {gl_Position = vec4(position, 1.0);}";
  104. String fragmentSource = "void main() {gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);}";
  105. try {
  106. vertexSource = inputStreamToString(assets.open("shaders/skinned.vert"));
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. try {fragmentSource = inputStreamToString(assets.open("shaders/skinned.frag"));
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. mProgram = loadProgram(vertexSource, fragmentSource);
  115. }
  116. /**
  117. * Tries to render at 30 Hz (see bottom part)
  118. */
  119. public void onDrawFrame(GL10 arg0) {
  120. long timeMillis = System.currentTimeMillis() - mStartTimeMillis;
  121. double animTime = 0.001 * (double)timeMillis; // in seconds
  122. // threat level state machine
  123. if (mTargetThreatLevel != mCurrentThreatLevel) {
  124. boolean blocked = false; // block until current action is completed
  125. if (mThreatLevelTransition == 0.0f) {
  126. if (androidMesh.isActionDone()) {
  127. switch (mTargetThreatLevel) {
  128. case NOT_MONITORING:
  129. androidMesh.startAction("sleep", false, false);
  130. break;
  131. case NO_THREAT:
  132. androidMesh.startAction("happy", true, false);
  133. break;
  134. case PAST_THREAT:
  135. androidMesh.startAction("fear", true, false);
  136. break;
  137. case LIVE_THREAT:
  138. androidMesh.startAction("panic", true, false);
  139. break;
  140. }
  141. } else blocked = true;
  142. }
  143. if (!blocked) {
  144. mThreatLevelTransition += 0.016f;
  145. if (mThreatLevelTransition >= 1.0f) {
  146. mCurrentThreatLevel = mTargetThreatLevel;
  147. mThreatLevelTransition = 1.0f;
  148. }
  149. }
  150. } else {
  151. if (mNextThreatLevel != mTargetThreatLevel) {
  152. mTargetThreatLevel = mNextThreatLevel;
  153. mThreatLevelTransition = 0.0f;
  154. // HACK!!! reverses the sleep animation to create smooth transition into other states
  155. if (mCurrentThreatLevel == ThreatLevel.NOT_MONITORING)
  156. androidMesh.startAction("sleep", false, true);
  157. }
  158. }
  159. androidMesh.tick(); // animate android
  160. // OpenGL drawing
  161. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  162. GLES20.glUseProgram(mProgram);
  163. int colorUniformLoc = GLES20.glGetUniformLocation(mProgram, "color");
  164. int textureUniformLoc = GLES20.glGetUniformLocation(mProgram, "texture");
  165. int mvpUniformLoc = GLES20.glGetUniformLocation(mProgram, "mvp");
  166. // animate color
  167. final float[] whiteColor = {1.0f, 1.0f, 1.0f, 1.0f};
  168. final float[] greyColor = {0.5f, 0.5f, 0.5f, 1.0f};
  169. final float[] redColor = {2.0f, 0.4f, 0.2f, 1.0f};
  170. final float[] yellowColor = {1.1f * 255.0f / 166.0f, 1.2f * 255.0f / 200.0f, 0.0f, 1.0f};
  171. float[] currentColor = whiteColor;
  172. float blink = 0.5f + 0.5f * (float)Math.sin(12.0 * animTime);
  173. switch (mCurrentThreatLevel) {
  174. case NOT_MONITORING:
  175. currentColor = greyColor;
  176. break;
  177. case PAST_THREAT:
  178. currentColor = mixColor(blink, whiteColor, yellowColor);
  179. break;
  180. case LIVE_THREAT:
  181. currentColor = mixColor(blink, whiteColor, redColor);
  182. break;
  183. }
  184. if (mTargetThreatLevel != mCurrentThreatLevel) {
  185. float[] targetColor = whiteColor;
  186. switch (mTargetThreatLevel) {
  187. case NOT_MONITORING:
  188. targetColor = greyColor;
  189. break;
  190. case PAST_THREAT:
  191. targetColor = mixColor(blink, whiteColor, yellowColor);
  192. break;
  193. case LIVE_THREAT:
  194. targetColor = mixColor(blink, whiteColor, redColor);
  195. break;
  196. }
  197. currentColor = mixColor(mThreatLevelTransition, currentColor, targetColor);
  198. }
  199. GLES20.glUniform4fv(colorUniformLoc, 1, currentColor, 0);
  200. GLES20.glUniform1i(textureUniformLoc, 0);
  201. // animate camera
  202. Matrix.setIdentityM(mModelview, 0);
  203. if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
  204. float delta = 1.0f;
  205. if (mThreatLevelTransition < 0.4f) { // animate only during the first 40% of the transition
  206. delta = mThreatLevelTransition / 0.4f;
  207. delta = -2.0f * delta * delta * delta + 3.0f * delta * delta; // ease in/out
  208. }
  209. if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT)
  210. delta = 1.0f - delta;
  211. Matrix.translateM(mModelview, 0, 0.0f, -0.6f - 0.2f * delta, -1.6f - 0.4f * delta); // 0.0f, -0.8f, -2.0f
  212. Matrix.rotateM(mModelview, 0, -85.0f + 5.0f * delta, 1.0f, 0.0f, 0.0f); // -80.0f
  213. } else {
  214. Matrix.translateM(mModelview, 0, 0.0f, -0.6f, -1.6f);
  215. Matrix.rotateM(mModelview, 0, -85.0f, 1.0f, 0.0f, 0.0f);
  216. }
  217. Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
  218. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
  219. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, androidTexture);
  220. androidMesh.draw(mProgram);
  221. // restore color
  222. GLES20.glUniform4fv(colorUniformLoc, 1, whiteColor, 0);
  223. if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
  224. // draw a bee rotating around the android
  225. float fadeIn = mThreatLevelTransition;
  226. if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT) fadeIn = 1.0f - fadeIn; // fade out
  227. float beePositionZ = 2.0f * (1.0f - fadeIn) * (1.0f - fadeIn); // animate the bee going in/out
  228. final float beeSize = 0.2f;
  229. Matrix.rotateM(mModelview, 0, (float)((-240.0 * animTime) % 360.0), 0.0f, 0.0f, 1.0f); // rotate around android
  230. Matrix.translateM(mModelview, 0, 0.6f, 0.0f, 0.7f + 0.1f * (float)Math.sin(12.0 * animTime) + beePositionZ); // go up and down
  231. Matrix.rotateM(mModelview, 0, 20.0f * (float)Math.cos(12.0 * animTime), 1.0f, 0.0f, 0.0f); // rock back and forth
  232. Matrix.scaleM(mModelview, 0, beeSize, beeSize, beeSize);
  233. Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
  234. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
  235. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, beeTexture);
  236. beeMesh.tick();
  237. beeMesh.draw(mProgram);
  238. }
  239. long deltaTime = System.currentTimeMillis() - mStartTimeMillis - timeMillis; // time for one frame
  240. if (deltaTime < 33) {
  241. try {
  242. Thread.sleep(33 - deltaTime); // sleep remaining time for 30 Hz
  243. } catch (InterruptedException e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. }
  248. /**
  249. * Informs renderer of changed surface dimensions
  250. */
  251. public void onSurfaceChanged(GL10 arg0, int w, int h) {
  252. int width = w;
  253. int height = h;
  254. float aspectRatio = (float) w / (float) h;
  255. //Matrix.orthoM(mProjection, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
  256. float near = 0.1f;
  257. float fov = 2.0f;
  258. Matrix.frustumM(mProjection, 0, near * -aspectRatio, near * aspectRatio, -near, near, fov * near, 100.0f);
  259. GLES20.glViewport(0, 0, width, height);
  260. }
  261. // some private helper methods
  262. private float[] mixColor(float alpha, float[] color1, float[] color2) {
  263. float[] color3 = new float[4];
  264. color3[0] = (1.0f - alpha) * color1[0] + alpha * color2[0];
  265. color3[1] = (1.0f - alpha) * color1[1] + alpha * color2[1];
  266. color3[2] = (1.0f - alpha) * color1[2] + alpha * color2[2];
  267. color3[3] = (1.0f - alpha) * color1[3] + alpha * color2[3];
  268. return color3;
  269. }
  270. private int loadTexture(String filePath) {
  271. Bitmap bitmap;
  272. try {
  273. bitmap = BitmapFactory.decodeStream(assets.open(filePath));
  274. } catch (IOException e) {
  275. e.printStackTrace();
  276. return 0;
  277. }
  278. int[] names = {0};
  279. GLES20.glGenTextures(1, names, 0);
  280. int tex = names[0];
  281. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  282. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  283. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  284. GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  285. bitmap.recycle(); // memory is now gpu -> free it
  286. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  287. return tex;
  288. }
  289. // see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
  290. private static String inputStreamToString(InputStream is) {
  291. Scanner scanner = new Scanner(is);
  292. Scanner s = scanner.useDelimiter("\\A");
  293. String result = s.hasNext() ? s.next() : "";
  294. scanner.close();
  295. return result;
  296. }
  297. private static int loadShader(int type, String source) {
  298. int shader = GLES20.glCreateShader(type);
  299. GLES20.glShaderSource(shader, source);
  300. GLES20.glCompileShader(shader);
  301. Log.i("gl", GLES20.glGetShaderInfoLog(shader));
  302. return shader;
  303. }
  304. private static int loadProgram(String vertexSource, String fragmentSource) {
  305. int program = GLES20.glCreateProgram();
  306. GLES20.glAttachShader(program, loadShader(GLES20.GL_VERTEX_SHADER, vertexSource));
  307. GLES20.glAttachShader(program, loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource));
  308. GLES20.glLinkProgram(program);
  309. return program;
  310. }
  311. }