J0k3R
J0k3R

Reputation: 303

touch object GLKIT or OpenGL any functions?

I am looking for a description to touch my OpenGL object or get an Event if I touch it. Have the GLKit or OpenGL ES some functions to use? Or I have to calculate the position of my Object and have to compare it with the coordination ob my Touch?

Upvotes: 1

Views: 886

Answers (1)

Fonix
Fonix

Reputation: 11597

There is no built in one, but here is a ported version of the gluProject function that will put a point from your object in screen coordinates, so you can see if your touch is near that point:

GLKVector3 gluProject(GLKVector3 position, 
                  GLKMatrix4 projMatrix,
                  GLKMatrix4 modelMatrix,
                  CGRect viewport
                  )
{
GLKVector4 in;
GLKVector4 out;

in = GLKVector4Make(position.x, position.y, position.z, 1.0);

out = GLKMatrix4MultiplyVector4(modelMatrix, in);
in = GLKMatrix4MultiplyVector4(projMatrix, out);

if (in.w == 0.0) NSLog(@"W = 0 in project function\n");
in.x /= in.w;
in.y /= in.w;
in.z /= in.w;
/* Map x, y and z to range 0-1 */
in.x = in.x * 0.5 + 0.5;
in.y = in.y * 0.5 + 0.5;
in.z = in.z * 0.5 + 0.5;

/* Map x,y to viewport */
in.x = in.x * (viewport.size.width) + viewport.origin.x;
in.y = in.y * (viewport.size.height) + viewport.origin.y;

return GLKVector3Make(in.x, in.y, in.z);

}

- (GLKVector2) getScreenCoordOfPoint {

GLKVector3 out = gluProject(self.point, modelMatrix, projMatrix, view.frame);

GLKVector2 point = GLKVector2Make(out.x, view.frame.size.height - out.y);

return point;
}

Upvotes: 3

Related Questions