From b98c9a979fcd047f6c078df5f18e1c9ad24598b3 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 21 Apr 2016 10:32:17 +0200 Subject: [PATCH] Added Rasterizer and first OpenGL project --- CGR3D-KM.sln | 46 ++++ OpenGL1/OpenGL1.vcxproj | 149 ++++++++++++ OpenGL1/OpenGL1.vcxproj.filters | 17 ++ Rasterizer/App.config | 6 + Rasterizer/Form1.Designer.cs | 147 ++++++++++++ Rasterizer/Form1.cs | 245 ++++++++++++++++++++ Rasterizer/Form1.resx | 123 ++++++++++ Rasterizer/Matrix.cs | 108 +++++++++ Rasterizer/Program.cs | 22 ++ Rasterizer/Properties/AssemblyInfo.cs | 36 +++ Rasterizer/Properties/Resources.Designer.cs | 71 ++++++ Rasterizer/Properties/Resources.resx | 117 ++++++++++ Rasterizer/Properties/Settings.Designer.cs | 30 +++ Rasterizer/Properties/Settings.settings | 7 + Rasterizer/Rasterizer.csproj | 92 ++++++++ Rasterizer/Vector3.cs | 78 +++++++ 16 files changed, 1294 insertions(+) create mode 100644 CGR3D-KM.sln create mode 100644 OpenGL1/OpenGL1.vcxproj create mode 100644 OpenGL1/OpenGL1.vcxproj.filters create mode 100644 Rasterizer/App.config create mode 100644 Rasterizer/Form1.Designer.cs create mode 100644 Rasterizer/Form1.cs create mode 100644 Rasterizer/Form1.resx create mode 100644 Rasterizer/Matrix.cs create mode 100644 Rasterizer/Program.cs create mode 100644 Rasterizer/Properties/AssemblyInfo.cs create mode 100644 Rasterizer/Properties/Resources.Designer.cs create mode 100644 Rasterizer/Properties/Resources.resx create mode 100644 Rasterizer/Properties/Settings.Designer.cs create mode 100644 Rasterizer/Properties/Settings.settings create mode 100644 Rasterizer/Rasterizer.csproj create mode 100644 Rasterizer/Vector3.cs diff --git a/CGR3D-KM.sln b/CGR3D-KM.sln new file mode 100644 index 0000000..4c90779 --- /dev/null +++ b/CGR3D-KM.sln @@ -0,0 +1,46 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenGL1", "OpenGL1\OpenGL1.vcxproj", "{D5EB815D-142C-4A10-B69D-A4B326B079FC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rasterizer", "Rasterizer\Rasterizer.csproj", "{ED0523E4-4D43-4F82-AA49-E878B53A0631}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Debug|Any CPU.ActiveCfg = Debug|Win32 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Debug|x64.ActiveCfg = Debug|x64 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Debug|x64.Build.0 = Debug|x64 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Debug|x86.ActiveCfg = Debug|Win32 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Debug|x86.Build.0 = Debug|Win32 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Release|Any CPU.ActiveCfg = Release|Win32 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Release|x64.ActiveCfg = Release|x64 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Release|x64.Build.0 = Release|x64 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Release|x86.ActiveCfg = Release|Win32 + {D5EB815D-142C-4A10-B69D-A4B326B079FC}.Release|x86.Build.0 = Release|Win32 + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|x64.ActiveCfg = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|x64.Build.0 = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|x86.ActiveCfg = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Debug|x86.Build.0 = Debug|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|Any CPU.Build.0 = Release|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|x64.ActiveCfg = Release|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|x64.Build.0 = Release|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|x86.ActiveCfg = Release|Any CPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/OpenGL1/OpenGL1.vcxproj b/OpenGL1/OpenGL1.vcxproj new file mode 100644 index 0000000..4b41cee --- /dev/null +++ b/OpenGL1/OpenGL1.vcxproj @@ -0,0 +1,149 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {D5EB815D-142C-4A10-B69D-A4B326B079FC} + Win32Proj + OpenGL1 + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + \ No newline at end of file diff --git a/OpenGL1/OpenGL1.vcxproj.filters b/OpenGL1/OpenGL1.vcxproj.filters new file mode 100644 index 0000000..6a1782f --- /dev/null +++ b/OpenGL1/OpenGL1.vcxproj.filters @@ -0,0 +1,17 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + \ No newline at end of file diff --git a/Rasterizer/App.config b/Rasterizer/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/Rasterizer/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Rasterizer/Form1.Designer.cs b/Rasterizer/Form1.Designer.cs new file mode 100644 index 0000000..7a5b331 --- /dev/null +++ b/Rasterizer/Form1.Designer.cs @@ -0,0 +1,147 @@ +namespace Rasterizer +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.timer1 = new System.Windows.Forms.Timer(this.components); + this.button1 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + this.button4 = new System.Windows.Forms.Button(); + this.button5 = new System.Windows.Forms.Button(); + this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); + this.SuspendLayout(); + // + // timer1 + // + this.timer1.Enabled = true; + this.timer1.Interval = 2; + this.timer1.Tick += new System.EventHandler(this.timer1_Tick); + // + // button1 + // + this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.button1.Location = new System.Drawing.Point(0, 484); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 0; + this.button1.Text = "Kubus"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // button2 + // + this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.button2.Location = new System.Drawing.Point(81, 484); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(75, 23); + this.button2.TabIndex = 1; + this.button2.Text = "Kegel"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // button3 + // + this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.button3.Location = new System.Drawing.Point(243, 484); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(75, 23); + this.button3.TabIndex = 2; + this.button3.Text = "Bol"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); + // + // button4 + // + this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.button4.Location = new System.Drawing.Point(162, 484); + this.button4.Name = "button4"; + this.button4.Size = new System.Drawing.Size(75, 23); + this.button4.TabIndex = 3; + this.button4.Text = "Cilinder"; + this.button4.UseVisualStyleBackColor = true; + this.button4.Click += new System.EventHandler(this.button4_Click); + // + // button5 + // + this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.button5.Location = new System.Drawing.Point(660, 484); + this.button5.Name = "button5"; + this.button5.Size = new System.Drawing.Size(75, 23); + this.button5.TabIndex = 5; + this.button5.Text = "Toepassen"; + this.button5.UseVisualStyleBackColor = true; + this.button5.Click += new System.EventHandler(this.button5_Click); + // + // numericUpDown1 + // + this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDown1.Location = new System.Drawing.Point(534, 484); + this.numericUpDown1.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.numericUpDown1.Name = "numericUpDown1"; + this.numericUpDown1.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDown1.Size = new System.Drawing.Size(120, 20); + this.numericUpDown1.TabIndex = 6; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(747, 509); + this.Controls.Add(this.numericUpDown1); + this.Controls.Add(this.button5); + this.Controls.Add(this.button4); + this.Controls.Add(this.button3); + this.Controls.Add(this.button2); + this.Controls.Add(this.button1); + this.Name = "Form1"; + this.Text = "Form1"; + this.Load += new System.EventHandler(this.Form1_Load); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + private System.Windows.Forms.Timer timer1; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.Button button4; + private System.Windows.Forms.Button button5; + private System.Windows.Forms.NumericUpDown numericUpDown1; + } +} + diff --git a/Rasterizer/Form1.cs b/Rasterizer/Form1.cs new file mode 100644 index 0000000..60a334f --- /dev/null +++ b/Rasterizer/Form1.cs @@ -0,0 +1,245 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Rasterizer +{ + + public partial class Form1 : Form + { + TextureBrush myBrush; + int dots = 48; + int buttonPressed = 0; + + + public Form1() + { + InitializeComponent(); + this.DoubleBuffered = true; + + + } + List vertices = new List(); + List, Color>> polygons = new List, Color>>(); + float rotation; + + private void Form1_Load(object sender, EventArgs e) + { + //Create_Cube(); + //myBrush = new TextureBrush(new Bitmap(@"C: \Users\kenny\Downloads\MTIwNjA4NjMzOTU5NTgxMTk2.bmp"), System.Drawing.Drawing2D.WrapMode.Tile); + } + + private void Create_Cube() + { + vertices.Clear(); + polygons.Clear(); + vertices.Add(new Vector3(-1, -1, -1)); + vertices.Add(new Vector3(1, -1, -1)); + vertices.Add(new Vector3(1, 1, -1)); + vertices.Add(new Vector3(-1, 1, -1)); + vertices.Add(new Vector3(-1, -1, 1)); + vertices.Add(new Vector3(1, -1, 1)); + vertices.Add(new Vector3(1, 1, 1)); + vertices.Add(new Vector3(-1, 1, 1)); + + polygons.Add(new Tuple, Color>(new List() { 0, 1, 2, 3 }, Color.Green)); + polygons.Add(new Tuple, Color>(new List() { 4, 5, 6, 7 }, Color.Green)); + polygons.Add(new Tuple, Color>(new List() { 0, 4, 5, 1 }, Color.Green)); + polygons.Add(new Tuple, Color>(new List() { 2, 6, 7, 3 }, Color.Green)); + } + + //private void Create_Kegel() + //{ + // //vertices.Clear(); + // vertices.Add(new Vector3(-1, 0, -1)); + // vertices.Add(new Vector3(0, 2, 0)); + // vertices.Add(new Vector3(1, 0, -1)); + // vertices.Add(new Vector3(-1, 0, 1)); + // vertices.Add(new Vector3(0, 2, 0)); + // vertices.Add(new Vector3(1, 0, 1)); + + // vertices.Add(new Vector3(-1, 0, 1)); + // vertices.Add(new Vector3(0, 2, 0)); + // vertices.Add(new Vector3(1, 0, 1)); + + // //vertices.Add(new Vector3(-1, 1, -1)); + // //vertices.Add(new Vector3(-1, -1, 1)); + // //vertices.Add(new Vector3(1, -1, 1)); + // //vertices.Add(new Vector3(1, 1, 1)); + // //vertices.Add(new Vector3(-1, 1, 1)); + + // polygons.Add(new List() { 0, 1, 2 }); + // polygons.Add(new List() { 3, 4, 5 }); + // polygons.Add(new List() { 0, 3, 5, 2 }); + // //polygons.Add(new List() { 2, 6, 7, 3 }); + //} + + private void Create_Pin() + { + vertices.Clear(); + polygons.Clear(); + for (int i = 0; i < dots; i++) + { + vertices.Add(new Vector3((float)Math.Cos(i * ((360.0f / dots)/180 * Math.PI)), (float)Math.Sin(i * ((360.0f / dots)/180 * Math.PI)), (float)0)); + polygons.Add(new Tuple, Color>(new List() { i,(i+1) % dots }, Color.Blue)); + } + vertices.Add(new Vector3(0, 0, 2)); + for(int t =0; t, Color>(new List() { t, dots}, Color.Blue)); + } + } + + private void Create_Buis() + { + vertices.Clear(); + polygons.Clear(); + for (int k = 0; k < 2; k++) + { + for (int i = 0; i < dots ; i++) + { + vertices.Add(new Vector3((float)Math.Cos(i * ((360.0f/ dots) / 180 * Math.PI)), (float)Math.Sin(i * ((360.0f/ dots) / 180 * Math.PI)), (float)k)); + polygons.Add(new Tuple, Color>(new List() { i, (i + 1)% dots }, Color.Blue)); + polygons.Add(new Tuple, Color>(new List() { i+dots, (i + 1) % dots +dots }, Color.Blue)); + } + } + for (int t = 0; t < dots; t++) + { + polygons.Add(new Tuple, Color>(new List() { t, t+dots }, Color.Blue)); + } + } + + private void Create_Bol() + { + vertices.Clear(); + polygons.Clear(); + int slices = dots; + int stacks = dots; + for(int stack = 0; stack < stacks; ++stack) + { + for(int slice = 0; slice < slices; ++slice) + { + double y = 2.0 * stack / stacks - 1.0; + double r = Math.Sqrt(1 - Math.Pow(y, 2)); + double x = r * Math.Sin(2.0 * Math.PI * slice / slices); + double z = r * Math.Cos(2.0 * Math.PI * slice / slices); + vertices.Add(new Vector3((float)x, (float)y, (float)z)); + } + } + } + + private void timer1_Tick(object sender, EventArgs e) + { + rotation += 0.01f; + Invalidate(); + } + + private void Form1_Paint(object sender, PaintEventArgs e) + { + var g = e.Graphics; + g.Clear(Color.Black); + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; + Pen pen = new Pen(Color.PaleVioletRed, 1); + + Matrix m = Matrix.identity(); + + m = m * Matrix.perspective((float)(Math.PI / 2), (float)Width/Height, 0.1f, 20f); + + m = m * Matrix.translate(new Vector3(0, 0, -3 + (float)Math.Cos(rotation*8))); + + m = m * Matrix.rotation(rotation, new Vector3(0, 1, 0)); + m = m * Matrix.rotation((float)Math.Sin(rotation*4)*2, new Vector3(1, 0, 0)); + + foreach (Vector3 v in vertices) + { + Vector3 vect = new Vector3(); + vect = m * v; + + vect.x = Width / 2 + vect.x / vect.z * Width / 2; + vect.y = Height / 2 + vect.y / vect.z * Height / 2; + + g.DrawRectangle(pen, vect.x - 5, vect.y - 5, 10, 10); + } + + pen = new Pen(Color.Aquamarine, 5); + + foreach (var _polygon in polygons) + { + var polygon = _polygon.Item1; + + Point[] points = new Point[polygon.Count]; + for (int p = 0; p < polygon.Count; p++) + { + int curr = polygon[p]; + int next = polygon[(p + 1) % polygon.Count]; + + Vector3 vect1 = m * vertices[curr]; + vect1.x = Width / 2 + vect1.x / vect1.z * Width / 2; + vect1.y = Height / 2 + vect1.y / vect1.z * Height / 2; + + Vector3 vect2 = m * vertices[next]; + vect2.x = Width / 2 + vect2.x / vect2.z * Width / 2; + vect2.y = Height / 2 + vect2.y / vect2.z * Height / 2; + + points[p] = new Point((int)vect1.x, (int)vect1.y); + g.DrawLine(new Pen(_polygon.Item2,5), vect1.x, vect1.y, vect2.x, vect2.y); + } + + // g.FillPolygon(myBrush, points); + } + } + + private void button1_Click(object sender, EventArgs e) + { + Create_Cube(); + buttonPressed = 0; + } + + private void button2_Click(object sender, EventArgs e) + { + Create_Pin(); + buttonPressed = 1; + } + + private void button4_Click(object sender, EventArgs e) + { + Create_Buis(); + buttonPressed = 2; + } + + private void button3_Click(object sender, EventArgs e) + { + Create_Bol(); + buttonPressed = 3; + } + + private void button5_Click(object sender, EventArgs e) + { + dots = (int) numericUpDown1.Value; + if(buttonPressed==0) + { + Create_Cube(); + } + else if (buttonPressed == 1) + { + Create_Pin(); + } + else if (buttonPressed == 2) + { + Create_Buis(); + } + else if (buttonPressed == 3) + { + Create_Bol(); + } + } + + + } +} diff --git a/Rasterizer/Form1.resx b/Rasterizer/Form1.resx new file mode 100644 index 0000000..ac9447d --- /dev/null +++ b/Rasterizer/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 10, 18 + + \ No newline at end of file diff --git a/Rasterizer/Matrix.cs b/Rasterizer/Matrix.cs new file mode 100644 index 0000000..2a3ef7c --- /dev/null +++ b/Rasterizer/Matrix.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rasterizer +{ + struct Matrix + { + float[,] data; + + private Matrix(int size = 4) + { + data = new float[size, size]; + } + + public static Matrix identity() + { + Matrix m = new Matrix(4); + m.data[0,0] = 1; + m.data[1,1] = 1; + m.data[2,2] = 1; + m.data[3,3] = 1; + return m; + } + + public static Matrix perspective(float fovy, float aspect, float zNear, float zFar) + { + Matrix m = new Matrix(4); + + m.data[0, 0] = (float)((1 / Math.Tan(fovy / 2)) / aspect); + m.data[1, 1] = (float)(1 / Math.Tan(fovy / 2)); + m.data[2, 2] = (float)(zFar/(zFar - zNear)); + m.data[2, 3] = -1; + m.data[3, 2] = (float)((zNear * zFar) / (zFar - zNear) ); + + return m; + } + + public static Matrix rotation(float angle, Vector3 axis) + { + Matrix m = Matrix.identity(); + + float c = (float) Math.Cos((double) angle); + float s = (float) Math.Sin((double) angle); + + m.data[0, 0] = (float) Math.Pow(axis.x, 2) * (1 - c) + c; + m.data[0, 1] = axis.x * axis.y * (1 - c) - axis.z * s; + m.data[0, 2] = axis.x * axis.z * (1 - c) - axis.y * s; + m.data[1, 0] = axis.x * axis.y * (1 - c) + axis.z * s; + m.data[1, 1] = (float)Math.Pow(axis.y, 2) * (1 - c) + c; + m.data[1, 2] = axis.y * axis.z * (1 - c) + axis.x * s; + m.data[2, 0] = axis.x * axis.z * (1 - c) + axis.y * s; + m.data[2, 1] = axis.y * axis.z * (1 - c) - axis.x * s; + m.data[2, 2] = (float)Math.Pow(axis.z, 2) * (1 - c) + c; + + return m; + } + public static Matrix translate(Vector3 offset) + { + Matrix m = Matrix.identity(); + + for(int i = 0; i < 3; i++) + { + m.data[i, 3] = offset.data[i]; + } + + return m; + } + + public static Vector3 operator * (Matrix mat, Vector3 vec) + { + Vector3 v = new Vector3(0,0,0); + + for(int i = 0; i < 4; i++) + { + v.data[i] = 0; + + for(int p = 0; p < 4; p++) + { + v.data[i] += mat.data[i, p] * vec.data[p]; + } + } + + return v; + } + public static Matrix operator * (Matrix mat1, Matrix mat2) + { + Matrix m = Matrix.identity(); + + for (int i = 0; i < 4; i++) + { + for (int p = 0; p < 4; p++) + { + m.data[i, p] = 0; + + for (int q = 0; q < 4; q++) + { + m.data[i, p] += mat1.data[i, q] * mat2.data[q, p]; + } + } + } + + return m; + } + } +} diff --git a/Rasterizer/Program.cs b/Rasterizer/Program.cs new file mode 100644 index 0000000..330dedd --- /dev/null +++ b/Rasterizer/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Rasterizer +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/Rasterizer/Properties/AssemblyInfo.cs b/Rasterizer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f9a069b --- /dev/null +++ b/Rasterizer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Rasterizer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Rasterizer")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("ed0523e4-4d43-4f82-aa49-e878b53a0631")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Rasterizer/Properties/Resources.Designer.cs b/Rasterizer/Properties/Resources.Designer.cs new file mode 100644 index 0000000..8fc3b1f --- /dev/null +++ b/Rasterizer/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Rasterizer.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Rasterizer.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/Rasterizer/Properties/Resources.resx b/Rasterizer/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/Rasterizer/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Rasterizer/Properties/Settings.Designer.cs b/Rasterizer/Properties/Settings.Designer.cs new file mode 100644 index 0000000..d11c56b --- /dev/null +++ b/Rasterizer/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Rasterizer.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/Rasterizer/Properties/Settings.settings b/Rasterizer/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/Rasterizer/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Rasterizer/Rasterizer.csproj b/Rasterizer/Rasterizer.csproj new file mode 100644 index 0000000..04d118d --- /dev/null +++ b/Rasterizer/Rasterizer.csproj @@ -0,0 +1,92 @@ + + + + + Debug + AnyCPU + {ED0523E4-4D43-4F82-AA49-E878B53A0631} + WinExe + Properties + Rasterizer + Rasterizer + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + \ No newline at end of file diff --git a/Rasterizer/Vector3.cs b/Rasterizer/Vector3.cs new file mode 100644 index 0000000..95e8da6 --- /dev/null +++ b/Rasterizer/Vector3.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Rasterizer +{ + struct Vector3 + { + public float[] data; + public float x { get { return data[0]; } set { data[0] = value; } } + public float y { get { return data[1]; } set { data[1] = value; } } + public float z { get { return data[2]; } set { data[2] = value; } } + + public float length { get { return (float)Math.Sqrt(x * x + y * y + z * z); } } + + public Vector3(float x, float y, float z) + { + data = new float[4]; + this.x = x; + this.y = y; + this.z = z; + data[3] = 1; + } + + public Vector3 normalize() + { + Vector3 v = new Vector3(0,0,0); + + if (length != 0) + { + v.x = this.x / length; + v.y = this.y / length; + v.z = this.z / length; + } + + return v; + } + + public static Vector3 operator *(Vector3 vec, float f) + { + Vector3 v = new Vector3(0,0,0); + + + + return v; + } + public static Vector3 operator *(float f, Vector3 vec) + { + Vector3 v = new Vector3(0,0,0); + + return v; + } + public static Vector3 operator -(Vector3 a, Vector3 b) + { + Vector3 v = new Vector3(0,0,0); + + return v; + } + public static Vector3 operator +(Vector3 a, Vector3 b) + { + Vector3 v = new Vector3(0,0,0); + + return v; + } + + public Vector3 cross(Vector3 other) + { + throw new NotImplementedException(); + } + public float dot(Vector3 other) + { + throw new NotImplementedException(); + } + + } +}