Nayanesh Gupte
Nayanesh Gupte

Reputation: 81

how to do Object detection in opengl Android?

I've started with OpenGl es for Android since 2 weeks and after trying 3D examples I'm stuckup at obect detection. Basically mapping between x,y coordinates of screen to x,y,z of 3d space and vice a versa.

I came across :

GLU.gluProject(objX, objY, objZ, model, modelOffset, project, projectOffset, view, viewOffset, win, winOffset);

GLU.gluUnProject(winX, winY, winZ, model, modelOffset, project, projectOffset, view, viewOffset, obj, objOffset);

but i failed to understand that How do I use them exactly?

Thanks in advance if you can elaborate with suitable example. :)

Upvotes: 4

Views: 2168

Answers (1)

the swine
the swine

Reputation: 11031

Well, if you have your matrices ready, you can do this:

float[] modelView = float[16];
float[] projection = float[16];
float[] view = {0, 0, 640, 480}; // viewport
float x = mouseX, y = mouseY, z = -1; 
// those are the inputs

float[] pos = new float[4]; 

GLU.gluUnProject(x, y, z, 
                modelView, 0, 
                projection, 0, 
                world.view().get_size(), 0, 
                pos, 0);

System.out.println("position of mouse in 3D is (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")");

If you want to select objects, you call gluUnProject() twice, once with z = -1 and once with z = 1. That gives you mouse positions at the near and far planes. Subtract them to get a view direction, use the first as an origin, and you have got yourself a nice raytracing task (object selection).

Upvotes: 2

Related Questions