0

I have three cubes rotating in my 3D scene. What I'm trying to achieve is calculating which one (if any) of the cubes that has been pressed.

I think I understand how gluUnproject works, I simply have to run gluUnproject twice, once for the near plane and once for the far plane and whatnot. The only problem I have is understanding which modelView to use.

The function prototype looks like this (I'm developing for Android):

public static int gluUnProject (float winX, float winY, float winZ, 
                                float[] model, int modelOffset, 
                                float[] project, int projectOffset, 
                                int[] view, int viewOffset, 
                                float[] obj, int objOffset)

What I don't understand is what modelView matrix I'm supposed to be using for the fourth argument. Do I have to calculate a ray once for every cube, using the modelView matrix of each cube?

Abu Hassan
  • 39
  • 6

1 Answers1

0

Do I have to calculate a ray once for every cube, using the modelView matrix of each cube?

That really depends on what you're going to test the ray against. If you want to test the ray against each cube in its own local coordinate system (i.e. cube centered at 0,0,0 in local coordinates) then, yes you must unproject each individual cube (to be honest, if this is what you want to do, you should look into Plücker coordinates which largely simplify this kind of test; but then you should probably use Plücker coordinates anyway for ray-intersection tests).

If you want to test against geometry in world coordinates, you need the modelview matrix as it is, when you reach the world stage in building your modelview, i.e. right after the view transformation, before applying the individual model transformations.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I do indeed want to test against geometry in world space, but I'm having trouble understanding what you're saying. Every time I draw a frame, the view matrix and view projection matrix are calculated in my Renderer class, which is then fed to the cubes like this: cube1.draw(mViewProjectionMatrix, mViewMatrix). Then every cube itself calculates the MVP matrix (using its individual model matrix) and feeds it to the shaders as an uniform. Which matrix should I be using when I'm calculationg the ray from the Renderer class? – Abu Hassan Nov 06 '15 at 09:48
  • @AbuHassan: Pass the view and projection matrix as provided by the renderer class. However since you get a premultiplied projectionview (which has only little use), you first have multiply out the view part. If projectionview PV = P·V, where P = projection you can retrieve the original P by r-multiplying with the inverse of view: P = PV·inv(V); i.e. you have to invert the view matrix first. – datenwolf Nov 06 '15 at 10:26
  • @AbuHassan: I get the impression, that you have only a murky understanding of how the whole transformation chain works. I strongly suggest you polish up your knowledge on linear algebra, how to work with matrices and how matrices can act as a linear operator. Things get a lot of easier with that knowledge. – datenwolf Nov 06 '15 at 10:27
  • Sorry, but English isn't my native tongue. I know how to calculate it manually, but had problems understanding the purpose of the model parameter since every example I found seemed to contradict eachother. Anyway, I simply used the view matrix to convert to world coordinates and successfully implemented a ray picker! – Abu Hassan Nov 06 '15 at 12:40