Thread: draw an object that looks the same size regarles the distance in perspective view

1. draw an object that looks the same size regarles the distance in perspective view

hello everyone,

I am trying to draw many objects that are place on a perspective projection view but that have to look the same size on the pc screen regardless the distance, something like text box on top of the head of a character showing what its talking, I thought on unproject the position to screen coordinate and then using a orthogonal projection and project the screen coordinate position back and draw the object, but I want faster way more efficient because I gonna be using this heavily and the application will run on a mobile device.

thank you.

2. Why don't you calculate the perspective projected space position where you want to place your text box using simple vector/matrix math and then render the text box using an ortho projection matrix.

3. can you explain that a little bit please? because since the projection are going to be diferent I don't see how will they be place a same position in screen view

4. Well, what you have to do is the following:

1. Set perspective projection matrix for rendering
3. Calculate normalized position of UI elements by multiplying your UI element positions with the perspective projection matrix (this should give you NDCs).
4. Set an identity matrix as your projection matrix
5. Render your UI in NDC space (i.e. X and Y are in the range [-1, +1]) using the coordinates calculated in step #3

Of course, there are a lot of other alternatives, you can use e.g. screen space instead of NDC space, etc. But this is probably the most simple.

5. I don't see how will they be place a same position in screen view

6. in your vertex shader, transform the vector (0,0,0) to clipspace. This will get the W the object's pivot will be divided by, thus you get the "inverseScale" that will be applied to vertices.

Code :
```before:
gl_Position = mvp * vec4(inVertexPos.xyz, 1);

after:

uniform float reciprScaleOnscreen = 0.01; // change value to match resolution.    = (2 * ObjectSizeOnscreenInPixels / ScreenWidthInPixels)

float w = (mvp * vec3(0,0,0)).w;
w *= reciprScaleOnscreen;

gl_Position = mvp * vec4(inVertexPos.xyz * w , 1);```

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•