package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.res.AssetManager; import android.opengl.GLES20; import android.opengl.GLSurfaceView.Renderer; import android.opengl.Matrix; import android.util.Log; public class ThreatIndicatorGLRenderer implements Renderer { public static int loadShader(int type, String source) { int shader = GLES20.glCreateShader(type); GLES20.glShaderSource(shader, source); GLES20.glCompileShader(shader); return shader; } public 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; } public static AssetManager assets; private int width; private int height; private float aspectRatio; private int program; private FloatBuffer vertexBuffer; private float [] modelview; private float [] projection; private float [] mvp; private AnimatedMesh androidMesh = null; public ThreatIndicatorGLRenderer() {} public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) { GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); GLES20.glEnable(GLES20.GL_DEPTH_TEST); GLES20.glEnable(GLES20.GL_CULL_FACE); try { InputStream is = assets.open("meshes/mario.amh"); androidMesh = new AnimatedMesh(is); } catch (IOException e) { Log.e("gl", "Couldn't open mesh"); } final float [] positions = { 0.0f, 0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, }; ByteBuffer bb = ByteBuffer.allocateDirect(4 * positions.length); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(positions); vertexBuffer.position(0); modelview = new float[16]; Matrix.setIdentityM(modelview, 0); projection = new float[16]; mvp = new float[16]; final String vertexSource = "uniform mat4 mvp;" + "attribute vec3 position;" + "attribute vec3 normal;" + "varying vec3 vertexNormal;" + "void main() {" + " vertexNormal = normal;" + " gl_Position = mvp * vec4(position, 1.0);" + "}"; final String fragmentSource = "precision mediump float;" + "uniform vec4 color;" + "varying vec3 vertexNormal;" + "void main() {" + " vec3 normal = normalize(vertexNormal);" + " gl_FragColor = max(0.0, -normal.y) * color;" + "}"; program = loadProgram(vertexSource, fragmentSource); } public void onDrawFrame(GL10 arg0) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); GLES20.glUseProgram(program); int colorUniformLoc = GLES20.glGetUniformLocation(program, "color"); int mvpUniformLoc = GLES20.glGetUniformLocation(program, "mvp"); float [] color = {1.0f, 1.0f, 1.0f, 1.0f}; GLES20.glUniform4fv(colorUniformLoc, 1, color, 0); //Matrix.rotateM(mvp, 0, 4.0f, 0.0f, 0.0f, 1.0f); Matrix.setIdentityM(modelview, 0); Matrix.rotateM(modelview, 0, -90.0f, 1.0f, 0.0f, 0.0f); Matrix.multiplyMM(mvp, 0, projection, 0, modelview, 0); GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0); /* GLES20.glEnableVertexAttribArray(positionAttribLoc); GLES20.glVertexAttribPointer(positionAttribLoc, 4, GLES20.GL_FLOAT, false, 0, vertexBuffer); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); GLES20.glDisableVertexAttribArray(positionAttribLoc); */ androidMesh.draw(program); } 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); GLES20.glViewport(0, 0, width, height); } }