Beispiel #1
0
    /// <summary>
    /// The forms paint routine where all drawing happens.
    /// </summary>
    private void frmHelloCube_Paint(object sender, PaintEventArgs e)
    {
        // start with identity every frame
        m_viewMatrix.Identity();

        // view transform: move the coordinate system away from the camera
        m_viewMatrix.Translate(0, 0, m_camZ);

        // model transform: rotate the coordinate system increasingly
        m_modelMatrix.Identity();
        //m_modelMatrix.Translate(1, 0, 0);
        m_modelMatrix.Rotate(m_rotAngle += 0.05f, 0, 1, 0);
        m_modelMatrix.Scale(2, 2, 2);

        // build combined matrix out of viewport, projection & modelview matrix
        SLMat4f m = new SLMat4f();

        m.Multiply(m_viewportMatrix);
        m.Multiply(m_projectionMatrix);
        m.Multiply(m_viewMatrix);
        m.Multiply(m_modelMatrix);

        // transform all vertices into screen space (x & y in pixels and z as the depth)
        SLVec3f[] v2 = new SLVec3f[8];
        for (int i = 0; i < m_v.Length; ++i)
        {
            v2[i] = m.Multiply(m_v[i]);
        }

        Graphics g = e.Graphics;

        g.SmoothingMode = SmoothingMode.AntiAlias;

        // draw front square
        g.DrawLine(Pens.Red, v2[0].x, v2[0].y, v2[1].x, v2[1].y);
        g.DrawLine(Pens.Red, v2[1].x, v2[1].y, v2[2].x, v2[2].y);
        g.DrawLine(Pens.Red, v2[2].x, v2[2].y, v2[3].x, v2[3].y);
        g.DrawLine(Pens.Red, v2[3].x, v2[3].y, v2[0].x, v2[0].y);
        // draw back square
        g.DrawLine(Pens.Green, v2[4].x, v2[4].y, v2[5].x, v2[5].y);
        g.DrawLine(Pens.Green, v2[5].x, v2[5].y, v2[6].x, v2[6].y);
        g.DrawLine(Pens.Green, v2[6].x, v2[6].y, v2[7].x, v2[7].y);
        g.DrawLine(Pens.Green, v2[7].x, v2[7].y, v2[4].x, v2[4].y);
        // draw from front corners to the back corners
        g.DrawLine(Pens.Blue, v2[0].x, v2[0].y, v2[4].x, v2[4].y);
        g.DrawLine(Pens.Blue, v2[1].x, v2[1].y, v2[5].x, v2[5].y);
        g.DrawLine(Pens.Blue, v2[2].x, v2[2].y, v2[6].x, v2[6].y);
        g.DrawLine(Pens.Blue, v2[3].x, v2[3].y, v2[7].x, v2[7].y);

        // Tell the system that the window should be repaint again
        this.Invalidate();
    }