private void BuildPrimitives()
{
{
VertexPt4[] Vertices = new VertexPt4[4];
Vertices[0] = new VertexPt4() { Pt = new float4( -1, +1, 0, 1 ) }; // Top-Left
Vertices[1] = new VertexPt4() { Pt = new float4( -1, -1, 0, 1 ) }; // Bottom-Left
Vertices[2] = new VertexPt4() { Pt = new float4( +1, +1, 0, 1 ) }; // Top-Right
Vertices[3] = new VertexPt4() { Pt = new float4( +1, -1, 0, 1 ) }; // Bottom-Right
ByteBuffer VerticesBuffer = VertexPt4.FromArray( Vertices );
m_Prim_Quad = new Primitive( m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4 );
}
{
VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[4];
Vertices[0] = new VertexP3N3G3B3T2() { P = new float3( -1, +1, 0 ), N = new float3( 0, 0, 1 ), T = new float3( 1, 0, 0 ), B = new float3( 0, 1, 0 ), UV = new float2( 0, 0 ) }; // Top-Left
Vertices[1] = new VertexP3N3G3B3T2() { P = new float3( -1, -1, 0 ), N = new float3( 0, 0, 1 ), T = new float3( 1, 0, 0 ), B = new float3( 0, 1, 0 ), UV = new float2( 0, 1 ) }; // Bottom-Left
Vertices[2] = new VertexP3N3G3B3T2() { P = new float3( +1, +1, 0 ), N = new float3( 0, 0, 1 ), T = new float3( 1, 0, 0 ), B = new float3( 0, 1, 0 ), UV = new float2( 1, 0 ) }; // Top-Right
Vertices[3] = new VertexP3N3G3B3T2() { P = new float3( +1, -1, 0 ), N = new float3( 0, 0, 1 ), T = new float3( 1, 0, 0 ), B = new float3( 0, 1, 0 ), UV = new float2( 1, 1 ) }; // Bottom-Right
ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray( Vertices );
m_Prim_Rectangle = new Primitive( m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3B3T2 );
}
{ // Build the sphere
const int W = 41;
const int H = 22;
VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[W*H];
for ( int Y=0; Y < H; Y++ ) {
double Theta = Math.PI * Y / (H-1);
float CosTheta = (float) Math.Cos( Theta );
float SinTheta = (float) Math.Sin( Theta );
for ( int X=0; X < W; X++ ) {
double Phi = 2.0 * Math.PI * X / (W-1);
float CosPhi = (float) Math.Cos( Phi );
float SinPhi = (float) Math.Sin( Phi );
float3 N = new float3( SinTheta * SinPhi, CosTheta, SinTheta * CosPhi );
float3 T = new float3( CosPhi, 0.0f, -SinPhi );
float3 B = N.Cross( T );
Vertices[W*Y+X] = new VertexP3N3G3B3T2() {
P = N,
N = N,
T = T,
B = B,
UV = new float2( 2.0f * X / W, 1.0f * Y / H )
};
}
}
ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray( Vertices );
uint[] Indices = new uint[(H-1) * (2*W+2)-2];
int IndexCount = 0;
for ( int Y=0; Y < H-1; Y++ ) {
for ( int X=0; X < W; X++ ) {
Indices[IndexCount++] = (uint) ((Y+0) * W + X);
Indices[IndexCount++] = (uint) ((Y+1) * W + X);
}
if ( Y < H-2 ) {
Indices[IndexCount++] = (uint) ((Y+1) * W - 1);
Indices[IndexCount++] = (uint) ((Y+1) * W + 0);
}
}
m_Prim_Sphere = new Primitive( m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3B3T2 );
}
{ // Build the cube
float3[] Normals = new float3[6] {
-float3.UnitX,
float3.UnitX,
-float3.UnitY,
float3.UnitY,
-float3.UnitZ,
float3.UnitZ,
};
float3[] Tangents = new float3[6] {
float3.UnitZ,
-float3.UnitZ,
float3.UnitX,
-float3.UnitX,
-float3.UnitX,
float3.UnitX,
};
VertexP3N3G3B3T2[] Vertices = new VertexP3N3G3B3T2[6*4];
uint[] Indices = new uint[2*6*3];
for ( int FaceIndex=0; FaceIndex < 6; FaceIndex++ ) {
float3 N = Normals[FaceIndex];
float3 T = Tangents[FaceIndex];
float3 B = N.Cross( T );
Vertices[4*FaceIndex+0] = new VertexP3N3G3B3T2() {
P = N - T + B,
N = N,
T = T,
B = B,
UV = new float2( 0, 0 )
};
Vertices[4*FaceIndex+1] = new VertexP3N3G3B3T2() {
P = N - T - B,
N = N,
T = T,
B = B,
UV = new float2( 0, 1 )
};
Vertices[4*FaceIndex+2] = new VertexP3N3G3B3T2() {
P = N + T - B,
N = N,
T = T,
B = B,
UV = new float2( 1, 1 )
};
Vertices[4*FaceIndex+3] = new VertexP3N3G3B3T2() {
P = N + T + B,
N = N,
T = T,
B = B,
UV = new float2( 1, 0 )
};
Indices[2*3*FaceIndex+0] = (uint) (4*FaceIndex+0);
Indices[2*3*FaceIndex+1] = (uint) (4*FaceIndex+1);
Indices[2*3*FaceIndex+2] = (uint) (4*FaceIndex+2);
Indices[2*3*FaceIndex+3] = (uint) (4*FaceIndex+0);
Indices[2*3*FaceIndex+4] = (uint) (4*FaceIndex+2);
Indices[2*3*FaceIndex+5] = (uint) (4*FaceIndex+3);
}
ByteBuffer VerticesBuffer = VertexP3N3G3B3T2.FromArray( Vertices );
m_Prim_Cube = new Primitive( m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3B3T2 );
}
}