Plotting a 3d point on a 2d graph with perspective

BlitzMax Forums/BlitzMax Programming/Plotting a 3d point on a 2d graph with perspective

Ryan Burnside(Posted 2006) [#1]
*EDIT* I posted in the wrong forum, I'm a Blitzmax user. Please move.

I would simply like to know how difficult it is to figure out where a point in located on a 2d graph given field of view, angle of view and distance from the "camera" or "eye".

I won't pester you with "Do it for me" I just want some direction and to know the difficulty. I'm not making a 3d engine I just need to draw some vector prisms. If you can show me how to calculate points that's what I need. I can easly do oblique things but the faces are always out of perspective. I never got past trig in school so if it is harder than alg and trig just say so.

Thanks for your time!


b32(Posted 2006) [#2]
For 3D projection, the general idea would be this:
pX = X / Z
pY = Y / Z
(X, Y) need to be centered and only points with a positive Z should be drawn. Z could be scaled a bit to adjust the 'zoom'.


Jim Teeuwen(Posted 2006) [#3]
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 );
}



ImaginaryHuman(Posted 2006) [#4]
I would just set the actual matrix to perspective and do a glVertex3i()


Ryan Burnside(Posted 2006) [#5]
THanks for your help guys, I'll tinker around with this a bit and get back to you if I need to.