Jimmy's papa參考網路上一堆Android OpenGL ES的教學文章,寫了個小範例,感覺起來好像沒有像寫Flash的PV3D or Away3D那麼便利,有空再查看看有沒有方便的工具可以從Blender自動產生相關的Mesh類別
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
public class AndroidGLSurfaceViewActivity extends Activity {
private MyGLSurfaceView myGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myGLSurfaceView = new MyGLSurfaceView(this);
setContentView(myGLSurfaceView);
}
@Override
protected void onResume() {
super.onResume();
myGLSurfaceView.onResume();
}
@Override
protected void onPause() {
super.onPause();
myGLSurfaceView.onPause();
}
class MyGLSurfaceView extends GLSurfaceView{
CubeRenderer myRenderer;
public MyGLSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myRenderer = new CubeRenderer(false);
setRenderer(myRenderer);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
// TODO Auto-generated method stub
queueEvent(new Runnable(){
public void run() {
myRenderer.setColor(event.getX()/getWidth(),
event.getY()/getHeight(), 1.0f);
}});
return true;
}
}
}
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class CubeRenderer implements android.opengl.GLSurfaceView.Renderer
{
private float red = 0;
private float green = 0;
private float blue = 0;
private boolean mTranslucentBackground;
private Cube mCube;
private float mAngle;
public CubeRenderer(boolean useTranslucentBackground) {
mTranslucentBackground = useTranslucentBackground;
mCube = new Cube();
}
@Override
public void onDrawFrame(GL10 gl) {
/*
* Usually, the first thing one might want to do is to clear
* the screen. The most efficient way of doing this is to use
* glClear().
*/
gl.glClearColor(red, green, blue, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
/*
* Now we're ready to draw some 3D objects
*/
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(-4.0f, 1.5f, -4.0f);
gl.glRotatef(mAngle, 0, 1, 0);
gl.glRotatef(mAngle*0.25f, 1, 0, 0);
mCube.draw(gl);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(-1.0f, 0, -5.0f);//(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle*2.0f, 0, 1, 1);
mCube.draw(gl);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(2.0f, -1, -7.0f);//(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle*2.0f, 1, 0, 1);
mCube.draw(gl);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(7.0f, -3, -9.0f);//(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle*2.0f, 1, 1, 0);
mCube.draw(gl);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(12.0f, -5, -10.0f);//(0.5f, 0.5f, 0.5f);
gl.glRotatef(mAngle*2.0f, 1, 0, 1);
mCube.draw(gl);
gl.glPopMatrix();
mAngle += 1.2f;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
/*
* Set our projection matrix. This doesn't have to be done
* each time we draw, but usually a new projection needs to
* be set when the viewport is resized.
*/
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
if (mTranslucentBackground) {
gl.glClearColor(0,0,0,0);
} else {
gl.glClearColor(1,1,1,1);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
public void setColor(float r, float g, float b){
red = r;
green = g;
blue = b;
}
}