Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the response to Touch screen events in OpenGL ES

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will give you a detailed explanation on how to analyze the response to touch screen events in OpenGL ES. The content of the article is of high quality, so the editor shares it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Like rotating triangles, let objects move according to a preset program to help get people's attention, but what if you want your OpenGL ES graphics to interact with the user? The key to enabling your OpenGL ES application to touch each other is to extend your GLSurfaceView implementation and rewrite its onTouchEvent () method to listen for touch events.

The editor will show you how to listen for touch events so that users can rotate OpenGL ES objects.

Set up touch listener

In order for your OpenGL ES application to respond to touch events, you must implement the onTouchEvent () event in your GLSurfaceView class. The following implementation example shows how to listen for MotionEvent.ACTION_MOVE events and convert them to the angle at which the graph is rotated.

@ Override public boolean onTouchEvent (MotionEvent e) {/ / MotionEvent reportsinput details from the touch screen / / and other inputcontrols. In this case, you are only / / interested in eventswhere 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.mAngle + = (dx + dy) * TOUCH_SCALE_FACTOR; / / = 180.0f / 320 requestRender ();} mPreviousX = x; mPreviousY = y; return true;}

Notice that after calculating the angle of rotation, this method calls the requestRender () method to tell the renderer that it's time to render the frame. The method used in the above example is the most effective, and the frame is redrawn only if there is a change in rotation. But to ask the renderer to redraw only when the data changes, use the setRenderMode () method to set the drawing mode.

PublicMyGLSurfaceView (Context context) {. / / Render the view onlywhen there is a change in the drawing data setRenderMode (GLSurfaceView.RENDERMODE_WHEN_DIRTY);}

Expose the angle of rotation

The above code requires you to expose the rotation angle through the renderer by adding a common member variable. Because the renderer code runs in a thread separate from the main user interface thread, you must declare a public variable as follows:

PublicclassMyGLRendererimplementsGLSurfaceView.Renderer {... Public volatile float mAngle

Apply rotation

The following code completes the rotation caused by the touch input:

PublicvoidonDrawFrame (GL10 gl) {/ / Create a rotation forthe triangle / / long time = SystemClock.uptimeMillis ()% 4000L; / / float angle = 0.090f * ((int) time); Matrix.setRotateM (mRotationMatrix, 0, mAngle, 0,0,-1.0f); / / Combine the rotationmatrix with the projection and camera view Matrix.multiplyMM (mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); / / Draw triangle mTriangle.draw (mMVPMatrix) } on how to analyze the response to touch screen events in OpenGL ES to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report