首先画个三角形
private float[] mTriangleArray={
0.0f,1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f
};
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, mTriangleArray.length/3);
然后画个两个三角形拼成一个正方形,把之前的坐标换一下就好了
private float[] mTriangleArray={
1.0f,1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f,
-1.0f,1.0f,0.0f,
1.0f,1.0f,0.0f
};
看来glDrawArrays是三个三个坐标来画的
我现在使用glDrawElements这个函数来画正方形,这函数不仅可以画多方形,而且还可以画3D
private float[] mTriangleArray={
1.0f,1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f,
-1.0f,1.0f,0.0f
};
private float[] mColor ={
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f
};
private byte[] cubeFacets = new byte[]{
0, 1, 2,
0, 2, 3
};
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP
, cubeFacetsBuffer.remaining(),
GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);
这函数的意思是你传他画三个点的坐标组的位置(第一个从0开始,一个坐标组有三个float),比如这个cubeFacets就是说0、1、2三个坐标连起来画个三角形,然后0、2、3坐标画个三角形。
以下是完整代码
public class MainActivity extends Activity {
private GLSurfaceView mGLView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mGLView = new GLSurfaceView(this); //这里使用的是自定义的GLSurfaceView的子类
mGLView.setRenderer(new GLRenderer());
setContentView(mGLView);
}
public void onPause(){
super.onPause();
mGLView.onPause();
}
public void onResume(){
super.onResume();
mGLView.onResume();
}
public class GLRenderer implements GLSurfaceView.Renderer {
private float[] mTriangleArray={
1.0f,1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,-1.0f,0.0f,
-1.0f,1.0f,0.0f
};
private float[] mColor ={
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f
};
private byte[] cubeFacets = new byte[]{
0, 1, 2,
0, 2, 3
};
private FloatBuffer mTriangleBuffer;
private FloatBuffer mColorBuffer;
ByteBuffer cubeFacetsBuffer;
public GLRenderer() {
mTriangleBuffer = floatBufferUtil(mTriangleArray);
mColorBuffer = floatBufferUtil(mColor);
cubeFacetsBuffer = ByteBuffer.wrap(cubeFacets);
}
@Override
public void onDrawFrame(GL10 gl) {
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 设置当前矩阵模式为模型视图。
gl.glMatrixMode(GL10.GL_MODELVIEW);
// 允许设置顶点
//GL10.GL_VERTEX_ARRAY顶点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 允许设置颜色
//GL10.GL_COLOR_ARRAY颜色数组
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 重置当前的模型观察矩阵
gl.glLoadIdentity();
//将三角形在z轴上移动
gl.glTranslatef(0f, 0.0f, -2.0f);
// 设置三角形
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
// 设置三角形颜色
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
// 绘制三角形
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP
, cubeFacetsBuffer.remaining(),
GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);
// 取消颜色设置
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// 取消顶点设置
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//绘制结束
gl.glFinish();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
float ratio = (float) width / height;
// 设置OpenGL场景的大小,(0,0)表示窗口内部视口的左下角,(w,h)指定了视口的大小
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.glClearColor(1, 1, 1, 1);
}
}
// 定义一个工具方法,将float[]数组转换为OpenGL ES所需的FloatBuffer
private FloatBuffer floatBufferUtil(float[] arr)
{
FloatBuffer mBuffer;
// 初始化ByteBuffer,长度为arr数组的长度*4,因为一个int占4个字节
ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
// 数组排列用nativeOrder
qbb.order(ByteOrder.nativeOrder());
mBuffer = qbb.asFloatBuffer();
mBuffer.put(arr);
mBuffer.position(0);
return mBuffer;
}
}
原文地址:http://blog.51cto.com/13591594/2067004
时间: 2024-10-10 10:33:23