Now I want to replace Canvas with OpenGL ES. I've looked at examples from SpriteMethodTest. Somehow it didn't work. I started with a simple clean code. I can draw a square and use the camera.
The only things I need now, exact behaviour with OpenGL as Canvas. So an 10x10px square on (0,0) must be the same in OpenGL as Canvas.
I've here some code:
OpenGLActivity
- Code: Select all
package org.opengl;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class OpenGLActivity extends Activity
{
private GLSurfaceView view;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Remove the title bar from the window.
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Make the windows into full screen mode.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
view = new GLSurfaceView(this);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
}
@Override
protected void onResume()
{
super.onResume();
view.onResume();
}
@Override
protected void onPause()
{
super.onPause();
view.onPause();
}
}
Square
- Code: Select all
package org.opengl;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Square
{
private float[] vertices = {
-1.0f, 1.0f, 0, // 0, Top left
-1.0f, -1.0f, 0, // 1, Bottom left
1.0f, -1.0f, 0, // 2, Bottom right
1.0f, 1.0f, 0
};
private short[] indices = {0, 1, 2, 0, 2, 3};
private FloatBuffer vertexBuffer;
private ShortBuffer indexBuffer;
public Square()
{
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl)
{
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}
OpenGLRenderer
- Code: Select all
package org.opengl;
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 OpenGLRenderer implements Renderer
{
private Square square;
private int mViewWidth;
private int mViewHeight;
public OpenGLRenderer()
{
square = new Square();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glClearColor(0.5f, 0.0f, 0.0f, .5f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}
@Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
square.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
mViewWidth = width;
mViewHeight = height;
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
// gl.glOrthof(0.0f, mViewWidth, 0.0f, mViewHeight, -1.0f, 1.0f);
}
}
The code is very simple. Draw a white 1x1px square on 0,0. What I get is an full size square on the screen. Using glFrustum I can adjust the distance of the camera, but I don't need that function. The engine is purely for creating 2D games. So I've to use ortho.
Can someone please help me creating a working example?




