Browse Source

gl: now using vertex buffer objects

Fabio Arnold 10 years ago
parent
commit
01544107b6

+ 38 - 8
src/de/tudarmstadt/informatik/hostage/ui2/fragment/opengl/AnimatedMesh.java

@@ -6,6 +6,7 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
 
 import android.opengl.GLES20;
 import android.util.Log;
@@ -18,6 +19,9 @@ public class AnimatedMesh {
 	private int triangleOffset;
 	private int triangleCount;
 	
+	private int vertexBuffer; // vbo
+	private int indexBuffer;
+	
 	public AnimatedMesh(InputStream is) {
 		ByteArrayOutputStream out = new ByteArrayOutputStream();
 		try {
@@ -50,17 +54,43 @@ public class AnimatedMesh {
 		int actionOffset = data.getInt();
 		int actionCount = data.getInt();
 		
-		Log.i("tag", data.getFloat()+" "+data.getFloat()+" "+data.getFloat());
+		IntBuffer buffers = IntBuffer.allocate(2);
+		GLES20.glGenBuffers(2, buffers); // buffer names
+		vertexBuffer = buffers.get();
+		indexBuffer = buffers.get();
+		
+		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
+		data.position(vertexOffset);
+		GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexSize * vertexCount, data.asFloatBuffer(), GLES20.GL_STATIC_DRAW);
+		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
+		
+		GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
+		data.position(triangleOffset);
+		GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, 12 * triangleCount, data.asIntBuffer(), GLES20.GL_STATIC_DRAW);
+		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
 	}
 	
-	public void draw() {
-		int positionIndex = 0;
+	public void draw(int program) {
+		// TODO: cache attrib locations
+		int positionIndex = GLES20.glGetAttribLocation(program, "position");
+		int normalIndex = GLES20.glGetAttribLocation(program, "normal");
+		
 		GLES20.glEnableVertexAttribArray(positionIndex);
-		data.position(vertexOffset);
-		GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, data.asFloatBuffer());
-		data.position(triangleOffset);
-		GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_INT, data.asIntBuffer());
-		//GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3 * triangleCount);
+		GLES20.glEnableVertexAttribArray(normalIndex);
+		
+		//data.position(vertexOffset);
+		//GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, data.asFloatBuffer());
+		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
+		GLES20.glVertexAttribPointer(positionIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 0);
+		GLES20.glVertexAttribPointer(normalIndex, 3, GLES20.GL_FLOAT, false, vertexSize, 12);
+		//data.position(triangleOffset);
+		//GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_INT, data.asIntBuffer());
+		GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
+		GLES20.glDrawElements(GLES20.GL_TRIANGLES, 3 * triangleCount, GLES20.GL_UNSIGNED_INT, 0);
+		GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
+		GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
+		
 		GLES20.glDisableVertexAttribArray(positionIndex);
+		GLES20.glDisableVertexAttribArray(normalIndex);
 	}
 }

+ 32 - 10
src/de/tudarmstadt/informatik/hostage/ui2/fragment/opengl/ThreatIndicatorGLRenderer.java

@@ -34,8 +34,14 @@ public class ThreatIndicatorGLRenderer implements Renderer {
 	
 	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;
@@ -44,6 +50,8 @@ public class ThreatIndicatorGLRenderer implements Renderer {
 	
 	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");
@@ -63,35 +71,45 @@ public class ThreatIndicatorGLRenderer implements Renderer {
 		vertexBuffer.put(positions);
 		vertexBuffer.position(0);
 		
+		modelview = new float[16];
+		Matrix.setIdentityM(modelview, 0);
+		projection = new float[16];
 		mvp = new float[16];
-		Matrix.setIdentityM(mvp, 0);
 		
 		final String vertexSource =
 				"uniform mat4 mvp;" +
-				"attribute vec4 position;" +
+				"attribute vec3 position;" +
+				"attribute vec3 normal;" +
+				"varying vec3 vertexNormal;" +
 				"void main() {" +
-				"	gl_Position = mvp * position;" +
+				"	vertexNormal = normal;" +
+				"	gl_Position = mvp * vec4(position, 1.0);" +
 				"}";
 		final String fragmentSource =
 				"precision mediump float;" +
 				"uniform vec4 color;" +
+				"varying vec3 vertexNormal;" +
 				"void main() {" +
-				"	gl_FragColor = color;" +
+				"	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.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
 		
 		GLES20.glUseProgram(program);
-		int positionAttribLoc = GLES20.glGetAttribLocation(program, "position");
 		int colorUniformLoc = GLES20.glGetUniformLocation(program, "color");
 		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);
-		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);
 		
 		/*
@@ -101,10 +119,14 @@ public class ThreatIndicatorGLRenderer implements Renderer {
 		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);
 	}
 }