Powered By Blogger

Monday 12 March 2012

Android in 3D


A 3d platform in android.That's 3D more feature and interesting


Example.java

package com.ex;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Example extends Activity {
   
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GLView2(this));
    }
   
}


GLView2

package com.ex;

import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GLView2 extends SurfaceView implements SurfaceHolder.Callback
{
 SurfaceHolder holder;
 ExThread gthread;
public GLView2(Context context) {
super(context);
// TODO Auto-generated constructor stub
getHolder().addCallback(this);
getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
gthread=new ExThread(this);
gthread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

}

GLThread.java


package com.ex;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLU;

import com.ex.GLView2;

public class ExThread extends Thread {
private final com.ex.GLView2 gview;
GLCube c=new GLCube();
private long startTime;
private boolean done = false;

public ExThread(com.ex.GLView2 view) {
// TODO Auto-generated constructor stub
this.gview=view;
}
public void run()
{
EGL10 gl=(EGL10)EGLContext.getEGL();
EGLDisplay display=gl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
int version[]=new int[2];
gl.eglInitialize(display, version);
int[] configSpec = { EGL10.EGL_RED_SIZE, 5,
EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5,
EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };

EGLConfig[] configs = new EGLConfig[1];
int[] numConfig = new int[1];
gl.eglChooseConfig(display, configSpec, configs, 1,
numConfig);
EGLConfig config = configs[0];

EGLContext glc = gl.eglCreateContext(display, config,
EGL10.EGL_NO_CONTEXT, null);
EGLSurface surface = gl.eglCreateWindowSurface(display,
 config, gview.getHolder(), null);
 gl.eglMakeCurrent(display, surface, surface, glc);

 GL10 egl = (GL10) (glc.getGL());
 init(egl);
 while (!done) {
  // Draw a single frame here...
  drawFrame(egl);
  gl.eglSwapBuffers(display, surface);

  // Error handling
  if (gl.eglGetError() == EGL11.EGL_CONTEXT_LOST) {
  Context c = gview.getContext();
  if (c instanceof Activity) {
  ((Activity) c).finish();
  }
  }
 }
 gl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE,
  EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
  gl.eglDestroySurface(display, surface);
  gl.eglDestroyContext(display, glc);
  gl.eglTerminate(display);





}
private void drawFrame(GL10 egl) {
// TODO Auto-generated method stub
egl.glClear(GL10.GL_COLOR_BUFFER_BIT
| GL10.GL_DEPTH_BUFFER_BIT);
egl.glMatrixMode(GL10.GL_MODELVIEW);
egl.glLoadIdentity();
egl.glTranslatef(0, 0, -3.0f);
c.draw(egl);
long elapsed=System.currentTimeMillis()-startTime;
egl.glRotatef(elapsed *(30f/1000f), 0, 1,0);
egl.glRotatef(elapsed *(15f/1000f),1, 0,0);


}
private void init(GL10 egl) {

egl.glViewport(0, 0, gview.getWidth(), gview.getHeight());
egl.glMatrixMode(GL10.GL_PROJECTION);
egl.glLoadIdentity();
float ratio = (float) gview.getWidth() / gview.getHeight();
GLU.gluPerspective(egl, 45.0f, ratio, 1, 100f);

egl.glEnable(GL10.GL_DEPTH_TEST);
egl.glDepthFunc(GL10.GL_LEQUAL);
egl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
float lightAmbient[] = new float[] { 0.2f, 0.2f, 0.2f, 1 };
float lightDiffuse[] = new float[] { 1, 1, 1, 1 };
float[] lightPos = new float[] { 1, 1, 1, 1 };
egl.glEnable(GL10.GL_LIGHTING);
egl.glEnable(GL10.GL_LIGHT0);
egl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
egl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
egl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
float matAmbient[] = new float[] { 1, 1, 1, 1 };
float matDiffuse[] = new float[] { 1, 1, 1, 1 };
egl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,
matAmbient, 0);
egl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE,
matDiffuse, 0);
startTime=System.currentTimeMillis();

}
public void requestExitAndWait() {
// Tell the thread to quit
done = true;
try {
join();
} catch (InterruptedException ex) {
// Ignore
}
}

}

GLcube


package com.ex;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLCube {
private IntBuffer mvertexbuffer;
public GLCube()
{
int one=65536;
int half=one/2;
int vertices[]={-half, -half, half, half, -half, half,
- -half, half, half, half, half, half,
- // BACK
- -half, -half, -half, -half, half, -half,
- half, -half, -half, half, half, -half,
// LEFT
- -half, -half, half, -half, half, half,
- -half, -half, -half, -half, half, -half,
- // RIGHT
- half, -half, -half, half, half, -half,
half, -half, half, half, half, half,
- // TOP
- -half, half, half, half, half, half,
- -half, half, -half, half, half, -half,
- // BOTTOM
-half, -half, half, -half, -half, -half,
- half, -half, half, half, -half, -half};
ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);
vbb.order(ByteOrder.nativeOrder());
mvertexbuffer=vbb.asIntBuffer();
mvertexbuffer.put(vertices);
mvertexbuffer.position(0);

}
public void draw(GL10 gl)
{

gl.glVertexPointer(3,GL10.GL_FIXED, 0,mvertexbuffer);

gl.glColor4f(1, 1,1,1);
gl.glNormal3f(0,0,1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);
gl.glNormal3f(0,0, -1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4,4);

gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(-1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glNormal3f(1, 0, 0);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(0, 1, 0);
 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
 gl.glNormal3f(0, -1, 0);
 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
}

}








No comments:

Post a Comment