ThreatIndicatorGLRenderer.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package de.tudarmstadt.informatik.hostage.ui.fragment.opengl;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.FloatBuffer;
  7. import java.util.Scanner;
  8. import javax.microedition.khronos.egl.EGLConfig;
  9. import javax.microedition.khronos.opengles.GL10;
  10. import android.content.Context;
  11. import android.content.res.AssetManager;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.graphics.Color;
  15. import android.opengl.GLES20;
  16. import android.opengl.GLSurfaceView.Renderer;
  17. import android.opengl.GLUtils;
  18. import android.opengl.Matrix;
  19. import android.util.Log;
  20. import de.tudarmstadt.informatik.hostage.R;
  21. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  22. /**
  23. * @author Fabio Arnold
  24. *
  25. * ThreatIndicatorGLRenderer
  26. * This class is responsible for drawing an animation representing the current threat level.
  27. * Use the method setThreatLevel to set the state (0 to 3).
  28. */
  29. public class ThreatIndicatorGLRenderer implements Renderer {
  30. public enum ThreatLevel {
  31. NOT_MONITORING,
  32. NO_THREAT,
  33. PAST_THREAT,
  34. LIVE_THREAT
  35. }
  36. /**
  37. * Set the threat level which should be indicated
  38. * @param threatLevel
  39. */
  40. public static void setThreatLevel(ThreatLevel threatLevel) {
  41. mNextThreatLevel = threatLevel;
  42. }
  43. public static void showSpeechBubble() {
  44. bubbleWait = 3.0f; // 3 seconds;
  45. }
  46. /**
  47. * Match the background color of the view holding this renderer
  48. * @param color 32 bit integer encoding the color
  49. */
  50. public static void setBackgroundColor(int color) {
  51. mBackgroundColor[0] = (float)Color.red(color) / 255.0f;
  52. mBackgroundColor[1] = (float)Color.green(color) / 255.0f;
  53. mBackgroundColor[2] = (float)Color.blue(color) / 255.0f;
  54. }
  55. private static float[] mBackgroundColor = new float[3];
  56. // OpenGL data
  57. private int mAnimatedProgram;
  58. private int mTexturedProgram;
  59. private float [] mModelview;
  60. private float [] mProjection;
  61. private float [] mMVP;
  62. private AnimatedMesh androidMesh = null;
  63. private AnimatedMesh beeMesh = null;
  64. private int androidTexture;
  65. private int beeTexture;
  66. private int speechBubbleTexture;
  67. private int mQuadVertexBuffer;
  68. private GLFont font = null;
  69. private static float bubbleAlpha = 0.0f;
  70. private static float bubbleWait = 0.0f;
  71. // threat state
  72. private static ThreatLevel mNextThreatLevel = ThreatLevel.NO_THREAT;
  73. private ThreatLevel mCurrentThreatLevel = ThreatLevel.NO_THREAT;
  74. private ThreatLevel mTargetThreatLevel = ThreatLevel.NO_THREAT;
  75. private float mThreatLevelTransition = 1.0f; // 1.0 means transition is complete
  76. private static boolean sPlayGreetingAnimation = true; // greet the first time
  77. private long mStartTimeMillis; // for animation
  78. public ThreatIndicatorGLRenderer() {
  79. mStartTimeMillis = System.currentTimeMillis();
  80. }
  81. /**
  82. * Initialization will be called after GL context is created and is current
  83. */
  84. public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
  85. GLES20.glClearColor(mBackgroundColor[0], mBackgroundColor[1], mBackgroundColor[2], 1.0f);
  86. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  87. GLES20.glEnable(GLES20.GL_CULL_FACE);
  88. GLES20.glEnable(GLES20.GL_TEXTURE_2D);
  89. AssetManager assets = MainActivity.getInstance().getAssets();
  90. try {
  91. InputStream is = assets.open("meshes/android.amh");
  92. androidMesh = new AnimatedMesh(is);
  93. /* play the greeting animation the first time the gets opened
  94. not each time the threatindicator gets created */
  95. if (sPlayGreetingAnimation) {
  96. androidMesh.startAction("greet", false, false);
  97. sPlayGreetingAnimation = false;
  98. } else if (mCurrentThreatLevel == ThreatLevel.NO_THREAT) { // default state
  99. androidMesh.startAction("happy", true, false); // play NO_THREAT animation
  100. }
  101. } catch (IOException e) {
  102. Log.e("gl", "Couldn't open android mesh");
  103. }
  104. androidTexture = loadTexture("textures/android-tex.png");
  105. try {
  106. InputStream is = assets.open("meshes/bee.amh");
  107. beeMesh = new AnimatedMesh(is);
  108. beeMesh.startAction("bee_armatureAct", true, false);
  109. } catch (IOException e) {
  110. Log.e("gl", "Couldn't open bee mesh");
  111. }
  112. beeTexture = loadTexture("textures/bee-tex.png");
  113. speechBubbleTexture = loadTexture("textures/speech-bubble.png");
  114. int buffers[] = new int[1];
  115. GLES20.glGenBuffers(1, buffers, 0); // buffer names
  116. mQuadVertexBuffer = buffers[0];
  117. font = new GLFont("fonts/komika.png", "fonts/komika.bin");
  118. mModelview = new float[16];
  119. Matrix.setIdentityM(mModelview, 0);
  120. mProjection = new float[16];
  121. mMVP = new float[16];
  122. // default shader
  123. String vertexSource = "attribute vec3 position; void main() {gl_Position = vec4(position, 1.0);}";
  124. String fragmentSource = "void main() {gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);}";
  125. try {
  126. vertexSource = inputStreamToString(assets.open("shaders/skinned.vert"));
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. try {fragmentSource = inputStreamToString(assets.open("shaders/skinned.frag"));
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. }
  134. mAnimatedProgram = loadProgram(vertexSource, fragmentSource);
  135. mTexturedProgram = loadProgram(
  136. "uniform vec2 resolution;" // vertex
  137. + "uniform float scale;"
  138. + "attribute vec2 position;"
  139. + "attribute vec2 texCoord;"
  140. + "varying vec2 vertexTexCoord;"
  141. + "void main() {"
  142. + " vertexTexCoord = texCoord;"
  143. + " gl_Position = vec4(scale * (2.0 * (position / resolution) - 1.0), 0.0, 1.0);"
  144. + "}",
  145. "precision mediump float;"
  146. + "uniform sampler2D colormap;" // fragment
  147. + "uniform vec4 color;"
  148. + "varying vec2 vertexTexCoord;"
  149. + "void main() {"
  150. + " vec4 texel = texture2D(colormap, vertexTexCoord);"
  151. + " gl_FragColor = color * texel;"
  152. + "}");
  153. }
  154. private void updateAndroidAndBee() {
  155. // threat level state machine
  156. if (mTargetThreatLevel != mCurrentThreatLevel) {
  157. boolean blocked = false; // block until current action is completed
  158. if (mThreatLevelTransition == 0.0f) {
  159. if (androidMesh.isActionDone()) {
  160. switch (mTargetThreatLevel) {
  161. case NOT_MONITORING:
  162. androidMesh.startAction("sleep", false, false);
  163. break;
  164. case NO_THREAT:
  165. androidMesh.startAction("happy", true, false);
  166. break;
  167. case PAST_THREAT:
  168. androidMesh.startAction("fear", true, false);
  169. break;
  170. case LIVE_THREAT:
  171. androidMesh.startAction("panic", true, false);
  172. break;
  173. }
  174. } else blocked = true;
  175. }
  176. if (!blocked) {
  177. mThreatLevelTransition += 0.016f;
  178. if (mThreatLevelTransition >= 1.0f) {
  179. mCurrentThreatLevel = mTargetThreatLevel;
  180. mThreatLevelTransition = 1.0f;
  181. }
  182. }
  183. } else {
  184. if (mNextThreatLevel != mTargetThreatLevel) {
  185. mTargetThreatLevel = mNextThreatLevel;
  186. mThreatLevelTransition = 0.0f;
  187. // HACK!!! reverses the sleep animation to create smooth transition into other states
  188. if (mCurrentThreatLevel == ThreatLevel.NOT_MONITORING)
  189. androidMesh.startAction("sleep", false, true);
  190. }
  191. }
  192. androidMesh.tick(); // animate android
  193. }
  194. private void drawAndroidAndBee(double animTime) {
  195. GLES20.glUseProgram(mAnimatedProgram);
  196. int colorUniformLoc = GLES20.glGetUniformLocation(mAnimatedProgram, "color");
  197. int textureUniformLoc = GLES20.glGetUniformLocation(mAnimatedProgram, "texture");
  198. int mvpUniformLoc = GLES20.glGetUniformLocation(mAnimatedProgram, "mvp");
  199. // animate color
  200. final float[] whiteColor = {1.0f, 1.0f, 1.0f, 1.0f};
  201. final float[] greyColor = {0.5f, 0.5f, 0.5f, 1.0f};
  202. final float[] redColor = {2.0f, 0.4f, 0.2f, 1.0f};
  203. final float[] yellowColor = {1.1f * 255.0f / 166.0f, 1.2f * 255.0f / 200.0f, 0.0f, 1.0f};
  204. float[] currentColor = whiteColor;
  205. float blink = 0.5f + 0.5f * (float)Math.sin(12.0 * animTime);
  206. switch (mCurrentThreatLevel) {
  207. case NOT_MONITORING:
  208. currentColor = greyColor;
  209. break;
  210. case PAST_THREAT:
  211. currentColor = mixColor(blink, whiteColor, yellowColor);
  212. break;
  213. case LIVE_THREAT:
  214. currentColor = mixColor(blink, whiteColor, redColor);
  215. break;
  216. }
  217. if (mTargetThreatLevel != mCurrentThreatLevel) {
  218. float[] targetColor = whiteColor;
  219. switch (mTargetThreatLevel) {
  220. case NOT_MONITORING:
  221. targetColor = greyColor;
  222. break;
  223. case PAST_THREAT:
  224. targetColor = mixColor(blink, whiteColor, yellowColor);
  225. break;
  226. case LIVE_THREAT:
  227. targetColor = mixColor(blink, whiteColor, redColor);
  228. break;
  229. }
  230. currentColor = mixColor(mThreatLevelTransition, currentColor, targetColor);
  231. }
  232. GLES20.glUniform4fv(colorUniformLoc, 1, currentColor, 0);
  233. GLES20.glUniform1i(textureUniformLoc, 0);
  234. // animate camera
  235. Matrix.setIdentityM(mModelview, 0);
  236. if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
  237. float delta = 1.0f;
  238. if (mThreatLevelTransition < 0.4f) { // animate only during the first 40% of the transition
  239. delta = mThreatLevelTransition / 0.4f;
  240. delta = -2.0f * delta * delta * delta + 3.0f * delta * delta; // ease in/out
  241. }
  242. if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT)
  243. delta = 1.0f - delta;
  244. Matrix.translateM(mModelview, 0, 0.0f, -0.6f - 0.2f * delta, -1.6f - 0.4f * delta); // 0.0f, -0.8f, -2.0f
  245. Matrix.rotateM(mModelview, 0, -85.0f + 5.0f * delta, 1.0f, 0.0f, 0.0f); // -80.0f
  246. } else {
  247. Matrix.translateM(mModelview, 0, 0.0f, -0.6f, -1.6f);
  248. Matrix.rotateM(mModelview, 0, -85.0f, 1.0f, 0.0f, 0.0f);
  249. }
  250. Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
  251. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
  252. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, androidTexture);
  253. androidMesh.draw(mAnimatedProgram);
  254. // restore color
  255. GLES20.glUniform4fv(colorUniformLoc, 1, whiteColor, 0);
  256. if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
  257. // draw a bee rotating around the android
  258. float fadeIn = mThreatLevelTransition;
  259. if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT) fadeIn = 1.0f - fadeIn; // fade out
  260. float beePositionZ = 2.0f * (1.0f - fadeIn) * (1.0f - fadeIn); // animate the bee going in/out
  261. final float beeSize = 0.2f;
  262. Matrix.rotateM(mModelview, 0, (float)((-240.0 * animTime) % 360.0), 0.0f, 0.0f, 1.0f); // rotate around android
  263. Matrix.translateM(mModelview, 0, 0.6f, 0.0f, 0.7f + 0.1f * (float)Math.sin(12.0 * animTime) + beePositionZ); // go up and down
  264. Matrix.rotateM(mModelview, 0, 20.0f * (float)Math.cos(12.0 * animTime), 1.0f, 0.0f, 0.0f); // rock back and forth
  265. Matrix.scaleM(mModelview, 0, beeSize, beeSize, beeSize);
  266. Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
  267. GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
  268. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, beeTexture);
  269. beeMesh.tick();
  270. beeMesh.draw(mAnimatedProgram);
  271. }
  272. }
  273. private float easeInOut(float alpha) {
  274. return 3.0f*alpha*alpha - 2.0f*alpha*alpha*alpha;
  275. }
  276. /**
  277. * Tries to render at 30 Hz (see bottom part)
  278. */
  279. public void onDrawFrame(GL10 arg0) {
  280. Context ctx = MainActivity.getInstance();
  281. long timeMillis = System.currentTimeMillis() - mStartTimeMillis;
  282. double animTime = 0.001 * (double)timeMillis; // in seconds
  283. float dt = 1.0f/30.0f;
  284. // OpenGL drawing
  285. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  286. updateAndroidAndBee();
  287. drawAndroidAndBee(animTime);
  288. if (bubbleWait > 0.0f) {
  289. bubbleAlpha += 4.0f * dt;
  290. if (bubbleAlpha >= 1.0f) {
  291. bubbleAlpha = 1.0f;
  292. bubbleWait -= dt;
  293. }
  294. } else if (bubbleAlpha > 0.0f) {
  295. bubbleAlpha -= 4.0f * dt;
  296. if (bubbleAlpha < 0.0f) {
  297. bubbleAlpha = 0.0f;
  298. }
  299. }
  300. if (bubbleAlpha > 0.0f) {
  301. GLES20.glDisable(GLES20.GL_DEPTH_TEST);
  302. GLES20.glEnable(GLES20.GL_BLEND);
  303. GLES20.glDisable(GLES20.GL_CULL_FACE);
  304. GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  305. GLES20.glUseProgram(mTexturedProgram);
  306. int resolutionUniformLoc = GLES20.glGetUniformLocation(mTexturedProgram, "resolution");
  307. int textureUniformLoc = GLES20.glGetUniformLocation(mTexturedProgram, "colormap");
  308. int colorUniformLoc = GLES20.glGetUniformLocation(mTexturedProgram, "color");
  309. int scaleUniformLoc = GLES20.glGetUniformLocation(mTexturedProgram, "scale");
  310. GLES20.glUniform2f(resolutionUniformLoc, 1024.0f, 1024.0f);
  311. GLES20.glUniform1i(textureUniformLoc, 0);
  312. GLES20.glUniform4f(colorUniformLoc, 1.0f, 1.0f, 1.0f, 0.8f);
  313. GLES20.glUniform1f(scaleUniformLoc, easeInOut(bubbleAlpha));
  314. String message = "???";
  315. switch (mNextThreatLevel) {
  316. case NOT_MONITORING:
  317. message = ctx.getString(R.string.honeypot_not_monitoring);
  318. break;
  319. case NO_THREAT:
  320. message = ctx.getString(R.string.honeypot_no_threat);
  321. break;
  322. case PAST_THREAT:
  323. message = ctx.getString(R.string.honeypot_past_threat);
  324. break;
  325. case LIVE_THREAT:
  326. message = ctx.getString(R.string.honeypot_live_threat);
  327. break;
  328. }
  329. float textWidth = font.getTextWidth(message);
  330. float textHeight = 40.0f;
  331. float bubbleDiameter = 256.0f;
  332. float bubbleWidth = textWidth + 0.75f*bubbleDiameter;
  333. float bubbleHeight = bubbleDiameter;
  334. float y = 0.8f * 1024.0f + 32.0f * (float) Math.sin(2.0 * animTime);
  335. float x = 0.5f * 1024.0f + 16.0f * (float) Math.cos(1.0 * animTime);
  336. drawSpeechBubble(speechBubbleTexture, x - 0.5f * bubbleWidth, y - 0.5f * bubbleHeight,
  337. bubbleWidth, bubbleHeight);
  338. GLES20.glUniform4f(colorUniformLoc, 0.0f, 0.0f, 0.0f, 1.0f);
  339. font.drawText(mTexturedProgram, message, x - 0.5f * textWidth,
  340. y - 0.5f * textHeight);
  341. GLES20.glUseProgram(0);
  342. GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  343. GLES20.glDisable(GLES20.GL_BLEND);
  344. }
  345. long deltaTime = System.currentTimeMillis() - mStartTimeMillis - timeMillis; // time for one frame
  346. if (deltaTime < 33) {
  347. try {
  348. Thread.sleep(33 - deltaTime); // sleep remaining time for 30 Hz
  349. } catch (InterruptedException e) {
  350. e.printStackTrace();
  351. }
  352. }
  353. }
  354. /**
  355. * Informs renderer of changed surface dimensions
  356. */
  357. public void onSurfaceChanged(GL10 arg0, int w, int h) {
  358. int width = w;
  359. int height = h;
  360. float aspectRatio = (float) w / (float) h;
  361. //Matrix.orthoM(mProjection, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
  362. float near = 0.1f;
  363. float fov = 2.0f;
  364. Matrix.frustumM(mProjection, 0, near * -aspectRatio, near * aspectRatio, -near, near, fov * near, 100.0f);
  365. GLES20.glViewport(0, 0, width, height);
  366. }
  367. // some helper functions
  368. private void drawSpeechBubble(int texture, float x, float y, float w, float h) {
  369. float bubbleRadius = 128.0f;
  370. int vertexCount = 8;
  371. int vertexSize = (2+2)*4; // size in bytes
  372. FloatBuffer buffer = ByteBuffer.allocateDirect(vertexCount * vertexSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  373. buffer.put(x); buffer.put(y+h);
  374. buffer.put(0.0f); buffer.put(1.0f);
  375. buffer.put(x); buffer.put(y);
  376. buffer.put(0.0f); buffer.put(0.0f);
  377. buffer.put(x+bubbleRadius); buffer.put(y+h);
  378. buffer.put(0.5f); buffer.put(1.0f);
  379. buffer.put(x+bubbleRadius); buffer.put(y);
  380. buffer.put(0.5f); buffer.put(0.0f);
  381. buffer.put(x+w-bubbleRadius); buffer.put(y+h);
  382. buffer.put(0.5f); buffer.put(1.0f);
  383. buffer.put(x+w-bubbleRadius); buffer.put(y);
  384. buffer.put(0.5f); buffer.put(0.0f);
  385. buffer.put(x+w); buffer.put(y+h);
  386. buffer.put(1.0f); buffer.put(1.0f);
  387. buffer.put(x+w); buffer.put(y);
  388. buffer.put(1.0f); buffer.put(0.0f);
  389. buffer.position(0); // rewind
  390. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
  391. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mQuadVertexBuffer);
  392. GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexSize * vertexCount, buffer, GLES20.GL_STREAM_DRAW);
  393. int positionIndex = GLES20.glGetAttribLocation(mTexturedProgram, "position");
  394. int texCoordIndex = GLES20.glGetAttribLocation(mTexturedProgram, "texCoord");
  395. GLES20.glEnableVertexAttribArray(positionIndex);
  396. GLES20.glEnableVertexAttribArray(texCoordIndex);
  397. GLES20.glVertexAttribPointer(positionIndex, 2, GLES20.GL_FLOAT, false, vertexSize, 0);
  398. GLES20.glVertexAttribPointer(texCoordIndex, 2, GLES20.GL_FLOAT, false, vertexSize, 8);
  399. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertexCount);
  400. GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
  401. GLES20.glDisableVertexAttribArray(positionIndex);
  402. GLES20.glDisableVertexAttribArray(texCoordIndex);
  403. }
  404. private float[] mixColor(float alpha, float[] color1, float[] color2) {
  405. float[] color3 = new float[4];
  406. color3[0] = (1.0f - alpha) * color1[0] + alpha * color2[0];
  407. color3[1] = (1.0f - alpha) * color1[1] + alpha * color2[1];
  408. color3[2] = (1.0f - alpha) * color1[2] + alpha * color2[2];
  409. color3[3] = (1.0f - alpha) * color1[3] + alpha * color2[3];
  410. return color3;
  411. }
  412. public static int loadTexture(String filePath) {
  413. AssetManager assets = MainActivity.getInstance().getAssets();
  414. Bitmap bitmap;
  415. try {
  416. bitmap = BitmapFactory.decodeStream(assets.open(filePath));
  417. } catch (IOException e) {
  418. e.printStackTrace();
  419. return 0;
  420. }
  421. int[] names = {0};
  422. GLES20.glGenTextures(1, names, 0);
  423. int tex = names[0];
  424. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  425. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
  426. GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
  427. GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  428. bitmap.recycle(); // memory is now gpu -> free it
  429. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
  430. return tex;
  431. }
  432. // see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
  433. private static String inputStreamToString(InputStream is) {
  434. Scanner scanner = new Scanner(is);
  435. Scanner s = scanner.useDelimiter("\\A");
  436. String result = s.hasNext() ? s.next() : "";
  437. scanner.close();
  438. return result;
  439. }
  440. private static int loadShader(int type, String source) {
  441. int shader = GLES20.glCreateShader(type);
  442. GLES20.glShaderSource(shader, source);
  443. GLES20.glCompileShader(shader);
  444. Log.i("gl", GLES20.glGetShaderInfoLog(shader));
  445. return shader;
  446. }
  447. private static int loadProgram(String vertexSource, String fragmentSource) {
  448. int program = GLES20.glCreateProgram();
  449. GLES20.glAttachShader(program, loadShader(GLES20.GL_VERTEX_SHADER, vertexSource));
  450. GLES20.glAttachShader(program, loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource));
  451. GLES20.glLinkProgram(program);
  452. return program;
  453. }
  454. }