Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3509 Members - Latest Member: lioneltenel

26. May 2012, 10:51:59 pm
Xith3D CommunityGeneral CategorySupport (Moderator: Marvin Fröhlich)Screen coordinate "picking"
Pages: 1 [2]
Print
Author Topic: Screen coordinate "picking"  (Read 2629 times)
cornholio127
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« Reply #15 on: 11. September 2008, 09:26:35 am »

I know it's an old thread and probably the original poster isn't interested anymore but still i'd like to make a contribution.

I encountered the same problem and wrote this method:
Code:
/**
     * Converts the given world coordinates into screen coordinates.
     *
     * @param p Point in world coordinates
     * @return Position on the screen
     */
    public static Point2f worldToScreen (Canvas3D canvas, Point3f p) {
// get the model-view and the projection matrix
Matrix4f mP = canvas.getView().calculatePerspective(canvas.getWidth(),
    canvas.getHeight()).getMatrix4f();
Matrix4f mM = canvas.getView().getModelViewTransform().getMatrix4f();

// convert the point into a vector of length 4
Vector4f v = new Vector4f(p.getX(), p.getY(), p.getZ(), 1);

// compute the distance between the eye and the view plane
float vpd = 1.0f / FastMath.tan(canvas.getView().getFieldOfView());

// v' = P x M x v
v.mul(mM, v);
v.mul(mP, v);

// scale the resulting vector so that it lies in the view plane
float x = v.getX() * vpd / v.getZ();
float y = v.getY() * vpd / v.getZ();

// convert normalized view plane coordinates into screen coordinates
float xs = (float) canvas.getWidth() * (x + 1f) / 2f;
float ys = (float) canvas.getHeight() / 2f * (1f - y);
return new Point2f(xs, ys);
}

Marvin, can you please have a look at it and tell me if it makes sense? If it is indeed correct you could add it to Xith as a utility method.


Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #16 on: 11. September 2008, 09:48:35 am »

This looks good. And I will definitely add it to xith as a utility. Thanks a lot.

What about parallel projection? Would it work, too? Does it handle screen-scale and center-of-view correctly in parallel-proj?

Marvin
Logged
cornholio127
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« Reply #17 on: 11. September 2008, 10:15:49 am »

I have only tested this with perspective projection. I won't use it for drawing. It's for transforming a part of the 3D scene into 2D, solving a problem (collision of stuff) in 2D and then transforming it back to 3D.

« Last Edit: 11. September 2008, 10:22:19 am by cornholio127 » Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #18 on: 11. September 2008, 06:26:33 pm »

ok.

I will add the necessary code to calculate from world to screen for parallel proj when I add this piece of code to xith. It should be fairly easy to do this, since you only need to scale and offset the world coords.

Marvin
Logged
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic