]> Pileus Git - ~andy/spades/commitdiff
Add initial OpenGL example
authorAndy Spencer <andy753421@gmail.com>
Sun, 14 Apr 2013 05:13:30 +0000 (05:13 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sun, 14 Apr 2013 05:13:30 +0000 (05:13 +0000)
res/layout/main.xml
src/org/pileus/spades/Cards.java
src/org/pileus/spades/Main.java

index 6b683d1e3d8eacd6e060d8f4c8ceca3e8dcb9be7..cde15f7d4edf739818f914539d483813f69da63e 100644 (file)
                                                android:text="Send" />
                                </LinearLayout>
                        </LinearLayout>
-                       <TextView
+                       <LinearLayout
                                android:id="@+id/spades"
+                               android:orientation="vertical"
                                android:layout_width="fill_parent"
-                               android:layout_height="fill_parent"
-                               android:text="Spades" />
+                               android:layout_height="fill_parent">
+                               <TextView
+                                       android:id="@+id/spades_text"
+                                       android:layout_width="fill_parent"
+                                       android:layout_height="wrap_content"
+                                       android:text="Spades" />
+                       </LinearLayout>
                        <ScrollView
                                android:id="@+id/debug_scroll"
                                android:layout_width="fill_parent"
index 59679742cdd1de70d8c55be5bbe53ba23144c5d2..083bf6fdbec86fbd33cc071ff66a49a6e5617fe1 100644 (file)
 package org.pileus.spades;
 
-public class Cards
+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.opengl.GLES20;
+import android.opengl.GLSurfaceView;
+
+import android.content.Context;
+
+public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
 {
-       /* Public Methods */
-       public Cards()
+       /* Shader data */
+       private final String vertSource =
+               "attribute vec4 vPosition;"  +
+               "void main() {"              +
+               "  gl_Position = vPosition;" +
+               "}";
+
+       private final String fragSource =
+               "precision mediump float;" +
+               "uniform vec4 vColor;"     +
+               "void main() {"            +
+               "  gl_FragColor = vColor;" +
+               "}";
+
+       /* Drawing data */
+       private final float coords[] = {
+               0.0f,  0.5f, 0.0f,
+               -0.5f, -0.5f, 0.0f,
+               0.5f, -0.5f, 0.0f,
+       };
+
+       private final float color[] = { 1, 0, 0, 1 };
+
+       /* Private data */
+       private int         program;
+       private FloatBuffer vertBuf;
+
+       /* Private methods */
+       private int loadShader(int type, String code)
+       {
+               int shader = GLES20.glCreateShader(type);
+               GLES20.glShaderSource(shader, code);
+               GLES20.glCompileShader(shader);
+               return shader;
+       }
+
+       /* GLSurfaceView Methods */
+       public Cards(Context context)
        {
+               super(context);
                Os.debug("Cards create");
+
+               this.setEGLContextClientVersion(2);
+               this.setRenderer(this);
+               this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
+       }
+
+       /* Renderer methods */
+       @Override
+       public void onSurfaceCreated(GL10 unused, EGLConfig config)
+       {
+               /* Initialize shaders */
+               int vertShader = this.loadShader(GLES20.GL_VERTEX_SHADER,   vertSource);
+               int fragShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragSource);
+
+               /* Link shaders into an OpenGL program */
+               this.program = GLES20.glCreateProgram();
+               GLES20.glAttachShader(program, vertShader);
+               GLES20.glAttachShader(program, fragShader);
+               GLES20.glLinkProgram(program);
+
+               /* Create Vertex Array  */
+               ByteBuffer byteBuf = ByteBuffer.allocateDirect(coords.length * 4);
+               byteBuf.order(ByteOrder.nativeOrder());
+
+               this.vertBuf = byteBuf.asFloatBuffer();
+               this.vertBuf.put(coords);
+               this.vertBuf.position(0);
+       }
+
+       @Override
+       public void onDrawFrame(GL10 unused)
+       {
+               /* Turn on the program */
+               GLES20.glUseProgram(program);
+               int posHandle = GLES20.glGetAttribLocation(program, "vPosition");
+               int clrHandle = GLES20.glGetUniformLocation(program, "vColor");
+
+               /* Draw */
+               GLES20.glClearColor(0, 0, 0, 1);
+               GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
+               GLES20.glEnableVertexAttribArray(posHandle);
+               GLES20.glVertexAttribPointer(posHandle, 3, GLES20.GL_FLOAT, false, 3*4, vertBuf);
+               GLES20.glUniform4fv(clrHandle, 1, color, 0);
+               GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
+               GLES20.glDisableVertexAttribArray(posHandle);
+       }
+
+       @Override
+       public void onSurfaceChanged(GL10 unused, int width, int height)
+       {
+               GLES20.glViewport(0, 0, width, height);
        }
 }
index 3c3941499f9f0a22e9e1c453f63b9e788d0ab9c7..0eae72e7ad3348dfb2d961c79651d97e8b1fc56c 100644 (file)
@@ -31,6 +31,7 @@ public class Main extends Activity
        private boolean      ready;
        private String       topic;
        private String       names;
+       private Cards        cards;
 
        /* Widgets */
        private TabHost      window;
@@ -39,7 +40,7 @@ public class Main extends Activity
        private TextView     log;
        private EditText     input;
        private Button       send;
-       private TextView     spades;
+       private LinearLayout spades;
        private TextView     debug;
 
        private ScrollView   lscroll;
@@ -210,7 +211,7 @@ public class Main extends Activity
                        this.log       = (TextView)     findViewById(R.id.log);
                        this.input     = (EditText)     findViewById(R.id.input);
                        this.send      = (Button)       findViewById(R.id.send);
-                       this.spades    = (TextView)     findViewById(R.id.spades);
+                       this.spades    = (LinearLayout) findViewById(R.id.spades);
                        this.debug     = (TextView)     findViewById(R.id.debug);
 
                        this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
@@ -231,6 +232,11 @@ public class Main extends Activity
                                        .newTabSpec("debug")
                                        .setIndicator("Debug")
                                        .setContent(R.id.debug));
+
+                       // Setup OpenGL view
+                       this.cards = new Cards(this);
+                       this.spades.addView(cards);
+                       
                } catch (Exception e) {
                        Os.debug("Error setting content view", e);
                        return;