Add clipping in homogenous coordinates.

This commit is contained in:
UnknownShadow200 2015-04-10 19:53:11 +10:00
parent ee01327628
commit 2bcaf5d46b
2 changed files with 26 additions and 4 deletions

View File

@ -122,6 +122,28 @@ namespace SoftwareRasterizer.Engine {
v.Y = -point.Y * height + height / 2.0f;
v.Z = point.Z;
}
bool ProjectClipped( ref Vector3 coord, out Vector3 v ) {
v = Vector3.Zero;
Vector4 point = Vector4.Zero;
Vector4 wCoord = new Vector4( coord.X, coord.Y, coord.Z, 1 );
Vector4.Transform( ref wCoord, ref worldViewProj, out point );
if( Math.Abs( point.X ) > Math.Abs( point.W ) &&
Math.Abs( point.Y ) > Math.Abs( point.W ) ) {
return true;
}
point.X /= point.W;
point.Y /= point.W;
point.Z /= point.W;
//x = ( point.X * 0.5f + 0.5f ) * width;
//y = ( point.Y * 0.5f + 0.5f ) * height;
v.X = point.X * width + width / 2.0f;
v.Y = -point.Y * height + height / 2.0f;
v.Z = point.Z;
return false;
}
void SetPixelClipped( ref Vector3 p, ref FastColour col ) {
if( p.X >= 0 && p.Y >= 0 && p.X < width && p.Y < height ) {

View File

@ -67,16 +67,16 @@ namespace SoftwareRasterizer {
};
rot.X += 0.01f;
Matrix4 world = Matrix4.RotateX( rot.X ) * Matrix4.RotateY( rot.X );
Matrix4 world = Matrix4.RotateX( rot.X ) * Matrix4.RotateZ( rot.X );
device.SetAllMatrices( ref proj, ref view, ref world );
device.Filled_DrawIndexedTriangles( vertices, indices );
/*for( int i = 0; i < 1000; i++ ) {
//device.Filled_DrawIndexedTriangles( vertices, indices );
for( int i = 0; i < 1000; i++ ) {
float offset = (float)rnd.NextDouble() - 0.5f;
for( int j = 0; j < vertices.Length; j++ ) {
vertices[j] += new Vector3( offset, offset, offset );
}
device.Line_DrawIndexedTriangles( vertices, indices );
}*/
}
device.Present( e.Graphics );
base.OnPaint( e );