Hi guys and gals,
I use DrawUserPrimitives
with PrimitiveType.TriangleStrip
to draw cubes with as few vertices as possible. Now my problem is, I want to light them, but setting vertex normals would be pretty useless, since the vertices belong to multiple triangles with differing triangle normals. How would I go about setting the normals in this case?
Here is the setup code for the cube:
private static VertexPositionColor[] CreateCubeMesh()
{
// Algorithm from "Optimizing Triangle Strips for Fast Rendering"
// by Francine Evans et al.
var cube = new VertexPositionColor[8];
cube[ 0 ].Position = new Vector3( .5f, .5f, .5f );
cube[ 1 ].Position = new Vector3( -.5f, .5f, .5f );
cube[ 2 ].Position = new Vector3( .5f, -.5f, .5f );
cube[ 3 ].Position = new Vector3( -.5f, -.5f, .5f );
cube[ 4 ].Position = new Vector3( .5f, .5f, -.5f );
cube[ 5 ].Position = new Vector3( -.5f, .5f, -.5f );
cube[ 6 ].Position = new Vector3( -.5f, -.5f, -.5f );
cube[ 7 ].Position = new Vector3( .5f, -.5f, -.5f );
for( var i = 0; i < 8; i++ )
{
cube[i].Color = Color.White;
}
return new[]
{
cube[ 3 ],
cube[ 2 ],
cube[ 6 ],
cube[ 7 ],
cube[ 4 ],
cube[ 2 ],
cube[ 0 ],
cube[ 3 ],
cube[ 1 ],
cube[ 6 ],
cube[ 5 ],
cube[ 4 ],
cube[ 1 ],
cube[ 0 ]
};
}
And the drawing code:
public override void Draw( Matrix viewMatrix, Matrix projectionMatrix )
{
var effect = Entity.GetOrCreateComponent<BasicEffectComponent>().BasicEffect;
effect.LightingEnabled = false;
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
effect.World = Entity.Transform.LocalToGlobalTransformation;
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply ();
effect.GraphicsDevice.DrawUserPrimitives
(
PrimitiveType.TriangleStrip,
Vertices,
0,
12
);
}
}
Cheers,
Georg
Edit: Or do I have to use a triangle list here, since for triangle strips per vertex normals are the only way to go?