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
原创粉丝点击