I'm trying to create a game for Android. I have experience using
DirectX and XNA, but I have never used OpenGL before, and it is really
starting to frustrate me.  I've looked over several examples of how to
draw a sprite with OpenGL, I would use the Android drawing API (matter
of fact I have used it to do a proof of concept) however there are a
large number of sprites on the screen and it really slows down.  Thus,
I am switching it over to OpenGL. Here is the Render that I have
attached to my GLSurfaceView:



package com.testing.GLSpriteTest;

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.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

public class SpriteRenderer implements Renderer {
        private static FloatBuffer spriteVertices;

        private int h, w;

        /* (non-Javadoc)
         * @see
android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
         */
        @Override
        public void onDrawFrame(GL10 gl) {
                gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

                // Setup the MODELVIEW matrix
                gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(w/2, h/2, 0);

        // Draw Square
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, spriteVertices);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        }

        /* (non-Javadoc)
         * @see
android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
int, int)
         */
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
                // avoid division by zero
        if (height == 0) height = 1;
        // draw on the entire screen
        gl.glViewport(0, 0, width, height);
        // setup projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        Log.d("SpriteRenderer", width + "x" + height);
        GLU.gluOrtho2D(gl, 0, width, height, 0);

        float qWidth = 5;

        float[] quadCoords = new float[] {
                 -qWidth, -qWidth,
                  qWidth, -qWidth,
                 -qWidth,  qWidth,
                  qWidth,  qWidth,
        };

        ByteBuffer bb =
ByteBuffer.allocateDirect(quadCoords.length*4);
                bb.order(ByteOrder.nativeOrder());
                spriteVertices = bb.asFloatBuffer();
                spriteVertices.put(quadCoords);

        w = width;
        h = height;
        }

        /* (non-Javadoc)
         * @see
android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
javax.microedition.khronos.egl.EGLConfig)
         */
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0, 0, 0, 0);

        gl.glDisable(GL10.GL_LIGHTING);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glDisable(GL10.GL_CULL_FACE);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
        }
}

>From what I have read, I thought this would work, but when I run it on
my G1 I see a white square bigger than I intended flash on the screen,
then disappear.  I'm sure I'm missing something, but I honestly don't
have a clue what it is.  If someone can point out my mistake and how
to fix or where I can find a solid tutorial on drawing in 2d using
OpenGL, I would be very thankful.

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to