Browse Source

initial mesh rendering

Fabio Arnold 10 years ago
parent
commit
526990964b

BIN
assets/meshes/mario.amh


+ 48 - 51
res/layout/fragment_home.xml

@@ -10,50 +10,49 @@
         android:layout_height="match_parent"
         android:layout_weight="1" >
 
-        <de.tudarmstadt.informatik.hostage.ui2.fragment.HomeGLSurfaceView
+        <de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.HomeGLSurfaceView
             android:id="@+id/surfaceview"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />
     </FrameLayout>
 
     <RelativeLayout
-        android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
-        android:weightSum="1"
-        android:baselineAligned="false">
+        android:baselineAligned="false"
+        android:orientation="horizontal"
+        android:weightSum="1" >
 
         <TextView
             android:id="@+id/textView"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:text="Current connection monitor"
-            android:textStyle="bold"
-            android:layout_centerVertical="true"
-            android:layout_alignParentRight="false"
             android:layout_alignParentEnd="true"
+            android:layout_alignParentLeft="true"
+            android:layout_alignParentRight="false"
+            android:layout_centerVertical="true"
             android:layout_gravity="center_horizontal"
+            android:layout_marginLeft="15dp"
             android:layout_weight="1"
-            android:layout_alignParentLeft="true"
-            android:layout_marginLeft="15dp" />
+            android:text="Monitor current connection"
+            android:textStyle="bold" />
 
         <Switch
             android:id="@+id/switch1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_weight="1"
+            android:layout_alignParentBottom="false"
+            android:layout_alignParentEnd="false"
             android:layout_alignParentLeft="false"
-            android:layout_marginLeft="0dp"
+            android:layout_alignParentRight="true"
             android:layout_alignParentTop="false"
-            android:layout_marginTop="0dp"
             android:layout_gravity="right"
-            android:layout_alignParentEnd="false"
-            android:layout_alignParentRight="true"
-            android:layout_alignParentBottom="false"
+            android:layout_marginLeft="0dp"
             android:layout_marginRight="15dp"
-            android:textSize="10sp"
-            android:checked="true" />
-
+            android:layout_marginTop="0dp"
+            android:layout_weight="1"
+            android:checked="true"
+            android:textSize="10sp" />
     </RelativeLayout>
 
     <View
@@ -66,57 +65,57 @@
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="171dp"
+        android:layout_gravity="bottom"
         android:layout_marginLeft="15dp"
         android:layout_marginRight="15dp"
         android:orientation="vertical"
-        android:weightSum="1"
-        android:layout_gravity="bottom">
+        android:weightSum="1" >
 
         <RelativeLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content">
+            android:layout_height="wrap_content" >
 
             <TextView
                 android:id="@+id/textView5"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
+                android:layout_below="@+id/textView2"
+                android:layout_centerHorizontal="true"
+                android:layout_gravity="center"
                 android:text="This network is being monitored by HosTaGe"
                 android:textAppearance="?android:attr/textAppearanceSmall"
                 android:textColor="@color/holo_dark_green"
-                android:textSize="11sp"
-                android:layout_gravity="center"
-                android:layout_below="@+id/textView2"
-                android:layout_centerHorizontal="true" />
+                android:textSize="11sp" />
 
             <TextView
                 android:id="@+id/textView2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
+                android:layout_alignParentTop="true"
+                android:layout_centerHorizontal="true"
                 android:layout_gravity="center_horizontal"
+                android:layout_marginTop="21dp"
                 android:text="Eduroam"
                 android:textAppearance="?android:attr/textAppearanceLarge"
-                android:textSize="26sp"
-                android:layout_alignParentTop="true"
-                android:layout_centerHorizontal="true"
-                android:layout_marginTop="21dp" />
+                android:textSize="26sp" />
 
             <ImageView
                 android:id="@+id/imageView"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:src="@android:drawable/presence_online"
-                android:layout_marginRight="10dp"
                 android:layout_alignBottom="@+id/textView5"
-                android:layout_toLeftOf="@+id/textView5" />
+                android:layout_marginRight="10dp"
+                android:layout_toLeftOf="@+id/textView5"
+                android:src="@android:drawable/presence_online" />
 
             <ImageView
+                android:id="@+id/imageView2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:id="@+id/imageView2"
-                android:src="@android:drawable/ic_menu_info_details"
-                android:layout_alignParentTop="true"
+                android:layout_alignParentEnd="true"
                 android:layout_alignParentRight="true"
-                android:layout_alignParentEnd="true" />
+                android:layout_alignParentTop="true"
+                android:src="@android:drawable/ic_menu_info_details" />
         </RelativeLayout>
 
         <TextView
@@ -124,11 +123,11 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
+            android:layout_marginTop="10dp"
             android:text="Insecure"
             android:textAppearance="@android:style/TextAppearance.Holo.Small"
             android:textColor="@color/holo_red"
-            android:textSize="13sp"
-            android:layout_marginTop="10dp" />
+            android:textSize="13sp" />
 
         <TextView
             android:id="@+id/textView4"
@@ -141,30 +140,28 @@
 
         <RelativeLayout
             android:layout_width="fill_parent"
-            android:layout_height="match_parent">
+            android:layout_height="match_parent" >
 
             <TextView
+                android:id="@+id/textView6"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:text="Active profile: "
-                android:id="@+id/textView6"
-                android:textStyle="bold"
-                android:textSize="11sp"
                 android:layout_alignParentTop="true"
                 android:layout_centerHorizontal="true"
-                android:layout_marginTop="10dp" />
+                android:layout_marginTop="10dp"
+                android:text="Active profile: "
+                android:textSize="11sp"
+                android:textStyle="bold" />
 
             <TextView
+                android:id="@+id/textView7"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:text="Windows 7"
-                android:id="@+id/textView7"
-                android:textSize="11sp"
                 android:layout_below="@+id/textView6"
-                android:layout_centerHorizontal="true" />
-
+                android:layout_centerHorizontal="true"
+                android:text="Windows 7"
+                android:textSize="11sp" />
         </RelativeLayout>
-
     </LinearLayout>
 
 </LinearLayout>

+ 3 - 2
src/de/tudarmstadt/informatik/hostage/ui2/activity/MainActivity.java

