画个三棱椎需要四个点,画四个面
private float[] mTriangleArray={
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
0.0f,1.0f,0.0f,
0.0f,0.0f,1.0f
};
// 定义三棱椎的4个顶点的颜色
private float[] mColor={
1.0f,0.0f,0.0f,0.0f,
0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
1.0f,1.0f,0.0f,0.0f,
};
private byte[] cubeFacets = new byte[]{
0, 1, 2,
0, 2, 3,
2, 1, 3,
0, 1, 3
};
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP
, cubeFacetsBuffer.remaining(),
GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);
如果我们要画个正方形就要八个点画六个面,其他代码不变
// 定义三棱椎的4个顶点的颜色
private float[] mColor={
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f
};
// 定义立方体的8个顶点
float[] cubeVertices = new float[] {
// 上顶面正方形的四个顶点
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
// 下底面正方形的四个顶点
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f
};
// 定义立方体所需要的6个面(一共是12个三角形所需的顶点)
private byte[] cubeFacets = new byte[]{
0, 2, 1,
0, 2, 3,
2, 7, 3,
2, 7, 6,
0, 7, 3,
0, 7, 4,
4, 6, 5,
4, 6, 7,
1, 4, 0,
1, 4, 5,
1, 6, 2,
1, 6, 5
};
然后加个旋转方便看清楚全貌
gl.glRotatef(rotate, 1f, 0f, 0f);
rotate++;
完整代码如下
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 {
// 定义三棱椎的4个顶点的颜色
private float[] mColor={
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f
};
// 定义立方体的8个顶点
float[] cubeVertices = new float[] {
// 上顶面正方形的四个顶点
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
// 下底面正方形的四个顶点
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f
};
// 定义立方体所需要的6个面(一共是12个三角形所需的顶点)
private byte[] cubeFacets = new byte[]{
0, 2, 1,
0, 2, 3,
2, 7, 3,
2, 7, 6,
0, 7, 3,
0, 7, 4,
4, 6, 5,
4, 6, 7,
1, 4, 0,
1, 4, 5,
1, 6, 2,
1, 6, 5
};
private FloatBuffer mTriangleBuffer;
ByteBuffer cubeFacetsBuffer;
FloatBuffer taperColorsBuffer;
public GLRenderer() {
mTriangleBuffer = floatBufferUtil(cubeVertices);
// 将三棱椎的四个定点的颜色数组包装成IntBuffer
taperColorsBuffer = 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);
// 沿着X轴旋转
gl.glRotatef(rotate, 1f, 0f, 0f);
// 设置三角形
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
// 设置顶点的颜色数据
gl.glColorPointer(4, GL10.GL_FIXED, 0, taperColorsBuffer);
// 绘制三角形
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();
rotate++;
}
int rotate = 0;
@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;
}
// 定义一个工具方法,将int[]数组转换为OpenGL ES所需的IntBuffer
private IntBuffer intBufferUtil(int[] arr)
{
IntBuffer mBuffer;
// 初始化ByteBuffer,长度为arr数组的长度*4,因为一个int占4个字节
ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
// 数组排列用nativeOrder
qbb.order(ByteOrder.nativeOrder());
mBuffer = qbb.asIntBuffer();
mBuffer.put(arr);
mBuffer.position(0);
return mBuffer;
}
}
原文地址:http://blog.51cto.com/13591594/2067005
时间: 2024-10-18 09:28:57