沿着旋转手指的android一个3D魔方 [英] rotate a 3d cube along finger android
本文介绍了沿着旋转手指的android一个3D魔方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我的旋转3D立方体usning我的手指触摸,但我不能这样做,该怎么做我的code是旋转的立方体
它rotatting相当nisely,但我想它应该在手指触摸例如旋转,如果右侧是shuold右方向旋转手指toches ANS等
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); GLSurfaceView视图=新GLSurfaceView(本);
view.setRenderer(新OpenGLRenderer());
的setContentView(视图);
}@覆盖
公共布尔onTouchEvent(MotionEvent事件){
super.onTouchEvent(事件);
开关(event.getAction()){ 案例MotionEvent.ACTION_DOWN: 打破;
案例MotionEvent.ACTION_MOVE:
//处理之间轮换 打破; 案例MotionEvent.ACTION_UP:
打破;
}
返回false;
}
}类OpenGLRenderer扩展活动实现渲染{ 私人立方mCube =新立方();
私人浮动mCubeRotation;
@覆盖
公共无效onDrawFrame(GL10 GL){
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); gl.glTranslatef(0.0,0.0,-10.0f);
gl.glRotatef(mCubeRotation,1.0F,1.0F,1.0F); mCube.draw(GL); gl.glLoadIdentity(); mCubeRotation - = 0.15f;
}
@覆盖
公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT){
gl.glViewport(0,0,宽度,高度);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(GL,45.0f,(浮点)宽/(浮点)的高度,0.1F,100.0f);
gl.glViewport(0,0,宽度,高度); gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@覆盖
公共无效onSurfaceCreated(GL10 GL,
javax.microedition.khronos.egl.EGLConfig配置){
gl.glClearColor(0.0,0.0,0.0,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); }}类立方{私人FloatBuffer mVertexBuffer;
私人FloatBuffer mColorBuffer;
私人ByteBuffer的mIndexBuffer;私人浮动顶点[] = {
-1.0F,-1.0F,-1.0F,
1.0F,-1.0F,-1.0F,
1.0F,1.0F,-1.0F,
-1.0F,1.0F,-1.0F,
-1.0F,-1.0F,1.0F,
1.0F,-1.0F,1.0F,
1.0F,1.0F,1.0F,
-1.0F,1.0F,1.0F
};
私人浮动颜色[] = {
0.0,1.0F,0.0,1.0F,
0.0,1.0F,0.0,1.0F,
1.0F,0.5F,0.0,1.0F,
1.0F,0.5F,0.0,1.0F,
1.0F,0.0,0.0,1.0F,
1.0F,0.0,0.0,1.0F,
0.0,0.0,1.0F,1.0F,
1.0F,0.0,1.0F,1.0F
};专用字节指数[] = {
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
};公共立方(){
ByteBuffer的byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(顶点);
mVertexBuffer.position(0); byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.put(颜色);
mColorBuffer.position(0); mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(指数);
mIndexBuffer.position(0);
}公共无效画(GL10 GL){
gl.glFrontFace(GL10.GL_CW); gl.glVertexPointer(3,GL10.GL_FLOAT,0,mVertexBuffer);
gl.glColorPointer(4,GL10.GL_FLOAT,0,mColorBuffer); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glDrawElements(GL10.GL_TRIANGLES,36,GL10.GL_UNSIGNED_BYTE,
mIndexBuffer); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
解决方案
我用这个code及其作品
@覆盖
公共布尔onTouchEvent(MotionEvent事件){
//
浮X = event.getX();
浮Y = event.getY(); //如果触摸移动屏幕上
如果(event.getAction()== MotionEvent.ACTION_MOVE){
//计算变化
浮DX = X - oldX;
浮DY = Y - oldY;
//在屏幕上定义为10%的上部区域
INT upperArea = this.getHeight()/ 10; //放大/缩小,如果触摸此举在上取得
如果(Y< upperArea){
ž - = DX * TOUCH_SCALE / 2; //绕轴线旋转,否则
}其他{
xrot + = DY * TOUCH_SCALE;
yrot + = DX * TOUCH_SCALE;
} //在屏幕上的preSS
}否则如果(event.getAction()== MotionEvent.ACTION_UP){
//定义为10%的上部区域以限定下部区域
INT upperArea = this.getHeight()/ 10;
INT lowerArea = this.getHeight() - upperArea; //改变光设置,如果下部区域已经pressed
如果(Y> lowerArea){
如果(光){
光= FALSE;
}其他{
光= TRUE;
}
}
} //记住值
oldX = X;
oldY = Y; //我们处理该事件
返回true;
}
i want to rotate my 3d cube usning my finger touch but i am unable to do that how to do it my code is for rotating cube it is rotatting quite nisely but i want it should rotate on finger touch for example if finger toches of right side it shuold rotate at right direction ans so on
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
break;
case MotionEvent.ACTION_MOVE :
// handle in between rotation
break;
case MotionEvent.ACTION_UP :
break;
}
return false;
}
}
class OpenGLRenderer extends Activity implements Renderer {
private Cube mCube = new Cube();
private float mCubeRotation;
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10.0f);
gl.glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f);
mCube.draw(gl);
gl.glLoadIdentity();
mCubeRotation -= 0.15f;
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
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.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl,
javax.microedition.khronos.egl.EGLConfig config) {
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);
}
}
class Cube {
private FloatBuffer mVertexBuffer;
private FloatBuffer mColorBuffer;
private ByteBuffer mIndexBuffer;
private float vertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f
};
private float colors[] = {
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f
};
private byte indices[] = {
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
};
public Cube() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mColorBuffer = byteBuf.asFloatBuffer();
mColorBuffer.put(colors);
mColorBuffer.position(0);
mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
mIndexBuffer.put(indices);
mIndexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
解决方案
i used this code and its works
@Override
public boolean onTouchEvent(MotionEvent event) {
//
float x = event.getX();
float y = event.getY();
//If a touch is moved on the screen
if(event.getAction() == MotionEvent.ACTION_MOVE) {
//Calculate the change
float dx = x - oldX;
float dy = y - oldY;
//Define an upper area of 10% on the screen
int upperArea = this.getHeight() / 10;
//Zoom in/out if the touch move has been made in the upper
if(y < upperArea) {
z -= dx * TOUCH_SCALE / 2;
//Rotate around the axis otherwise
} else {
xrot += dy * TOUCH_SCALE;
yrot += dx * TOUCH_SCALE;
}
//A press on the screen
} else if(event.getAction() == MotionEvent.ACTION_UP) {
//Define an upper area of 10% to define a lower area
int upperArea = this.getHeight() / 10;
int lowerArea = this.getHeight() - upperArea;
//Change the light setting if the lower area has been pressed
if(y > lowerArea) {
if(light) {
light = false;
} else {
light = true;
}
}
}
//Remember the values
oldX = x;
oldY = y;
//We handled the event
return true;
}
这篇关于沿着旋转手指的android一个3D魔方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文