Android3D绘图之OpenGL ES(四)
来源:互联网 发布:3d怎么优化模型减少面 编辑:程序博客网 时间:2024/04/29 17:56
下面我来绘制一个正在旋转的立方体,用到方法和技巧在前面的博客中都用过了,在这里不再赘述。直接看代码,给自己看一下,复习一遍。
package mobile.android.rotate.cube;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.FloatBuffer;import javax.microedition.khronos.opengles.GL10;public class Cube{private float v; private float[] cubeVertices;private FloatBuffer cubeVerticesBuffer;public Cube(float size){v = size / 2;cubeVertices = new float[]{// Z杞存??瑰?骞抽?-v, v, v, v, v, v,v, v, v,v,-v, v,v,-v, v,-v, -v, v,-v, -v, v,-v, v, v, // Z杞磋??瑰?骞抽?-v, v, -v,v, v, -v,v, v, -v,v,-v, -v,v,-v, -v,-v, -v, -v,-v, -v, -v,-v, v, -v,// 宸?晶骞抽?-v, v, -v,-v, v, v,-v, v, v,-v,-v, v,-v,-v, v,-v, -v, -v,-v, -v, -v,-v, v, -v,// ?充晶骞抽?v, v, -v,v, v, v,v, v, v,v,-v, v,v,-v, v,v, -v, -v,v, -v, -v,v, v, -v,// 椤堕?-v, v, v,v, v, v,v, v, v,v,v, -v,v,v, -v,-v, v, -v,-v, v, -v,-v, v, v,// 搴??-v, -v, v,v, -v, v,v, -v, v,v,-v, -v,v,-v, -v,-v, -v, -v,-v, -v, -v,-v, -v, v,};ByteBuffer byteBuffer = ByteBuffer.allocateDirect(cubeVertices.length * 4);byteBuffer.order(ByteOrder.nativeOrder());cubeVerticesBuffer = byteBuffer.asFloatBuffer();cubeVerticesBuffer.put(cubeVertices);cubeVerticesBuffer.position(0);}public void drawSelf(GL10 gl){gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeVerticesBuffer);// 缁??6涓?钩??/*for(int i = 0; i < 6; i++){//gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4 , 4);}*/for(int i = 0; i < 24; i++){gl.glDrawArrays(GL10.GL_LINES,i*2 , 2);}}}
package mobile.android.rotate.cube;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView.Renderer;public class CubeSurfaceView implements Renderer{private Cube cube;@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config){cube = new Cube(4);}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height){float ratio = (float) width / height;gl.glViewport(0, 0, width, height);gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();gl.glFrustumf(-ratio * 2, ratio * 2, -2, 2, 1, 10);gl.glMatrixMode(GL10.GL_MODELVIEW);}private int angle = 0;@Overridepublic void onDrawFrame(GL10 gl){gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glLoadIdentity();gl.glTranslatef(0.0f, 0.0f, -5.0f);gl.glRotatef(angle++, 1, 0,0);gl.glRotatef(angle++, 0, 1,0);gl.glRotatef(angle++, 0, 0,1); cube.drawSelf(gl); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}}
package mobile.android.rotate.cube;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;public class Main extends Activity{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);GLSurfaceView glSurfaceView = new GLSurfaceView(this);glSurfaceView.setRenderer(new CubeSurfaceView());setContentView(glSurfaceView);}}
下面来绘制一个有颜色的正方体
package mobile.android.color.rotate.cube;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig;import javax.microedition.khronos.opengles.GL10;import android.opengl.GLSurfaceView.Renderer;public class MyRender implements Renderer{float rotateQuad;int one = 0x10000;private IntBuffer colorBuffer;private int[] colors = new int[]{one / 2, one, 0, one, one / 2, one, 0, one, one / 2, one, 0, one, one / 2,one, 0, one,one, one / 2, 0, one, one, one / 2, 0, one, one, one / 2, 0, one,one, one / 2, 0, one, one, one, 0, one, one, one, 0, one, one, one,0, one, one, one, 0, one, one, 0, 0, one, one, 0, 0, one, one, 0,0, one, one, 0, 0, one,0, 0, one, one, 0, 0, one, one, 0, 0, one, one, 0, 0, one, one,one, 0, one, one, one, 0, one, one, one, 0, one, one, one, 0, one,one, };private IntBuffer quaterBuffer;private int[] quaterVertices = new int[]{ one, one, -one, -one, one, -one, one, one, one, -one, one, one,one, -one, one, -one, -one, one, one, -one, -one, -one, -one, -one,one, one, one, -one, one, one, one, -one, one, -one, -one, one,one, -one, -one, -one, -one, -one, one, one, -one, -one, one, -one,-one, one, one, -one, one, -one, -one, -one, one, -one, -one, -one,one, one, -one, one, one, one, one, -one, -one, one, -one, one, };@Overridepublic void onDrawFrame(GL10 gl){gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);gl.glLoadIdentity();gl.glTranslatef(0.0f, 0.0f, -4.0f);gl.glRotatef(rotateQuad, 1.0f, 0.0f, 0.0f);gl.glRotatef(rotateQuad, 0.0f, 1.0f, 0.0f);gl.glRotatef(rotateQuad, 0.0f, 0.0f, 1.0f);gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer);for (int i = 0; i < 6; i++){gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);}gl.glFinish();gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_COLOR_ARRAY);rotateQuad -= 1.0f;}@Overridepublic void onSurfaceChanged(GL10 gl, int width, int height){float ratio = (float) width / height;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();}@Overridepublic 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);ByteBuffer byteBuffer = ByteBuffer.allocateDirect(colors.length * 4);byteBuffer.order(ByteOrder.nativeOrder());colorBuffer = byteBuffer.asIntBuffer();colorBuffer.put(colors);colorBuffer.position(0);byteBuffer = ByteBuffer.allocateDirect(quaterVertices.length * 4);byteBuffer.order(ByteOrder.nativeOrder());quaterBuffer = byteBuffer.asIntBuffer();quaterBuffer.put(quaterVertices);quaterBuffer.position(0);}}
package mobile.android.color.rotate.cube;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;public class Main extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); GLSurfaceView glView = new GLSurfaceView(this);MyRender myRender = new MyRender();glView.setRenderer(myRender);setContentView(glView);}}
0 0
- Android3D绘图之OpenGL ES(四)
- android3D绘图之OpenGL ES(一)
- android3D绘图之OpenGL ES(二)
- Android3D绘图之OpenGL ES(三)
- OpenGL ES之二 – 简单绘图
- 3D绘图之OpenGL ES
- OPENGL-ES之顶点索引绘图
- 使用Android OpenGL ES 2.0绘图之四:应用投影和相机视口
- Android OpenGL ES绘图教程之四 :投影和相机视图变换
- OpenGL ES 绘图基础
- OpenGL ES绘图
- OpenGL ES学习笔记之四
- 学习《Android3D游戏开发技术宝典:OpenGL.ES.2.0》一:初识OpenGL ES 2.0应用程序
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- 从零开始学习OpenGL ES之二 – 简单绘图概述
- django操作数据库例子
- 受打击
- 使用RPM包管理命令的笔记
- linux 8080端口占用
- Android--Apache HttpClient
- Android3D绘图之OpenGL ES(四)
- git学习四(远程分支)
- 17个新手常见Python运行时错误
- Quick-Cocos2d-X 捋一捋框架流程
- 黑马程序员--------------高新技术之内省和泛型
- js触发input onchange事件
- 关于软件测试的几点反思 - 关于测试团队的组织
- 资源的加载、简易的资源管理器
- “如果不是因为你救过我,我就算打不过你,也早跟你拼命了”,杨辰沙哑着说道,死死盯着蒙逍遥,如同黑暗中的一头恶狼。