you can do it with a simple function that uses some raw opengl, provided you are using the GL Driver in bmax.
This is the C# version of it, but it should be very easy to convert to bmax. It uses gluProject();
public static Vector2 Project(float x, float y, float z)
{
int[] viewport = new int[4];
double[] modelview = new double[16];
double[] projection = new double[16];
double projectedx, projectedy;
Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelview);
Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projection);
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
Glu.gluProject(x, y, z, modelview, projection, viewport, out projectedx, out wyprojectedy);
return new Vector2((float)projectedx, (float)projectedy);
}
And if you want to go the other way around, here s how to get a point in 3D space from 2D coordinates. Nice for some simple mousepicking
public Vector3 Unproject( int x, int y )
{
int[] viewport = new int[4];
double[] modelview = new double[16];
double[] projection = new double[16];
float winX, winY, winZ;
double posX, posY, posZ;
Gl.glGetDoublev( Gl.GL_MODELVIEW_MATRIX, modelview );
Gl.glGetDoublev( Gl.GL_PROJECTION_MATRIX, projection );
Gl.glGetIntegerv( Gl.GL_VIEWPORT, viewport );
winX = (float)x;
winY = (float)viewport[3] - (float)y;
Gl.glReadPixels( x, (int)winY, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, out winZ );
Glu.gluUnProject( winX, winY, winZ, modelview, projection, viewport, out posX, out posY, out posZ);
return new Vector3( (float)posX, (float)posY, (float)posZ );
}
|