Moving objects in OpenGL - opengl-es

It's my first time to create an iPhone game. I would like to move a ball, I think there are two ways move an object in OpenGL:
Let each object has a unique glViewPort, then change the origin of
it to move the object.
Let each object has a unique translation matrix.
I did move one object using glViewPort, but not sure if this is the best practice or not.

Whenever you are rendering an object (If you are new, I assume you draw within some glBegin glEnd block) load the GL_MODELVIEW_MATRIX and apply your different transformations on it (Translation, Rotation, Scale). The ModelView Matrix should be set before starting your glBegin/End block.
BTW, it is not a wise idea to move the camera too much unless it is really required.

Related

Efficient way to create click targets larger that the actual scene object

What’s a good way to have click targets that are larger than the actual scene object?
So far we have been using a larger invisible (yet raycastable) object to do this but it comes at the cost of requiring two draw calls instead of one.
Is there any better solutions?
So far we have been using a larger invisible (yet raycastable) object to do this but it comes at the cost of requiring two draw calls instead of one.
There is no additional draw call if you set Object3D.visible to false. However, you can still perform raycasting against invisible 3D objects. Use Raycaster.layers to selectively ignore 3D objects when performing intersection tests.
So what you are doing is already fine. You might want to consider to raycast only against bounding volumes if the raycasting performance becomes a bottleneck in your app. The idea is to create an instance of Box3 (AABB) or Sphere (bounding sphere) of your actual scene object and only use it for raycasting.

Is it possible to select multiple objects with box selection?

I know that with QObjectPicker I can mouse pick a single entity. But how can I select multiple objects by drawing a rectangle on the screen?
I think this is actually pretty complicated. But here are my two cents:
If you only need to be able to select unoccluded objects
(i.e. don't need to select occluded ones) you could add a second frame graph branch to your existing one and draw each object with a unique color but to an offscreen texture. Then retrieve this texture, check which colors lie within the drawn rectangle and retrieve the corresponding objects and select them (compare to this question/answer).
I'm not sure how well this works in Qt3D because I've always had some issues with QRenderCapture. It didn't seem to have an impact where I added it in the frame graph, i.e. always captured the last state so maybe even if you have multiple render targets it might capture the wrong one etc. Qt3D is still in a pretty rough state I'd say.
If you need an example of how to render to an offscreen texture check out my example on GitHub.
If you need to be able to select occluded objects too
then it gets pretty complicated. I'm just providing some ideas here. I don't know if they will work.
If you don't have that many objects maybe you could implement the idea from above for each single object. I.e. for each object you have an offscreen frame graph branch that filters out all other objects. Then you could check each rendered texture for the rectangle drawn with the mouse. But again I'm not sure how well this works with Qt3D and if you have many objects (like in a game) it will probably crash because of the many offscreen textures.
You could also implement something like "inverse" frustum culling. In frustum culling, you omit rendering objects that lie outside the view frustum of the camera. You could compute a frustum using the rectangle coordinates drawn with the mouse. Check out the QFrustumCulling code. You would need to compute the planes differently of course, using a modified view matrix. When the user draws the rectangle, compute the frustum and check all objects. Unfortunately, this also selects objects whose bounding sphere intersects with the frustum, even though you might visible not touch any part of the object. If that bothers you, you could directly select all objects whose sphere is completely within the frustum and for all objects which only partly intersect do the intersection computation on a per-triangle basis and exit computation for the current object as soon as a triangle intersects the frustum. Depending on the number of triangles this could be very costly computational-wise.
I'd definitely stick to being able to select only unoccluded objects especially because picking in OpenGL seems to be realized by drawing the ojbects with colors these days.

Camera Collision in Three.js

Does anyone know how can I perform a simple collision check to push back camera so that it doesn't "go through" scene? Is it possible to make a camera respect the 3D objects "masses"? I'm trying to make a small simulator to fly around a building, and enter some rooms, but the camera will always go inside objects.
You can set 3 raycasters for each 3D directions. You append them and the camera to an empty object that you move (not the camera) so everything moves at once.
When your raycasters intersects something you make the difference between the intersection point coordinates and the empty object position. This gives you all the information you can need.
If you want to define objects parameters like masses, add them to object.userData and access them via the returned intersection array.

How to change texture coord after a TriangleMesh was created in JavaFx?

I created a TriangleMesh in JavaFx. The vertices were assigned some initial texture coordinates. I want to animate the texture on the mesh by changing the texture coordinates of the vertices slowly over time. Is this possible? If so, how to do it? If not, what is the best way to achieve this effect?
It is possible...
One approach would be to create a property Double for uvStartX, uvStartY (u,v) ..
Override invalidated method, in the invalidated method, call setMesh(meshMethod)
change those values in a timeline..
I've done something similar with points, meshdivisions etc...
It works and is rather smooth too.
Hard part is making sure all your code maps to a point between 0-1.0 and keeping those points
lined up to each other.

Vertex buffer objects and glutsolidsphere

I have to draw a great collection of spheres in a 3D physical simulation of a "spring-mass" like system.
I would like to know an efficient method to draw spheres without having to compile a display list at every step of my simulation (each step may vary from milliseconds to seconds, depending on the number of bodies involved in the computation).
I've read that vertex-buffer objects are an efficient method to draw objects which need also to be sometimes updated.
Is there any method to draw OpenGL spheres in a way faster than glutSolidSphere?
Spheres are self-similar; every sphere is just a scaled version of any other sphere. I see no need to regenerate any geometry. Indeed, I see no need to have more than one sphere at all.
It's simply a matter of providing the proper scaling matrix. I would suggest a sphere of radius one centered at the origin for your display list or buffer object mesh. Then you can just transform it to different locations, using a scale to set the new radius.
I would like to know an efficient method to draw spheres without having to compile a display list at every step of my simulation (each step may vary from milliseconds to seconds, depending on the number of bodies involved in the computation).
Why are you generating a display list at all, if the geometry you put into is is dynamic. Display lists are meant for static geometry that never or only seldomly changes.
I've read that vertex-buffer objects are an efficient method to draw objects which need also to be sometimes updated.
Actually VBOs are most efficient with static geometry as well. In general you want to keep the number of actual geometry updates as low as possible. In your case the only thing updating are the positions (and maybe the size) of the spheres. This is a prime example for instanced drawing. However this also works well, with updating only a uniform or the transformation matrix and do the call drawing a sphere.
The idea of Vertex Arrays and VBOs is, that you draw a whole batch of geometry with a single call. A sphere would be such a batch.

Resources