package com.zlb.DemoRect2;
import static android.opengl.GLES10.GL_CCW;
import static android.opengl.GLES10.GL_FLOAT;
import static android.opengl.GLES10.GL_TEXTURE_2D;
import static android.opengl.GLES10.GL_TRIANGLE_STRIP;
import static android.opengl.GLES10.GL_UNSIGNED_SHORT;
import static android.opengl.GLES10.glDrawElements;
import static android.opengl.GLES10.glEnable;
import static android.opengl.GLES10.glFrontFace;
import static android.opengl.GLES10.glTexCoordPointer;
import static android.opengl.GLES10.glVertexPointer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
public class DemoRect2Activity extends Activity {
GLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(new RectRenderer());
setContentView(mGLSurfaceView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mGLSurfaceView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mGLSurfaceView.onResume();
}
}
class RectRenderer implements Renderer {
private Triangle mTriangle;
private long mLastTime;
public RectRenderer() {
mTriangle = new Triangle();
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// gl.glTranslatef(0.0f, 0.0f, -8.0f);
GLU.gluLookAt(gl, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, 0f);
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
Log.d("zhang", "///////////////////////////////////////=======angle = "+angle);
gl.glRotatef(angle, 0, 1f, 0);
mTriangle.draw(gl);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) {
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
Log
.d("zhanglibin",
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
// 100.0f);
// gl.glFrustumf(-(float)width/height,(float)width/height, -1f, 1f, 2f, 100f);
// GLU.gluOrtho2D(gl, 0f, (float)width, 0f, (float)height);
// gl.glOrthof(-(float)width/2, (float)width/2, -(float)height/2,
// (float)height/2, -1, 100f);
GLU.gluOrtho2D(gl, -(float)width/2, (float)width/2, -(float)height/2,
(float)height/2);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepthf(1.0f);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
class Triangle {
public Triangle() {
ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
tbb.order(ByteOrder.nativeOrder());
mTexBuffer = tbb.asFloatBuffer();
ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
ibb.order(ByteOrder.nativeOrder());
mIndexBuffer = ibb.asShortBuffer();
// float[] coords = {
// // X, Y, Z
// -0.5f, -0.25f, 0, 0.5f, -0.25f, 0, 0.0f, 0.559016994f, 0 };
// float[] coords = {
// // X, Y, Z
// 0, 0, -20, 1, 0, -20, 0.5f, 1, -20 };
float[] coords = {
// X, Y, Z
100f, -100f, 0f, -100f, -100f, 0f, 100f, 100f, 0f, -100f, 100f, 0f};
for (int i = 0; i < VERTS; i++) {
for (int j = 0; j < 3; j++) {
mFVertexBuffer.put(coords[i * 3 + j] * 2.0f);
}
}
for (int i = 0; i < VERTS; i++) {
for (int j = 0; j < 2; j++) {
mTexBuffer.put(coords[i * 3 + j] * 2.0f + 0.5f);
}
}
for (int i = 0; i < VERTS; i++) {
mIndexBuffer.put((short) i);
}
mFVertexBuffer.position(0);
mTexBuffer.position(0);
mIndexBuffer.position(0);
}
public float getWidth() {
return 0f;
}
public void draw(GL10 gl) {
glFrontFace(GL_CCW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);
glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLE_STRIP, VERTS, GL_UNSIGNED_SHORT,
mIndexBuffer);
}
private final static int VERTS = 4;
private FloatBuffer mFVertexBuffer;
private FloatBuffer mTexBuffer;
private ShortBuffer mIndexBuffer;
}
旋转效果不咋滴,和GLU.gluOrtho2D这个方法有关,因为这个方法是正投影,如果把这个方法替换为GLU.gluPerspective或者gl.glFrustumf则效果正常。