While this sounds simple it is actually quite complex and because of this I removed the "chunked level of detail" code from it until I got everything working properly.
Let's first talk about projecting a cube to a sphere briefly. There are a few ways to do this and my way is by no means the "best." One option is to write a Vertex Shader. This provides the advantage that all math related to projecting the cube is done on the graphics card and can actually speed up the process. I have decided to do it in a derived mesh class however. This allows me to more quickly make changes to the code and makes it so I do not need to write another shader. I may change methods in the future but this works in the meantime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for ( unsigned int i = 0; i < m_vertices.size(); i++ ) { | |
Vector3<float> position = m_vertices[ i ]; | |
float x2 = position.GetX() * position.GetX(); | |
float y2 = position.GetY() * position.GetY(); | |
float z2 = position.GetZ() * position.GetZ(); | |
float dx = position.GetX() * sqrtf( 1.0f - ( y2 * 0.5f ) - ( z2 * 0.5f ) + ( ( y2 * z2 ) / 3.0f ) ); | |
float dy = position.GetY() * sqrtf( 1.0f - ( z2 * 0.5f ) - ( x2 * 0.5f ) + ( ( z2 * x2 ) / 3.0f ) ); | |
float dz = position.GetZ() * sqrtf( 1.0f - ( x2 * 0.5f ) - ( y2 * 0.5f ) + ( ( x2 * y2 ) / 3.0f ) ); | |
m_normals[ i ] = Vector3<float>( dx, dy, dz ).Normalized(); | |
m_vertices[ i ] = m_normals[ i ] * pRadius; | |
} |
Now that we have the sphere we can apply the simplex noise formula over it. This is just a matter of getting the correct coordinates of the sphere and running the algorithm over those points. We save these points as a black-and-white image and render it on the sphere. When we do this we get a planet that looks like this.
As you can see I only applied the colorization to one face of the planet! I also did some linear interpolation based on the height/depth of the land/ocean. This gives the deeper parts a darker blue and the higher parts a greener. While this still looks nothing like a planet it is progress!
It also opens new doors for what we can do. It is now possible to use noise algorithms for any use we want on a planet! This could be heatmaps to generate biomes or noise-y clouds! That will come at a later time though!
No comments:
Post a Comment