AndroidOpenGL绘制3D立方体全解.docx

上传人:b****6 文档编号:6669288 上传时间:2023-01-08 格式:DOCX 页数:54 大小:115.81KB
下载 相关 举报
AndroidOpenGL绘制3D立方体全解.docx_第1页
第1页 / 共54页
AndroidOpenGL绘制3D立方体全解.docx_第2页
第2页 / 共54页
AndroidOpenGL绘制3D立方体全解.docx_第3页
第3页 / 共54页
AndroidOpenGL绘制3D立方体全解.docx_第4页
第4页 / 共54页
AndroidOpenGL绘制3D立方体全解.docx_第5页
第5页 / 共54页
点击查看更多>>
下载资源
资源描述

AndroidOpenGL绘制3D立方体全解.docx

《AndroidOpenGL绘制3D立方体全解.docx》由会员分享,可在线阅读,更多相关《AndroidOpenGL绘制3D立方体全解.docx(54页珍藏版)》请在冰豆网上搜索。

AndroidOpenGL绘制3D立方体全解.docx

AndroidOpenGL绘制3D立方体全解

AndroidOpenGLES案例总结

3D旋转立方体

彩色立方体

控制大小:

gl.glScalef(1.2f,1.2f,1.2f);//**********大小

控制旋转速度

roate+=0.3f;//********设置旋转角度的增量

packagecom.spl.canvasdemo31.renderer;

importandroid.opengl.GLSurfaceView.Renderer;

importjava.nio.ByteBuffer;

importjava.nio.ByteOrder;

importjava.nio.IntBuffer;

importjavax.microedition.khronos.egl.EGLConfig;

importjavax.microedition.khronos.opengles.GL10;

/**

*Cube渲染器类

*@authorspl

*

*/

publicclassCubeRendererimplementsRenderer{

publicclassMyCube{

privateIntBuffervertexBuffer;

privateIntBuffercolorBuffer;

privateByteBufferindexBuffer;

publicMyCube()

{

intone=65535;

//每个顶点的坐标值(x,y,z)

intvertex[]={

-one,-one,-one,

one,-one,-one,

one,one,-one,

-one,one,-one,

-one,-one,one,

one,-one,one,

one,one,one,

-one,one,one

};

//立方体每个顶点的颜色值,格式为RRGGBBAA

intcolors[]={

0,0,0,one,

one,0,0,one,

one,one,0,one,

0,one,0,one,

0,0,one,one,

one,0,one,one,

one,one,one,one,

0,one,one,one

};

//三角形顶点绘制顺序

byteindex[]={

0,4,5,0,5,1,

1,5,6,1,6,2,

2,6,7,2,7,3,

3,7,4,3,4,0,

4,7,6,4,6,5,

3,0,1,3,1,2

};

ByteBuffervbb=ByteBuffer.allocateDirect(vertex.length*4);

vbb.order(ByteOrder.nativeOrder());

vertexBuffer=vbb.asIntBuffer();

vertexBuffer.put(vertex);

vertexBuffer.position(0);

ByteBuffercbb=ByteBuffer.allocateDirect(colors.length*4);

cbb.order(ByteOrder.nativeOrder());

colorBuffer=cbb.asIntBuffer();

colorBuffer.put(colors);

colorBuffer.position(0);

indexBuffer=ByteBuffer.allocateDirect(index.length);

indexBuffer.put(index);

indexBuffer.position(0);

}

publicvoiddraw(GL10gl)

{

gl.glFrontFace(GL10.GL_CW);

gl.glVertexPointer(3,GL10.GL_FIXED,0,vertexBuffer);

gl.glColorPointer(4,GL10.GL_FIXED,0,colorBuffer);

gl.glDrawElements(GL10.GL_TRIANGLES,36,GL10.GL_UNSIGNED_BYTE,indexBuffer);

}

}

privateMyCubemyCube;

privatefloatroate;

publicCubeRenderer(){

myCube=newMyCube();

}

publicvoidonDrawFrame(GL10gl){

//填充屏幕

gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

//设置模型视景矩阵为当前操作矩阵

gl.glMatrixMode(GL10.GL_MODELVIEW);

//将坐标原点移动到屏幕中心

gl.glLoadIdentity();

//移动坐标系

gl.glTranslatef(0,0,-6.0f);

//在Y轴方向旋转坐标系

gl.glRotatef(roate,0,1,0);

//在X轴方向旋转坐标系

gl.glRotatef(roate*0.25f,1,0,0);

gl.glRotatef(roate,0,0,1);

/////////////////////////////////////

gl.glScalef(1.2f,1.2f,1.2f);//**********大小

///////////////////////////////////

//开启顶点坐标

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

//开启颜色

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

myCube.draw(gl);

roate+=0.3f;//********设置旋转角度的增量

}

publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){

gl.glViewport(0,0,width,height);

floatratio=(float)width/height;

gl.glMatrixMode(GL10.GL_PROJECTION);

gl.glLoadIdentity();

gl.glFrustumf(-ratio,ratio,-1,1,1,10);

}

publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){

gl.glEnable(GL10.GL_CULL_FACE);

gl.glClearColor(0.5F,0.5F,0.5F,1.0F);

}

}

 

纯色立方体

packagecom.spl.canvasdemo31.renderer;

importandroid.opengl.GLSurfaceView.Renderer;

importandroid.opengl.GLU;

importjava.nio.ByteBuffer;

importjava.nio.ByteOrder;

importjava.nio.FloatBuffer;

importjavax.microedition.khronos.egl.EGLConfig;

importjavax.microedition.khronos.opengles.GL10;

/**

*立方体(纯色)

*/

publicclassOpenGL3DRendererimplementsRenderer{

FloatBuffer_mCubeVertexBuffer;

//立方体顶点坐标

privatefloatbox[]={

//FRONT

-0.5f,-0.5f,0.5f,

0.5f,-0.5f,0.5f,

-0.5f,0.5f,0.5f,

0.5f,0.5f,0.5f,

//BACK

-0.5f,-0.5f,-0.5f,

-0.5f,0.5f,-0.5f,

0.5f,-0.5f,-0.5f,

0.5f,0.5f,-0.5f,

//LEFT

-0.5f,-0.5f,0.5f,

-0.5f,0.5f,0.5f,

-0.5f,-0.5f,-0.5f,

-0.5f,0.5f,-0.5f,

//RIGHT

0.5f,-0.5f,-0.5f,

0.5f,0.5f,-0.5f,

0.5f,-0.5f,0.5f,

0.5f,0.5f,0.5f,

//TOP

-0.5f,0.5f,0.5f,

0.5f,0.5f,0.5f,

-0.5f,0.5f,-0.5f,

0.5f,0.5f,-0.5f,

//BOTTOM

-0.5f,-0.5f,0.5f,

-0.5f,-0.5f,-0.5f,

0.5f,-0.5f,0.5f,

0.5f,-0.5f,-0.5f

};

privatefloatrotx;

privatefloatroty;

privatefloatrotz;

publicvoidonDrawFrame(GL10gl){

//清除屏幕

gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

//重置模型矩阵

gl.glLoadIdentity();

/***************这里设置颜色*********************/

gl.glColor4f(1f,0f,0f,1f);//纯色的****************

//开启顶点坐标功能

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

//设置顶点坐标

gl.glVertexPointer(3,GL10.GL_FLOAT,0,_mCubeVertexBuffer);

//保存matrix当前状态

gl.glPushMatrix();

//向屏幕移入5个单位

gl.glTranslatef(0.0f,0.0f,-5.0f);

//设置旋转轴,以x轴旋转

gl.glRotatef(rotx,1,0,0);

//设置旋转轴,以y轴旋转

gl.glRotatef(roty,0,1,0);

//设置旋转轴,以z轴旋转

gl.glRotatef(rotz,0,0,1);

//绘制第一个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);

//绘制第二个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,4,4);

//绘制第三个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,8,4);

//绘制第四个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,12,4);

//绘制第五个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,16,4);

//绘制第六个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,20,4);

//将matrix回复成上面push时的matrix

gl.glPopMatrix();

//关闭设置顶点功能

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

//使旋转角度不断变化

rotx+=0.2f;

roty+=0.3f;

rotz+=0.4f;

}

publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){

//设置场景大小

gl.glViewport(0,0,width,height);

//设置投影矩阵

gl.glMatrixMode(GL10.GL_PROJECTION);

//重置模型矩阵

gl.glLoadIdentity();

//设置窗口比例和透视图

GLU.gluPerspective(gl,45.0f,(float)width/(float)height,0.1f,100.0f);

//设置模型矩阵

gl.glMatrixMode(GL10.GL_MODELVIEW);

//重置模型矩阵

gl.glLoadIdentity();

}

publicvoidonSurfaceCreated(GL10gl,EGLConfigconfig){

//将顶点坐标转换为native类型的数据

_mCubeVertexBuffer=createFloatBuffer(box);

//平滑着色

gl.glShadeModel(GL10.GL_SMOOTH);

//设置黑色背景

gl.glClearColor(0.0f,0.0f,0.0f,0.5f);

//设置深度缓存

gl.glClearDepthf(1.0f);

//启动深度测试

gl.glEnable(GL10.GL_DEPTH_TEST);

//深度测试的类型

gl.glDepthFunc(GL10.GL_LEQUAL);

//告诉系统对透视进行修正

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);

}

 

//转换为Native类型的浮点缓存

publicstaticFloatBuffercreateFloatBuffer(floatdata[])

{

ByteBuffervbb=ByteBuffer.allocateDirect(data.length*4);

vbb.order(ByteOrder.nativeOrder());

FloatBufferoutBuffer=vbb.asFloatBuffer();

outBuffer.put(data).position(0);

returnoutBuffer;

}

}

贴图立方体

 

packagecom.spl.canvasdemo31.renderer;

importandroid.content.Context;

importandroid.graphics.Bitmap;

importandroid.opengl.GLSurfaceView.Renderer;

importandroid.opengl.GLU;

importandroid.opengl.GLUtils;

importcom.spl.canvasdemo31.R;

importcom.spl.canvasdemo31.util.Utility;

importjava.nio.FloatBuffer;

importjavax.microedition.khronos.egl.EGLConfig;

importjavax.microedition.khronos.opengles.GL10;

/**

*立方体(贴图)

*/

publicclassOpenGL3DRendererTeximplementsRenderer{

FloatBuffer_mCubeVertexBuffer;

FloatBuffer_mCubeTexBuffer;

Context_context;

publicOpenGL3DRendererTex(Contextcontext)

{

_context=context;

}

//立方体顶点坐标

privatefloatbox[]={

//FRONT

-0.5f,-0.5f,0.5f,

0.5f,-0.5f,0.5f,

-0.5f,0.5f,0.5f,

0.5f,0.5f,0.5f,

//BACK

-0.5f,-0.5f,-0.5f,

-0.5f,0.5f,-0.5f,

0.5f,-0.5f,-0.5f,

0.5f,0.5f,-0.5f,

//LEFT

-0.5f,-0.5f,0.5f,

-0.5f,0.5f,0.5f,

-0.5f,-0.5f,-0.5f,

-0.5f,0.5f,-0.5f,

//RIGHT

0.5f,-0.5f,-0.5f,

0.5f,0.5f,-0.5f,

0.5f,-0.5f,0.5f,

0.5f,0.5f,0.5f,

//TOP

-0.5f,0.5f,0.5f,

0.5f,0.5f,0.5f,

-0.5f,0.5f,-0.5f,

0.5f,0.5f,-0.5f,

//BOTTOM

-0.5f,-0.5f,0.5f,

-0.5f,-0.5f,-0.5f,

0.5f,-0.5f,0.5f,

0.5f,-0.5f,-0.5f

};

//立方体纹理坐标

privatefloattexCoords[]={

//FRONT

0.0f,0.0f,

1.0f,0.0f,

0.0f,1.0f,

1.0f,1.0f,

//BACK

1.0f,0.0f,

1.0f,1.0f,

0.0f,0.0f,

0.0f,1.0f,

//LEFT

1.0f,0.0f,

1.0f,1.0f,

0.0f,0.0f,

0.0f,1.0f,

//RIGHT

1.0f,0.0f,

1.0f,1.0f,

0.0f,0.0f,

0.0f,1.0f,

//TOP

0.0f,0.0f,

1.0f,0.0f,

0.0f,1.0f,

1.0f,1.0f,

//BOTTOM

1.0f,0.0f,

1.0f,1.0f,

0.0f,0.0f,

0.0f,1.0f

};

 

privatefloatrotx;

privatefloatroty;

privatefloatrotz;

publicvoidonDrawFrame(GL10gl){

//TODOAuto-generatedmethodstub

//清除屏幕

gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

//重置模型矩阵

gl.glLoadIdentity();

//开启顶点坐标功能

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

//设置顶点坐标

gl.glVertexPointer(3,GL10.GL_FLOAT,0,_mCubeVertexBuffer);

gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,_mCubeTexBuffer);

//保存matrix当前状态

gl.glPushMatrix();

//向屏幕移入5个单位

gl.glTranslatef(0.0f,0.0f,-5.0f);

//设置旋转轴,以x轴旋转

gl.glRotatef(rotx,1,0,0);

//设置旋转轴,以y轴旋转

gl.glRotatef(roty,0,1,0);

//设置旋转轴,以z轴旋转

gl.glRotatef(rotz,0,0,1);

/***************************************/

floatfscale=1.0f;

gl.glScalef(fscale,fscale,fscale);//大小

//----------------------------------------//

//绘制第一个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);

//绘制第二个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,4,4);

//绘制第三个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,8,4);

//绘制第四个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,12,4);

//绘制第五个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,16,4);

//绘制第六个立方体面

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,20,4);

//将matrix回复成上面push时的matrix

gl.glPopMatrix();

//关闭设置顶点功能

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

//使旋转角度不断变化

rotx+=0.2f;

roty+=0.3f;

rotz+=0.4f;

}

publicvoidonSurfaceChanged(GL10gl,intwidth,intheight){

//TODOAuto-g

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 解决方案

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1