位置: 编程技术 - 正文

Android OpenGL ES绘图教程之六 :响应触摸事件(opengl es api)

编辑:rootadmin
使对象根据预设的程序进行运动,比如旋转三角形,可以吸引人的注意力。但是如果你想让用户同你的OpenGL ES图形进行交互会怎么样呢?使你的OpenGL ES应用程序触摸互动的关键是要扩展GLSurfaceView,复写onTouchEvent()方法,来监听touch事件。本教程展示了,如何监听透出事件,让用户旋转一个OpenGL ES对象。1. 设置一个Touch Listener 为了使你的OpenGL ES应用响应touch事件,你必须在GLSurfaceView类中实现OnTouchEvent()方法,下面的例子展示了如何监听 MotionEvent.ACTION_MOVE事件,并把它们转换成图形的旋转角度。private final float TOUCH_SCALE_FACTOR = .0f / ;private float mPreviousX;private float mPreviousY;@Overridepublic boolean onTouchEvent(MotionEvent e) { // MotionEvent reports input details from the touch screen // and other input controls. In this case, you are only // interested in events where the touch position changed. float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - mPreviousX; float dy = y - mPreviousY; // reverse direction of rotation above the mid-line if (y > getHeight() / 2) { dx = dx * -1 ; } // reverse direction of rotation to left of the mid-line if (x < getWidth() / 2) { dy = dy * -1 ; } mRenderer.setAngle( mRenderer.getAngle() &#; ((dx &#; dy) * TOUCH_SCALE_FACTOR)); requestRender(); } mPreviousX = x; mPreviousY = y; return true;} 要注意的是,当计算了旋转角度后,这个方法调用了requestRender()方法来告诉renderer需要渲染该帧,在本例中的这种途径是最优效的,因为帧只有在角度更改的时候才会重新绘制。但是,当你设置了渲染模式为RENDERMODE_WHEN_DIRTY的时候才会提高效率,所以确保renderer中该行的代码是没有被注释掉的:public MyGLSurfaceView(Context context) { ... // Render the view only when there is a change in the drawing data setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);}2. public旋转角度 上面的示例代码需要你通过添加public成员来暴露旋转角度参数。因为renderer代码是运行在一个独立于主线程的线程里面,所以你必须声明该参数为volatile:public class MyGLRenderer implements GLSurfaceView.Renderer { ... public volatile float mAngle; public float getAngle() { return mAngle; } public void setAngle(float angle) { mAngle = angle; }}3. 应用角度 为了应用touch输入产生的角度,注释掉产生调度的代码,添加由touch输入产生的mAngle:public void onDrawFrame(GL gl) { ... float[] scratch = new float[]; // Create a rotation for the triangle // long time = SystemClock.uptimeMillis() % L; // float angle = 0.f * ((int) time); Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f); // Combine the rotation matrix with the projection and camera view // Note that the mMVPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0); // Draw triangle mTriangle.draw(scratch);}

推荐整理分享Android OpenGL ES绘图教程之六 :响应触摸事件(opengl es api),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android skia opengl,android skia opengl,android opengl es教程,opengl es api,android view opengl,android view opengl,android opengl es教程,安卓opengl es3.0安装,内容如对您有帮助,希望把文章链接给更多的朋友!

当你完成了上面描述的步骤,执行代码,就可以在屏幕上拖动手指来旋转这个三角形了。

Android OpenGL ES绘图教程之六 :响应触摸事件(opengl es api)

原文地址 :

VS下基于Glut OpenGL显示一些立体图形示例程序 转自:

[置顶] Bezier曲线的动态绘制 #includegl/glut.h#includemath.h#pragmacomment(linker,/subsystem:windows/entry:mainCRTStartup)intSCREEN_HEIGHT=;intNUMPOINTS=0;classPoint{public:floatx,y;voidsetxy(floatx2,floaty2){x=x2;y=y2;}};

glutSwapBuffers函数用法 文章来源:

本文链接地址:https://www.jiuchutong.com/biancheng/369575.html 转载请保留说明!

上一篇:Android OpenGL ES绘图教程之五 :加入运动(android opengles2.0教程)

下一篇:VS2012下基于Glut OpenGL显示一些立体图形示例程序(vs2019怎么用gets)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络