Drawing shape on button click using OpenGL in android

白昼怎懂夜的黑 提交于 2019-12-11 02:19:59

问题


As new to the android OpenGL section.I had downloaded the sample example given on site Displaying Graphics with OpenGL ES

so run the downloaded sample and its running with an error and display the output as expected. I had do draw the triangle in the GLSurfaceview and its working fine. The screenshot for the triangle drawn:-

Now as the requirement change i have to draw the triangle onclick of the imageview but not on starting the application. Here is the screenshot for the above mention requirement

the black triangle is the imageview and right side is the GLSurfaceView area where i need to draw the triangle.Currently when i click the imageview its doesn't draw image but gives some exception, here is the logcat message:

by Log Tag libEGL

by Log Message called unimplemented OpenGL ES API

i not able to understand where i m doing something wrong.Stuck of this part.

So, this kind of requirement can be completed with OpenGL in Android.


回答1:


It sounds like you are trying to make OpenGL calls from your UI thread. GLSurfaceView creates a rendering thread, and an OpenGL context that is current to this rendering thread. All the rendering initiated by the GLSurfaceView is then executed in this rendering thread.

You can't make OpenGL calls without having a current context, and the UI thread does not have one unless you specifically create one. Doing that can get hairy, and I wouldn't recommend it for this case. If you're interested to read up on it anyway, I answered a related question the other day where I try to explain how to create and use multiple contexts: about opengles and texture on android.

The cleanest way to do what you need is to store a representation of what needs to be drawn (e.g. the coordinates of the 3 triangle vertices in your case) as member variables in the GLSurfaceView.Renderer implementation. The flow is then:

  1. In response to the UI input, you invoke methods on the Renderer implementation that set the triangle coordinates in member variables of the class. You may want to use synchronized methods because these coordinates will be accessed both by the UI thread and the rendering thread.
  2. You trigger a redraw of the GLSurfaceView by calling its requestRender() method.
  3. The onDrawFrame() method of the Renderer implementation will be invoked in response. It grabs the new coordinates from the member variables, and draws the triangle.


来源:https://stackoverflow.com/questions/24954264/drawing-shape-on-button-click-using-opengl-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!