0 0

android opengl 点击正方形的一个面给出相应事件0

用OpenGL做的可旋转的正方形,有6个面,现在想做点击其中一个面,让我知道点的是谁,如何做呢?
public class MyRender implements Renderer {
	public static float step = 15;
    boolean key;
    boolean light = true;
    
    public static float xrot, yrot;    //x,y轴旋转
    public static float xspeed, yspeed;//旋转的速度
    private int one = 0x10000;
    private int[] textures = new int[1];
    /**
     * @param context
     *            顶点及纹理
     */
    // verticesBuffer
    private IntBuffer verticesBuffer;
    // texcoordBuffer
    private IntBuffer texcoordBuffer;
    // 顶点数组vertices
    private int[] vertices;
    // 纹理数组texcoord
    private int[] texcoord;

    // 构造方法
    public MyRender() {
        // 在构造方法中初始化
        initData();
        initBuffer();
    }

    // 初始化各类值
    public void initData() {
        vertices = new int[] { -one, -one, one, one, -one, one, -one, one, one,
                one, one, one, one, -one, one, one, -one, -one, one, one, one,
                one, one, -one, one, -one, -one, -one, -one, -one, one, one,
                -one, -one, one, -one, -one, -one, -one, -one, -one, one, -one,
                one, -one, -one, one, one, -one, one, -one, one, one, -one,
                -one, one, one, one, one, one, -one, -one, -one, -one, -one,
                one, one, -one, -one, one, -one, one };

        texcoord = new int[] { 0, 0, one, 0, 0, one, one, one };
    }

    public void initBuffer() {
        // verticesbyteBuffer
        ByteBuffer verticesbyteBuffer = ByteBuffer
                .allocateDirect(vertices.length * 4);
        verticesbyteBuffer.order(ByteOrder.nativeOrder());
        verticesBuffer = verticesbyteBuffer.asIntBuffer();
        verticesBuffer.put(vertices);
        verticesBuffer.position(0);

        // texcoordBuffer
        ByteBuffer texcoordbyteBuffer = ByteBuffer
                .allocateDirect(texcoord.length * 4 * 6);
        texcoordbyteBuffer.order(ByteOrder.nativeOrder());
        texcoordBuffer = texcoordbyteBuffer.asIntBuffer();
        for (int i = 0; i < 6; i++) {
            texcoordBuffer.put(texcoord);
        }
        texcoordBuffer.position(0);
    }

    @Override
    public void onDrawFrame(GL10 gl) {

        // 清除深度缓存和颜色
        gl.glClear(GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_COLOR_BUFFER_BIT);
        // 重置观察模型
        gl.glLoadIdentity();
        // 启用vertex及texcoord
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // 向左移动1.5f并向里移动6.0f
        gl.glTranslatef(0.0f, 0.0f, -6.0f);
        
        //设置旋转
        gl.glRotatef(MainActivity5.anglex, 0, 1, 0);
        gl.glRotatef(MainActivity5.angley, 1, 0, 0);
        
        // 指定顶点映射
        gl.glVertexPointer(3, GL10.GL_FIXED, 0, verticesBuffer);
        // 指定纹理映射(每次都写错,烦)
        gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texcoordBuffer);

        for (int i = 0; i < 6; i++) {
        	switch(i){
	        	case 0:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapA, 0);
	        		break;
	        	case 1:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapB, 0);
	        		break;
	        	case 2:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapC, 0);
	        		break;
	        	case 3:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapD, 0);
	        		break;
	        	case 4:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapE, 0);
	        		break;
	        	case 5:
	        		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapF, 0);
	        		break;
        	}
        	gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
        }

        // 取消顶点及纹理映射
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
        // 设置观察模型
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // 告诉系统对透视进行修正
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        // 设置背景色为绿色
        gl.glClearColor(0, 1, 0, 0);
        // 启用阴影平滑
        gl.glShadeModel(GL10.GL_SMOOTH);

        // 设置深度缓存
        gl.glClearDepthf(one);
        // 启用深度测试
        gl.glEnable(GL10.GL_DEPTH_TEST);
        // 所做深度测试的类型
        gl.glDepthFunc(GL10.GL_LEQUAL);

        // 启用纹理
        gl.glEnable(GL10.GL_TEXTURE_2D); // 不启用没有效果的
        // 创建纹理
        gl.glGenTextures(1, textures, 0);
        // 绑定纹理
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
        // 生成纹理
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, BitGL.bitmapA, 0);
        
        // 线性滤波  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);  
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);
    }
}


