开发者

Does OpenGL care if I work with large numbers?

开发者 https://www.devze.com 2023-02-05 12:51 出处:网络
For example, I could relate the positions where I draw my entities to the screen before sending them to OpenGL - or: I give OpenGL the direct coordinates, relative to the game world, which are of cour

For example, I could relate the positions where I draw my entities to the screen before sending them to OpenGL - or: I give OpenGL the direct coordinates, relative to the game world, which are of course in a much larger scale.


There aren't speed issues, but you will find that you get accuracy issues as the differences between your numbers will be very small in relation to the size of the numbers.

For example, if you model is centred at (0,0,0) and is 10 x 10 x 10 then two points at opposite sides of your model are at (-5,-5,-5) and (5,5,5). If that same model is centred at (1000,1000,1000) then your points are (995,995,995) and (1005,1005,1005).

While the absolute difference is the same, the difference relative to the coordinates isn't.

This could lead to rounding errors and display artefacts.

You should arrange the coordinates of models so that the centre of the model is at (0,0,0).


Speed of larger values will be the same as smaller values of the same data type. But if you want more than the normal 32bit precision and enable something like GL_EXT_vertex_attrib_64bit, then there may be performance penalties for that.

Large values are allowed, but one extra thing to watch out for other than the standard floating point precision issues is the precision of the depth buffer.

The depth precision is non-linearly distributed. So it is more precise close up and less precise for things far away. So if you want to be able to use a wide range of values for vertex coordinates, be aware that you may see z-fighting artifacts if you intend to render giant objects very far away from the camera very small things close to the camera. Setting your near and far clip planes as close as possible to your actual scene extents will help but may not be enough to fix the problem. See http://www.opengl.org/resources/faq/technical/depthbuffer.htm, especially the last two points for more details.

Also, I know you asked about vertex positions, but texture coordinates may have additional precision limits beyond normal floating point rules. Assigning a texture coordinate of (9999999.5, 9999999.5) and expecting it to wrap properly to the (0..1, 0..1) range may not work as you expect (depending on implementation/hardware).


The floating point pipeline has constant speed on operations, regardless of the size of the operands.

The only danger is getting abnormally large or small numbers where the error gets out of hand and you end up with subnormal numbers.

0

精彩评论

暂无评论...
验证码 换一张
取 消