Android的OpenGL学习笔记(6)
16lz
2021-12-04
Android的OpenGL学习笔记(6)
绘制多个3D模型,让你深刻体会3D空间,这里开启了深度测试功能:
Code:- packagecom.droidnova.android.games.vortex;
- importjava.nio.ByteBuffer;
- importjava.nio.ByteOrder;
- importjava.nio.FloatBuffer;
- importjava.nio.ShortBuffer;
- importjavax.microedition.khronos.egl.EGLConfig;
- importjavax.microedition.khronos.opengles.GL10;
- importandroid.opengl.GLSurfaceView;
- importandroid.util.Log;
- publicclassVortexRendererimplementsGLSurfaceView.Renderer{
- privatestaticfinalStringLOG_TAG=VortexRenderer.class.getSimpleName();
- //arawbuffertoholdindicesallowingareuseofpoints.
- privateShortBuffer_indexBuffer;
- //arawbuffertoholdthevertices
- privateFloatBuffer_vertexBuffer;
- //arawbuffertoholdthecolors
- privateFloatBuffer_colorBuffer;
- privateint_nrOfVertices=0;
- privatefloat_xAngle;
- privatefloat_yAngle;
- privatefloat_width=320f;
- privatefloat_height=480f;
- @Override
- publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){
- //preparation
- Log.i(LOG_TAG,"onSurfaceCreated()");
- gl.glMatrixMode(GL10.GL_PROJECTION);//透视模型
- floatsize=.01f*(float)Math.tan(Math.toRadians(45.0)/2);//角度制转换为弧度制,然后除以2,这里不是取整。然后求正切值
- floatratio=_width/_height;
- //perspective:
- gl.glFrustumf(-size,size,-size/ratio,size/ratio,0.01f,100.0f);
- //orthographic:
- //gl.glOrthof(-1,1,-1/ratio,1/ratio,0.01f,100.0f);
- gl.glViewport(0,0,(int)_width,(int)_height);
- gl.glMatrixMode(GL10.GL_MODELVIEW);//视图模型
- gl.glEnable(GL10.GL_DEPTH_TEST);//允许深度测试
- //definethecolorwewanttobedisplayedasthe"clippingwall"
- gl.glClearColor(0f,0f,0f,1.0f);
- //enablethedifferentiationofwhichsidemaybevisible
- gl.glEnable(GL10.GL_CULL_FACE);
- //whichisthefront?theonewhichisdrawncounterclockwise
- gl.glFrontFace(GL10.GL_CCW);
- //whichoneshouldNOTbedrawn
- gl.glCullFace(GL10.GL_BACK);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- initTriangle();
- }
- @Override
- publicvoidonSurfaceChanged(GL10gl,intw,inth){
- Log.i(LOG_TAG,"onSurfaceChanged()");
- _width=w;
- _height=h;
- gl.glViewport(0,0,w,h);
- }
- publicvoidsetXAngle(floatangle){
- _xAngle=angle;
- }
- publicfloatgetXAngle(){
- return_xAngle;
- }
- publicvoidsetYAngle(floatangle){
- _yAngle=angle;
- }
- publicfloatgetYAngle(){
- return_yAngle;
- }
- @Override
- publicvoidonDrawFrame(GL10gl){
- //clearthecolorbufferandthedepthbuffer
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
- gl.glVertexPointer(3,GL10.GL_FLOAT,0,_vertexBuffer);
- gl.glColorPointer(4,GL10.GL_FLOAT,0,_colorBuffer);
- for(inti=1;i<=10;i++){
- gl.glLoadIdentity();
- gl.glTranslatef(0.0f,-1f,-1.0f+-1.5f*i);
- //setrotation
- gl.glRotatef(-_xAngle,1f,0f,0f);
- gl.glRotatef(-_yAngle,0f,1f,0f);
- gl.glDrawElements(GL10.GL_TRIANGLES,_nrOfVertices,GL10.GL_UNSIGNED_SHORT,_indexBuffer);
- }
- }
- privatevoidinitTriangle(){
- float[]coords={
- -0.5f,-0.5f,0.5f,//0
- 0.5f,-0.5f,0.5f,//1
- 0f,-0.5f,-0.5f,//2
- 0f,0.5f,0f,//3
- };
- _nrOfVertices=coords.length;
- float[]colors={
- 1f,0f,0f,1f,//point0red
- 0f,1f,0f,1f,//point1green
- 0f,0f,1f,1f,//point2blue
- 1f,1f,1f,1f,//point3white
- };
- short[]indices=newshort[]{
- 0,1,3,//rwg
- 0,2,1,//rbg
- 0,3,2,//rbw
- 1,2,3,//bwg
- };
- //floathas4bytes,coordinate*4bytes
- ByteBuffervbb=ByteBuffer.allocateDirect(coords.length*4);
- vbb.order(ByteOrder.nativeOrder());
- _vertexBuffer=vbb.asFloatBuffer();
- //shorthas2bytes,indices*2bytes
- ByteBufferibb=ByteBuffer.allocateDirect(indices.length*2);
- ibb.order(ByteOrder.nativeOrder());
- _indexBuffer=ibb.asShortBuffer();
- //floathas4bytes,colors(RGBA)*4bytes
- ByteBuffercbb=ByteBuffer.allocateDirect(colors.length*4);
- cbb.order(ByteOrder.nativeOrder());
- _colorBuffer=cbb.asFloatBuffer();
- _vertexBuffer.put(coords);
- _indexBuffer.put(indices);
- _colorBuffer.put(colors);
- _vertexBuffer.position(0);
- _indexBuffer.position(0);
- _colorBuffer.position(0);
- }
- }
VortexView.java
Code:- packagecom.droidnova.android.games.vortex;
- importandroid.content.Context;
- importandroid.opengl.GLSurfaceView;
- importandroid.view.MotionEvent;
- publicclassVortexViewextendsGLSurfaceView{
- privatestaticfinalStringLOG_TAG=VortexView.class.getSimpleName();
- privateVortexRenderer_renderer;
- privatefloat_x=0;
- privatefloat_y=0;
- publicVortexView(Contextcontext){
- super(context);
- _renderer=newVortexRenderer();
- setRenderer(_renderer);
- }
- publicbooleanonTouchEvent(finalMotionEventevent){
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- _x=event.getX();
- _y=event.getY();
- }
- if(event.getAction()==MotionEvent.ACTION_MOVE){
- finalfloatxdiff=(_x-event.getX());
- finalfloatydiff=(_y-event.getY());
- queueEvent(newRunnable(){
- publicvoidrun(){
- _renderer.setXAngle(_renderer.getXAngle()+ydiff);
- _renderer.setYAngle(_renderer.getYAngle()+xdiff);
- }
- });
- _x=event.getX();
- _y=event.getY();
- }
- returntrue;
- }
- }
更多相关文章
- Android软件安全风险及规范
- Android事件处理的两种模型
- 新书出版:《Android深度探索(卷1):HAL与驱动开发》
- Android安全模型之Android安全机制(进程通信)
- 浅析Android线程模型
- 新书出版:《Android深度探索(卷1):HAL与驱动开发》
- 将tensorflow训练好的模型移植到android
- Android(安卓)中的消息模型(Message,MessageQueue,handle,looper
- 【android】布局之盒模型、对齐方式、填充(类比web理解)