public void FillTriangle(Vertex v1, Vertex v2, Vertex v3) { Matrix4f screenSpaceTransform = new Matrix4f().InitScreenSpaceTransform(backBuffer.Width/2, backBuffer.Height/2); Vertex minYVert = v1.Transform(screenSpaceTransform).PerspectiveDivide(); Vertex midYVert = v2.Transform(screenSpaceTransform).PerspectiveDivide(); Vertex maxYVert = v3.Transform(screenSpaceTransform).PerspectiveDivide(); if(maxYVert.GetY() < midYVert.GetY()) { Vertex temp = maxYVert; maxYVert = midYVert; midYVert = temp; } if(midYVert.GetY() < minYVert.GetY()) { Vertex temp = midYVert; midYVert = minYVert; minYVert = temp; } if(maxYVert.GetY() < midYVert.GetY()) { Vertex temp = maxYVert; maxYVert = midYVert; midYVert = temp; } float area = minYVert.TriangleAreaTimes2(maxYVert, midYVert); int handedness = area >= 0 ? 1 : 0; ScanConvertTriangle(minYVert, midYVert, maxYVert, handedness); FillShape((int)minYVert.GetY(), (int)maxYVert.GetY()); }
public Matrix4f InitRotation(float x, float y, float z) { Matrix4f rx = new Matrix4f(); Matrix4f ry = new Matrix4f(); Matrix4f rz = new Matrix4f(); rz.m[0, 0] = (float)Math.Cos(z); rz.m[0, 1] = -(float)Math.Sin(z); rz.m[0, 2] = 0; rz.m[0, 3] = 0; rz.m[1, 0] = (float)Math.Sin(z); rz.m[1, 1] = (float)Math.Cos(z); rz.m[1, 2] = 0; rz.m[1, 3] = 0; rz.m[2, 0] = 0; rz.m[2, 1] = 0; rz.m[2, 2] = 1; rz.m[2, 3] = 0; rz.m[3, 0] = 0; rz.m[3, 1] = 0; rz.m[3, 2] = 0; rz.m[3, 3] = 1; rx.m[0, 0] = 1; rx.m[0, 1] = 0; rx.m[0, 2] = 0; rx.m[0, 3] = 0; rx.m[1, 0] = 0; rx.m[1, 1] = (float)Math.Cos(x); rx.m[1, 2] = -(float)Math.Sin(x); rx.m[1, 3] = 0; rx.m[2, 0] = 0; rx.m[2, 1] = (float)Math.Sin(x); rx.m[2, 2] = (float)Math.Cos(x); rx.m[2, 3] = 0; rx.m[3, 0] = 0; rx.m[3, 1] = 0; rx.m[3, 2] = 0; rx.m[3, 3] = 1; ry.m[0, 0] = (float)Math.Cos(y); ry.m[0, 1] = 0; ry.m[0, 2] = -(float)Math.Sin(y); ry.m[0, 3] = 0; ry.m[1, 0] = 0; ry.m[1, 1] = 1; ry.m[1, 2] = 0; ry.m[1, 3] = 0; ry.m[2, 0] = (float)Math.Sin(y); ry.m[2, 1] = 0; ry.m[2, 2] = (float)Math.Cos(y); ry.m[2, 3] = 0; ry.m[3, 0] = 0; ry.m[3, 1] = 0; ry.m[3, 2] = 0; ry.m[3, 3] = 1; m = rz.Mul(ry.Mul(rx)).GetM(); return(this); }
private void RenderGame() { target.BeginRender(); target.Clear(); rotCounter += (float)timeDelta; Matrix4f translation = new Matrix4f().InitTranslation(0.0f, 0.0f, 3.0f); Matrix4f rotation = new Matrix4f().InitRotation(0.0f, rotCounter, 0.0f); Matrix4f transform = projection.Mul(translation.Mul(rotation)); target.FillTriangle(maxYVert.Transform(transform), midYVert.Transform(transform), minYVert.Transform(transform)); target.EndRender(); Refresh(); }
public Form1() { InitializeComponent(); Application.Idle += HandleApplicaitonIdle; backBuffer = new Bitmap(Width, Height); target = new RenderContext(backBuffer); // set render target bitmap backbuffer previousTime = DateTime.Now; frameCount = 0; rotCounter = 0.0f; projection = new Matrix4f().InitPerspective((float)Math.PI / 2.0f, (float)(Width / Height), 0.1f, 1000.0f); }
public Matrix4f Mul(Matrix4f r) { Matrix4f res = new Matrix4f(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { res.Set(i, j, m[i, 0] * r.Get(0, j) + m[i, 1] * r.Get(1, j) + m[i, 2] * r.Get(2, j) + m[i, 3] * r.Get(3, j)); } } return(res); }
public void FillTriangle(Vertex v1, Vertex v2, Vertex v3) { Matrix4f screenSpaceTransform = new Matrix4f().InitScreenSpaceTransform(backBuffer.Width / 2, backBuffer.Height / 2); Vertex minYVert = v1.Transform(screenSpaceTransform).PerspectiveDivide(); Vertex midYVert = v2.Transform(screenSpaceTransform).PerspectiveDivide(); Vertex maxYVert = v3.Transform(screenSpaceTransform).PerspectiveDivide(); if (maxYVert.GetY() < midYVert.GetY()) { Vertex temp = maxYVert; maxYVert = midYVert; midYVert = temp; } if (midYVert.GetY() < minYVert.GetY()) { Vertex temp = midYVert; midYVert = minYVert; minYVert = temp; } if (maxYVert.GetY() < midYVert.GetY()) { Vertex temp = maxYVert; maxYVert = midYVert; midYVert = temp; } float area = minYVert.TriangleAreaTimes2(maxYVert, midYVert); int handedness = area >= 0 ? 1 : 0; ScanConvertTriangle(minYVert, midYVert, maxYVert, handedness); FillShape((int)minYVert.GetY(), (int)maxYVert.GetY()); }
public Matrix4f InitRotation(float x, float y, float z) { Matrix4f rx = new Matrix4f(); Matrix4f ry = new Matrix4f(); Matrix4f rz = new Matrix4f(); rz.m[0, 0] = (float)Math.Cos(z);rz.m[0, 1] = -(float)Math.Sin(z);rz.m[0, 2] = 0; rz.m[0, 3] = 0; rz.m[1, 0] = (float)Math.Sin(z);rz.m[1, 1] = (float)Math.Cos(z);rz.m[1, 2] = 0; rz.m[1, 3] = 0; rz.m[2, 0] = 0; rz.m[2, 1] = 0; rz.m[2, 2] = 1; rz.m[2, 3] = 0; rz.m[3, 0] = 0; rz.m[3, 1] = 0; rz.m[3, 2] = 0; rz.m[3, 3] = 1; rx.m[0, 0] = 1; rx.m[0, 1] = 0; rx.m[0, 2] = 0; rx.m[0, 3] = 0; rx.m[1, 0] = 0; rx.m[1, 1] = (float)Math.Cos(x);rx.m[1, 2] = -(float)Math.Sin(x);rx.m[1, 3] = 0; rx.m[2, 0] = 0; rx.m[2, 1] = (float)Math.Sin(x);rx.m[2, 2] = (float)Math.Cos(x);rx.m[2, 3] = 0; rx.m[3, 0] = 0; rx.m[3, 1] = 0; rx.m[3, 2] = 0; rx.m[3, 3] = 1; ry.m[0, 0] = (float)Math.Cos(y);ry.m[0, 1] = 0; ry.m[0, 2] = -(float)Math.Sin(y);ry.m[0, 3] = 0; ry.m[1, 0] = 0; ry.m[1, 1] = 1; ry.m[1, 2] = 0; ry.m[1, 3] = 0; ry.m[2, 0] = (float)Math.Sin(y);ry.m[2, 1] = 0; ry.m[2, 2] = (float)Math.Cos(y);ry.m[2, 3] = 0; ry.m[3, 0] = 0; ry.m[3, 1] = 0; ry.m[3, 2] = 0; ry.m[3, 3] = 1; m = rz.Mul(ry.Mul(rx)).GetM(); return this; }
public Matrix4f Mul(Matrix4f r) { Matrix4f res = new Matrix4f(); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { res.Set(i, j, m[i, 0] * r.Get(0, j) + m[i, 1] * r.Get(1, j) + m[i, 2] * r.Get(2, j) + m[i, 3] * r.Get(3, j)); } } return res; }
public Vertex Transform(Matrix4f transform) { return(new Vertex(transform.Transform(m_pos))); }
public Vertex Transform(Matrix4f transform) { return new Vertex(transform.Transform(m_pos)); }