Android 3D游戏通过OpenGL绘制的多个立方体如何单独拖动任何一个沿坐标轴运动?

Janlor 发布于 2012/07/26 20:34
阅读 4K+
收藏 0

通过eclipse编写,准备制作一个类似于孔明锁的游戏,效果如图:,图中为6个长方体,现要求点击屏幕后, 如果点击空白处,则可以绕原点同时旋转六个长方体(这步已经实现) 。如果点击长方体,按住不放,可以沿坐标轴(x,y,z)拖动长方体,直至拖出屏幕后消失。我要问的就是第二步怎么实现,实现后还需要碰撞检测。下面是已经完成的代码,菜鸟一个~求解答~

说明:以下有三个类,另外颜色是通过纹理实现的

package gongziya.android.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

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

import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;

public class OpenGLRenderer implements Renderer
{
	float step = 0.4f;
	boolean key;
	boolean light = true;
	
	float xrot, yrot;	//x,y轴旋转
	float xspeed, yspeed;//旋转的速度
	float x=0.0f,y=0.0f;
	float z = -4.0f;//深入屏幕的距离
	float touchX, touchY;
	
	//定义环境光(r,g,b,a)
	private float[] lightAmbient ={0.5f,0.5f,0.5f,1.0f}; 
	//定义漫射光
	private float[] lightDiffuse = {1.0f,1.0f,1.0f,1.0f};
	//光源的位置
	private float[] lightPosition = {0.0f,0.0f,2.0f,1.0f}; 
	
	//过滤的类型
	int filter = 1;
	//纹理效果
	int [] texture;
	
