I have created randomly generated terrain using simplex noise. My code looks like this:
float [ ] vertices = new float [ SIZE * SIZE * 3 ];
short [ ] indices = new short [ ( SIZE - 1 ) * ( SIZE - 1 ) * 2 * 3 ];
for ( int x = 0 , index = 0 ; x < SIZE ; x ++ )
{
for ( int z = 0 ; z < SIZE ; z ++ )
{
vertices [ index ++ ] = x;
vertices [ index ++ ] = ( float ) SimplexNoise.noise ( x , z );
vertices [ index ++ ] = z;
}
}
for ( int i = 0 , index = 0 ; i < ( SIZE - 1 ) ; i ++ )
{
int offset = i * SIZE;
for ( int j = 0 ; j < ( SIZE - 1 ) ; j ++ )
{
indices [ index ++ ] = ( short ) ( j + offset );
indices [ index ++ ] = ( short ) ( j + offset + 1 );
indices [ index ++ ] = ( short ) ( j + offset + 1 + SIZE );
indices [ index ++ ] = ( short ) ( j + offset + 1 + SIZE );
indices [ index ++ ] = ( short ) ( j + offset + SIZE );
indices [ index ++ ] = ( short ) ( j + offset );
}
}
That gives me the indices and vertices but unless I use GL_LINES
it looks like a mob of color. I can't continue progress on the terrain until I actually know what I am looking at but the only way I know how to use normals is to load them in with a model and send them off to the shader. I have no clue how to actually generate them. I have searched around and read a lot about getting the normals of the surrounding faces and normalizing them and some other stuff but I didn't understand most of it and to the best of my knowledge you only have access to a single vertex in the vertex shader at a time so I am not sure how you would process the entire triangle. As you can see much help would be appreciated.
EDIT:
I have did some research and have now calculated the normals of all the triangles:
float [ ] triNormals = new float [ indices.length ];
for ( int i = 0 , index = 0 ; i < indices.length ; )
{
float x , y , z;
x = vertices [ ( 3 * indices [ i ] ) ];
y = vertices [ ( 3 * indices [ i ] ) + 1 ];
z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];
Vector3f p1 = new Vector3f ( x , y , z );
x = vertices [ ( 3 * indices [ i ] ) ];
y = vertices [ ( 3 * indices [ i ] ) + 1 ];
z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];
Vector3f p2 = new Vector3f ( x , y , z );
x = vertices [ ( 3 * indices [ i ] ) ];
y = vertices [ ( 3 * indices [ i ] ) + 1 ];
z = vertices [ ( 3 * indices [ i ++ ] ) + 2 ];
Vector3f p3 = new Vector3f ( x , y , z );
Vector3f u = Vector3f.subtract ( p2 , p1 );
Vector3f v = Vector3f.subtract ( p3 , p1 );
Vector3f normal = Vector3f.crossProduct ( u , v );
triNormals [ index ++ ] = normal.x;
triNormals [ index ++ ] = normal.y;
triNormals [ index ++ ] = normal.z;
}
Now I just need to know how to calculate the normals of the vertices using the normals of the surrounding triangles.