Reputation: 1266
In android-opengles,we know that any primitive can be rendered on the android screen through OnDrawFrame(GL10 gl) {.....} that is rendered contineously. as if we want to draw a triangle which is defined in triangle class we use
triangle tri=new triangle();
OnDrawFrame(GL10 gl)
{
.
.
.
tri.draw(gl);
.
.
.
}
so,what i need is,i want to draw triangle whenever the user touches the screen through
OnTouchEvent(MotionEvent e)
{
.
.
.
tri.draw(gl);
.
.
.
}
is it possible? or is there any alternative way?
Upvotes: 1
Views: 97
Reputation: 16774
As already mentioned you can not do that directly. No matter the thread you should not just insert the drawing at some random point in your application as you have no idea what state the rendering is actually in.
Always try to separate the logic from the drawing. In your case that seems to mean you will need an array of objects (triangles). Whenever user touches you can add a triangle object to this array and on draw the triangles can be drawn.
Now you have 2 situations with the triangles. First if you clear the buffer on every draw then the array should keep growing on touch events and each frame all of the triangles are being drawn. Second if you do not clear the buffer and simply keep drawing one object over the other then you should simply remove all objects from the array when you draw them.
In both cases there is also an option to only call the draw method when the changes are done. In this case it is most common to use a boolean value something like needsRedraw
and upon true the rendering should trigger, view refreshed and needsRedraw
set to false. The value needsRedraw
would then again be set to true when some action is made such as another triangle added.
There are some other ways such as you may create a shared context on another thread to which the touch events will report. On this context you can create a FBO with an attached texture. The main context would then redraw this texture as full screen and the result would be the same... Still this is a harder procedure and there is no reason to implement it. Not to mention that you need to ensure these 2 threads are not the same or keep changing the current context on the thread; Also double buffering would probably be mandatory in this case so we are looking at 2 textures on the FBO, locking system and attachment/detachment system.
Upvotes: 2
Reputation: 2779
OpenGL ES Documents say that all GL commands for a context should be called from the same thread. In your case, Android and iOS have to send commands from the main thread. You cannot pass the command from a touch event.
My suggestion, is to have a boolean flag enabled in the touch event and use the same boolean on the main thread to draw something.
Upvotes: 1