	private float[] verticeshuang = 
	{
			-2.0f,-0.02f,-0.2f,
			-2.0f,-0.02f,0.2f,	
			-0.4f,-0.02f,0.2f,
			-0.4f,-0.02f,-0.2f,//左上面
			
			-0.4f,-0.22f,-0.2f,
			-0.4f,-0.22f,0.2f,
			0.4f,-0.22f,0.2f,
			0.4f,-0.22f,-0.2f,//凹
			
			0.4f,-0.02f,0.2f,
			2.0f,-0.02f,0.2f,
			2.0f,-0.02f,-0.2f,
			0.4f,-0.02f,-0.2f,//右上面			
			
			-2.0f,-0.42f,-0.2f,
			2.0f,-0.22f,-0.2f,
			2.0f,-0.42f,0.2f,
			-2.0f,-0.42f,0.2f,//下面
			
			2.0f,-0.42f,-0.2f,
			2.0f,-0.02f,-0.2f,
			2.0f,-0.02f,0.2f,
			2.0f,-0.42f,0.2f,//右侧外
			
			0.4f,-0.02f,0.2f,
			0.4f,-0.02f,-0.2f,
			0.4f,-0.22f,-0.2f,
			0.4f,-0.22f,0.2f,//右侧内
			
			-0.4f,-0.02f,-0.2f,
			-0.4f,-0.02f,0.2f,
			-0.4f,-0.22f,0.2f,
			-0.4f,-0.22f,-0.2f,//左侧内
			
			-2.0f,-0.42f,-0.2f,
			-2.0f,-0.42f,0.2f,
			-2.0f,-0.02f,0.2f,
			-2.0f,-0.02f,-0.2f,//左侧外
			
			-2.0f,-0.02f,0.2f,
			-2.0f,-0.42f,0.2f,
			-0.4f,-0.42f,0.2f,
			-0.4f,-0.02f,0.2f,//前左
			
			-0.4f,-0.22f,0.2f,
			-0.4f,-0.42f,0.2f,
			0.4f,-0.42f,0.2f,
			0.4f,-0.22f,0.2f,//前中
			
			2.0f,-0.42f,0.2f,
			2.0f,-0.02f,0.2f,
			0.4f,-0.02f,0.2f,
			0.4f,-0.42f,0.2f,//前右
			
			2.0f,-0.02f,-0.2f,
			2.0f,-0.42f,-0.2f,
			0.4f,-0.42f,-0.2f,
			0.4f,-0.02f,-0.2f,//后右
			
			0.4f,-0.22f,-0.2f,
			0.4f,-0.42f,-0.2f,
			-0.4f,-0.42f,-0.2f,
			-0.4f,-0.22f,-0.2f,//后中
			
			-0.4f,-0.02f,-0.2f,
			-0.4f,-0.42f,-0.2f,
			-2.0f,-0.42f,-0.2f,
			-2.0f,-0.02f,-0.2f,//后左

	};
	private float[] verticesbai = 
	{
			-2.0f,0.02f,-0.2f,
			-0.4f,0.02f,-0.2f,
			-0.4f,0.02f,0.2f,
			-2.0f,0.02f,0.2f,//左下面
			
			-0.4f,0.22f,-0.2f,
			-0.4f,0.22f,0.2f,			
			0.4f,0.22f,0.2f,
			0.4f,0.22f,-0.2f,//凹
			
			0.4f,0.02f,0.2f,
			0.4f,0.02f,-0.2f,
			2.0f,0.02f,-0.2f,
			2.0f,0.02f,0.2f,//右下面			
			
			-2.0f,0.42f,-0.2f,
			-2.0f,0.42f,0.2f,
			2.0f,0.42f,0.2f,
			2.0f,0.42f,-0.2f,//上面
			
			2.0f,0.02f,-0.2f,
			2.0f,0.42f,-0.2f,
			2.0f,0.42f,0.2f,
			2.0f,0.02f,0.2f,//右侧外
			
			0.4f,0.22f,0.2f,
			0.4f,0.22f,-0.2f,
			0.4f,0.02f,-0.2f,
			0.4f,0.02f,0.2f,//右侧内
			
			-0.4f,0.02f,0.2f,
			-0.4f,0.02f,-0.2f,
			-0.4f,0.22f,-0.2f,
			-0.4f,0.22f,0.2f,//左侧内
			
			-2.0f,0.02f,-0.2f,
			-2.0f,0.02f,0.2f,
			-2.0f,0.42f,0.2f,
			-2.0f,0.42f,-0.2f,//左侧外
			
			-2.0f,0.42f,0.2f,
			-2.0f,0.02f,0.2f,
			-0.4f,0.02f,0.2f,
			-0.4f,0.42f,0.2f,//前左
			
			-0.4f,0.42f,0.2f,
			-0.4f,0.22f,0.2f,
			0.4f,0.22f,0.2f,
			0.4f,0.42f,0.2f,//前中
			
			2.0f,0.02f,0.2f,
			2.0f,0.42f,0.2f,
			0.4f,0.42f,0.2f,
			0.4f,0.02f,0.2f,//前右
			
			2.0f,0.42f,-0.2f,
			2.0f,0.02f,-0.2f,
			0.4f,0.02f,-0.2f,
			0.4f,0.42f,-0.2f,//后右
			
			0.4f,0.42f,-0.2f,
			0.4f,0.22f,-0.2f,
			-0.4f,0.22f,-0.2f,
			-0.4f,0.42f,-0.2f,//后中
			
			-0.4f,0.42f,-0.2f,
			-0.4f,0.02f,-0.2f,
			-2.0f,0.02f,-0.2f,
			-2.0f,0.42f,-0.2f,//后左

	};
	private float[] verticeslan={
			0.0f,-0.2f,2f,
			0.0f,0.2f,2f,
			-0.4f,0.2f,2f,
			-0.4f,-0.2f,2f,//前面
			
			0.0f,-0.2f,-2f,
			-0.4f,-0.2f,-2f,
			-0.4f,0.2f,-2f,
			0.0f,0.2f,-2f,//后面
			
			-0.4f,-0.2f,2f,
			-0.4f,0.2f,2f,
			-0.4f,0.2f,-2f,
			-0.4f,-0.2f,-2f,//左面
			
			0.0f,-0.2f,2f,
			0.0f,-0.2f,-2f,
		    0.0f,0.2f,-2f,
			0.0f,0.2f,2f,//右面
			
			-0.4f,0.2f,2f,
			 0.0f,0.2f,2f,
			 0.0f,0.2f,-2f,
			-0.4f,0.2f,-2f,//上面
			
		     0.0f,-0.2f,2f,
			-0.4f,-0.2f,2f,
			-0.4f,-0.2f,-2f,
			 0.0f,-0.2f,-2f,//下面
	};
	private float[] verticeslu={
			 0.0f, 0.2f,2f,
			 0.0f,-0.2f,2f,
			 0.4f,-0.2f,2f,
			 0.4f, 0.2f,2f,//前面
			 
			 0.0f, 0.2f,0.2f,
			 0.0f,-0.2f,0.2f,
			 0.2f,-0.2f,0.2f,
			 0.2f, 0.2f,0.2f,//前面1
			 
			 0.0f, 0.2f,-0.4f,
			 0.0f,-0.2f,-0.4f,
			 0.2f,-0.2f,-0.4f,
			 0.2f, 0.2f,-0.4f,//前面2
			
			 0.4f,-0.2f,-2f,
			 0.0f,-0.2f,-2f,
			 0.0f,0.2f,-2f,
			 0.4f,0.2f,-2f,//后面
			 
			 0.2f,-0.2f,-0.2f,
			 0.0f,-0.2f,-0.2f,
			 0.0f,0.2f,-0.2f,
			 0.2f,0.2f,-0.2f,//后面1
			 
			 0.2f,-0.2f,0.4f,
			 0.0f,-0.2f,0.4f,
			 0.0f,0.2f,0.4f,
			 0.2f,0.2f,0.4f,//后面2
			 
			 0.4f,-0.2f,-2f,
			 0.0f,-0.2f,-2f,
			 0.0f,0.2f,-2f,
			 0.4f,0.2f,-2f,//后面3
			 
			
			 0.0f,-0.2f,2f,
			 0.0f,0.2f,2f,
			 0.0f,0.2f,0.4f,
			 0.0f,-0.2f,0.4f,//左面1
			
			0.2f,-0.2f,0.4f,
			0.2f,0.2f,0.4f,
			0.2f,0.2f,0.2f,
			0.2f,-0.2f,0.2f,//左面2
			
			 0.0f,-0.2f,0.2f,
			 0.0f,0.2f,0.2f,
			 0.0f,0.2f,-0.2f,
			 0.0f,-0.2f,-0.2f,//左面3
			
			0.2f,-0.2f,-0.2f,
			0.2f,0.2f,-0.2f,
			0.2f,0.2f,-0.4f,
			0.2f,-0.2f,-0.4f,//左面4
			
			 0.0f,-0.2f,-0.4f,
			 0.0f,0.2f,-0.4f,
			 0.0f,0.2f,-2f,
			 0.0f,-0.2f,-2f,//左面5
			
			 0.4f,-0.2f,2f,
			 0.4f,-0.2f,-2f,
		     0.4f,0.2f,-2f,
			 0.4f,0.2f,2f,//右面
			
			 0.0f,0.2f,2f,
			 0.4f,0.2f,2f,
			 0.4f,0.2f,0.4f,
			 0.0f,0.2f,0.4f,//上面1
			 
			 0.4f,0.2f,0.4f,
			 0.4f,0.2f,0.2f,
			 0.2f,0.2f,0.2f,
			 0.2f,0.2f,0.4f,//上面2
			 
			 0.4f,0.2f,0.2f,
			 0.4f,0.2f,-0.2f,
			 0.0f,0.2f,-0.2f,
			 0.0f,0.2f,0.2f,//上面3
			 
			 0.4f,0.2f,-0.2f,
			 0.4f,0.2f,-0.4f,
			 0.2f,0.2f,-0.4f,
			 0.2f,0.2f,-0.2f,//上面4
			 
			 0.4f,0.2f,-0.4f,
			 0.4f,0.2f,-2f,
			 0.0f,0.2f,-2f,
			 0.0f,0.2f,-0.4f,//上面5
			
		     0.4f,-0.2f,0.4f,
			 0.4f,-0.2f,2f,
			 0.0f,-0.2f,2f,
			 0.0f,-0.2f,0.4f,//下面1

		     0.4f,-0.2f,0.2f,
			 0.4f,-0.2f,0.4f,
			 0.2f,-0.2f,0.4f,
			 0.2f,-0.2f,0.2f,//下面2

		     0.4f,-0.2f,-0.2f,
			 0.4f,-0.2f,0.2f,
			 0.0f,-0.2f,0.2f,
			 0.0f,-0.2f,-0.2f,//下面3

		     0.4f,-0.2f,-0.4f,
			 0.4f,-0.2f,-0.2f,
			 0.2f,-0.2f,-0.2f,
			 0.2f,-0.2f,-0.4f,//下面4

		     0.4f,-0.2f,-2f,
			 0.4f,-0.2f,-0.4f,
			 0.0f,-0.2f,-0.4f,
			 0.0f,-0.2f,-2f,//下面5
	};
	private float[] verticeshei={
			 0.4f, 0.2f,0.4f,
			 0.4f, 2.0f,0.4f,
			 -0.0f,2.0f,0.4f,
			 -0.0f,0.2f,0.4f,//前面1
			
			 0.2f,-0.2f,0.4f,
			 0.2f,0.2f,0.4f,
			 -0.0f,0.2f,0.4f,
			 -0.0f,-0.2f,0.4f,//前面2
			 
			 0.4f,-2.0f,0.4f,
			 0.4f,-0.2f,0.4f,
			 -0.0f,-0.2f,0.4f,
			 -0.0f,-2.0f,0.4f,//前面3
			 
			 0.0f,0.4f,0.0f,
			 0.0f,2.0f,0.0f,
		     0.4f,2.0f,0.0f,
			 0.4f,0.4f,0.0f,//后面1
			
			 0.0f,0.2f,0.2f,
			 0.0f,0.4f,0.2f,
			 0.4f,0.4f,0.2f,
			 0.4f,0.2f,0.2f,//后面2
			
			 0.0f,-0.2f,0.2f,
			 0.0f,0.2f,0.2f,
			 0.2f,0.2f,0.2f,
			 0.2f,-0.2f,0.2f,//后面3
			
			 0.0f,-0.4f,0.2f,
			 0.0f,-0.2f,0.2f,
			 0.4f,-0.2f,0.2f,
			 0.4f,-0.4f,0.2f,//后面4
			
			 0.0f,-2.0f,0.0f,
			 0.0f,-0.4f, 0.0f,
			 0.4f,-0.4f, 0.0f,
			 0.4f,-2.0f,0.0f,//后面5
			
			 0.0f,0.4f,0.0f,
			 0.0f,0.4f,0.4f,
			 0.0f,2.0f,0.4f,
			 0.0f,2.0f,0.0f,//左面1
			
			 0.0f,-0.4f,0.2f,
			 0.0f,-0.4f,0.4f,
		     0.0f,0.4f,0.4f,
			 0.0f,0.4f,0.2f,//左面2
			
			 0.0f,-2.0f,0.0f,
			 0.0f,-2.0f,0.4f,
			 0.0f,-0.4f,0.4f,
			 0.0f,-0.4f,0.0f,//左面3
			 
			 0.4f,0.4f,0.0f,
			 0.4f,2.0f,0.0f,
			 0.4f,2.0f,0.4f,
			 0.4f,0.4f,0.4f,//右面1
			 
			 0.4f,0.2f,0.2f,
			 0.4f,0.4f,0.2f,
			 0.4f,0.4f,0.4f,
			 0.4f,0.2f,0.4f,//右面2
			 
			 0.2f,-0.2f,0.2f,
			 0.2f,0.2f,0.2f,
			 0.2f,0.2f,0.4f,
			 0.2f,-0.2f,0.4f,//右面3
			 
			 0.4f,-0.4f,0.2f,
			 0.4f,-0.2f,0.2f,
			 0.4f,-0.2f,0.4f,
			 0.4f,-0.4f,0.4f,//右面4
			
			 0.4f,-2.0f,0.0f,
			 0.4f,-0.4f,0.0f,
			 0.4f,-0.4f,0.4f,
			 0.4f,-2.0f,0.4f,//右面5

		     0.0f,2.0f,0.0f,
			 0.0f,2.0f,0.4f,
			 0.4f,2.0f,0.4f,
			 0.4f,2.0f,0.0f,//上面1

		     0.0f,-0.4f,0.0f,
			 0.0f,-0.4f,0.2f,
			 0.4f,-0.4f,0.2f,
			 0.4f,-0.4f,0.0f,//上面2

		     0.2f,-0.2f,0.2f,
			 0.2f,-0.2f,0.4f,
			 0.4f,-0.2f,0.4f,
			 0.4f,-0.2f,0.2f,//上面3

		     0.0f,-2.0f,0.0f,
			 0.4f,-2.0f,0.0f,
			 0.4f,-2.0f,0.4f,
			 0.0f,-2.0f,0.4f,//下面1

		     0.0f,0.4f,0.0f,
			 0.4f,0.4f,0.0f,
			 0.4f,0.4f,0.2f,
			 0.0f,0.4f,0.2f,//下面2

		     0.2f,0.2f,0.2f,
			 0.4f,0.2f,0.2f,
			 0.4f,0.2f,0.4f,
			 0.2f,0.2f,0.4f,//下面3
	};
	private float[] verticescheng={
			
			 0.0f,0.4f,0.0f,
			 0.4f,0.4f,0.0f,
		     0.4f,2.0f,0.0f,
			 0.0f,2.0f,0.0f,//前面1
			
			 0.0f,0.2f,-0.2f,
			 0.4f,0.2f,-0.2f,
			 0.4f,0.4f,-0.2f,
			 0.0f,0.4f,-0.2f,//前面2
			
			 0.0f,-0.2f,-0.2f,
			 0.2f,-0.2f,-0.2f,
			 0.2f,0.2f,-0.2f,
			 0.0f,0.2f,-0.2f,//前面3
			
			 0.0f,-0.4f,-0.2f,
			 0.4f,-0.4f,-0.2f,
			 0.4f,-0.2f,-0.2f,
			 0.0f,-0.2f,-0.2f,//前面4
			
			 0.0f,-2.0f,0.0f,
			 0.4f,-2.0f,0.0f,
			 0.4f,-0.4f,0.0f,
			 0.0f,-0.4f,0.0f,//前面5
			 
			 0.4f, 0.2f,-0.4f,
			 0.0f,0.2f,-0.4f,
			 0.0f,2.0f,-0.4f,
			 0.4f, 2.0f,-0.4f,//后面1
			
			 0.2f,-0.2f,-0.4f,
			 0.0f,-0.2f,-0.4f,
			 0.0f,0.2f,-0.4f,
			 0.2f,0.2f,-0.4f,//后面2
			 
			 0.4f,-2.0f,-0.4f,
			 0.0f,-2.0f,-0.4f,
			 0.0f,-0.2f,-0.4f,
			 0.4f,-0.2f,-0.4f,//后面3
			 
			 0.0f,0.4f,0.0f,
			 0.0f,2.0f,0.0f,
			 0.0f,2.0f,-0.4f,
			 0.0f,0.4f,-0.4f,//左面1
			
			 0.0f,-0.4f,-0.2f,
			 0.0f,0.4f,-0.2f,
			 0.0f,0.4f,-0.4f,
			 0.0f,-0.4f,-0.4f,//左面2
			
			 0.0f,-2.0f,0.0f,
			 0.0f,-0.4f,0.0f,
			 0.0f,-0.4f,-0.4f,
			 0.0f,-2.0f,-0.4f,//左面3
			 
			 0.4f,0.4f,0.0f,
			 0.4f,0.4f,-0.4f,
			 0.4f,2.0f,-0.4f,
			 0.4f,2.0f,0.0f,//右面1
			 
			 0.4f,0.2f,-0.2f,
			 0.4f,0.2f,-0.4f,
			 0.4f,0.4f,-0.4f,
			 0.4f,0.4f,-0.2f,//右面2
			 
			 0.2f,-0.2f,-0.2f,
			 0.2f,-0.2f,-0.4f,
			 0.2f,0.2f,-0.4f,
			 0.2f,0.2f,-0.2f,//右面3
			 
			 0.4f,-0.4f,-0.2f, 
			 0.4f,-0.4f,-0.4f,
			 0.4f,-0.2f,-0.4f,
			 0.4f,-0.2f,-0.2f,//右面4
			
			 0.4f,-2.0f,0.0f,
			 0.4f,-2.0f,-0.4f,
			 0.4f,-0.4f,-0.4f,
			 0.4f,-0.4f,0.0f,//右面5

		     0.0f,2.0f,0.0f,
		     0.4f,2.0f,0.0f, 
		     0.4f,2.0f,-0.4f,
			 0.0f,2.0f,-0.4f,//上面1

		     0.0f,-0.4f,0.0f, 
		     0.4f,-0.4f,0.0f,
		     0.4f,-0.4f,-0.2f,
			 0.0f,-0.4f,-0.2f,//上面2

		     0.2f,-0.2f,-0.2f,
		     0.4f,-0.2f,-0.2f,
		     0.4f,-0.2f,-0.4f,
			 0.2f,-0.2f,-0.4f,//上面3

		     0.0f,-2.0f,0.0f,
		     0.0f,-2.0f,-0.4f,
		     0.4f,-2.0f,-0.4f,
			 0.4f,-2.0f,0.0f,//下面1

		     0.0f,0.4f,0.0f, 
		     0.0f,0.4f,-0.2f,
		     0.4f,0.4f,-0.2f,
			 0.4f,0.4f,0.0f,//下面2

		     0.2f,0.2f,-0.2f,
		     0.2f,0.2f,-0.4f,
		     0.4f,0.2f,-0.4f,
			 0.4f,0.2f,-0.2f,//下面3
	};
	private float[]  normals = 
	{
			0,0,1.0f,
			0,0,1.0f,
			0,0,1.0f,
			0,0,1.0f,
			
			0,0,-1.0f,
			0,0,-1.0f,
			0,0,-1.0f,
			0,0,-1.0f,
			
			0,1.0f,0,
			0,1.0f,0,
			0,1.0f,0,
			0,1.0f,0,
			
			0,-1.0f,0,
			0,-1.0f,0,
			0,-1.0f,0,
			0,-1.0f,0,
			
			1.0f,0,0,
			1.0f,0,0,
			1.0f,0,0,
			1.0f,0,0,
			
			-1.0f,0,0,
			-1.0f,0,0,
			-1.0f,0,0,
			-1.0f,0,0,
	};
	
