|
@@ -34,8 +34,14 @@ public class ThreatIndicatorGLRenderer implements Renderer {
|
|
|
|
|
|
public static AssetManager assets;
|
|
public static AssetManager assets;
|
|
|
|
|
|
|
|
+ private int width;
|
|
|
|
+ private int height;
|
|
|
|
+ private float aspectRatio;
|
|
|
|
+
|
|
private int program;
|
|
private int program;
|
|
private FloatBuffer vertexBuffer;
|
|
private FloatBuffer vertexBuffer;
|
|
|
|
+ private float [] modelview;
|
|
|
|
+ private float [] projection;
|
|
private float [] mvp;
|
|
private float [] mvp;
|
|
|
|
|
|
private AnimatedMesh androidMesh = null;
|
|
private AnimatedMesh androidMesh = null;
|
|
@@ -44,6 +50,8 @@ public class ThreatIndicatorGLRenderer implements Renderer {
|
|
|
|
|
|
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
|
|
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
|
|
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
+ GLES20.glEnable(GLES20.GL_DEPTH_TEST);
|
|
|
|
+ GLES20.glEnable(GLES20.GL_CULL_FACE);
|
|
|
|
|
|
try {
|
|
try {
|
|
InputStream is = assets.open("meshes/mario.amh");
|
|
InputStream is = assets.open("meshes/mario.amh");
|
|
@@ -63,35 +71,45 @@ public class ThreatIndicatorGLRenderer implements Renderer {
|
|
vertexBuffer.put(positions);
|
|
vertexBuffer.put(positions);
|
|
vertexBuffer.position(0);
|
|
vertexBuffer.position(0);
|
|
|
|
|
|
|
|
+ modelview = new float[16];
|
|
|
|
+ Matrix.setIdentityM(modelview, 0);
|
|
|
|
+ projection = new float[16];
|
|
mvp = new float[16];
|
|
mvp = new float[16];
|
|
- Matrix.setIdentityM(mvp, 0);
|
|
|
|
|
|
|
|
final String vertexSource =
|
|
final String vertexSource =
|
|
"uniform mat4 mvp;" +
|
|
"uniform mat4 mvp;" +
|
|
- "attribute vec4 position;" +
|
|
|
|
|
|
+ "attribute vec3 position;" +
|
|
|
|
+ "attribute vec3 normal;" +
|
|
|
|
+ "varying vec3 vertexNormal;" +
|
|
"void main() {" +
|
|
"void main() {" +
|
|
- " gl_Position = mvp * position;" +
|
|
|
|
|
|
+ " vertexNormal = normal;" +
|
|
|
|
+ " gl_Position = mvp * vec4(position, 1.0);" +
|
|
"}";
|
|
"}";
|
|
final String fragmentSource =
|
|
final String fragmentSource =
|
|
"precision mediump float;" +
|
|
"precision mediump float;" +
|
|
"uniform vec4 color;" +
|
|
"uniform vec4 color;" +
|
|
|
|
+ "varying vec3 vertexNormal;" +
|
|
"void main() {" +
|
|
"void main() {" +
|
|
- " gl_FragColor = color;" +
|
|
|
|
|
|
+ " vec3 normal = normalize(vertexNormal);" +
|
|
|
|
+ " gl_FragColor = max(0.0, -normal.y) * color;" +
|
|
"}";
|
|
"}";
|
|
program = loadProgram(vertexSource, fragmentSource);
|
|
program = loadProgram(vertexSource, fragmentSource);
|
|
}
|
|
}
|
|
|
|
|
|
public void onDrawFrame(GL10 arg0) {
|
|
public void onDrawFrame(GL10 arg0) {
|
|
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
+ GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
GLES20.glUseProgram(program);
|
|
GLES20.glUseProgram(program);
|
|
- int positionAttribLoc = GLES20.glGetAttribLocation(program, "position");
|
|
|
|
int colorUniformLoc = GLES20.glGetUniformLocation(program, "color");
|
|
int colorUniformLoc = GLES20.glGetUniformLocation(program, "color");
|
|
int mvpUniformLoc = GLES20.glGetUniformLocation(program, "mvp");
|
|
int mvpUniformLoc = GLES20.glGetUniformLocation(program, "mvp");
|
|
|
|
|
|
- float [] color = {0.0f, 1.0f, 0.0f, 1.0f};
|
|
|
|
|
|
+ float [] color = {1.0f, 1.0f, 1.0f, 1.0f};
|
|
GLES20.glUniform4fv(colorUniformLoc, 1, color, 0);
|
|
GLES20.glUniform4fv(colorUniformLoc, 1, color, 0);
|
|
- Matrix.rotateM(mvp, 0, 4.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
+ //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.glUniformMatrix4fv(mvpUniformLoc, 1, false, mvp, 0);
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -101,10 +119,14 @@ public class ThreatIndicatorGLRenderer implements Renderer {
|
|
GLES20.glDisableVertexAttribArray(positionAttribLoc);
|
|
GLES20.glDisableVertexAttribArray(positionAttribLoc);
|
|
*/
|
|
*/
|
|
|
|
|
|
- androidMesh.draw();
|
|
|
|
|
|
+ androidMesh.draw(program);
|
|
}
|
|
}
|
|
|
|
|
|
- public void onSurfaceChanged(GL10 arg0, int width, int height) {
|
|
|
|
|
|
+ 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);
|
|
GLES20.glViewport(0, 0, width, height);
|
|
}
|
|
}
|
|
}
|
|
}
|