11-OpenGLES绘制颜色立方体
程序员文章站
2022-12-03 10:25:11
public class MyActivity extends Activity {private MyRenderer render;private MyGLSurfaceView view;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);view = new MyGLSurfaceView(this);render = ne......
public class MyActivity extends Activity {
private MyRenderer render;
private MyGLSurfaceView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MyGLSurfaceView(this);
render = new MyRenderer();
view.setEGLConfigChooser(5, 6, 5, 0, 8, 8);
view.setRenderer(render);
view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);// 脏渲染,命令渲染
setContentView(view);
}
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
class MyRenderer implements GLSurfaceView.Renderer {
private float xrotate, yrotate;
private float ratio;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 清平色
gl.glClearColor(0f, 0f, 0f, 1f);
// 启用顶点缓冲区数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 颜色缓冲区
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// 设置视口
gl.glViewport(0, 0, width, height);
ratio = (float) width / (float) height;
// 投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION);
// 加载单位矩阵
gl.glLoadIdentity();
// 设置平截头体
gl.glFrustumf(-ratio, ratio, -1, 1, 3f, 7f);
// 正交投影
// gl.glOrthof(-ratio, ratio, -1, 1, 3f, 7f);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(0f, 0f, 1f, 1f);
// 模型视图矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
// 旋转
gl.glRotatef(xrotate, 1, 0, 0);
gl.glRotatef(yrotate, 0, 1, 0);
float r = 0.4f;
// 定义点坐标
float[] coords = { -r, r, r, // front left up
-r, -r, r, // front let bottom
r, r, r, // front right up
r, -r, r, // front right bottom
-r, r, -r, // back left up
-r, -r, -r, // back let bottom
r, r, -r, // back right up
r, -r, -r, // back right bottom
};
// 顶点的索引顺序
byte[] indices = { 0, 1, 2, 2, 1, 3, // front
4, 5, 6, 6, 5, 7, // back
0, 1, 4, 4, 1, 5, // left
2, 3, 6, 6, 3, 7, // right
4, 0, 2, 4, 2, 6, // top
5, 1, 3, 5, 3, 7// bottom
};
// 颜色
float[] colors = { 0f, 1f, 1f, 1f, // 青色
0, 1, 0, 1, // 红色
1, 1, 1, 1, // 白色
1, 1, 0, 1, // 黄色
0, 0, 1, 1, // 4
0, 0, 0, 1, // 5
1, 0, 1, 1, // 6
1, 0, 0, 1// 7
};
gl.glColorPointer(4, GL10.GL_FLOAT, 0, arr2FloatBuffer(colors));
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, arr2FloatBuffer(coords));
// 使用顶点索引方式绘制(三角形集方式)
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE,
BufferUtil.arr2ByteBuffer(indices));
}
}
private FloatBuffer arr2FloatBuffer(float[] coords) {
ByteBuffer ibb = ByteBuffer.allocateDirect(coords.length * 4);
ibb.order(ByteOrder.nativeOrder());
FloatBuffer fbb = ibb.asFloatBuffer();
fbb.put(coords);
fbb.position(0);
return fbb;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
float step = 5f;
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
render.xrotate -= step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
render.xrotate += step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
render.yrotate += step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
render.yrotate -= step;
}
// 请求渲染,和脏渲染配合使用
view.requestRender();
return super.onKeyDown(keyCode, event);
}
}
本文地址:https://blog.csdn.net/itzyjr/article/details/107380322
上一篇: 推荐阅读博客九周年:稳中求胜 持续发展
下一篇: 有好车开