Tuesday, October 10, 2017

opengl - How to load a spherical planet and its regions?


I'm designing a game partially composed of planets exploration. I want to use pseudo-random generation for them, regenerating from a defined seed when I have to load them rather than store every detail, which would be too heavy. So I will just store in a file the random seed and modifications done by the player if any.


The player must be able to see the planet from orbit (with very low level of details, then go down to the ground, increasing slowly the level of details of the region where he/she is landing, and unloading the ones on the other sides of the planet, which go outside the player's field of view.



If I had to do it on a plane ground, I would do it easily with a square chunk system. But the problem here is that planets are - almost - spheres.


So what would be the best way to load ground details (relief and grounded objects) around a precise point ?
I already though on two solutions, but both have a weak point:


1. Cutting the sphere in square chunks.


square chunks method


Once the player is close enough of the ground, I just have to improve details of closest squares from his/her position.
If it is not enough, I still can cut each square in sub-squares to load when the players is on or really close of the ground.


But as you can see on the picture, there is a problem if the player try to land on a pole: squares become very slim rectangles, or even triangles for the last line, and additionally to the fact that they would be many to load, generation would appear distorted.


2. Starting from an icosahedron.


icosahedron method



Here, I could just increase triangle tessellation around player's position when he/she is getting close.


But I don't know how to locate triangles close than player's position. I heard Cartesian coordinates could be usefull in that case, but I don't know how to use them.




I'm using C++/OpenGL for it, so the main thing to generate and load here are vertices representing the surface relief and color/texture.



Answer



Okay, so I wrote it out here:


http://www.maths.kisogo.com/index.php?title=Notes:Spherical_coordinates


(I needed the math-markup and also it's really quite long)




Applying the document



The document starts by introducing the notion of a manifold, a manifold is this thing where chunks of it are "homeomorphic" (basically: the same as) chunks of R^n (R^2 is the x/y plane, as you might know)


A chart covers some (possibly all, although in the case of a sphere it CANNOT cover all) of a manifold.


In the article I develop 4 charts for the sphere that preserve angles, that is they keep regular distance.


As you've found out giving coordinates to points on a sphere is actually quite difficult! What we do instead (although on a circle in the example) is give each point a coordinate of the form (i,x,y) where i is a number between 1 and 6 for a sphere, 1 and 4 for a circle. This is the chart number.


The x and y refer to the angles on that chart (or just x if it's a circle).


The 6 charts of a sphere are the top/bottom hemispheres, left/right and front/back hemispheres.





Now you can give evey point a "nice" coordinate that is well behaved. In mathematical terms the domains of the charts are "open" maps, this means there exists some positive number such that a ball around each point is also in the set. For example the range (0,1) (the set that contains x if 0< x<1) is open, take any p in (0,1) (for example 0.001) then there is a number (for example 0.0005) such that any point within 0.0005 of 0.001 is also in (0,1).


What this means is you can pass directions through charts.



Now there is 45 degrees of overlap in the charts we develop. This means if you have a feature at coordinates (i,x,y) you can SAFELY specify points of the form (i,x+a,y+b) as long as a and b are between -45 and +45 (in degrees)


Any point of the form (i,x+a,y+b) can be easily transformed to a point in "normal" 3-dimensional space without problem.




Implementation


You now have a way to store coordinates for something on a sphere, and denote regions large swaths of space with these coordinates, they also behave like coordinates, they're open for example (which is a problem if you use 2 angles instead)


You can also totally discard "how to create a regular sphere" answers now because all you have to do is do 6 planes, and make sure the edges of them align (which is trivial) and the result is:


You'll have a nice sphere with easy to use coordinates


Any questions please comment, I've tried to assume little prior knowledge. I'm also new to teaching people


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...