123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Scanner;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.opengl.GLES20;
- import android.opengl.GLSurfaceView.Renderer;
- import android.opengl.GLUtils;
- import android.opengl.Matrix;
- import android.util.Log;
- /**
- * @author Fabio Arnold
- *
- * ThreatIndicatorGLRenderer
- * This class is responsible for drawing an animation representing the current threat level.
- * Use the method setThreatLevel to set the state (0 to 3).
- */
- public class ThreatIndicatorGLRenderer implements Renderer {
- public static final int kThreatLevelNotMonitoring = 0;
- public static final int kThreatLevelNoThreat = 1;
- public static final int kThreatLevelPastThreat = 2;
- public static final int kThreatLevelPersistentThreat = 3;
-
- /**
- * Set the threat level which should be indicated
- * @param level 0 to 3 but better use the constants
- */
- public static void setThreatLevel(int level) {
- assert(level >= 0 && level <= 3);
- nextThreatLevel = level;
- Log.i("threat indicator", "next threat level = " + level);
- }
- /**
- * Match the background color of the view holding this renderer
- * @param color 32 bit integer encoding the color
- */
- public static void setBackgroundColor(int color) {
- backgroundColor[0] = (float)Color.red(color) / 255.0f;
- backgroundColor[1] = (float)Color.green(color) / 255.0f;
- backgroundColor[2] = (float)Color.blue(color) / 255.0f;
- }
- private static float[] backgroundColor = new float[3];
-
- public static AssetManager assets = null; // needs to be set by the Activity using this class
-
- private int width;
- private int height;
- private float aspectRatio;
-
- // OpenGL data
- private int program;
- private float [] modelview;
- private float [] projection;
- private float [] mvp;
-
- private AnimatedMesh androidMesh = null;
- private AnimatedMesh beeMesh = null;
- private int androidTexture;
- private int beeTexture;
-
- // threat state
- private static int nextThreatLevel = kThreatLevelNoThreat;
-
- private int currentThreatLevel = kThreatLevelNoThreat;
- private int targetThreatLevel = kThreatLevelNoThreat;
- private float threatLevelTransition = 1.0f; // 1.0 means transition is complete
- private long startTimeMillis; // for animation
- public ThreatIndicatorGLRenderer() {
- startTimeMillis = System.currentTimeMillis();
- }
-
- /**
- * Initialization will be called after GL context is created and is current
- */
- public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
- GLES20.glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], 1.0f);
- GLES20.glEnable(GLES20.GL_DEPTH_TEST);
- GLES20.glEnable(GLES20.GL_CULL_FACE);
- GLES20.glEnable(GLES20.GL_TEXTURE_2D);
-
- try {
- InputStream is = assets.open("meshes/android.amh");
- androidMesh = new AnimatedMesh(is);
- androidMesh.startAction("greet", false, false);
- } catch (IOException e) {
- Log.e("gl", "Couldn't open android mesh");
- }
- androidTexture = loadTexture("textures/android-tex.png");
- try {
- InputStream is = assets.open("meshes/bee.amh");
- beeMesh = new AnimatedMesh(is);
- beeMesh.startAction("bee_armatureAct", true, false);
- } catch (IOException e) {
- Log.e("gl", "Couldn't open bee mesh");
- }
- beeTexture = loadTexture("textures/bee-tex.png");
-
- modelview = new float[16];
- Matrix.setIdentityM(modelview, 0);
- projection = new float[16];
- mvp = new float[16];
-
- // default shader
- String vertexSource = "attribute vec3 position; void main() {gl_Position = vec4(position, 1.0);}";
- String fragmentSource = "void main() {gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);}";
- try {
- vertexSource = inputStreamToString(assets.open("shaders/skinned.vert"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {fragmentSource = inputStreamToString(assets.open("shaders/skinned.frag"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- program = loadProgram(vertexSource, fragmentSource);
- }
-
- /**
- * Tries to render at 30 Hz (see bottom part)
- */
- public void onDrawFrame(GL10 arg0) {
- long timeMillis = System.currentTimeMillis() - startTimeMillis;
- double animTime = 0.001 * (double)timeMillis; // in seconds
-
- // threat level state machine
- if (targetThreatLevel != currentThreatLevel) {
- boolean blocked = false; // block until current action is completed
- if (threatLevelTransition == 0.0f) {
- if (androidMesh.isActionDone()) {
- switch (targetThreatLevel) {
- case kThreatLevelNotMonitoring:
- androidMesh.startAction("sleep", false, false);
- break;
- case kThreatLevelNoThreat:
- androidMesh.startAction("happy", true, false);
- break;
- case kThreatLevelPastThreat:
- androidMesh.startAction("fear", true, false);
- break;
- case kThreatLevelPersistentThreat:
- androidMesh.startAction("panic", true, false);
- break;
- }
- } else blocked = true;
- }
-
- if (!blocked) {
- threatLevelTransition += 0.016f;
- if (threatLevelTransition >= 1.0f) {
- currentThreatLevel = targetThreatLevel;
- threatLevelTransition = 1.0f;
- }
- }
- } else {
- if (nextThreatLevel != targetThreatLevel) {
- targetThreatLevel = nextThreatLevel;
- threatLevelTransition = 0.0f;
-
- // HACK!!! reverses the sleep animation to create smooth transition into other states
- if (currentThreatLevel == kThreatLevelNotMonitoring)
- androidMesh.startAction("sleep", false, true);
- }
- }
-
- androidMesh.tick(); // animate android
-
- // OpenGL drawing
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
-
- GLES20.glUseProgram(program);
- int colorUniformLoc = GLES20.glGetUniformLocation(program, "color");
- int textureUniformLoc = GLES20.glGetUniformLocation(program, "texture");
- int mvpUniformLoc = GLES20.glGetUniformLocation(program, "mvp");
- // animate color
- final float[] whiteColor = {1.0f, 1.0f, 1.0f, 1.0f};
- final float[] greyColor = {0.5f, 0.5f, 0.5f, 1.0f};
- final float[] redColor = {2.0f, 0.4f, 0.2f, 1.0f};
- final float[] yellowColor = {1.1f * 255.0f / 166.0f, 1.2f * 255.0f / 200.0f, 0.0f, 1.0f};
-
- float[] currentColor = whiteColor;
- float blink = 0.5f + 0.5f * (float)Math.sin(12.0 * animTime);
- switch (currentThreatLevel) {
- case kThreatLevelNotMonitoring:
- currentColor = greyColor;
- break;
- case kThreatLevelPastThreat:
- currentColor = mixColor(blink, whiteColor, yellowColor);
- break;
- case kThreatLevelPersistentThreat:
- currentColor = mixColor(blink, whiteColor, redColor);
- break;
- }
- if (targetThreatLevel != currentThreatLevel) {
- float[] targetColor = whiteColor;
- switch (targetThreatLevel) {
- case kThreatLevelNotMonitoring:
- targetColor = greyColor;
- break;
- case kThreatLevelPastThreat:
- targetColor = mixColor(blink, whiteColor, yellowColor);
- break;
- case kThreatLevelPersistentThreat:
- targetColor = mixColor(blink, whiteColor, redColor);
- break;
- }
- currentColor = mixColor(threatLevelTransition, currentColor, targetColor);
- }
- GLES20.glUniform4fv(colorUniformLoc, 1, currentColor, 0);
- GLES20.glUniform1i(textureUniformLoc, 0);
-
- // animate camera
- Matrix.setIdentityM(modelview, 0);
- if (currentThreatLevel == kThreatLevelPersistentThreat || targetThreatLevel == kThreatLevelPersistentThreat) {
- float delta = 1.0f;
- if (threatLevelTransition < 0.4f) { // animate only during the first 40% of the transition
- delta = threatLevelTransition / 0.4f;
- delta = -2.0f * delta * delta * delta + 3.0f * delta * delta; // ease in/out
- }
- if (targetThreatLevel != kThreatLevelPersistentThreat)
- delta = 1.0f - delta;
- Matrix.translateM(modelview, 0, 0.0f, -0.6f - 0.2f * delta, -1.6f - 0.4f * delta); // 0.0f, -0.8f, -2.0f
- Matrix.rotateM(modelview, 0, -85.0f + 5.0f * delta, 1.0f, 0.0f, 0.0f); // -80.0f
-
- } else {
- Matrix.translateM(modelview, 0, 0.0f, -0.6f, -1.6f);
- Matrix.rotateM(modelview, 0, -85.0f, 1.0f, 0.0f, 0.0f);
- }
- Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0);
- GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0);
-
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, androidTexture);
- androidMesh.draw(program);
-
- // restore color
- GLES20.glUniform4fv(colorUniformLoc, 1, whiteColor, 0);
- if (currentThreatLevel == kThreatLevelPersistentThreat || targetThreatLevel == kThreatLevelPersistentThreat) {
- // draw a bee rotating around the android
-
- float fadeIn = threatLevelTransition;
- if (targetThreatLevel != kThreatLevelPersistentThreat) fadeIn = 1.0f - fadeIn; // fade out
- float beePositionZ = 2.0f * (1.0f - fadeIn) * (1.0f - fadeIn); // animate the bee going in/out
-
- final float beeSize = 0.2f;
- Matrix.rotateM(modelview, 0, (float)((-240.0 * animTime) % 360.0), 0.0f, 0.0f, 1.0f); // rotate around android
- Matrix.translateM(modelview, 0, 0.6f, 0.0f, 0.7f + 0.1f * (float)Math.sin(12.0 * animTime) + beePositionZ); // go up and down
- Matrix.rotateM(modelview, 0, 20.0f * (float)Math.cos(12.0 * animTime), 1.0f, 0.0f, 0.0f); // rock back and forth
- Matrix.scaleM(modelview, 0, beeSize, beeSize, beeSize);
- Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0);
- GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0);
-
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, beeTexture);
- beeMesh.tick();
- beeMesh.draw(program);
- }
-
- long deltaTime = System.currentTimeMillis() - startTimeMillis - timeMillis; // time for one frame
- if (deltaTime < 33) {
- try {
- Thread.sleep(33 - deltaTime); // sleep remaining time for 30 Hz
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * Informs renderer of changed surface dimensions
- */
- public void onSurfaceChanged(GL10 arg0, int w, int h) {
- width = w;
- height = h;
- aspectRatio = (float)w / (float)h;
- Matrix.orthoM(projection, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
- float near = 0.1f;
- float fov = 2.0f;
- Matrix.frustumM(projection, 0, near * -aspectRatio, near * aspectRatio, -near, near, fov * near, 100.0f);
- GLES20.glViewport(0, 0, width, height);
- }
-
- // some private helper methods
-
- private float[] mixColor(float alpha, float[] color1, float[] color2) {
- float[] color3 = new float[4];
- color3[0] = (1.0f - alpha) * color1[0] + alpha * color2[0];
- color3[1] = (1.0f - alpha) * color1[1] + alpha * color2[1];
- color3[2] = (1.0f - alpha) * color1[2] + alpha * color2[2];
- color3[3] = (1.0f - alpha) * color1[3] + alpha * color2[3];
- return color3;
- }
-
- private int loadTexture(String filePath) {
- Bitmap bitmap = null;
- try {
- bitmap = BitmapFactory.decodeStream(assets.open(filePath));
- } catch (IOException e) {
- e.printStackTrace();
- return 0;
- }
-
- int[] names = {0};
- GLES20.glGenTextures(1, names, 0);
- int tex = names[0];
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
- GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
- bitmap.recycle(); // memory is now gpu -> free it
-
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
-
- return tex;
- }
-
- // see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
- private static String inputStreamToString(InputStream is) {
- Scanner scanner = new Scanner(is);
- Scanner s = scanner.useDelimiter("\\A");
- String result = s.hasNext() ? s.next() : "";
- scanner.close();
- return result;
- }
-
- private static int loadShader(int type, String source) {
- int shader = GLES20.glCreateShader(type);
- GLES20.glShaderSource(shader, source);
- GLES20.glCompileShader(shader);
- Log.i("gl", GLES20.glGetShaderInfoLog(shader));
- return shader;
- }
-
- private static int loadProgram(String vertexSource, String fragmentSource) {
- int program = GLES20.glCreateProgram();
- GLES20.glAttachShader(program, loadShader(GLES20.GL_VERTEX_SHADER, vertexSource));
- GLES20.glAttachShader(program, loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource));
- GLES20.glLinkProgram(program);
- return program;
- }
- }
|