Reputation: 3303
Currently I'm drawing with OpenGL ES 2.0 an object with pins in it and display it on a CAEAGLLayer. I'm able to identify objects via color picking. Now I need to calculate the screen coordinates for the pin's world coordinates in order to draw for example a label on the right position (I want to use cocoa touch components). What would be a proper way to calculate the screen coordinates (hidden objects should be ignored)? Running through the whole image and use each pixel to perform a color picking on it doesn't sound like the right way to go.
Thanks in advance.
Upvotes: 0
Views: 207
Reputation: 11724
Apple provides its own flavour of GL math functions : See GLKMathUtils documentation.
As Lukas pointed out, you can use it to project (world -> screen coordinates) or un-project (screen -> world coordinates)
So, if you're already using GLKit for your matrix transformations, you can use this :
GLKVector3 screenPoint = GLKMathProject(modelPoint, modelViewMatrix, projectionMatrix, viewport);
Upvotes: 1
Reputation: 3303
I could answer this question myself. On some versions of OpenGL glProject() is available and can be used to calculate the position of a vertex on the screen. Unfortunately this function is not available in OpenGL ES 2.0 so you have to do the calculations yourself or you use a math library like OpenGL Mathematics which provides a function glProject():
#include "matrix_transform.hpp"
#include "transform.hpp"
vec3 pinScreenPosition = glm::project(vec3(0,0,0), modelMatrix, projectionMatrix, vec4(0, 0, screenDimensions.x * screenScale, screenDimensions.y * screenScale));
Upvotes: 0