建立一个安卓工程,新建一个MyGLSurfaceView继承GLSurfaceView并实现GLSurfaceView.Renderer接口
MyGLSurfaceView类的代码
public class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer
{
//储存OpenGL对象用到的顶点坐标和颜色
private FloatBuffer mVertBuf,mVertColorBuf;
//储存OpenGL对象的顶点在mVertBuf中的索引
private ShortBuffer mIndexBuf;
//OpenGL对象用到的顶点坐标,在程序中会将它们设定给mVertBuf
//每一列是一个顶点的xyz坐标
private float[] m3DObjVert={
-0.5f,-0.5f,0.5f,
0.5f,-0.5f,0.5f,
0f,-0.5f,-0.5f,
0f,0.5f,0f
};
//OpenGL对象用到的顶点的颜色,在程序中会将它们设定给mVertColorBuf
//每一列是一个顶点的颜色,颜色值的顺序为rgba,a是alpha值
private float[] m3DObjVertColor={
1.0f,1.0f,0.0f,1.0f,
1.0f,0.0f,1.0f,1.0f,
0.0f,1.0f,1.0f,1.0f,
1.0f,1.0f,0.5f,1.0f,
};
//OpenGL对象的顶点在mVertBuf中的索引,每一列代表一个triangle
//在程序中会将它们设定给mIndexBuf
private short[] m3DObjVertIndex={
0,2,1,
3,2,0,
3,1,2,
1,3,0
};
//最远方的底色
private float backColorR=0.3f,backColorG=0.2f,backColorB=0.1f,backColorA=1.0f;
private float mfRotaAng=0f;
private void setup()
{
//建立OpenGL专用的vertex buffer
ByteBuffer verBuf = ByteBuffer.allocateDirect(4*m3DObjVert.length);
verBuf.order(ByteOrder.nativeOrder());
mVertBuf=verBuf.asFloatBuffer();
//建立OpenGL专用的vertex color buffer
ByteBuffer verColorBuf = ByteBuffer.allocateDirect(4*m3DObjVertColor.length);
verColorBuf.order(ByteOrder.nativeOrder());
mVertColorBuf=verColorBuf.asFloatBuffer();
//建立OpenGL专用的index buffer
ByteBuffer indexBuf = ByteBuffer.allocateDirect(2*m3DObjVertIndex.length);
indexBuf.order(ByteOrder.nativeOrder());
mIndexBuf=indexBuf.asShortBuffer();
mVertBuf.put(m3DObjVert);
mVertColorBuf.put(m3DObjVertColor);
mIndexBuf.put(m3DObjVertIndex);
mVertBuf.position(0);
mVertColorBuf.position(0);
mIndexBuf.position(0);
}
public MyGLSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
setRenderer(this);
}
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
//清除场景填上背景颜色,并且清除z-buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
//设定对象用到的顶点坐标
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertBuf);
//设定对象用到的顶点颜色
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mVertColorBuf);
gl.glLoadIdentity();
//将对象沿指定的轴移动
gl.glTranslatef(0f, 0f, -3f);
//将对象沿着指定的轴旋转
gl.glRotatef(mfRotaAng, 0f, 1f, 0f);
mfRotaAng+=1f;
//设定对象顶点的索引并绘制对象
gl.glDrawElements(GL10.GL_TRIANGLES, m3DObjVertIndex.length, GL10.GL_UNSIGNED_SHORT, mIndexBuf);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
//设定透视投影的参数
//设定最近和最远的可视范围和左右视角
//上下可视范围由左右视角和屏幕的宽高比来计算
final float fNEAREST=0.1f,
fFAREST=100f,
fVIEW_ANGLE=45f;
gl.glMatrixMode(GL10.GL_PROJECTION);//切换到投影矩阵模式
float fViewWidth = fNEAREST*(float)Math.tan(Math.toRadians(fVIEW_ANGLE)/2);
float aspectRatio = (float)width/(float)height;
gl.glFrustumf(-fViewWidth, fViewWidth, -fViewWidth/aspectRatio, fViewWidth/aspectRatio, fNEAREST, fFAREST);
gl.glMatrixMode(GL10.GL_MODELVIEW);//切换到原来的模式
gl.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
//设定3D场景的背景颜色,也就是clipping wall的颜色
gl.glClearColor(backColorR, backColorB, backColorG, backColorA);
//设定OpenGL的功能
gl.glEnable(GL10.GL_DEPTH_TEST);//物体远近的遮蔽效果
gl.glEnable(GL10.GL_CULL_FACE);//区分Triangle的正反面
gl.glFrontFace(GL10.GL_CCW);//逆时针顶点顺序为正面
gl.glCullFace(GL10.GL_BACK);//反面的Triangle不显示
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//使用顶点数组
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//使用颜色数组
setup();
}
}
在主类中添加以下黑体的代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
MyGLSurfaceView glSurfView = new MyGLSurfaceView(this);
setContentView(glSurfView);
}
}