public static unsafe void TestRasterize() { Rasterizer.StartRasterize(CRenderSettings.ResolutionF); RenderBuffer <float> tex0 = new RenderBuffer <float>(CRenderSettings.Resolution, 3); CharRenderBuffer <float> texChar = new CharRenderBuffer <float>(tex0); Vector2 *points = stackalloc Vector2[4]; for (float i = 0; i < JMath.PI_TWO; i += .02f) { Vector2 dir = new Vector2(MathF.Cos(i) * .3f, MathF.Sin(i) * .3f); Vector2 orthoDir = new Vector2(-dir.Y, dir.X); points[0] = new Vector2(.5f, .5f) + dir; points[1] = new Vector2(.5f, .5f) - dir; points[2] = new Vector2(.5f, .5f) + orthoDir; points[3] = new Vector2(.5f, .5f) - orthoDir; LinePrimitive *line = stackalloc LinePrimitive[1] { new LinePrimitive(points, null, 0) }; Rasterizer.Rasterize <Line, LinePrimitive>(line); tex0.WritePixel(Rasterizer.ContriveResult(), new GenericVector <float>(3) { 1f, 1f, 1f }); CRenderer.Render(texChar); tex0.Clear(); Thread.Sleep(16); } Rasterizer.EndRasterize(); }
public TestApplication() { v1 = new Vector3(-10, 0, -10); v2 = new Vector3(10, 0, -10); v3 = new Vector3(0, 0, 10); v4 = new Vector3(0, 20, 0); tx = 50.0f; ty = 40.0f; tz = 40.0f; rx = 0.0f; ry = 0.0f; rz = 0.0f; this.Width = 500; this.Height = 500; this.Text = "Hallo Welt"; bmp = new Bitmap(500, 500); pbx = new PictureBox(); pbx.Dock = DockStyle.Fill; pbx.SizeMode = PictureBoxSizeMode.StretchImage; pbx.Image = bmp; Controls.Add(pbx); rb = new RenderBuffer(bmp); rb.Clear(); rb.Unlock(); pbx.Invalidate(); tmr1 = new Timer(); tmr1.Interval = 1; tmr1.Enabled = true; tmr1.Tick += new System.EventHandler(this.tm1_Tick); this.KeyPress += new KeyPressEventHandler(this.MainForm_KeyPress); }
public RenderBuffer <float> Draw(RenderEntity[] entities, ICamera camera) { _renderTarget.Clear(); _matrixWorldToView = camera.WorldToView; TApp appdata = new TApp(); TV2F[][] vertexV2FData = new TV2F[entities.Length][]; IPrimitive[][] primitives = new IPrimitive[entities.Length][]; Vector2[][] screenCoords = new Vector2[entities.Length][]; for (int i = 0; i < entities.Length; i++) { RenderEntity instanceCopy = entities[i].GetInstanceToApply(); _matrixObjectToWorld = instanceCopy.Transform.LocalToWorld; _matrixObjectToView = _matrixWorldToView * _matrixObjectToWorld; Vector3[] vertices = instanceCopy.Model.Vertices; vertexV2FData[i] = new TV2F[vertices.Length]; screenCoords[i] = new Vector2[vertices.Length]; primitives[i] = instanceCopy.Model.Primitives; for (int j = 0; j < vertices.Length; j++) { appdata.AssignAppdata(ref instanceCopy.Model, j); TV2F v2f = Vertex(ref appdata); vertexV2FData[i][j] = v2f; screenCoords[i][j] = ViewToScreen(v2f.Vertex_VOut); } } //Octree is so annoying //Clipping(); Vector2Int[][][] rasterization = Rasterize(screenCoords, vertexV2FData, primitives); GenericVector <float> whiteColor = new GenericVector <float>(3) { 1, 1, 1 }; //Model for (int i = 0; i < rasterization.Length; i++) { //Primitive for (int j = 0; j < rasterization[i].Length; j++) { //PixelPos for (int k = 0; k < rasterization[i][j].Length; k++) { _renderTarget.WritePixel(rasterization[i][j][k].X, rasterization[i][j][k].Y, whiteColor); } } } return(_renderTarget); }
private void tm1_Tick(object sender, System.EventArgs e) { //while (KEY_ESC_PRESSED==false) { if (fpsCount == 0) { fpsBegin = DateTime.Now; } fpsCount++; rx += 0.03; if (rx > Math.PI * 2) { rx -= (Math.PI * 2); } ry += 0.03; if (ry > Math.PI * 2) { ry -= (Math.PI * 2); } rz += 0.03; if (rz > Math.PI * 2) { rz -= (Math.PI * 2); } Matrix4 tm = Matrix4.CreateRotationMatrix(rx, ry, rz) * Matrix4.CreateTranslationMatrix(tx, ty, tz) * Matrix4.CreatePerspectiveMatrixFOV(Math.PI / 7, 1, 20, 500); Vector3 tv1 = tm * v1; Vector3 tv2 = tm * v2; Vector3 tv3 = tm * v3; Vector3 tv4 = tm * v4; rb.Clear(); rb.DrawLine((int)tv1.X, (int)tv1.Y, (int)tv2.X, (int)tv2.Y, Color.Red); rb.DrawLine((int)tv2.X, (int)tv2.Y, (int)tv3.X, (int)tv3.Y, Color.Green); rb.DrawLine((int)tv3.X, (int)tv3.Y, (int)tv1.X, (int)tv1.Y, Color.Blue); rb.DrawLine((int)tv1.X, (int)tv1.Y, (int)tv4.X, (int)tv4.Y, Color.White); rb.DrawLine((int)tv2.X, (int)tv2.Y, (int)tv4.X, (int)tv4.Y, Color.Coral); rb.DrawLine((int)tv3.X, (int)tv3.Y, (int)tv4.X, (int)tv4.Y, Color.Chartreuse); rb.Unlock(); pbx.Refresh(); if (fpsCount == 100) { fpsCount = 0; fpsEnd = DateTime.Now; double fps = 100.0f / (fpsEnd - fpsBegin).TotalSeconds; this.Text = "FPS: " + fps; } //} }