Android应用: 3D旋转球

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/lla"
    >							<!-- LinearLayout布局 -->
	<RatingBar 
      android:id="@+id/RatingBar01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:max="5"
      android:rating="1"
      >
   </RatingBar>					<!-- 添加ToggleButton -->
</LinearLayout>

Ball类
package com.example.android_sample_5_2;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

public class Ball {
	private IntBuffer   mVertexBuffer;//顶点坐标数据缓冲
	private IntBuffer   mNormalBuffer;//顶点法向量数据缓冲
    private ByteBuffer  mIndexBuffer;//顶点构建索引数据缓冲
    public float mAngleX;//沿x轴旋转角度
    public float mAngleY;//沿y轴旋转角度 
    public float mAngleZ;//沿z轴旋转角度 
    int vCount=0;
    int iCount=0;
    public Ball(int scale)
    {
    	//顶点坐标数据的初始化================begin============================
    	final int UNIT_SIZE=10000;
    	ArrayList<Integer> alVertix=new ArrayList<Integer>();//存放顶点坐标的ArrayList
    	final int angleSpan=18;//将球进行单位切分的角度
        for(int vAngle=-90;vAngle<=90;vAngle=vAngle+angleSpan)//垂直方向angleSpan度一份
        {
        	for(int hAngle=0;hAngle<360;hAngle=hAngle+angleSpan)//水平方向angleSpan度一份
        	{//纵向横向各到一个角度后计算对应的此点在球面上的坐标
        		double xozLength=scale*UNIT_SIZE*Math.cos(Math.toRadians(vAngle));
        		int x=(int)(xozLength*Math.cos(Math.toRadians(hAngle)));
        		int z=(int)(xozLength*Math.sin(Math.toRadians(hAngle)));
        		int y=(int)(scale*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
        		//将计算出来的XYZ坐标加入存放顶点坐标的ArrayList
        		alVertix.add(x);alVertix.add(y);alVertix.add(z);
        	}
        } 	
        vCount=alVertix.size()/3;//顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
    	
        //将alVertix中的坐标值转存到一个int数组中
        int vertices[]=new int[vCount*3];
    	for(int i=0;i<alVertix.size();i++)
    	{
    		vertices[i]=alVertix.get(i);
    	}
		
        //创建顶点坐标数据缓冲
        //vertices.length*4是因为一个整数四个字节
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序
        mVertexBuffer = vbb.asIntBuffer();//转换为int型缓冲
        mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置     
        
        
        //创建顶点法向量数据缓冲
        //vertices.length*4是因为一个float四个字节
        ByteBuffer nbb = ByteBuffer.allocateDirect(vertices.length*4);
        nbb.order(ByteOrder.nativeOrder());//设置字节顺序
        mNormalBuffer = vbb.asIntBuffer();//转换为int型缓冲
        mNormalBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
        mNormalBuffer.position(0);//设置缓冲区起始位置
        
        
        //特别提示:由于不同平台字节顺序不同数据单元不是字节的一定要经过ByteBuffer
        //转换,关键是要通过ByteOrder设置nativeOrder(),否则有可能会出问题
        //顶点坐标数据的初始化================end============================
        
               
        //三角形构造索引数据初始化==========begin==========================
        ArrayList<Integer> alIndex=new ArrayList<Integer>();
        int row=(180/angleSpan)+1;//球面切分的行数
        int col=360/angleSpan;//球面切分的列数
        for(int i=0;i<row;i++)//对每一行循环
        {
        	if(i>0&&i<row-1)
        	{//中间行
        		for(int j=-1;j<col;j++)
				{//中间行的两个相邻点与下一行的对应点构成三角形
					int k=i*col+j;
					alIndex.add(k+col);
					alIndex.add(k+1);
					alIndex.add(k);		
				}
        		for(int j=0;j<col+1;j++)
				{//中间行的两个相邻点与上一行的对应点构成三角形				
					int k=i*col+j;
					alIndex.add(k-col);
					alIndex.add(k-1);
					alIndex.add(k);	
				}
        	}
        }
        iCount=alIndex.size();
        byte indices[]=new byte[alIndex.size()];
        for(int i=0;i<alIndex.size();i++)
        {
        	indices[i]=alIndex.get(i).byteValue();
        } 
        //创建三角形构造索引数据缓冲
        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);//向缓冲区中放入三角形构造索引数据
        mIndexBuffer.position(0);//设置缓冲区起始位置
      //三角形构造索引数据初始化==========end==============================
    }