@@ -20,6 +20,7 @@ import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.DrawerListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.HomeFragment;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.ProfileManagerFragment;
+import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
 import de.tudarmstadt.informatik.hostage.ui2.model.DrawerListItem;
 
 /**
@@ -42,6 +43,8 @@ public class MainActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         MainActivity.context = getApplicationContext();
+        
+        ThreatIndicatorGLRenderer.assets = getAssets();
 
         setContentView(R.layout.activity_drawer_main);
 
@@ -90,8 +93,6 @@ public class MainActivity extends Activity {
             }
         };
         mDrawerLayout.setDrawerListener(mDrawerToggle);
-        
-        //HomeGLSurfaceView surfaceView = (HomeGLSurfaceView)findViewById(R.id.surfaceview);
 
         if (savedInstanceState == null) {
             // on first time display view for first nav item

+ 66 - 0
src/de/tudarmstadt/informatik/hostage/ui2/fragment/opengl/AnimatedMesh.java

@@ -0,0 +1,66 @@
+package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+
+import android.opengl.GLES20;
+import android.util.Log;
+
+public class AnimatedMesh {
+	private ByteBuffer data;
+	
+	private int vertexOffset;
+	private int vertexSize;
+	private int triangleOffset;
+	private int triangleCount;
+	
+	public AnimatedMesh(InputStream is) {
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		try {
+			final int EOF = -1;
+			int len;
+			byte[] buffer = new byte[1 << 12];
+			while (EOF != (len = is.read(buffer)))
+				out.write(buffer, 0, len);
+		} catch (IOException e) {
+			e.printStackTrace();
+			return;
+		}
+
+		//data = ByteBuffer.wrap(out.toByteArray());
+		data = ByteBuffer.allocateDirect(out.size());
+		data.order(ByteOrder.nativeOrder());
+		data.put(out.toByteArray());
+		data.position(0);
+
+		int magicNum = data.getInt();
+		int version = data.getInt();
+		
+		vertexSize = 48;
+		vertexOffset = data.getInt();
+		int vertexCount = data.getInt();
+		triangleOffset = data.getInt();
+		triangleCount = data.getInt();
+		int boneOffset = data.getInt();
+		int boneCount = data.getInt();
+		int actionOffset = data.getInt();
+		int actionCount = data.getInt();
+		
+		Log.i("tag", data.getFloat()+" "+data.getFloat()+" "+data.getFloat());
+	}
+	
+	public void draw() {
+		int positionIndex = 0;
+		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.glDisableVertexAttribArray(positionIndex);
+	}
+}

+ 5 - 10
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeGLSurfaceView.java → src/de/tudarmstadt/informatik/hostage/ui2/fragment/opengl/HomeGLSurfaceView.java

@@ -1,4 +1,4 @@
-package de.tudarmstadt.informatik.hostage.ui2.fragment;
+package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;
 
 import android.content.Context;
 import android.opengl.GLSurfaceView;
@@ -6,19 +6,14 @@ import android.util.AttributeSet;
 import android.util.Log;
 
 public class HomeGLSurfaceView extends GLSurfaceView {
-
-	public HomeGLSurfaceView(Context context) {
+	public HomeGLSurfaceView(Context context) { // won't be called
 		super(context);
-		Log.d("gl", "constructor w/o attr");
-		// TODO Auto-generated constructor stub
+		Log.e("gl", "called wrong constructor (w/o attributes)");
 	}
 
-	public HomeGLSurfaceView(Context context, AttributeSet attrs) {
+	public HomeGLSurfaceView(Context context, AttributeSet attrs) { // will be called
 		super(context, attrs);
-		Log.d("gl", "constructor attr");
 		setEGLContextClientVersion(2); // OpenGL ES 2.0
-        setRenderer(new HomeGLRenderer());
-		// TODO Auto-generated constructor stub
+        setRenderer(new ThreatIndicatorGLRenderer());
 	}
-
 }

+ 26 - 3
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeGLRenderer.java → src/de/tudarmstadt/informatik/hostage/ui2/fragment/opengl/ThreatIndicatorGLRenderer.java

@@ -1,5 +1,7 @@
-package de.tudarmstadt.informatik.hostage.ui2.fragment;
+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;
@@ -7,11 +9,14 @@ 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 HomeGLRenderer implements Renderer {
+public class ThreatIndicatorGLRenderer implements Renderer {
 	public static int loadShader(int type, String source) {
 		int shader = GLES20.glCreateShader(type);
 		GLES20.glShaderSource(shader, source);
@@ -27,14 +32,25 @@ public class HomeGLRenderer implements Renderer {
 		return program;
 	}
 	
+	public static AssetManager assets;
+	
 	private int program;
 	private FloatBuffer vertexBuffer;
 	private float [] mvp;
+	
+	private AnimatedMesh androidMesh = null;
 
-	public HomeGLRenderer() {}
+	public ThreatIndicatorGLRenderer() {}
 	
 	public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
 		GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+		
+		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,
@@ -67,18 +83,25 @@ public class HomeGLRenderer implements Renderer {
 	
 	public void onDrawFrame(GL10 arg0) {
 		GLES20.glClear(GLES20.GL_COLOR_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};
 		GLES20.glUniform4fv(colorUniformLoc, 1, color, 0);
 		Matrix.rotateM(mvp, 0, 4.0f, 0.0f, 0.0f, 1.0f);
 		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();
 	}
 
 	public void onSurfaceChanged(GL10 arg0, int width, int height) {