return materialFaces;
}
}
public void CalculateNormals ()
{
if ( indices == null ) return;
normals = new Vector [vertices. Length];
Vector[] temps = new Vector [ indices. Length ];
for ( int ii=0 ; ii < indices. Length ; ii++ )
{
Triangle tr = indices [ii];
Vector v1 = vertices [ tr. Vertex1 ] - vertices [ tr. Vertex2 ];
Vector v2 = vertices [ tr. Vertex2 ] - vertices [ tr. Vertex3 ];
temps [ii] = v1.CrossProduct ( v2 );
}
for ( int ii = 0; ii < vertices. Length ; ii++ )
{
Vector v = new Vector ();
for ( int jj = 0; jj < indices. Length ; jj++ )
{
Triangle tr = indices [jj];
if ( tr. Vertex1 == ii || tr. Vertex2 == ii || tr. Vertex3 == ii )
{
v += temps [jj];
}
}
normals [ii] = v. Normalize ();
}
normalized = true;
}
public void Render ()
{
if ( indices == null ) return;
if (glListNo >= 0)
{
Gl. glCallList(glListNo);
return;
}
glListNo = Gl. glGenLists(1);
System. Console. WriteLine(glListNo);
Gl. glNewList(glListNo, Gl. GL_COMPILE);
bool noMaterials = materialFaces. Count == 0;
if (noMaterials)
{
MaterialFaces m = new MaterialFaces();
m. Material = new Material();
materialFaces. Add(m);
}
foreach (MaterialFaces m in materialFaces)
{
Material material = m. Material;
Gl. glMaterialfv (Gl. GL_FRONT_AND_BACK, Gl. GL_AMBIENT, material. Ambient);
Gl. glMaterialfv (Gl. GL_FRONT_AND_BACK, Gl. GL_DIFFUSE, material. Diffuse);
Gl. glMaterialfv (Gl. GL_FRONT_AND_BACK, Gl. GL_SPECULAR, material. Specular);
Gl. glMaterialf (Gl. GL_FRONT_AND_BACK, Gl. GL_SHININESS, material. Shininess);
if (material. TextureId >= 0 )
{
Gl. glBindTexture ( Gl. GL_TEXTURE_2D, material. TextureId );
Gl. glEnable( Gl. GL_TEXTURE_2D );
}
Gl. glBegin ( Gl. GL_TRIANGLES);
for (int ii = 0; ii < (noMaterials? indices. Length : m. Faces. Length); ii++)
{
Triangle tri = noMaterials? indices[ii] : indices[m. Faces[ii]];
if (normalized) Gl. glNormal3d ( normals[tri. Vertex1].X, normals[tri. Vertex1].Y, normals[tri. Vertex1].Z );
if ( material. TextureId >= 0 ) Gl. glTexCoord2f ( texcoords [ tri. Vertex1 ].U, texcoords [ tri. Vertex1 ].V);
Gl. glVertex3d ( vertices[tri. Vertex1].X, vertices[tri. Vertex1].Y, vertices[tri. Vertex1].Z );
if (normalized) Gl. glNormal3d ( normals[tri. Vertex2].X, normals[tri. Vertex2].Y, normals[tri. Vertex2].Z );
if ( material. TextureId >= 0 ) Gl. glTexCoord2f ( texcoords [ tri. Vertex2 ].U, texcoords [ tri. Vertex2 ].V);
Gl. glVertex3d ( vertices[tri. Vertex2].X, vertices[tri. Vertex2].Y, vertices[tri. Vertex2].Z );
if (normalized) Gl. glNormal3d ( normals[tri. Vertex3].X, normals[tri. Vertex3].Y, normals[tri. Vertex3].Z );
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |


