Using java Syntax Highlighting
- package com.testing.GLSpriteTest;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11Ext;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.opengl.GLU;
- import android.opengl.GLUtils;
- import android.opengl.GLSurfaceView.Renderer;
- import android.util.Log;
- public class SpriteRenderer implements Renderer {
- private static FloatBuffer spriteVertices;
- private static IntBuffer texturesBuffer;
- Context context;
- private int h, w;
- /* (non-Javadoc)
- * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
- */
- 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)
- */
- 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 = width/8;
- 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);
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
When that didn't work I looked around a bit and decided to use the draw texture functions in GL11Ext. And that still didn't work. Here is my code for that:
Using java Syntax Highlighting
- package com.testing.GLSpriteTest;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11Ext;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.opengl.GLU;
- import android.opengl.GLUtils;
- import android.opengl.GLSurfaceView.Renderer;
- import android.util.Log;
- public class SpriteRenderer implements Renderer {
- private static IntBuffer texturesBuffer;
- Context context;
- public SpriteRenderer(Context c)
- {
- context = c;
- }
- /* (non-Javadoc)
- * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
- */
- public void onDrawFrame(GL10 gl) {
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(0));
- ((GL11Ext) gl).glDrawTexiOES(1, 1, 0, 128, 128);
- }
- /* (non-Javadoc)
- * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10, int, int)
- */
- 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);
- }
- /* (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);
- // create texture
- gl.glEnable(GL10.GL_TEXTURE_2D);
- texturesBuffer = IntBuffer.allocate(1);
- gl.glGenTextures(1, texturesBuffer);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(0));
- // setup texture parameters
- gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
- gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
- gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
- gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
- // set the texture
- Bitmap texture = BitmapFactory.decodeResource(context.getResources(), R.drawable.sphere);
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
- texture.recycle();
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
So if someone could take a look at my code and tell me what I am missing that would be great. I've been trying to get this to work almost all day and I am done with it. I'm about to just take the slightly jerky animation in my Canvas, but I figured I'd give here a shot before I gave up for good.