	private float[] texCoords =
	{
		1.0f,0,0,0,0,1.0f,1.0f,1.0f,	
		0,0,0,1.0f,1.0f,1.0f,1.0f,0,
		1.0f,1.0f,1.0f,0,0,0,0,1.0f,
		0,1.0f,1.0f,1.0f,1.0f,0,0,0,
		0,0,0,1.0f,1.0f,1.0f,1.0f,0,
		1.0f,0,0,0,0,1.0f,1.0f,1.0f,
	};
	
	private FloatBuffer QuaterBufferhuang;
	private FloatBuffer QuaterBufferbai;
	private FloatBuffer QuaterBufferlan;
	private FloatBuffer QuaterBufferlu;
	private FloatBuffer QuaterBufferhei;
	private FloatBuffer QuaterBuffercheng;
	private FloatBuffer TexCoordBuffer;
	private FloatBuffer NormalBuffer;
	private FloatBuffer AmbientBuffer;
	private FloatBuffer DiffuseBuffer;
	private FloatBuffer PositionBuffer;

	public OpenGLRenderer()
	{		
		ByteBuffer qbb = ByteBuffer.allocateDirect(verticeshuang.length * 4);
		qbb.order(ByteOrder.nativeOrder());
		QuaterBufferhuang = qbb.asFloatBuffer();
		QuaterBufferhuang.put(verticeshuang);
		QuaterBufferhuang.position(0);
		
		ByteBuffer qbb1 = ByteBuffer.allocateDirect(verticesbai.length * 4);
		qbb1.order(ByteOrder.nativeOrder());
		QuaterBufferbai = qbb1.asFloatBuffer();
		QuaterBufferbai.put(verticesbai);
		QuaterBufferbai.position(0);
		
		ByteBuffer qbb2 = ByteBuffer.allocateDirect(verticeslan.length * 4);
		qbb2.order(ByteOrder.nativeOrder());
		QuaterBufferlan = qbb2.asFloatBuffer();
		QuaterBufferlan.put(verticeslan);
		QuaterBufferlan.position(0);
		
		ByteBuffer qbb3 = ByteBuffer.allocateDirect(verticeslu.length * 4);
		qbb3.order(ByteOrder.nativeOrder());
		QuaterBufferlu = qbb3.asFloatBuffer();
		QuaterBufferlu.put(verticeslu);
		QuaterBufferlu.position(0);
		
		ByteBuffer qbb4 = ByteBuffer.allocateDirect(verticeshei.length * 4);
		qbb4.order(ByteOrder.nativeOrder());
		QuaterBufferhei = qbb4.asFloatBuffer();
		QuaterBufferhei.put(verticeshei);
		QuaterBufferhei.position(0);
		
		ByteBuffer qbb5 = ByteBuffer.allocateDirect(verticescheng.length * 4);
		qbb5.order(ByteOrder.nativeOrder());
		QuaterBuffercheng = qbb5.asFloatBuffer();
		QuaterBuffercheng.put(verticescheng);
		QuaterBuffercheng.position(0);
		
		ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
		tbb.order(ByteOrder.nativeOrder());
		TexCoordBuffer = tbb.asFloatBuffer();
		TexCoordBuffer.put(texCoords);
		TexCoordBuffer.position(0);
		
		ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length * 4);
		nbb.order(ByteOrder.nativeOrder());
		NormalBuffer = nbb.asFloatBuffer();
		NormalBuffer.put(normals);
		NormalBuffer.position(0);
		
