android 3D 圆柱体

09-22 2627人

[java][/java] view plaincopy
  1. package wyf.lgz;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.util.ArrayList;
  6. import javax.microedition.khronos.opengles.GL10;
  7. public class DrawCylinder
  8. {
  9.     private FloatBuffer myVertexBuffer;//顶点坐标缓冲
  10.     private FloatBuffer myNormalBuffer;//法向量缓冲
  11.     private FloatBuffer myTexture;//纹理缓冲
  12.     int textureId;
  13.     int vCount;//顶点数量
  14.     float length;//圆柱长度
  15.     float circle_radius;//圆截环半径
  16.     float degreespan;  //圆截环每一份的度数大小
  17.     public float mAngleX;
  18.     public float mAngleY;
  19.     public float mAngleZ;
  20.     public DrawCylinder(float length,float circle_radius,float degreespan,int textureId)
  21.     {
  22.         this.circle_radius=circle_radius;
  23.         this.length=length;
  24.         this.degreespan=degreespan;
  25.         this.textureId=textureId;
  26.         float collength=(float)length;//圆柱每块所占的长度
  27.         int spannum=(int)(360.0f/degreespan);
  28.         ArrayList<Float> val=new ArrayList<Float>();//顶点存放列表
  29.         ArrayList<Float> ial=new ArrayList<Float>();//法向量存放列表
  30.         for(float circle_degree=180.0f;circle_degree>0.0f;circle_degree-=degreespan)//循环行
  31.         {
  32.                 float x1 =(float)(-length/2);
  33.                 float y1=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
  34.                 float z1=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
  35.                 float a1=0;
  36.                 float b1=y1;
  37.                 float c1=z1;
  38.                 float l1=getVectorLength(a1, b1, c1);//模长
  39.                 a1=a1/l1;//法向量规格化
  40.                 b1=b1/l1;
  41.                 c1=c1/l1;
  42.                 float x2 =(float)(-length/2);
  43.                 float y2=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
  44.                 float z2=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
  45.                 float a2=0;
  46.                 float b2=y2;
  47.                 float c2=z2;
  48.                 float l2=getVectorLength(a2, b2, c2);//模长
  49.                 a2=a2/l2;//法向量规格化
  50.                 b2=b2/l2;
  51.                 c2=c2/l2;
  52.                 float x3 =(float)(length/2);
  53.                 float y3=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
  54.                 float z3=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
  55.                 float a3=0;
  56.                 float b3=y3;
  57.                 float c3=z3;
  58.                 float l3=getVectorLength(a3, b3, c3);//模长
  59.                 a3=a3/l3;//法向量规格化
  60.                 b3=b3/l3;
  61.                 c3=c3/l3;
  62.                 float x4 =(float)(length/2);
  63.                 float y4=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
  64.                 float z4=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
  65.                 float a4=0;
  66.                 float b4=y4;
  67.                 float c4=z4;
  68.                 float l4=getVectorLength(a4, b4, c4);//模长
  69.                 a4=a4/l4;//法向量规格化
  70.                 b4=b4/l4;
  71.                 c4=c4/l4;
  72.                 val.add(x1);val.add(y1);val.add(z1);//两个三角形,共6个顶点的坐标
  73.                 val.add(x2);val.add(y2);val.add(z2);
  74.                 val.add(x4);val.add(y4);val.add(z4);
  75.                 val.add(x2);val.add(y2);val.add(z2);
  76.                 val.add(x3);val.add(y3);val.add(z3);
  77.                 val.add(x4);val.add(y4);val.add(z4);
  78.                 ial.add(a1);ial.add(b1);ial.add(c1);//顶点对应的法向量
  79.                 ial.add(a2);ial.add(b2);ial.add(c2);
  80.                 ial.add(a4);ial.add(b4);ial.add(c4);
  81.                 ial.add(a2);ial.add(b2);ial.add(c2);
  82.                 ial.add(a3);ial.add(b3);ial.add(c3);
  83.                 ial.add(a4);ial.add(b4);ial.add(c4);
  84.         }
  85.         vCount=val.size()/3;//确定顶点数量
  86.         //顶点
  87.         float[] vertexs=new float[vCount*3];
  88.         for(int i=0;i<vCount*3;i++)
  89.         {
  90.             vertexs[i]=val.get(i);
  91.         }
  92.         ByteBuffer vbb=ByteBuffer.allocateDirect(vertexs.length*4);
  93.         vbb.order(ByteOrder.nativeOrder());
  94.         myVertexBuffer=vbb.asFloatBuffer();
  95.         myVertexBuffer.put(vertexs);
  96.         myVertexBuffer.position(0);
  97.         //法向量
  98.         float[] normals=new float[vCount*3];
  99.         for(int i=0;i<vCount*3;i++)
  100.         {
  101.             normals[i]=ial.get(i);
  102.         }
  103.         ByteBuffer ibb=ByteBuffer.allocateDirect(normals.length*4);
  104.         ibb.order(ByteOrder.nativeOrder());
  105.         myNormalBuffer=ibb.asFloatBuffer();
  106.         myNormalBuffer.put(normals);
  107.         myNormalBuffer.position(0);
  108.         //纹理
  109.         float[] textures=generateTexCoor(spannum);
  110.         ByteBuffer tbb=ByteBuffer.allocateDirect(textures.length*4);
  111.         tbb.order(ByteOrder.nativeOrder());
  112.         myTexture=tbb.asFloatBuffer();
  113.         myTexture.put(textures);
  114.         myTexture.position(0);
  115.     }
  116.     public void drawSelf(GL10 gl)
  117.     {
  118.         gl.glRotatef(mAngleX, 1, 0, 0);//旋转
  119.         gl.glRotatef(mAngleY, 0, 1, 0);
  120.         gl.glRotatef(mAngleZ, 0, 0, 1);
  121.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//打开顶点缓冲
  122.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, myVertexBuffer);//指定顶点缓冲
  123.         gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);//打开法向量缓冲
  124.         gl.glNormalPointer(GL10.GL_FLOAT, 0, myNormalBuffer);//指定法向量缓冲
  125.         gl.glEnable(GL10.GL_TEXTURE_2D);
  126.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  127.         gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, myTexture);
  128.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
  129.         gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vCount);//绘制图像
  130.         gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//关闭缓冲
  131.         gl.glEnable(GL10.GL_TEXTURE_2D);
  132.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  133.         gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
  134.     }
  135.     //法向量规格化,求模长度
  136.     public float getVectorLength(float x,float y,float z)
  137.     {
  138.         float pingfang=x*x+y*y+z*z;
  139.         float length=(float) Math.sqrt(pingfang);
  140.         return length;
  141.     }
  142.     //自动切分纹理产生纹理数组的方法
  143.     public float[] generateTexCoor(int bh)
  144.     {
  145.         float[] result=new float[bh*6*2];
  146.         float REPEAT=2;
  147.         float sizeh=1.0f/bh;//行数
  148.         int c=0;
  149.         for(int i=0;i<bh;i++)
  150.         {
  151.                 //每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
  152.                 float t=i*sizeh;
  153.                 result[c language="++"][/c]=0;
  154.                 result[c language="++"][/c]=t;
  155.                 result[c language="++"][/c]=0;
  156.                 result[c language="++"][/c]=t+sizeh;
  157.                 result[c language="++"][/c]=REPEAT;
  158.                 result[c language="++"][/c]=t;
  159.                 result[c language="++"][/c]=0;
  160.                 result[c language="++"][/c]=t+sizeh;
  161.                 result[c language="++"][/c]=REPEAT;
  162.                 result[c language="++"][/c]=t+sizeh;
  163.                 result[c language="++"][/c]=REPEAT;
  164.                 result[c language="++"][/c]=t;
  165.         }
  166.         return result;
  167.     }
  168. }

 

