OpenGL: Zoom adaptive grid lines

2020-07-17 15:19发布

I'm trying to make a viewport grid like the ones seen in most 3D Modelers where zooming adjusts the grid spacing so lines don't get too dense or too far apart. For example zooming out changes the spacing from 1m to 2m then 5m then 10m and 20m etc. Any help?

标签: c++ opengl
1条回答
欢心
2楼-- · 2020-07-17 16:23

Here it's usually not as complicated as you might think it would be initially. For example, you might notice that panning and orbiting the camera has no effect on that grid density, even though the nearest distance from grid plane to viewer could be changing. Likewise, changing FOV has no effect on the chosen grid unit size, even though ideally perhaps it should given how it can significantly skew and narrow the view.

So there's usually just this basic, 1-dimensional kind of notion of zoom distance relative to the look at target (camera pivot), and it involves no fancy math, just adjusting a scalar that affects values passed to glScale or glTranslate, e.g.

As it increases, so does the unit size used for drawing the grid (and possibly snapping to it), and here it's often not some brilliant, mathematical solution but just a linear mapping from that zoom distance to hard-coded unit sizes like 1mm, 2mm, 5mm, 10mm, 20mm, 50mm, 1cm, etc.

These kinds of things usually aren't the result of some algorithmic paper but just a developer/designer sitting down and tweaking things until they look/feel about right. If you're trying to develop a 3D software, I'd recommend never to overlook this basic solution for things related to user interaction because it's easy to become convinced in 3D that everything has to be complicated and have a lot of research and a perfectly-accurate mathematical solution behind it. For the UI parts, you can get away with far more informal solutions.

Sometimes the formal mathematical solutions for these UI visuals/interactions don't work quite as well in practice as these kludged solutions, as you might have noticed in the user interfaces coming from the more academic realm (which are usually much smarter mathematically and algorithmically for these things, but actually less intuitive).

查看更多
登录 后发表回答