		ByteBuffer abb = ByteBuffer.allocateDirect(lightAmbient.length * 4);
		abb.order(ByteOrder.nativeOrder());
		AmbientBuffer = abb.asFloatBuffer();
		AmbientBuffer.put(lightAmbient);
		AmbientBuffer.position(0);
		
		ByteBuffer dbb = ByteBuffer.allocateDirect(lightDiffuse.length * 4);
		dbb.order(ByteOrder.nativeOrder());
		DiffuseBuffer = dbb.asFloatBuffer();
		DiffuseBuffer.put(lightDiffuse);
		DiffuseBuffer.position(0);
		
		ByteBuffer pbb = ByteBuffer.allocateDirect(lightPosition.length * 4);
		pbb.order(ByteOrder.nativeOrder());
		PositionBuffer = pbb.asFloatBuffer();
		PositionBuffer.put(lightPosition);
		PositionBuffer.position(0);
	}
	

	public void onDrawFrame(GL10 gl)
	{
		// 清除屏幕和深度缓存
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// 重置当前的模型观察矩阵
		gl.glLoadIdentity();
		
		//如果不启用GL_LIGHTING光就什么都看不见
		gl.glEnable(GL10.GL_LIGHTING);
	
		//==========黄=========
		gl.glTranslatef(x, y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);		

		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
		
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);
			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
		//绘制四边形
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBufferhuang);
		for(int i=0; i<14; i++)
 	    {
 	    	gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
 	    }
		
	    
	    //===========白========
		 gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);				
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBufferbai);
		for(int i=0; i<14; i++)
 	    {
 	    	gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
 	    }
		//==========蓝==========
		gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);				
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBufferlan);
		for(int i=0; i<6; i++)
	 	   {
	 	      gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
	 	   }
			//==========绿==========
		gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]);				
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBufferlu);
		for(int i=0; i<23; i++)
	 	  {
	 	    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
	 	   }
		//===========黑==========
		gl.glLoadIdentity();
		gl.glTranslatef(x,y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]);				
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBufferhei);
		for(int i=0; i<22; i++)
	 	  {
	 	    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
	 	   }
		//===========橙============
		gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		//设置旋转
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
		//选择使用的纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]);				
		gl.glNormalPointer(GL10.GL_FLOAT, 0, NormalBuffer);			
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, TexCoordBuffer);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, QuaterBuffercheng);
		for(int i=0; i<22; i++)
	 	  {
	 	    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, i*4, 4);
	 	   }
		//取消数组
	    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
	    //旋转角度
	    if ( key )
		{
		    xrot+=xspeed; 
		    yrot+=yspeed; 
		}
	    
    	if (!light)				// 判断是否开始光源
		{
    		gl.glDisable(GL10.GL_LIGHT1);		// 禁用一号光源
		}
		else					// 否则
		{
			gl.glEnable(GL10.GL_LIGHT1);		// 启用一号光源
		}
	}

	
	public void onSurfaceChanged(GL10 gl, int width, int height)
	{
		float ratio = (float) width / height;
		//设置OpenGL场景的大小
		gl.glViewport(0, 0, width, height);
		//设置投影矩阵
		gl.glMatrixMode(GL10.GL_PROJECTION);
		//重置投影矩阵
		gl.glLoadIdentity();
		// 设置视口的大小
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		// 选择模型观察矩阵
		gl.glMatrixMode(GL10.GL_MODELVIEW);	
		// 重置模型观察矩阵
		gl.glLoadIdentity();	
	}

	
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		// 告诉系统对透视进行修正
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
		// 黄色背景
		gl.glClearColor(255, 255, 255,0);		
		gl.glEnable(GL10.GL_CULL_FACE);
		// 启用阴影平滑
		gl.glShadeModel(GL10.GL_SMOOTH);
		// 启用深度测试
        gl.glEnable(GL10.GL_DEPTH_TEST);
		
		
		IntBuffer textureBuffer = IntBuffer.allocate(6);
		// 创建纹理
		gl.glGenTextures(6, textureBuffer);
		texture = textureBuffer.array();
		
		// 创建 Nearest 滤波贴图
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmapbai, 0);
		
		// 创建线性滤波纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmaphuang, 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmaplan, 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmaplu, 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmaphei, 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmapcheng, 0);
		
		gl.glClearDepthf(1.0f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		//设置环境光
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, AmbientBuffer);

	    //设置漫射光
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, DiffuseBuffer);

	    //设置光源的位置
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, PositionBuffer);
	    
	    //启用一号光源
	    gl.glEnable(GL10.GL_LIGHT1);
	}
	
	

	public boolean onTouchEvent(MotionEvent event) {
		
		if(event.getAction()==MotionEvent.ACTION_DOWN)	{

	       //处理屏幕屏点下事件 手指点击屏幕时触发
			
			touchX=event.getX();

			touchY=event.getY();
			
		}
		
		else if(event.getAction()==MotionEvent.ACTION_MOVE)	{

	       //处理移动事件 手指在屏幕上移动时触发

			yrot+= (event.getX() -touchX);

		    xrot+= (event.getY()  -touchY);

		    touchX = event.getX();

		    touchY = event.getY();
		}

		return true;  //此处需要返回true,才可以正常处理move事件
	}
}



