/// <summary> /// Calls DrawPolygon for the vertecies with the same indices /// backface culling calculations /// </summary> /// <param name="v">vertecies</param> /// <param name="index">indecies</param> /// <param name="cam">camera for backfaceculling</param> /// <param name="bmp">bitmap to draw on</param> private void drawVertices(List <SLVertex> v, int[] index, SLVec3f cam, BmpG bmp) { for (int i = 0; i < index.Length; i += 3) { if (backfaceCulling(v[index[i]], v[index[i + 1]], v[index[i + 2]], cam)) { bmp.DrawPolygon(v[index[i]], v[index[i + 1]], v[index[i + 2]]); } } }
/// <summary> /// The forms paint routine where all drawing happens. /// </summary> private void frmHelloCube_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; #region graphicsSetup g.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; #endregion addFps(); zBuffer.Reset(); #region cameraMovement // start with identity every frame m_viewMatrix.Identity(); // view transform: move the coordinate system away from the camera m_viewMatrix.Translate(m_cam); // add new Rotations for camera m_viewMatrix.Rotate(m_rotAngleUp + (cursorPosition.y - preCursorPosition.y), new SLVec3f(1, 0, 0)); m_viewMatrix.Rotate(m_rotAngleSide + (cursorPosition.x - preCursorPosition.x), new SLVec3f(0, 1, 0)); #endregion using (BmpG bmpGraphics = new BmpG(ClientRectangle.Width, ClientRectangle.Height, zBuffer, light)) { #region graphicsMode bmpGraphics.phong = phongActive; bmpGraphics.wireframe = xWireframeActive; bmpGraphics.showZ = zShowActive; #endregion foreach (Mesh mesh in meshes) { // all transformed vertecies of the mesh are temporary saved in vertex2 List <SLVertex> vertex2 = new List <SLVertex>(); // Vertex Shader #region transformPipeline SLMat4f mv = new SLMat4f(m_viewMatrix); mv.Multiply(mesh.modelMatrix); SLMat3f nm = new SLMat3f(mv.InverseTransposed()); // build combined matrix out of viewport, projection & modelview matrix SLMat4f mvp = new SLMat4f(); mvp.Multiply(m_viewportMatrix); // screen mvp.Multiply(m_projectionMatrix); // projektion mvp.Multiply(mv); // kamera & view (cube) for (int n = 0; n < mesh.vertices.Length; n++) { vertex2.Add(new SLVertex(mvp.Multiply(mesh.vertices[n].position), nm.Multiply(mesh.vertices[n].normale), mesh.color, mv.Multiply(mesh.vertices[n].position))); } #endregion // Fragment Shader drawVertices(vertex2, mesh.indices, m_cam, bmpGraphics); } // Pixel output g.DrawImageUnscaled(bmpGraphics.Result(), 0, 0); } // Tell the system that the window should be repaint again this.Invalidate(); }