1
$\begingroup$

I am new to OpenGL and I am trying to code un optic flow with dots appearing from the background and moving towards the viewer. As a consequence of a perspective projection, the dots are faster and bigger when they move towards the viewer, but they are smaller and slower when they are farther away from the viewer.

As a result of OpenGL perspective projection, I got the dots moving faster when approaching the viewer, but their size does not change with the distance. Isn't the change of size a consequence of the projection -- just as the change of speed? If not, shall I compute it separately?

Thanks in advance for your help![enter image description here]1

$\endgroup$
2
  • 1
    $\begingroup$ What method are you using to draw the dots? Are they point sprites, points, triangle meshes, or something else? $\endgroup$
    – Dan Hulme
    Sep 16, 2019 at 13:14
  • 1
    $\begingroup$ They are points, I used GL.GL_POINTS $\endgroup$
    – Kathia
    Sep 16, 2019 at 13:27

1 Answer 1

1
$\begingroup$

When you draw a point with GL_POINTS, it's not a 3D object with a size in world-space: its size is a fixed number of pixels. Because of that, the size of the point is independent of the distance from the camera.

If you want to draw dots whose size depends on distance, you either need to compute the perspective yourself and change the point size for each point (not recommended, because state changes like that are expensive); or use another method to draw them. For instance, you might draw a quad and use the fragment shader to make it a circle (e.g. by discarding the fragment if the world co-ordinate was more than a particular distance from the centre of the quad, or by using a transparent texture with a circle in it).

$\endgroup$
4
  • $\begingroup$ Excellent, thanks for your explanation! I understand now why the size doesn't change. However, I don't understand why it worked with the speed? The speed increased when approaching the viewer (unless it was an illusion due to my expectation) $\endgroup$
    – Kathia
    Sep 16, 2019 at 13:57
  • $\begingroup$ Well, the position is perspective-projected, regardless of what primitive (lines, points, triangles) you use. Since the speed is just the change in position over time, you should expect that to work correctly. It's like you've written code to turn on a single pixel. You use a perspective projection to determine the $(x,y)$ co-ordinates of the pixel to turn on, but you're still always turning on a single pixel. $\endgroup$
    – Dan Hulme
    Sep 16, 2019 at 14:42
  • 1
    $\begingroup$ But you can very well change the point size per-vertex in the vertex shader. I wouldn't count that as a "state change" in the classical sense (and personally have made good experiences with per-vertex point size). Of course you'd have to compute the screen-space size yourself from the perspective and there's other problems like point clipping, but the statement on "state changes" is a bit confusing when the same paragraph talks about other shader-based approaches. $\endgroup$ Sep 16, 2019 at 22:04
  • $\begingroup$ @ChristianRau That's true, I'd forgotten you can set point size in the vertex shader. That's a better approach if you don't mind writing the perspective computation there. $\endgroup$
    – Dan Hulme
    Sep 17, 2019 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.