}
// now compute vertex normals
Vector3D sum (0, 0, 0 ); Vector3D zero ( sum );
for ( i = 0; i < numVertices; i++ )
// process each vertex in turn
{
for ( int j =0; j < numFaces; j++ )
// pocess each face { // check if the vertex is shared
//by another face if ( faces [j].index [0] == i || faces [j].index [1] == i || faces [j].index [2] == i )
sum += tempNormals [j];
}

А. В. Боресков. Графика трехлкрной компьютерной игры

normals [i] = sum.normalize ();
sum = zero; // reset the sum
}
delete tempNormals; // free face normals
}

В простейшем случае вывод всех граней может быть осуществлен при помощи следующего фрагмента кода: га

void Mesh3D :: draw ( Views view, const Cameras camera,
const Vector4D& color, Fog * fog. Texture * txt, bool transparent ) const
{
if ( lisOk () ) return;
int shadeModel;
if ( txt != NULL )
view.bindTexture ( txt ); else
view.bindTexture ( texture );
glGetlntegerv ( GL_SHADE_MODEL, SshadeModel ); glColor4fv ( color );
if ( smooth )
glShadeModel ( GL_SMOOTH ); else
glShadeModel ( GL_FLAT );
glEnableClientState ( GL_VERTEX_ARRAY ); glVertexPointer ( 3, GL_FLOAT, 0, vertices );
if ( colors != NULL ) {
glColorPointer ( 4, GL_FLOAT, 0, colors >;
glEnableClientState ( GL_COLOR. ARRAY );
}
else
glDisableClientState ( GL„COLOR_ARRAY-)";

Добавляем модели

if ( normals != NULL )
{
glNormalPointer ( GL_FLOAT, 0, normals );
glEnableClientState ( GL_NORMAL_ARRAY );
}
else
glDisableClientState ( GL_NORMAL_ARRAY );
if ( texCoords != NULL ) {
glTexCoordPointer ( 2, GL_FLOAT, 0, texCoords ); glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
}
else
glDisableClientState ( GL_TEXTURE__C0ORD_ARRAY );
glDrawElements ( GL_TRIANGLES, 3*numFaces, GL_UNSIGNED_INT, faces ) ;
glDisableClientState ( GL_VERTEX__ARRAY );
glDisableClientState ( GL_COLOR_ARRAY );
glDisableClientState ( GL_NORMAL_ARRAY );
glDisableClientState ( GL_TEXTURE_COORD_ARRAY );

⇐ Предыдущая| |Следующая ⇒