public class MainActivity5  extends Activity implements OnGestureListener {
	// 定义旋转角度  
    public static float anglex = 0f;  
    public static float angley = 0f;  
    static final float ROTATE_FACTOR = 60;  
    // 定义手势检测器实例  
    GestureDetector detector;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        // 载入图片
        BitGL.init(this.getResources());
        // 创建一个GLSurfaceView,用于显示OpenGL绘制的图形  
        GLSurfaceView glView = new GLSurfaceView(this);  
        // 创建GLSurfaceView的内容绘制器  
        MyRender myRender = new MyRender();  
        // 为GLSurfaceView设置绘制器  
        glView.setRenderer(myRender);  
        setContentView(glView);  
        // 创建手势检测器  
        detector = new GestureDetector(this);  
    }  
    @Override  
    public boolean onTouchEvent(MotionEvent me)  
    {  
        // 将该Activity上的触碰事件交给GestureDetector处理  
    	anglex = me.getX();
    	angley = me.getY();
    	return true;
    }  
  
    @Override  
    public boolean onFling(MotionEvent event1, MotionEvent event2,  
        float velocityX, float velocityY)  
    {  
        return true;  
    }  
  
    @Override  
    public boolean onDown(MotionEvent arg0)  
    {  
        return false;  
    }  
  
    @Override  
    public void onLongPress(MotionEvent event)  
    {  
    }  
  
    @Override  
    public boolean onScroll(MotionEvent event1, MotionEvent event2,  
        float distanceX, float distanceY)  
    {  
        return false;  
    }  
  
    @Override  
    public void onShowPress(MotionEvent event)  
    {  
    }  
  
    @Override  
    public boolean onSingleTapUp(MotionEvent event)  
    {  
        return false;  
    }
}
2014年10月15日 15:22
目前还没有答案

相关推荐

    安卓ndk实现opengl绘制正方形纹理

    在Android开发中,Native Development Kit (NDK) 是一个允许开发者使用C和C++编写代码的工具集,它能够提供更高的性能和更低级的硬件访问权限。本话题将深入探讨如何利用NDK和OpenGL ES在Android平台上实现正方形...

    【OpenGL ES】绘制正方形

    本篇文章将深入探讨如何使用OpenGL ES 绘制一个基本的正方形。 首先,我们需要理解OpenGL ES的工作原理。它是一个状态机,通过设置各种状态(如颜色、纹理、深度测试等)来控制渲染过程。然后,我们通过调用`...

    Android OpenGL入门示例:绘制三角形和正方形

    首先,要使用OpenGL ES,你需要在Android项目中添加相应的依赖。通常,这可以通过在build.gradle文件中引入`implementation 'androidx.opengles:opengl-es-utils:1.0.0'`来完成。确保你的项目支持JNI(Java Native ...

    Android OpenGL实现立方体多纹理图片映射

    一个简单的立方体由六个正方形面组成,每个面有四个顶点。我们可以用数组存储这些顶点的坐标,同时考虑到纹理坐标,还需要额外的数组来存储每个顶点对应的纹理坐标。例如,使用`FloatBuffer`存储这些数据,然后通过`...

    安卓Android源码——OpenGL3D立方体多纹理贴图源码.zip

    在安卓(Android)平台上开发图形应用时,OpenGL ES(OpenGL for Embedded Systems)是一个常见的选择,它是OpenGL的一个轻量级版本,特别为嵌入式设备如智能手机和平板电脑设计。本篇将详细介绍Android环境下使用...

    山寨“爱消除”游戏7日教程--DAY 3

    在Android平台上,我们可以使用Android的EGL库来初始化OpenGLES上下文,并创建一个渲染表面。EGL处理窗口系统、显示配置以及与OpenGLES的交互。 在"DAY 3"的教程中,我们将关注游戏的核心元素——图形绘制。这包括...

Global site tag (gtag.js) - Google Analytics