0
\$\begingroup\$

I am trying to find a way to render certain objects in my 3d scene without the effects of the perspective projection. E.g. I want them to have the same pixel dimension independent of distance to the camera or the dimensions of the viewport.

Here is what I tried in the vertex shader so far

uniform vec2 screen_size;

varying float camera_distance;

void main() {
    mat4 mvp = projectionMatrix * modelViewMatrix;

    float scaleFactor = 2.0 * 50.0 / screen_size.x;
    float w = (mvp * vec4(0.0, 0.0, 0.0, 1.0)).w;
    w = w * scaleFactor;

    gl_Position = mvp * vec4(position.xyz * w, 1.0);
}

So far no luck.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Can't you just render them with an orthographic projection matrix? \$\endgroup\$ Commented Sep 1, 2016 at 11:56
  • \$\begingroup\$ @StefanAgartsson I thought about that too, but I am not sure if I achieve the desired effect: I have a 3d starmap and want to display a ring around some of the to display information. If I mix a 3d and an orthographic camera will the star and ring still line up? \$\endgroup\$ Commented Sep 1, 2016 at 12:01
  • 1
    \$\begingroup\$ For that case, it sounds like it would be simpler to scale your more distant rings using their transforms, rather than writing a new shader. \$\endgroup\$ Commented Sep 1, 2016 at 12:22

1 Answer 1

-1
\$\begingroup\$

Here's the shader code:

void main() {

mat4 mvp = projectionMatrix * modelViewMatrix;

vec4 temp = mvp * vec4(position.xyz, 1.0);

gl_Position = vec4(temp.xy*w,temp.zw);
}

To adjust the size of the ring just change the size of the quad or triangle.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.