From 2bcaf5d46bb46ecc824db581d81942a3125450ab Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 10 Apr 2015 19:53:11 +1000 Subject: [PATCH] Add clipping in homogenous coordinates. --- SoftwareEngine/Engine/SoftwareEngine.cs | 22 ++++++++++++++++++++++ SoftwareEngine/MainForm.cs | 8 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/SoftwareEngine/Engine/SoftwareEngine.cs b/SoftwareEngine/Engine/SoftwareEngine.cs index fa3f1b653..6d92cd69d 100644 --- a/SoftwareEngine/Engine/SoftwareEngine.cs +++ b/SoftwareEngine/Engine/SoftwareEngine.cs @@ -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 ) { diff --git a/SoftwareEngine/MainForm.cs b/SoftwareEngine/MainForm.cs index d615662ba..755ba1469 100644 --- a/SoftwareEngine/MainForm.cs +++ b/SoftwareEngine/MainForm.cs @@ -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 );