【发布时间】:2015-10-31 01:55:18
【问题描述】:
我使用单纯形噪声创建了随机生成的地形。我的代码如下所示:
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 );
}
}
这给了我索引和顶点,但除非我使用GL_LINES,否则它看起来像一群有色人种。在我真正知道我在看什么之前,我无法继续在地形上取得进展,但我知道如何使用法线的唯一方法是将它们与模型一起加载并将它们发送到着色器。我不知道如何实际生成它们。我已经四处搜索并阅读了很多关于获取周围面的法线并将它们归一化以及其他一些内容,但我不了解其中的大部分内容,据我所知,您只能访问顶点中的单个顶点一次着色器,所以我不确定您将如何处理整个三角形。如您所见,我们将不胜感激。
编辑:
我做了一些研究,现在已经计算了所有三角形的法线:
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;
}
现在我只需要知道如何使用周围三角形的法线来计算顶点的法线。
【问题讨论】:
标签: java android opengl-es glsl normals