您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > Android OpenGL ES教程
第一课OpenGlES介绍Android用的是OpenGL图形函数库,它可以跨平台。OpenGL的定义:OpenGL(OpenGraphicsLibrary)是个定义了一个跨编程语言、跨平台的编程接口的规格,它用于三维图象(二维的亦可)。OpenGL是个专业的图形程序接口,是一个功能强大,调用方便的底层图形库。Android所用到的是它的一个子集OpenGLES(OpenGLforEmbeddedSystems(嵌入式系统))就是OpenGL的嵌入式版本。1.主activityJava代码1.packagecom.jftt.opengl;2.3.importandroid.app.Activity;4.importandroid.os.Bundle;5.6.publicclassOpenGLextendsActivity{7.privatestaticfinalStringTAG=OpenGL.class.getSimpleName();8.privateVortexViewvortexView;9.10.@Override11.publicvoidonCreate(BundlesavedInstanceState){12.super.onCreate(savedInstanceState);13.vortexView=newVortexView(this);14.setContentView(vortexView);15.}16.17.@Override18.protectedvoidonPause(){19.super.onPause();20.vortexView.onPause();21.}22.23.@Override24.protectedvoidonResume(){25.super.onResume();26.vortexView.onResume();27.}28.}2.继承GLSurfaceView类的自己的ViewJava代码1.packagecom.jftt.opengl;2.3.importandroid.content.Context;4.importandroid.opengl.GLSurfaceView;5.importandroid.view.MotionEvent;6.7.publicclassVortexViewextendsGLSurfaceView{8.privatestaticfinalStringLOG_TAG=VortexView.class.getSimpleName();9.privateVortexRendererrenderer;10.11.publicVortexView(Contextcontext){12.super(context);13.renderer=newVortexRenderer();14.setRenderer(renderer);15.}16.17.@Override18.publicbooleanonTouchEvent(finalMotionEventevent){19.queueEvent(newRunnable(){20.@Override21.publicvoidrun(){22.renderer.setColor(event.getX()/getWidth(),event.getY()/getHeight(),1.0f);23.renderer.setAngle(event.getX()/10);24.}25.});26.returnsuper.onTouchEvent(event);27.}28.}3.实现渲染器renderer类Java代码1.packagecom.jftt.opengl;2.3.importjava.nio.ByteBuffer;4.importjava.nio.ByteOrder;5.importjava.nio.FloatBuffer;6.importjava.nio.ShortBuffer;7.8.importjavax.microedition.khronos.egl.EGLConfig;9.importjavax.microedition.khronos.opengles.GL10;10.11.importandroid.opengl.GLSurfaceView;12.13.publicclassVortexRendererimplementsGLSurfaceView.Renderer{14.privatestaticfinalStringTAG=VortexRenderer.class.getSimpleName();15.16.privatefloatred=0f;17.privatefloatgreen=0f;18.privatefloatblue=0f;19.20.privateShortBufferindexBuffer;21.privateFloatBuffervertexBuffer;22.privateshort[]indicesArray={0,1,2};23.privateintnumberOfVertices=3;24.25.privatefloatangle;26.27.privatevoidinitTriangle(){28.ByteBuffervbb=ByteBuffer.allocateDirect(numberOfVertices*3*4);29.vbb.order(ByteOrder.nativeOrder());30.vertexBuffer=vbb.asFloatBuffer();31.32.ByteBufferibb=ByteBuffer.allocateDirect(numberOfVertices*2);33.ibb.order(ByteOrder.nativeOrder());34.indexBuffer=ibb.asShortBuffer();35.36.float[]coords={-0.5f,-0.5f,0f,0.5f,-0.5f,0f,0f,0.5f,0f};37.38.vertexBuffer.put(coords);39.indexBuffer.put(indicesArray);40.41.vertexBuffer.position(0);42.indexBuffer.position(0);43.}44.45.publicvoidsetColor(floatred,floatgreen,floatblue){46.this.red=red;47.this.green=green;48.this.blue=blue;49.}50.51.publicvoidsetAngle(floatangle){52.this.angle=angle;53.}54.55.@Override56.publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){57.gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);58.initTriangle();59.}60.61.@Override62.publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){63.gl.glViewport(0,0,width,height);64.}65.66.@Override67.publicvoidonDrawFrame(GL10gl){68.gl.glClearColor(red,green,blue,1.0f);69.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);70.gl.glRotatef(angle,0f,1f,0f);71.gl.glColor4f(0.5f,0f,0f,0.5f);72.gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);73.gl.glDrawElements(GL10.GL_TRIANGLES,numberOfVertices,GL10.GL_UNSIGNED_SHORT,indexBuffer);74.}75.}4.封装的三角形的类Java代码1.packagecom.jftt.opengl;2.3.importjava.nio.ByteBuffer;4.importjava.nio.ByteOrder;5.importjava.nio.FloatBuffer;6.importjava.nio.ShortBuffer;7.importjava.util.Random;8.9.publicclassTriangle{10.privateshort[]myIndecesArray={0,1,2};11.privateintnumberOfVertices=3;12.13.privatefloatred=0f;14.privatefloatgreen=0f;15.privatefloatblue=0f;16.17.//arawbuffertoholdindicesallowingareuseofpoints.18.privateShortBufferindexBuffer;19.//arawbuffertoholdthevertices20.privateFloatBuffervertexBuffer;21.privateRandomrandom=newRandom();22.23.publicTriangle(){24.red=random.nextFloat();25.red=red-(float)Math.floor(red);26.green=random.nextFloat();27.green=green-(float)Math.floor(green);28.blue=random.nextFloat();29.blue=blue-(float)Math.floor(blue);30.31.ByteBuffervbb=ByteBuffer.allocateDirect(numberOfVertices*3*4);32.vbb.order(ByteOrder.nativeOrder());33.vertexBuffer=vbb.asFloatBuffer();34.35.ByteBufferibb=ByteBuffer.allocateDirect(numberOfVertices*2);36.ibb.order(ByteOrder.nativeOrder());37.indexBuffer=ibb.asShortBuffer();38.39.float[]coords={randomValue(red),randomValue(red),40.randomValue(red),randomValue(green),randomValue(green),41.randomValue(green),randomValue(blue),randomValue(blue),42.randomValue(blue)};43.44.for(inti=0;inumberOfVertices;i++){45.for(intj=0;j3;j++){46.vertexBuffer.put(coords[i*3+j]);47.}48.}49.50.for(inti=0;i3;i++){51.indexBuffer.put(myIndecesArray[i]);52.}53.vertexBuffer.position(0);54.indexBuffer.position(0);55.}56.57.privatefloatrandomValue(floatvalue){58.inttmp=random.nextInt(2);59.if(
本文标题:Android OpenGL ES教程
链接地址:https://www.777doc.com/doc-3301754 .html