/** Called when the activity is first created. */
package com.MyOpengl;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
public class MyRender implements Renderer{
Cube mCube ;
Context mContext ;
public MyRender(Context mContext)
{
this.mContext=mContext ;
mCube = new Cube(mContext) ;
}
@Override
public void onDrawFrame(GL10 gl) {
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT) ;
mCube.OnDraw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
float ratio = (float) width/height ;
//设置Opengl场景的大小
gl.glViewport(0, 0, width, height) ;
//设置投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION) ;
//重置投影矩阵
gl.glLoadIdentity() ;
//设置视口的大小
gl.glFrustumf(-ratio, ratio, -1, 1,1, 10) ;
//选择模型观察矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW) ;
//重置模型观察矩阵
gl.glLoadIdentity() ;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH) ;
//黑色背景
gl.glClearColor(0, 0, 0, 0) ;
//设置深度缓存
gl.glClearDepthf(1.0f) ;
//启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST) ;
//所作深度测试类型
gl.glDepthFunc(GL10.GL_LEQUAL) ;
//告诉系统对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST) ;
//初始化 纹理
mCube.initTexture2D(gl);
}
}
package com.MyOpengl;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class Cube {
Context mContext;
float roteangle;
FloatBuffer CubeVetBuffer;
FloatBuffer TextureVetBuffer;
int mTextureID;
float[] TextureVerex = new float[] {
// front
1f, 0, 0, 0, 0, 1f, 1f, 1f,
// right
0, 0, 0, 1f, 1f, 1f, 1f, 0,
// left
1f, 0, 0, 0, 0, 1f, 1f, 1f,
// back
0, 0, 0, 1f, 1f, 1f, 1f, 0,
// bottom
0, 1f, 1f, 1f, 1f, 0, 0, 0,
// top
1f, 1f, 1f, 0, 0, 0, 0, 1f };
float[] CubeVertex = new float[] {
// front
0.0f, 1.0f, 1.0f,//
1.0f, 1.0f, 1.0f,//
1.0f, 0.0f, 1.0f,//
0.0f, 0.0f, 1.0f,//
// right
1.0f, 1.0f, 1.0f, //
1.0f, 1.0f, 0.0f, //
1.0f, 0.0f, 0.0f,//
1.0f, 0.0f, 1.0f,//
// left
0.0f, 1.0f, 1.0f, //
0.0f, 0.0f, 1.0f, //
0.0f, 0.0f, 0.0f,//
0.0f, 1.0f, 0.0f,//
// back
0.0f, 0.0f, 0.0f,//
1.0f, 0.0f, 0.0f,//
1.0f, 1.0f, 0.0f,//
0.0f, 1.0f, 0.0f,//
// bottom
0.0f, 0.0f, 0.0f,//
1.0f, 0.0f, 0.0f,//
1.0f, 0.0f, 1.0f,//
0.0f, 0.0f, 1.0f,//
// top
0.0f, 1.0f, 1.0f,//
0.0f, 1.0f, 0.0f,//
1.0f, 1.0f, 0.0f,//
1.0f, 1.0f, 1.0f //
};
public Cube(Context mContext) {
this.mContext = mContext;
// 分配Buffer给声明的FloatBuffer
ByteBuffer vbb = ByteBuffer.allocateDirect(CubeVertex.length * 4);
vbb.order(ByteOrder.nativeOrder());
CubeVetBuffer = vbb.asFloatBuffer();
ByteBuffer tbb = ByteBuffer.allocateDirect(TextureVerex.length * 4);
tbb.order(ByteOrder.nativeOrder());
TextureVetBuffer = tbb.asFloatBuffer();
// FloatBuffer赋值
CubeVetBuffer.put(CubeVertex);
TextureVetBuffer.put(TextureVerex);
CubeVetBuffer.position(0);
TextureVetBuffer.position(0);
}
public void OnDraw(GL10 gl) {
gl.glLoadIdentity();
// 一般在绘制开始时,使用glLoadIdentity()函数,在绘制中一般使用glPushMatrix()和glPopMatrix()控制一个图元
gl.glPushMatrix();
gl.glTranslatef(0f, 0f, -2.5f);
gl.glRotatef(roteangle, 0, 1, 0);
gl.glRotatef(10, 1, 0, 0);
// 绑定纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, CubeVetBuffer);
// 指定纹理映射,由于是2D纹理所以第一个参数为2
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TextureVetBuffer);
// 绘制Cube
for (int i = 0; i < 6; i++)
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i * 4, 4);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();
roteangle += 0.5;
}
public void initTexture2D(GL10 gl) {
// 允许2D贴图,纹理
gl.glEnable(GL10.GL_TEXTURE_2D);
// 创建纹理
int textures[] = new int[1];
gl.glGenTextures(1, textures, 0);
mTextureID = textures[0];
// 设置要使用的纹理
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
Bitmap bitmap;
// 获取图片
InputStream is = mContext.getResources().openRawResource(
R.drawable.texture);
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
} catch (Exception e) {
}
}
// 生成纹理
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// 线性滤波,使得纹理从很远和很近时,显示都很平滑
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
// bitmap回收
bitmap.recycle();
}
}