Question

I have used GLKMathUnProject to implement ray picking so that I can touch an object in 3D space. Now what I would like to do is (for instance) show a label above that object. So I need to convert 3D space to 2D screen corrds (I know using OpenGl and UIKit is a bad idea and already have text to texture implemented).

I have used the following method below however I think I am missing something. I get on a iPad, The item is in front of me, to be honest not sure what to do with z.:
{922.111, 763.055, 0.990884}

 GLKVector3 x = GLKMathProject(building.position, _baseModelViewMatrix, _projectionMatrix, viewport);

  NSLog(@"%@", NSStringFromGLKVector3(x));

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x.x, x.y, 50, 20)];
  label.text = @"TEST";
  [self.view addSubview:label];
Was it helpful?

Solution

The result vector you get depends on the viewport coordinate system, not your view coordinate system. In general the X and Y have to be divided by viewport width and height and then multiplied by view width and height. In your case this is dividing by 2 because of the retina display.

As for the Z coordinate you do not need it for the position but you may use it for Z order. For instance if you will have multiple text labels you may order them by Z value so those in the back will stay in the back and if 2 of them are in the same screen position the correct one will be in front. Also you could use this value to make some far labels smaller.

As for mixing UIKit with OpenGL there should be no problem with it and it is not a bad practice. You should note though you can not mix the depth buffer: You will not be able to draw some OpenGL drawn object in front of some label. If in the end you do choose to draw labels in OpenGL I suggest creating an image from the UIView (or label) and use it as a texture rather then trying to find some libraries for text drawing. By doing so you will be able to use all the tools already done such as fonts, colours, wrapping...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top