-
import java.nio.FloatBuffer;
-
-
import javax.microedition.khronos.opengles.GL10;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.SurfaceHolder;
-
import android.view.SurfaceView;
-
import android.graphics.OpenGLContext;
-
import android.content.Context;
-
import android.opengl.GLU;
-
-
-
public class GlTest extends Activity {
-
@Override
-
public void onCreate(Bundle icicle) {
-
super.onCreate(icicle);
-
setContentView(new SurfaceTest(this));
-
}
-
}
-
-
class SurfaceTest
extends SurfaceView
implements SurfaceHolder.
Callback,
Runnable {
-
final static private float SCREEN_WIDTH = 320;
-
final static private float SCREEN_HEIGHT = 320;
-
-
private OpenGLContext glContext_;
-
private GL10 glObject_;
-
-
private boolean threadRunnable_;
-
-
-
super(c);
-
Log.i("userlog", "SurfaceTest.SurfaceTest()");
-
-
getHolder().addCallback(this);
-
}
-
-
-
private boolean initialize() {
-
try {
-
glContext_ = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);
-
glContext_.makeCurrent(getHolder());
-
glObject_ = (GL10)(glContext_.getGL());
-
-
glObject_.glMatrixMode(GL10.GL_PROJECTION);
-
glObject_.glLoadIdentity();
-
-
GLU.gluOrtho2D(glObject_, -1.0f, 1.0f, -1.0f, 1.0f);
-
glObject_.glViewport((int)(getWidth()-SCREEN_WIDTH)/2, (int)(getHeight() - SCREEN_HEIGHT)/2,
-
(int)SCREEN_WIDTH, (int)SCREEN_HEIGHT);
-
-
Log.i("userlog", e.toString());
-
return false;
-
}
-
return true;
-
}
-
-
-
@Override
-
public void run() {
-
Log.i("userlog", "SurfaceTest.run()");
-
-
if (initialize() == false) return;
-
-
final float[] vertex = new float[] {
-
-0.0f, 0.6f, 0.0f,
-
-0.6f, -0.6f, 0.0f,
-
0.6f, -0.6f, 0.0f,
-
};
-
-
final float[] color = new float[] {
-
1.0f, 0.0f, 0.0f,
-
0.0f, 1.0f, 0.0f,
-
0.0f, 0.0f, 1.0f,
-
};
-
-
final FloatBuffer colors = FloatBuffer.wrap(color);
-
final FloatBuffer squareBuff = FloatBuffer.wrap(vertex);
-
-
threadRunnable_ = true;
-
-
while (threadRunnable_) {
-
glObject_.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-
glObject_.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
-
-
glObject_.glMatrixMode(GL10.GL_MODELVIEW);
-
glObject_.glLoadIdentity();
-
glObject_.glTranslatef(0.0f, 0.0f, -1.0f);
-
-
glObject_.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuff);
-
glObject_.glEnableClientState(GL10.GL_VERTEX_ARRAY);
-
glObject_.glEnableClientState(GL10.GL_COLOR_ARRAY);
-
glObject_.glColorPointer(3, GL10.GL_FLOAT, 0, colors);
-
glObject_.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
-
-
glContext_.post();
-
-
try {
-
-
-
Log.i("userlog", e.toString());
-
}
-
}
-
glContext_.makeCurrent((SurfaceHolder)null);
-
}
-
-
-
@Override
-
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
-
Log.i("userlog", "-- SurfaceTest.surfaceChanged");
-
}
-
-
-
@Override
-
public void surfaceCreated(SurfaceHolder holder) {
-
Log.i("userlog", "-- SurfaceTest.surfaceCreated");
-
-
thread_.start();
-
}
-
-
-
@Override
-
public void surfaceDestroyed(SurfaceHolder holder) {
-
Log.i("userlog", "-- Surface.surfaceDestroyed");
-
threadRunnable_ = false;
-
try {
-
thread_.join();
-
}
-
-
Log.i("userlog", e.toString());
-
}
-
thread_ = null;
-
}
-
}