I have code where I made a cube (using 12 triangles) and it moves back and forth in the z-direction.
Is there a way to render this cube (say) 5 times simultaneously just at different positions in space. As of now, I would have to create a new buffer for each cube, which seems wrong.
if CubeObj.draw() is your cube's draw function (likely a call to glVertexPointer then glDrawElements),
glPushMatrix(); //save the current matrix
glTranslatef(translatex, translatey, translatez);
//glRotatef(), glScale, etc.
CubeObj.draw();
glPopMatrix(); //restore the matrix
the glPush/PopMatrix() calls ensure that transformation commands in the block are only applied to that particular cube.
You can call your object's draw function multiple times without reconstructing the object.
That is to say, you could have another copy of the above code and change the transformation commands, and you would appear to have 2 separate cubes.
Hope this helped.
EDIT:
make sure you have a call to glLoadIdentity() at the top of your display function
Related
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?
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.
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.
I have just begun working with OpenGL ES and I want to have an object that does not move when I call gltranslatef. I have tried first rendering everything but this object, then calling gltranslatef with the same values as before, but *(-1). This makes the object stay in its place, but seems to "quiver" slightly. Is what I am describing even possible?
Since you're using glTranslatef(), I figure this must be ES 1.1. The cleaner and more generic approach is to use glPushMatrix() and glPopMatrix() to restore the original transformation state, instead of applying the inverse translation yourself. The structure of your would then look like this:
glPushMatrix();
glTranslatef(...);
// draw objects that need to be translated
glPopMatrix();
// draw objects that should not be translated
glPopMatrix() restores the transformation matrix to the state it was in before the glPushMatrix() call. So in this code sequence, the translation will not be applied to the objects drawn after the glPopMatrix() call.
You could of course also draw the stationary object first, but you will still need to restore the original transformation at the end of the frame anyway, to get ready for the next frame. So it doesn't make much of a difference if you draw it first or last.
From what you're describing, I'm not quite sure what's going on with the "quivering". Since we're dealing with limited floating point precision, it's true that applying a transformation, and then later applying the inverse transformation, does not generally give you exactly the original transformation. But after just applying a couple of translations, I wouldn't expect the rounding error to be large enough to cause a noticeable visual effect.
I want to rotate a object(Some thing like 3d cube ) without touching the surface , say I have a specific x,y,z axis UI and when I change this axis value From the UI , It in turn should turn the 3D object .
The rotation of your object, when being drawn, is set by some variables (a rotation matrix, or a quaternion or such).
Your UI will provide some values, either by widgets you retrieve the values from or by sending events. Using these values you adjust the drawing controlling variable and issue a full redraw of the scene.
OpenGL is not a scene graph. It doesn't maintain a scene, it just draws points, lines or triangles to a pixel based framebuffer. As soon as a primitive (point, line, triangle) has been drawn OpenGL has no recollection about it whatsoever. You want something altered => >ou redraw the whole scene.
The concept you're looking for is a "track ball". Here's some info:
Trackball
Lets say I have a square 'S'. I need to make a circle 'C' go through it in such a way that the S would split open and C would pass through it. Although I need to give it a 3D effect, 2D would also be fine (At least I'll get a head start).
Is there a way to do this using some built-in feature or maybe a way to translate individual pixels or a part of an object? I'm not sure how you map a hole inside an object but you could make an object of the hole's shape with the colour of your background to make it look like a hole right? But the real question is how to make the square split open? (The halves would somehow drift apart and then rejoin).
Ok, since "You want the circle to move over time (from frame to frame) and the square to disappear wherever the circle is and once was", an it's just in 2D, you can use the stencil buffer for this.
The stencil buffer basically stores an integer value for each pixel and lets you change this value and compare it to newly drawn pixels, in order to exclude pixels from drawing. So what we want to do is draw the circle into the stencil buffer (setting it to 1 wherever a circle pixel is) and then draw the square wherever no circle pixel was drawn (wherever the stencil buffer is 0). The only difference to the "usual" stenciling approach is, that we don't clear the stencil buffer in between frames, so that it keeps the circle values wheever the circle once has been. So when you sweep the circle across the screen over time, the square won't be drawn wherever a circle pixel has once been (of course with the same temporal discretization errors like drawn pixels where the circle should have been, but wasn't because it was moved too fast between frames).
So first we draw the circle into the stencil buffer (if you want to use the circle only for stenciling and not draw an actual circle to the screen, you can disable color writes with glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);, but don't forget to reenable color writes afterwards):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //but don't clear stencil
glEnable(GL_STENCIL_TEST); //use stenciling
glStencilFunc(GL_ALWAYS, 1, -1); //draw everywhere, use 1 as ref value
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); //set pixels to ref value (1)
//draw circle
glStencilFunc(GL_NOTEQUAL, 1, -1); //draw wherever not 1 (no circle pixels)
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //leave stencil values unchanged
//draw square
This is just a basic example. You can use the stencil buffer for other things, too. At the moment the circle will change the stencil value no matter if it is behind another already drawn object, but this can be fine-tuned by changing the values in glStencilOp (which configure how to change the stencil value depending on if the stencil test failed, succceeded and depth test failed, or both succeeded), while glStencilFunc controls what test is to be performed and against which reference value (the value of the newly drawn object). But this example and the linked documentation should get you started on the matter.
But still keep in mind that the stencil buffer is really for rather "on-the-fly" image space stenciling operations. If this gets much more complicated and you need to perform actual geometrical operations on your objects, or if this needs to be done in 3D (keyword: Constructive Solid Geometry (CSG)), then OpenGL is not the right tool for the job and in this case an actual geometry library would be the best idea. All OpenGL does is draw simple things (like points, lines, or triangles) to the screen, nothing else. It is neither a scene management, nor a geometry, nor an image processing library (though it might be "misused" for the latter not that difficultly).
I have a byte buffer in my Android application,consider it as a vector.The data in the buffer is changed dynamically(There is a separate thread to update the buffer).I want to draw these data dynamically.
Every data represents a point's Y coordinate in the View,connect the consecutive points to form a curve.As the buffer is updated periodically,the curve looks like moving forward smoothly.
Firstly,I implement this by drawing lines in the View's onDraw(Canvas canvas) method,but it is very ineffective.When calling invalidate method is too frequently, the CPU consume is very heavy.
So I change to use the SurfaceView, draw the dynamic curve in the separate thread, but It is still not satisfactory.
I want to know whether there is any good methods to achieve this.Whether is OpenGL ES a choice?
In OpenGL ES 1.0 you can use glDrawArrays in GL_LINES mode. It will do exactly what the Canvas is doing with your data, but considerably faster