    public void drawSelf(GL10 gl)
    {
    	gl.glRotatef(mAngleZ, 0, 0, 1);//沿Z轴旋转
    	gl.glRotatef(mAngleX, 1, 0, 0);//沿X轴旋转
        gl.glRotatef(mAngleY, 0, 1, 0);//沿Y轴旋转
        
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
        
		//为画笔指定顶点坐标数据
        gl.glVertexPointer
        (
        		3,				//每个顶点的坐标数量为3  xyz 
        		GL10.GL_FIXED,	//顶点坐标值的类型为 GL_FIXED
        		0, 				//连续顶点坐标数据之间的间隔
        		mVertexBuffer	//顶点坐标数据
        );
        
        //为画笔指定顶点法向量数据
        gl.glNormalPointer(GL10.GL_FIXED, 0, mNormalBuffer);
		
        //绘制图形
        gl.glDrawElements
        (
        		GL10.GL_TRIANGLES, 		//以三角形方式填充
        		iCount, 			 	//一共icount/3个三角形,iCount个顶点
        		GL10.GL_UNSIGNED_BYTE, 	//索引值的尺寸
        		mIndexBuffer			//索引值数据
        ); 
    }

}

 
 



MySyrfaceView类

package com.example.android_sample_5_2;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class MySurfaceView extends GLSurfaceView{

    private final float TOUCH_SCALE_FACTOR = 180.0f/320;//角度缩放比例
    private SceneRenderer mRenderer;//场景渲染器
    private float mPreviousY;//上次的触控位置Y坐标
    private float mPreviousX;//上次的触控位置Y坐标
    boolean openLightFlag=true;//开灯标记,false为关灯,true为开灯
    int openLightNum=1;			//开灯数量标记,1为一盏灯,2,为两盏灯...
	public MySurfaceView(Context context) {
        super(context);
        mRenderer = new SceneRenderer();	//创建场景渲染器
        setRenderer(mRenderer);				//设置渲染器		
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染   
    }

    //触摸事件回调方法
    @Override public boolean onTouchEvent(MotionEvent e) {
        float y = e.getY();
        float x = e.getX();
        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float dy = y - mPreviousY;//计算触控笔Y位移
            float dx = x - mPreviousX;//计算触控笔Y位移
            mRenderer.ball.mAngleX += dy * TOUCH_SCALE_FACTOR;//设置沿x轴旋转角度
            mRenderer.ball.mAngleZ += dx * TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
            requestRender();//重绘画面
        }
        mPreviousY = y;//记录触控笔位置
        mPreviousX = x;//记录触控笔位置
        return true;
    }
	private class SceneRenderer implements GLSurfaceView.Renderer 
    {   
    	Ball ball=new Ball(4);
    	
    	public SceneRenderer(){
    	}
        public void onDrawFrame(GL10 gl){   
        	gl.glShadeModel(GL10.GL_SMOOTH);
        	gl.glEnable(GL10.GL_LIGHTING);//允许光照  
        	initMaterialWhite(gl);//初始化材质为白色
        	float[] positionParams0={2,1,0,1};//最后的1表示是定位光,此为0号灯位置参数。
        	float[] positionParams1={-2,1,0,1};//最后的1表示是定位光,此为1号灯位置参数。
        	float[] positionParams2={0,0,2,1};//最后的1表示是定位光,此为2号灯位置参数。
        	float[] positionParams3={1,1,2,1};//最后的1表示是定位光,此为3号灯位置参数。
        	float[] positionParams4={-1,-1,2,1};//最后的1表示是定位光,此为4号灯位置参数。
        	gl.glDisable(GL10.GL_LIGHT0);	//每次绘制前,取消已开启的灯光效果
        	gl.glDisable(GL10.GL_LIGHT1);	//每次绘制前,取消已开启的灯光效果
        	gl.glDisable(GL10.GL_LIGHT2);	//每次绘制前,取消已开启的灯光效果
        	gl.glDisable(GL10.GL_LIGHT3);	//每次绘制前,取消已开启的灯光效果
        	gl.glDisable(GL10.GL_LIGHT4);	//每次绘制前,取消已开启的灯光效果
        	
        	switch(openLightNum){
        		case 1:					//开启一盏灯
        			initLight0(gl);//初始化0号灯
        			
                    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams0,0); 
        			break;
        		case 2:					//开启两盏灯
        			initLight0(gl);//初始化0号灯
                    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams0,0); 
                    
                    initLight1(gl);//初始化1号灯
                    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParams1,0); 
        			break;
        		case 3:					//开启三盏灯
        			initLight0(gl);//初始化0号灯
                    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams0,0); 
                    
                    initLight1(gl);//初始化1号灯
                    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParams1,0); 
                    
                    initLight2(gl);//初始化2号灯
                    gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_POSITION, positionParams2,0); 
        			break;
        		case 4:					//开启四盏灯
        			initLight0(gl);//初始化0号灯
                    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams0,0); 
                    
                    initLight1(gl);//初始化1号灯
                    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParams1,0); 
                    
                    initLight2(gl);//初始化2号灯
                    gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_POSITION, positionParams2,0); 
        			
                    initLight3(gl);//初始化3号灯
                    gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_POSITION, positionParams3,0); 
                    break;
        		case 5:					//开启五盏灯
        			initLight0(gl);//初始化0号灯
                    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, positionParams0,0); 
                    
                    initLight1(gl);//初始化1号灯
                    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParams1,0); 
                    
                    initLight2(gl);//初始化2号灯
                    gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_POSITION, positionParams2,0); 
        			
                    initLight3(gl);//初始化3号灯
                    gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_POSITION, positionParams3,0); 
                    
                    initLight4(gl);//初始化4号灯
                    gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_POSITION, positionParams4,0); 
                    break;
        	}
        	//清除颜色缓存
        	gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        	//设置当前矩阵为模式矩阵
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            //设置当前矩阵为单位矩阵
            gl.glLoadIdentity();     
            
            gl.glTranslatef(0, 0f, -1.8f);  
            ball.drawSelf(gl);
            gl.glLoadIdentity();
        }
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            //设置视窗大小及位置 
        	gl.glViewport(0, 0, width, height);
        	//设置当前矩阵为投影矩阵
            gl.glMatrixMode(GL10.GL_PROJECTION);
            //设置当前矩阵为单位矩阵
            gl.glLoadIdentity();
            //计算透视投影的比例
            float ratio = (float) width / height;
            //调用此方法计算产生透视投影矩阵
            gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
        }
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            //关闭抗抖动 
        	gl.glDisable(GL10.GL_DITHER);
        	//设置特定Hint项目的模式,这里为设置为使用快速模式
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
            //设置屏幕背景色黑色RGBA
            gl.glClearColor(0,0,0,0);
            //设置着色模型为平滑着色   
            gl.glShadeModel(GL10.GL_SMOOTH);//GL10.GL_SMOOTH  GL10.GL_FLAT
            //启用深度测试
            gl.glEnable(GL10.GL_DEPTH_TEST);
        }
    }
	private void initLight0(GL10 gl){
        gl.glEnable(GL10.GL_LIGHT0);//打开0号灯  ,白色
        //环境光设置
        float[] ambientParams={0.1f,0.1f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientParams,0);            
        //散射光设置
        float[] diffuseParams={0.5f,0.5f,0.5f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseParams,0); 
        //反射光设置
        float[] specularParams={1.0f,1.0f,1.0f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specularParams,0);     
	}
	private void initLight1(GL10 gl)
	{
        gl.glEnable(GL10.GL_LIGHT1);//打开1号灯  ,红色
        //环境光设置
        float[] ambientParams={0.2f,0.03f,0.03f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientParams,0);            
        //散射光设置
        float[] diffuseParams={0.5f,0.1f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseParams,0); 
        //反射光设置
        float[] specularParams={1.0f,0.1f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, specularParams,0);     
	}
	private void initLight2(GL10 gl)
	{
        gl.glEnable(GL10.GL_LIGHT2);//打开1号灯  ,蓝色
        //环境光设置
        float[] ambientParams={0.03f,0.03f,0.2f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_AMBIENT, ambientParams,0);            
        //散射光设置
        float[] diffuseParams={0.1f,0.1f,0.5f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_DIFFUSE, diffuseParams,0); 
        //反射光设置
        float[] specularParams={0.1f,0.1f,1.0f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_SPECULAR, specularParams,0);     
	}
	private void initLight3(GL10 gl)
	{
        gl.glEnable(GL10.GL_LIGHT3);//打开3号灯  ,绿色
        //环境光设置
        float[] ambientParams={0.03f,0.2f,0.03f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_AMBIENT, ambientParams,0);            
        //散射光设置
        float[] diffuseParams={0.1f,0.5f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_DIFFUSE, diffuseParams,0); 
        //反射光设置
        float[] specularParams={0.1f,1.0f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_SPECULAR, specularParams,0);     
	}
	private void initLight4(GL10 gl)
	{
        gl.glEnable(GL10.GL_LIGHT4);//打开3号灯  ,黄色
        //环境光设置
        float[] ambientParams={0.2f,0.2f,0.03f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_AMBIENT, ambientParams,0);            
        //散射光设置
        float[] diffuseParams={0.5f,0.5f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_DIFFUSE, diffuseParams,0); 
        //反射光设置
        float[] specularParams={1.0f,1.0f,0.1f,1.0f};//光参数 RGBA
        gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_SPECULAR, specularParams,0);     
	}
	private void initMaterialWhite(GL10 gl)
	{//材质为白色时什么颜色的光照在上面就将体现出什么颜色
        //环境光为白色材质
        float ambientMaterial[] = {0.4f, 0.4f, 0.4f, 1.0f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, ambientMaterial,0);
        //散射光为白色材质
        float diffuseMaterial[] = {0.8f, 0.8f, 0.8f, 1.0f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, diffuseMaterial,0);
        //高光材质为白色
        float specularMaterial[] = {1.0f, 1.0f, 1.0f, 1.0f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, specularMaterial,0);
        //高光反射区域,数越大高亮区域越小越暗
        float shininessMaterial[] = {1.5f};
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shininessMaterial,0);
	}
}

MainActivity类


package com.example.android_sample_5_2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	MySurfaceView msv;
	RatingBar rb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		msv = new MySurfaceView(this);
		setContentView(R.layout.activity_main);
		rb = (RatingBar) findViewById(R.id.RatingBar01);
		msv.requestFocus();
		msv.setFocusableInTouchMode(true);
		LinearLayout lla = (LinearLayout) findViewById(R.id.lla);
		lla.addView(msv);
		
		rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
			
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				if (rating >= 0 && rating <= 1) {
					msv.openLightNum = 1;
				} else if(rating > 1 && rating <= 2){
					msv.openLightNum = 2;
				}else if(rating > 2 && rating <= 3){
					msv.openLightNum = 3;
				}else if(rating > 3 && rating <= 4){
					msv.openLightNum = 4;
				}else if(rating > 4 && rating <= 5){
					msv.openLightNum = 5;
				}
				Toast.makeText(MainActivity.this, "开启了" + msv.openLightNum + "盏灯", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		msv.onPause();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		msv.onResume();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}



你可能感兴趣的:(3D,android应用,光照效果,旋转球)