Hello Guest

How to select objects in 3D?

  • 5 Replies
  • 5205 Views
How to select objects in 3D?
« on: May 08, 2016, 14:52:26 »
Hey everyone, I'm trying to make a Voxel Minecraft game in LWJGL in eclipse and so far I've got perlin noise terrain generation, texture alias support, frustum culling to speed up performance and terrain generation that generates an infinite world and recently I've also added lighting
but now I got to a point where It's time to do some block placement and destroy Cuz so far I've only worked on the render engine and I strugled my but off to get it to render fast I need some help to convert a mouse position to 3d space
I have my source code attached s you guys can check it out. but note that I'm using the new OpenGL3 stuff and none of the old stuff and I also didnt include the LWJGL jar or natives and it also needs slick utils and PNGDecoder.jar (latest version) to work so just add it to the build path.
Ive also just added the exported exe (Dont worry all the resources and natives are included in the single exe).
so yea you guys can check it out to see how you can help me with the mouse picking thing so that i can implement block placement and destroy.

For controls, hold RMB to rotate camera and AWSD to move the player
Source + exe can be found on my dropbox here: https://www.dropbox.com/s/2ufryv6ugfvzbxs/Voxel%20Engine%28souce%20%2B%20runnable%20exe%29.zip?dl=0

*

Kai

Re: How to select objects in 3D?
« Reply #1 on: May 08, 2016, 16:44:32 »
If you want to do it analytically, you need the following steps:
1. Compute the ray in world-coordinates that originates from the eye/camera along the mouse cursor position
2. Do a "ray vs. axis-aligned box" intersection test with the ray and the boxes of the scene

About step 1:
Do it by unprojecting the mouse window coordinates at some arbitrary Z distance (say 0.0). This can be done by calculating:
pos = invViewProjMatrix * (ndcX, ndcY, 0, 1)
ndcX and ndcY are the mouse coordinates converted to normalized device coordinates. Look at the sources of gluUnproject().
Do perspective division on the resulting vector pos and then the world-space ray is defined as:
r(t) = eyePos + t * (pos - eyePos)

About step 2:
Preferably, this step is accelerated via a spatial acceleration structure such as an octree to quickly discard cubes that cannot intersect the ray.
« Last Edit: May 08, 2016, 18:42:54 by Kai »

Re: How to select objects in 3D?
« Reply #2 on: May 10, 2016, 15:44:26 »
Thanks now I at least know where to start. Ill do some further digging on those methods to figure them out.
Thanks

Re: How to select objects in 3D?
« Reply #3 on: May 11, 2016, 15:48:22 »
this is a colour picking method

You have to render all objets in diferents color with a random function and save the color with the object in array,

this render have to bind in a fbo with same size of screen

Then, when you clic the screen, use glReadPixles with mouse coordinates and compare with you saved list

*

Kai

Re: How to select objects in 3D?
« Reply #4 on: May 11, 2016, 20:19:42 »
There is a simple OpenGL 1.1 demo showcasing Minecraft-like cube picking with attaching and removing cubes: https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/BoxPickingDemo.java

Re: How to select objects in 3D?
« Reply #5 on: May 12, 2016, 09:04:19 »
Thanks ill see if that works for my engine