x


Converting world point to screen point

i am trying to make labels for my objects in the scene. obviously i dont know how to properly convert world to screen coords, they seem to rotate around 3d space but they shouldnt

here is the code i use:

    function OnPostRender() 
{       
        var origin : Vector3=cam.camera.WorldToScreenPoint(cam.targetPosition);

        CreateLineMaterial(); 
        lineMaterial.SetPass(0); 
        GL.Begin( GL.LINES ); 
        GL.Color(Color(1,1,1,1)); 
        GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z ); 
        GL.Vertex3( 1,0, 0 ); 
        GL.End(); 
}

i know this code isnt good, it was just for testing. i want line to go from the middle of the object (that is targetPosition Vector3) and other point should be, let's say 1 point long left or right from the object position, it should just project to the screen, not in 3D space

EDIT: i tried this, but it didnt get me too far (i used origin variable):

function OnPostRender(){ 

origin=cam.camera.WorldToViewportPoint(SceneControl.ReturnCameraTargetTransform().position);

    Debug.Log(origin.ToString());

    CreateLineMaterial(); 
    lineMaterial.SetPass(0); 

        GL.Begin(GL.LINES); 
        GL.Color(Color(1,1,1,1)); 

        GL.Vertex3(origin.x ,origin.y, 0);

        GL.Vertex3(origin.x+0.1 ,origin.y+0.1,0); 
        GL.End();

}

i simply dont get this.. :( what i am trying to do is to do labels for my objects in the scene. first step would be to get the position of the object and draw the line from that point and point that is offset a little. that worked fine if i used for example

GL.Vertex3(cam.targetPosition.x ,cam.targetPosition.y, cam.targetPosition.z);
GL.Vertex3(cam.targetPosition.x+0.5 ,cam.targetPosition.y+0.5, cam.targetPosition.z); 

but it rotated in 3d space so my guess was that i need to project the offset point to viewport of screen space, but every combination i tried didnt work...it is so frustrating...

if i use WorldToScreenPoint i get huge numbers for origin.ToString() so i am using WorldToViewportPoint... this seems to be so simple, but i cant get it to work

more ▼

asked Jul 01 '10 at 04:29 PM

pretender gravatar image

pretender
512 122 134 146

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You declared the "origin" variable but you're not doing anything with it.

more ▼

answered Jul 01 '10 at 08:24 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

i know, i tried it before and it didnt work out

Jul 01 '10 at 10:12 PM pretender

how would you do it?

Jul 06 '10 at 09:25 AM pretender
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x501
x129
x116
x115
x53

asked: Jul 01 '10 at 04:29 PM

Seen: 6318 times

Last Updated: Oct 19 '12 at 07:59 AM