[java][/java] view plaincopy
  1. package wyf.lgz;
  2. import android.app.Activity;
  3. import android.content.pm.ActivityInfo;
  4. import android.os.Bundle;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. public class Activity_GL_Cylinder extends Activity {
  8.     private MyGLSurfaceView mGLSurfaceView;
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  14.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  15.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  16.         mGLSurfaceView = new MyGLSurfaceView(this);
  17.         setContentView(mGLSurfaceView);
  18.         mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控
  19.         mGLSurfaceView.requestFocus();//获取焦点
  20.     }
  21.     @Override
  22.     protected void onResume() {
  23.         super.onResume();
  24.         mGLSurfaceView.onResume();
  25.     }
  26.     @Override
  27.     protected void onPause() {
  28.         super.onPause();
  29.         mGLSurfaceView.onPause();
  30.     }
  31. }

 

[java][/java] view plaincopy
  1. package wyf.lgz;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import android.opengl.GLSurfaceView;
  5. import android.opengl.GLUtils;
  6. import javax.microedition.khronos.egl.EGLConfig;
  7. import javax.microedition.khronos.opengles.GL10;
  8. import javax.microedition.khronos.opengles.GL11;
  9. import android.content.Context;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.view.MotionEvent;
  13. public class MyGLSurfaceView extends GLSurfaceView {
  14.     private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
  15.     private SceneRenderer mRenderer;//场景渲染器
  16.     private float mPreviousY;//上次的触控位置Y坐标
  17.     private float mPreviousX;//上次的触控位置Y坐标
  18.     private int lightAngle=90;//灯的当前角度
  19.     public MyGLSurfaceView(Context context) {
  20.         super(context);
  21.         mRenderer = new SceneRenderer();    //创建场景渲染器
  22.         setRenderer(mRenderer);             //设置渲染器
  23.         setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染
  24.     }
  25.     //触摸事件回调方法
  26.     @Override
  27.     public boolean onTouchEvent(MotionEvent e) {
  28.         float y = e.getY();
  29.         float x = e.getX();
  30.         switch (e.getAction()) {
  31.         case MotionEvent.ACTION_MOVE:
  32.             float dy = y - mPreviousY;//计算触控笔Y位移
  33.             float dx = x - mPreviousX;//计算触控笔Y位移
  34.             mRenderer.cylinder.mAngleX += dy * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度
  35.             mRenderer.cylinder.mAngleZ += dx * TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
  36.             requestRender();//重绘画面
  37.         }
  38.         mPreviousY = y;//记录触控笔位置
  39.         mPreviousX = x;//记录触控笔位置
  40.         return true;
  41.     }
  42.     private class SceneRenderer implements GLSurfaceView.Renderer
  43.     {
  44.         int textureId;//纹理名称ID
  45.         DrawCylinder cylinder;//创建圆柱体
  46.         public SceneRenderer()
  47.         {
  48.         }
  49.         public void onDrawFrame(GL10 gl) {
  50.             //清除颜色缓存
  51.             gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  52.             //设置当前矩阵为模式矩阵
  53.             gl.glMatrixMode(GL10.GL_MODELVIEW);
  54.             //设置当前矩阵为单位矩阵
  55.             gl.glLoadIdentity();
  56.             gl.glPushMatrix();//保护变换矩阵现场
  57.             float lx=0; //设定光源的位置
  58.             float ly=(float)(7*Math.cos(Math.toRadians(lightAngle)));
  59.             float lz=(float)(7*Math.sin(Math.toRadians(lightAngle)));
  60.             float[] positionParamsRed={lx,ly,lz,0};
  61.             gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParamsRed,0);
  62.             initMaterial(gl);//初始化纹理
  63.             gl.glTranslatef(0, 0, -10f);//平移
  64.             initLight(gl);//开灯
  65.             cylinder.drawSelf(gl);//绘制
  66.             closeLight(gl);//关灯
  67.             gl.glPopMatrix();//恢复变换矩阵现场
  68.         }
  69.         public void onSurfaceChanged(GL10 gl, int width, int height) {
  70.             //设置视窗大小及位置
  71.             gl.glViewport(0, 0, width, height);
  72.             //设置当前矩阵为投影矩阵
  73.             gl.glMatrixMode(GL10.GL_PROJECTION);
  74.             //设置当前矩阵为单位矩阵
  75.             gl.glLoadIdentity();
  76.             //计算透视投影的比例
  77.             float ratio = (float) width / height;
  78.             //调用此方法计算产生透视投影矩阵
  79.             gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100);
  80.         }
  81.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  82.             //关闭抗抖动
  83.             gl.glDisable(GL10.GL_DITHER);
  84.             //设置特定Hint项目的模式,这里为设置为使用快速模式
  85.             gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
  86.             //设置屏幕背景色黑色RGBA
  87.             gl.glClearColor(0,0,0,0);
  88.             //设置着色模型为平滑着色
  89.             gl.glShadeModel(GL10.GL_SMOOTH);
  90.             //启用深度测试
  91.             gl.glEnable(GL10.GL_DEPTH_TEST);
  92.             textureId=initTexture(gl,R.drawable.stone);//纹理ID
  93.             cylinder=new DrawCylinder(10f,2f,18f,textureId);//创建圆柱体
  94. //            //开启一个线程自动旋转光源
  95. //            new Thread()
  96. //            {
  97. //                public void run()
  98. //                {
  99. //                    while(true)
  100. //                    {
  101. //                      lightAngle+=5;//转动灯
  102. //                      mRenderer.cylinder.mAngleY+=2*TOUCH_SCALE_FACTOR;//球沿Y轴转动
  103. //                    requestRender();//重绘画面
  104. //                    try
  105. //                    {
  106. //                        Thread.sleep(50);//休息10ms再重绘
  107. //                    }
  108. //                    catch(Exception e)
  109. //                    {
  110. //                        e.printStackTrace();
  111. //                    }
  112. //                    }
  113. //                }
  114. //            }.start();
  115.         }
  116.     }
  117.     //初始化白色灯
  118.     private void initLight(GL10 gl)
  119.     {
  120.         gl.glEnable(GL10.GL_LIGHTING);//允许光照
  121.         gl.glEnable(GL10.GL_LIGHT1);//打开1号灯
  122.         //环境光设置
  123.         float[] ambientParams={0.2f,0.2f,0.2f,1.0f};//光参数 RGBA
  124.         gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientParams,0);
  125.         //散射光设置
  126.         float[] diffuseParams={1f,1f,1f,1.0f};//光参数 RGBA
  127.         gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseParams,0);
  128.         //反射光设置
  129.         float[] specularParams={1f,1f,1f,1.0f};//光参数 RGBA
  130.         gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, specularParams,0);
  131.     }
  132.     //关闭灯
  133.     private void closeLight(GL10 gl)
  134.     {
  135.         gl.glDisable(GL10.GL_LIGHT1);
  136.         gl.glDisable(GL10.GL_LIGHTING);
  137.     }
  138.     //初始化材质
  139.     private void initMaterial(GL10 gl)
  140.     {
  141.         //环境光
  142.         float ambientMaterial[] = {248f/255f, 242f/255f, 144f/255f, 1.0f};
  143.         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, ambientMaterial,0);
  144.         //散射光
  145.         float diffuseMaterial[] = {248f/255f, 242f/255f, 144f/255f, 1.0f};
  146.         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, diffuseMaterial,0);
  147.         //高光材质
  148.         float specularMaterial[] = {248f/255f, 242f/255f, 144f/255f, 1.0f};
  149.         gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, specularMaterial,0);
  150.         gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 100.0f);
  151.     }
  152.     //初始化纹理
  153.     public int initTexture(GL10 gl,int drawableId)//textureId
  154.     {
  155.         //生成纹理ID
  156.         int[] textures = new int[1];
  157.         gl.glGenTextures(1, textures, 0);
  158.         int currTextureId=textures[0];
  159.         gl.glBindTexture(GL10.GL_TEXTURE_2D, currTextureId);
  160.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR_MIPMAP_NEAREST);
  161.         gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR_MIPMAP_LINEAR);
  162.         ((GL11)gl).glTexParameterf(GL10.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE);
  163.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,GL10.GL_REPEAT);
  164.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,GL10.GL_REPEAT);
  165.         InputStream is = this.getResources().openRawResource(drawableId);
  166.         Bitmap bitmapTmp;
  167.         try
  168.         {
  169.             bitmapTmp = BitmapFactory.decodeStream(is);
  170.         }
  171.         finally
  172.         {
  173.             try
  174.             {
  175.                 is.close();
  176.             }
  177.             catch(IOException e)
  178.             {
  179.                 e.printStackTrace();
  180.             }
  181.         }
  182.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmapTmp, 0);
  183.         bitmapTmp.recycle();
  184.         return currTextureId;
  185.     }
  186. }
  • result[c language="++"][/c]=0;
                    result[c language="++"][/c]=t;
                    result[c language="++"][/c]=0;
                    result[c language="++"][/c]=t+sizeh;
                    result[c language="++"][/c]=REPEAT;
                    result[c language="++"][/c]=t;
                    result[c language="++"][/c]=0;
                    result[c language="++"][/c]=t+sizeh;
                    result[c language="++"][/c]=REPEAT;
                    result[c language="++"][/c]=t+sizeh;
                    result[c language="++"][/c]=REPEAT;
                    result[c language="++"][/c]=t;
    这是什么意思呀,写进去报错呀。android

  • 
    
    	
    色迷迷 哭 呕吐 大笑 口水 微笑 啵一个 发怒

    Hi,您需要填写昵称和邮箱!

    • 必填项
    • 必填项