package gongziya.android.opengl;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class Lesson07Activity extends Activity 
{
	OpenGLRenderer render = new OpenGLRenderer();

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//(NEW)
	    getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)

		GLSurfaceView glView = new GLSurfaceView(this);
		GLImage.load(this.getResources());
		glView.setRenderer(render);
		setContentView(glView);
	}
	
	//处理事件
	
	public boolean onTouchEvent(MotionEvent event){

		render.onTouchEvent(event);

		return super.onTouchEvent(event) ;
	}
}


package gongziya.android.opengl;
import gongziya.android.opengl.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

class GLImage
{
	public static Bitmap mBitmapbai;
	public static Bitmap mBitmaphuang;
	public static Bitmap mBitmaplan;
	public static Bitmap mBitmapcheng;
	public static Bitmap mBitmaplu;
	public static Bitmap mBitmaphei;
	
	public static void load(Resources resources)
	{
		mBitmapbai  = BitmapFactory.decodeResource(resources, R.drawable.bai);
		mBitmaphuang = BitmapFactory.decodeResource(resources, R.drawable.huang);
		mBitmaplan = BitmapFactory.decodeResource(resources, R.drawable.lan);
		mBitmapcheng = BitmapFactory.decodeResource(resources, R.drawable.cheng);
		mBitmaplu = BitmapFactory.decodeResource(resources, R.drawable.lu);
		mBitmaphei = BitmapFactory.decodeResource(resources, R.drawable.hei);
	}
}

加载中
0
小耶果
小耶果
这是要看死我啊.基础问题看看任何一本3D数学方面的书就可以了.
Janlor
Janlor
不好意思~我怕描述不清楚所以就把源码配上了~ 我查了很多资料都没解决~这才专门问的~ 你能说的详细一点吗?给点思路也行~~
0
小耶果
小耶果

既然旋转你已经实现,那么向量平移着么会不会呢?莫非你遇到的问题是鼠标点击空间物体和拖动的检测?
射线拾取检测法即可.

Janlor
Janlor
整了一天了啊~也没弄出来~思路都混乱了,向量平移思路会~但没弄出来。检测触点在正方体上还是在空白处这个纯粹不会弄~我基础太差了, 射线拾取检测法不知道怎么回事,你能帮我写写代码吗?~
返回顶部
顶部