寄件者 scratchlab |
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;
}
}
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Cube {
private IntBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
public Cube()
{
int one = 65536;//0x10000,有趣的技巧,呵呵
int vertices[] = {
-one, -one, -one,
one, -one, -one,
one, one, -one,
-one, one, -one,
-one, -one, one,
one, -one, one,
one, one, one,
-one, one, one,
};
float colors[] = {
0, 0, 0, 1,
1, 0, 0, 1,
1, 1, 0, 1,
0, 1, 0, 1,
0, 0, 1, 1,
1, 0, 1, 1,
1, 1, 1, 1,
0, 1, 1, 1,
};
byte indices[] = {
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0,
4, 7, 6, 4, 6, 5,
3, 0, 1, 3, 1, 2
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
cbb.order(ByteOrder.nativeOrder());
mColorBuffer = cbb.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl)
{
gl.glFrontFace(gl.GL_CW);
gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);
gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
}
}
沒有留言:
張貼留言