Exemple #1
0
    private float m_rotAngle;             // angle of cube rotation
    #endregion

    /// <summary>
    /// We initialize the matrices and the vertices for the wire frame cube
    /// </summary>
    public frmHelloCube()
    {
        // Create matrices
        m_modelMatrix      = new SLMat4f();
        m_viewMatrix       = new SLMat4f();
        m_projectionMatrix = new SLMat4f();
        m_viewportMatrix   = new SLMat4f();

        // define the 8 vertices of a cube
        m_v    = new SLVec3f[8];
        m_v[0] = new SLVec3f(-0.5f, -0.5f, 0.5f);  // front lower left
        m_v[1] = new SLVec3f(0.5f, -0.5f, 0.5f);   // front lower right
        m_v[2] = new SLVec3f(0.5f, 0.5f, 0.5f);    // front upper right
        m_v[3] = new SLVec3f(-0.5f, 0.5f, 0.5f);   // front upper left
        m_v[4] = new SLVec3f(-0.5f, -0.5f, -0.5f); // back lower left
        m_v[5] = new SLVec3f(0.5f, -0.5f, -0.5f);  // back lower right
        m_v[6] = new SLVec3f(0.5f, 0.5f, -0.5f);   // back upper left
        m_v[7] = new SLVec3f(-0.5f, 0.5f, -0.5f);  // back upper right

        m_camZ     = -4;                           // backwards movement of the camera
        m_rotAngle = 0;                            // initial rotation angle

        // Without double buffering it would flicker
        this.DoubleBuffered = true;

        InitializeComponent();
    }
Exemple #2
0
    /// <summary>
    /// Post multiplies a translation matrix defined by the vector t
    /// </summary>
    public void Translate(SLVec3f t)
    {
        SLMat4f Tr = new SLMat4f();

        Tr.Translation(t);
        Multiply(Tr);
    }
Exemple #3
0
    /// <summary>
    /// Post multiplies a rotation matrix defined by
    /// the angle degAng and the rotation axis [axisx,axisy,axisz]
    /// </summary>
    public void Rotate(float degAng, float axisx, float axisy, float axisz)
    {
        SLMat4f R = new SLMat4f();

        R.Rotation(degAng, axisx, axisy, axisz);
        Multiply(R);
    }
Exemple #4
0
    /// <summary>
    /// Post multiplies a scaling matrix defined by the vector s
    /// </summary>
    public void Scale(SLVec3f s)
    {
        SLMat4f S = new SLMat4f();

        S.Scaling(s);
        Multiply(S);
    }
    private bool m_mouseLeftDown;         // flag if mouse is down
    #endregion

    /// <summary>
    /// We intialize the matrices the the vertices foth the wireframe cube
    /// </summary>
    public frmHelloCube()
    {
        InitializeComponent();

        // Create matrices
        m_modelViewMatrix  = new SLMat4f();
        m_projectionMatrix = new SLMat4f();
        m_viewportMatrix   = new SLMat4f();

        // define the 8 vertices of a cube
        m_v    = new SLVec3f[8];
        m_v[0] = new SLVec3f(-0.5f, -0.5f, 0.5f);  // front lower left
        m_v[1] = new SLVec3f(0.5f, -0.5f, 0.5f);   // front lower right
        m_v[2] = new SLVec3f(0.5f, 0.5f, 0.5f);    // front upper right
        m_v[3] = new SLVec3f(-0.5f, 0.5f, 0.5f);   // front upper left
        m_v[4] = new SLVec3f(-0.5f, -0.5f, -0.5f); // back lower left
        m_v[5] = new SLVec3f(0.5f, -0.5f, -0.5f);  // back lower right
        m_v[6] = new SLVec3f(0.5f, 0.5f, -0.5f);   // back upper right
        m_v[7] = new SLVec3f(-0.5f, 0.5f, -0.5f);  // back upper left

        m_camZ          = -2;                      // backwards movment of the camera
        m_rotx          = 0;
        m_roty          = 0;
        m_dx            = 0;
        m_dy            = 0;
        m_mouseLeftDown = false;

        // Without double buffering it would flicker
        this.DoubleBuffered = true;
    }
Exemple #6
0
    /// <summary>
    /// Returns the transposed of the matrix
    /// </summary>
    public SLMat4f Transposed()
    {
        SLMat4f t = new SLMat4f(this);

        t.Transpose();
        return(t);
    }
Exemple #7
0
    /// <summary>
    /// Post multiplies the matrix by matrix A
    /// </summary>
    public void Multiply(SLMat4f A)
    {
        //     | 0  4  8 12 |   | 0  4  8 12 |
        //     | 1  5  9 13 |   | 1  5  9 13 |
        // M = | 2  6 10 14 | x | 2  6 10 14 |
        //     | 3  7 11 15 |   | 3  7 11 15 |

        Set(m[0] * A.m[0] + m[4] * A.m[1] + m[8] * A.m[2] + m[12] * A.m[3], //row 1
            m[0] * A.m[4] + m[4] * A.m[5] + m[8] * A.m[6] + m[12] * A.m[7],
            m[0] * A.m[8] + m[4] * A.m[9] + m[8] * A.m[10] + m[12] * A.m[11],
            m[0] * A.m[12] + m[4] * A.m[13] + m[8] * A.m[14] + m[12] * A.m[15],

            m[1] * A.m[0] + m[5] * A.m[1] + m[9] * A.m[2] + m[13] * A.m[3], //row 2
            m[1] * A.m[4] + m[5] * A.m[5] + m[9] * A.m[6] + m[13] * A.m[7],
            m[1] * A.m[8] + m[5] * A.m[9] + m[9] * A.m[10] + m[13] * A.m[11],
            m[1] * A.m[12] + m[5] * A.m[13] + m[9] * A.m[14] + m[13] * A.m[15],

            m[2] * A.m[0] + m[6] * A.m[1] + m[10] * A.m[2] + m[14] * A.m[3], //row 3
            m[2] * A.m[4] + m[6] * A.m[5] + m[10] * A.m[6] + m[14] * A.m[7],
            m[2] * A.m[8] + m[6] * A.m[9] + m[10] * A.m[10] + m[14] * A.m[11],
            m[2] * A.m[12] + m[6] * A.m[13] + m[10] * A.m[14] + m[14] * A.m[15],

            m[3] * A.m[0] + m[7] * A.m[1] + m[11] * A.m[2] + m[15] * A.m[3], //row 4
            m[3] * A.m[4] + m[7] * A.m[5] + m[11] * A.m[6] + m[15] * A.m[7],
            m[3] * A.m[8] + m[7] * A.m[9] + m[11] * A.m[10] + m[15] * A.m[11],
            m[3] * A.m[12] + m[7] * A.m[13] + m[11] * A.m[14] + m[15] * A.m[15]);
    }
Exemple #8
0
    /// <summary>
    /// Post multiplies a translation matrix defined by the vector [tx,ty,tz]
    /// </summary>
    public void Translate(float tx, float ty, float tz)
    {
        SLMat4f Tr = new SLMat4f();

        Tr.Translation(tx, ty, tz);
        Multiply(Tr);
    }
Exemple #9
0
 /// <summary>
 /// Sets the matrix with the matrix A
 /// </summary>
 public void Set(SLMat4f A)
 {
     m[0] = A.m[0]; m[4] = A.m[4]; m[8] = A.m[8];  m[12] = A.m[12];
     m[1] = A.m[1]; m[5] = A.m[5]; m[9] = A.m[9];  m[13] = A.m[13];
     m[2] = A.m[2]; m[6] = A.m[6]; m[10] = A.m[10]; m[14] = A.m[14];
     m[3] = A.m[3]; m[7] = A.m[7]; m[11] = A.m[11]; m[15] = A.m[15];
 }
Exemple #10
0
 /// <summary>
 /// number of vertices and indices
 /// </summary>
 /// <param name="vSize">vertices</param>
 /// <param name="iSize">indices</param>
 public Mesh(int vSize, int iSize)
 {
     vertices    = new SLVertex[vSize];
     indices     = new int[iSize];
     color       = new SLVec3f();
     modelMatrix = new SLMat4f();
 }
Exemple #11
0
    /// <summary>
    /// Matrix - Matrix multiplication
    /// </summary>
    /// <param name="u">A <see cref="SLMat4f"/> instance.</param>
    /// <param name="v">A <see cref="SLMat4f"/> instance.</param>
    /// <returns>A new <see cref="SLMat4f"/> instance multiplied by the matrix m</returns>
    /// <summary>
    public static SLMat4f operator*(SLMat4f m1, SLMat4f m2)
    {
        SLMat4f m = new SLMat4f(m1);

        m.Multiply(m2);
        return(m);
    }
Exemple #12
0
    /// <summary>
    /// Post multiplies a scaling matrix defined by the vector [sx,sy,sz]
    /// </summary>
    public void Scale(float sx, float sy, float sz)
    {
        SLMat4f S = new SLMat4f();

        S.Scaling(sx, sy, sz);
        Multiply(S);
    }
Exemple #13
0
    /// <summary>
    /// Post multiplies a rotation matrix defined by
    /// the angle degAng and the rotation axis
    /// </summary>
    public void Rotate(float degAng, SLVec3f axis)
    {
        SLMat4f R = new SLMat4f();

        R.Rotation(degAng, axis);
        Multiply(R);
    }
Exemple #14
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();
    }
Exemple #15
0
    /// <summary>
    /// Retrieves the camera vectors eye, at and up if this matrix would be a view matrix
    /// </summary>
    /// <param name="Eye"></param>
    /// <param name="At"></param>
    /// <param name="Up"></param>
    public void GetLookAt(ref SLVec3f Eye, ref SLVec3f At, ref SLVec3f Up)
    {
        SLMat4f invRot      = new SLMat4f(this);
        SLVec3f translation = new SLVec3f(m[12], m[13], m[14]);

        invRot.m[12] = 0;
        invRot.m[13] = 0;
        invRot.m[14] = 0;
        invRot.Transpose();
        Eye.Set(invRot.Multiply(-translation)); // vector to the eye
        Up.Set(m[1], m[5], m[9]);               // normalized look up vector
        At.Set(-m[2], -m[6], -m[10]);           // normalized look at vector
    }
Exemple #16
0
    /// <summary>
    /// We initialize the matrices and the vertices for the wire frame cube
    /// </summary>
    public frmHelloCube()
    {
        // Create matrices
        m_viewMatrix       = new SLMat4f();
        m_projectionMatrix = new SLMat4f();
        m_viewportMatrix   = new SLMat4f();
        m_rotationMatrix   = new SLMat4f();


        light            = new SLLight();
        phongActive      = false;
        xWireframeActive = false;
        m_camZ           = -5.5f;         // backwards movement of the camera
        m_cam            = new SLVec3f(0, 0, m_camZ);

        add_rotAxis       = new SLVec3f();
        tForce            = 0.1f;
        preCursorPosition = new SLVec3f();
        cursorPosition    = new SLVec3f();

        sw = new Stopwatch();
        sw.Start();
        second = new TimeSpan(0, 0, 1);
        fps    = 0;


        meshes = new List <Mesh>();
        Cube c1 = new Cube(1f);

        c1.modelMatrix.Translate(-.5f, 0, 0);
        c1.color = colorGreen;

        Sphere s1 = new Sphere(1.5f, 15, 15);

        s1.modelMatrix.Translate(.5f, 0, 0);
        s1.color = colorRed;

        meshes.Add(c1);
        meshes.Add(s1);

        // Without double buffering it would flicker
        this.DoubleBuffered = true;
        InitializeComponent();
    }
Exemple #17
0
 /// <summary>
 /// Returns a value indicating whether this instance is equal to
 /// the specified object.
 /// </summary>
 /// <param name="obj">An object to compare to this instance.</param>
 /// <returns>True if <paramref name="obj"/> is a <see cref="SLMat3f"/> and has the same values as this instance; otherwise, False.</returns>m[3].ToString("0.00") + " " + m[7].ToString("0.00") + " " + m[11].ToString("0.00") + " " + m[15].ToString("0.00"));
 public override bool Equals(object obj)
 {
     if (obj is SLMat4f)
     {
         SLMat4f m = (SLMat4f)obj;
         return((this.m[0] == m.m[0]) &&
                (this.m[1] == m.m[1]) &&
                (this.m[2] == m.m[2]) &&
                (this.m[3] == m.m[3]) &&
                (this.m[4] == m.m[4]) &&
                (this.m[5] == m.m[5]) &&
                (this.m[6] == m.m[6]) &&
                (this.m[7] == m.m[7]) &&
                (this.m[8] == m.m[8]) &&
                (this.m[9] == m.m[9]) &&
                (this.m[10] == m.m[10]) &&
                (this.m[11] == m.m[11]) &&
                (this.m[12] == m.m[12]) &&
                (this.m[13] == m.m[13]) &&
                (this.m[14] == m.m[14]) &&
                (this.m[15] == m.m[15]));
     }
     return(false);
 }
    /// <summary>
    /// OnRenderFrame is called on every frame for rendering
    /// </summary>
    /// <param name="e">event arguments</param>
    protected override void OnRenderFrame(FrameEventArgs e)
    {
        base.OnRenderFrame(e);

        // Clear the color & depth buffer
        gl.Clear(ClearBufferMask.ColorBufferBit |
                 ClearBufferMask.DepthBufferBit);

        // Start with identity every frame
        _viewMatrix.Identity();

        // View transform: move the coordinate system away from the camera
        _viewMatrix.Translate(0, 0, _camZ);

        // View transform: rotate the coordinate system increasingly
        _viewMatrix.Rotate(_rotX + _deltaX, 1, 0, 0);
        _viewMatrix.Rotate(_rotY + _deltaY, 0, 1, 0);

        // Transform light position & direction into view space
        SLVec3f lightPosVS = _viewMatrix * _lightPos;

        // The light dir is not a position. We only take the rotation of the mv matrix.
        SLMat3f viewRot    = _viewMatrix.Mat3();
        SLVec3f lightDirVS = viewRot * _lightDir;

        // Rotate the model so that we see it
        _modelMatrix.Identity();
        _modelMatrix.Rotate(90, -1, 0, 0);

        // Build the combined modelview-projection matrix
        SLMat4f mvp = new SLMat4f(_projectionMatrix);
        SLMat4f mv  = new SLMat4f(_viewMatrix);

        mv.Multiply(_modelMatrix);
        mvp.Multiply(mv);

        // Build normal matrix
        SLMat3f nm = mv.InverseTransposed();

        // Pass the matrix uniform variables
        unsafe
        { gl.UniformMatrix4(_mvMatrixLoc, 1, false, mv.m);
          gl.UniformMatrix3(_nMatrixLoc, 1, false, nm.m);
          gl.UniformMatrix4(_mvpMatrixLoc, 1, false, mvp.m);

          // Pass lighting uniforms variables
          gl.Uniform4(_globalAmbiLoc, 1, (float[])_globalAmbi);
          gl.Uniform3(_lightPosVSLoc, 1, (float[])lightPosVS);
          gl.Uniform3(_lightSpotDirVSLoc, 1, (float[])lightDirVS);
          gl.Uniform4(_lightAmbientLoc, 1, (float[])_lightAmbient);
          gl.Uniform4(_lightDiffuseLoc, 1, (float[])_lightDiffuse);
          gl.Uniform4(_lightSpecularLoc, 1, (float[])_lightSpecular);
          gl.Uniform4(_matAmbientLoc, 1, (float[])_matAmbient);
          gl.Uniform4(_matDiffuseLoc, 1, (float[])_matDiffuse);
          gl.Uniform4(_matSpecularLoc, 1, (float[])_matSpecular);
          gl.Uniform4(_matEmissiveLoc, 1, (float[])_matEmissive); }
        gl.Uniform1(_matShininessLoc, _matShininess);
        gl.Uniform1(_texture0Loc, 0);

        //////////////////////
        // Draw with 2 VBOs //
        //////////////////////

        // Enable all of the vertex attribute arrays
        gl.EnableVertexAttribArray(_pLoc);
        gl.EnableVertexAttribArray(_nLoc);
        gl.EnableVertexAttribArray(_tLoc);

        // Activate VBOs
        gl.BindBuffer(BufferTarget.ArrayBuffer, _vboV);
        gl.BindBuffer(BufferTarget.ElementArrayBuffer, _vboI);

        // Activate Texture
        gl.BindTexture(TextureTarget.Texture2D, _textureID);

        // For VBO only offset instead of data pointer
        int stride  = 32;
        int offsetN = 3 * sizeof(float);
        int offsetT = 6 * sizeof(float);

        gl.VertexAttribPointer(_pLoc, 3, VertexAttribPointerType.Float, false, stride, 0);
        gl.VertexAttribPointer(_nLoc, 3, VertexAttribPointerType.Float, false, stride, offsetN);
        gl.VertexAttribPointer(_tLoc, 2, VertexAttribPointerType.Float, false, stride, offsetT);

        /////////////////////////////////////////////////////////////////////////////
        // Draw cube model triangles by indexes
        gl.DrawElements(BeginMode.Triangles, _numI, DrawElementsType.UnsignedInt, 0);
        /////////////////////////////////////////////////////////////////////////////

        // Deactivate buffers
        gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

        // Disable the vertex arrays
        gl.DisableVertexAttribArray(_pLoc);
        gl.DisableVertexAttribArray(_nLoc);
        gl.DisableVertexAttribArray(_tLoc);

        // Fast copy the back buffer to the front buffer. This is OS dependent.
        SwapBuffers();

        // Check for errors
        glUtils.GetGLError("OnRenderFrame", true);
    }
Exemple #19
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 public SLMat4f(SLMat4f A)
 {
     Set(A);
 }
Exemple #20
0
    /// <summary>
    /// Returns the inverse of the matrix
    /// </summary>
    /// <returns></returns>
    public SLMat4f Inverse()
    {
        SLMat4f I = new SLMat4f();

        // Code from Mesa-2.2\src\glu\project.c
        float det, d12, d13, d23, d24, d34, d41;

        // Inverse = adjoint / det. (See linear algebra texts.)
        // pre-compute 2x2 dets for last two rows when computing
        // cof_actors of first two rows.
        d12 = (m[2] * m[7] - m[3] * m[6]);
        d13 = (m[2] * m[11] - m[3] * m[10]);
        d23 = (m[6] * m[11] - m[7] * m[10]);
        d24 = (m[6] * m[15] - m[7] * m[14]);
        d34 = (m[10] * m[15] - m[11] * m[14]);
        d41 = (m[14] * m[3] - m[15] * m[2]);

        I.m[0] = (m[5] * d34 - m[9] * d24 + m[13] * d23);
        I.m[1] = -(m[1] * d34 + m[9] * d41 + m[13] * d13);
        I.m[2] = (m[1] * d24 + m[5] * d41 + m[13] * d12);
        I.m[3] = -(m[1] * d23 - m[5] * d13 + m[9] * d12);

        // Compute determinant as early as possible using these cof_actors.
        det = m[0] * I.m[0] + m[4] * I.m[1] + m[8] * I.m[2] + m[12] * I.m[3];

        // Run singularity test.
        if (Math.Abs(det) <= 0.00005)
        {
            throw new DivideByZeroException("Matrix is singular. Inversion impossible.");
        }
        else
        {
            float invDet = 1 / det;
            // Compute rest of inverse.
            I.m[0] *= invDet;
            I.m[1] *= invDet;
            I.m[2] *= invDet;
            I.m[3] *= invDet;

            I.m[4] = -(m[4] * d34 - m[8] * d24 + m[12] * d23) * invDet;
            I.m[5] = (m[0] * d34 + m[8] * d41 + m[12] * d13) * invDet;
            I.m[6] = -(m[0] * d24 + m[4] * d41 + m[12] * d12) * invDet;
            I.m[7] = (m[0] * d23 - m[4] * d13 + m[8] * d12) * invDet;

            // Pre-compute 2x2 dets for first two rows when computing
            // cofactors of last two rows.
            d12 = m[0] * m[5] - m[1] * m[4];
            d13 = m[0] * m[9] - m[1] * m[8];
            d23 = m[4] * m[9] - m[5] * m[8];
            d24 = m[4] * m[13] - m[5] * m[12];
            d34 = m[8] * m[13] - m[9] * m[12];
            d41 = m[12] * m[1] - m[13] * m[0];

            I.m[8]  = (m[7] * d34 - m[11] * d24 + m[15] * d23) * invDet;
            I.m[9]  = -(m[3] * d34 + m[11] * d41 + m[15] * d13) * invDet;
            I.m[10] = (m[3] * d24 + m[7] * d41 + m[15] * d12) * invDet;
            I.m[11] = -(m[3] * d23 - m[7] * d13 + m[11] * d12) * invDet;
            I.m[12] = -(m[6] * d34 - m[10] * d24 + m[14] * d23) * invDet;
            I.m[13] = (m[2] * d34 + m[10] * d41 + m[14] * d13) * invDet;
            I.m[14] = -(m[2] * d24 + m[6] * d41 + m[14] * d12) * invDet;
            I.m[15] = (m[2] * d23 - m[6] * d13 + m[10] * d12) * invDet;
        }
        return(I);
    }
    /// <summary>
    /// OnLoad is called once at the beginning for OpenGL inits.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        BuildSquare();

        _viewMatrix       = new SLMat4f();
        _modelMatrix      = new SLMat4f();
        _projectionMatrix = new SLMat4f();

        // Set light parameters
        _globalAmbi    = new SLVec4f(0.0f, 0.0f, 0.0f);
        _lightPos      = new SLVec3f(0.0f, 0.0f, 100.0f);
        _lightDir      = new SLVec3f(0.0f, 0.0f, -1.0f);
        _lightAmbient  = new SLVec4f(0.1f, 0.1f, 0.1f);
        _lightDiffuse  = new SLVec4f(1.0f, 1.0f, 1.0f);
        _lightSpecular = new SLVec4f(1.0f, 1.0f, 1.0f);
        _matAmbient    = new SLVec4f(1.0f, 1.0f, 1.0f);
        _matDiffuse    = new SLVec4f(1.0f, 1.0f, 1.0f);
        _matSpecular   = new SLVec4f(1.0f, 1.0f, 1.0f);
        _matEmissive   = new SLVec4f(0.0f, 0.0f, 0.0f);
        _matShininess  = 100.0f;

        // backwards movement of the camera
        _camZ = -3.0f;

        // Mouse rotation parameters
        _rotX          = 0;
        _rotY          = 0;
        _deltaX        = 0;
        _deltaY        = 0;
        _mouseLeftDown = false;

        // Load textures
        _textureID = glUtils.BuildTexture("../_data/images/textures/earth2048_C.jpg",
                                          TextureMinFilter.LinearMipmapLinear,
                                          TextureMagFilter.Linear,
                                          TextureWrapMode.Repeat,
                                          TextureWrapMode.Repeat);

        // Load, compile & link shaders
        _shaderVertID = glUtils.BuildShader("../_data/shaders/ADSTex.vert", ShaderType.VertexShader);
        _shaderFragID = glUtils.BuildShader("../_data/shaders/ADSTex.frag", ShaderType.FragmentShader);
        _shaderProgID = glUtils.BuildProgram(_shaderVertID, _shaderFragID);

        // Activate the shader program
        gl.UseProgram(_shaderProgID);

        // Get the variable locations (identifiers) within the program
        _pLoc              = gl.GetAttribLocation(_shaderProgID, "a_position");
        _nLoc              = gl.GetAttribLocation(_shaderProgID, "a_normal");
        _tLoc              = gl.GetAttribLocation(_shaderProgID, "a_texCoord");
        _mvMatrixLoc       = gl.GetUniformLocation(_shaderProgID, "u_mvMatrix");
        _mvpMatrixLoc      = gl.GetUniformLocation(_shaderProgID, "u_mvpMatrix");
        _nMatrixLoc        = gl.GetUniformLocation(_shaderProgID, "u_nMatrix");
        _globalAmbiLoc     = gl.GetUniformLocation(_shaderProgID, "u_globalAmbi");
        _lightPosVSLoc     = gl.GetUniformLocation(_shaderProgID, "u_lightPosVS");
        _lightSpotDirVSLoc = gl.GetUniformLocation(_shaderProgID, "u_lightSpotDirVS");
        _lightAmbientLoc   = gl.GetUniformLocation(_shaderProgID, "u_lightAmbient");
        _lightDiffuseLoc   = gl.GetUniformLocation(_shaderProgID, "u_lightDiffuse");
        _lightSpecularLoc  = gl.GetUniformLocation(_shaderProgID, "u_lightSpecular");
        _matAmbientLoc     = gl.GetUniformLocation(_shaderProgID, "u_matAmbient");
        _matDiffuseLoc     = gl.GetUniformLocation(_shaderProgID, "u_matDiffuse");
        _matSpecularLoc    = gl.GetUniformLocation(_shaderProgID, "u_matSpecular");
        _matEmissiveLoc    = gl.GetUniformLocation(_shaderProgID, "u_matEmissive");
        _matShininessLoc   = gl.GetUniformLocation(_shaderProgID, "u_matShininess");
        _texture0Loc       = gl.GetUniformLocation(_shaderProgID, "u_texture0");

        // Set some OpenGL states
        gl.ClearColor(0.0f, 0.0f, 0.0f, 1);  // Set the background color
        gl.Enable(EnableCap.DepthTest);      // Enables depth test
        gl.Enable(EnableCap.CullFace);       // Enables the culling of back faces

        // Attach mouse wheel handler
        Mouse.WheelChanged += new EventHandler <OpenTK.Input.MouseWheelEventArgs>(Mouse_WheelChanged);

        glUtils.GetGLError("OnLoad", true);
    }
    /// <summary>
    /// The forms paint routine where all drawing happens.
    /// </summary>
    private void frmHelloCube_Paint(object sender, PaintEventArgs e)
    {
        // start with identity every frame
        m_modelViewMatrix.Identity();

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

        // model transform: rotate the coordinate system increasingly
        m_modelViewMatrix.Rotate(m_rotx + m_dx, 1, 0, 0);
        m_modelViewMatrix.Rotate(m_roty + m_dy, 0, 1, 0);

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

        m.Multiply(m_viewportMatrix);
        m.Multiply(m_projectionMatrix);
        m.Multiply(m_modelViewMatrix);

        // 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.Count(); ++i)
        {
            v2[i] = m.Multiply(m_v[i]);
        }

        SLVec3f nZ = new SLVec3f(0, 0, 1);

        // Calculate the cubes plane normals in screen space
        // Be aware that y is inverse due to MS top-left zero coord.
        SLVec3f nN       = (v2[1] - v2[0]).cross(v2[0] - v2[3]);
        SLVec3f nF       = (v2[5] - v2[4]).cross(v2[7] - v2[4]);
        SLVec3f nL       = (v2[4] - v2[0]).cross(v2[3] - v2[0]);
        SLVec3f nR       = (v2[5] - v2[1]).cross(v2[1] - v2[2]);
        SLVec3f nT       = (v2[7] - v2[3]).cross(v2[2] - v2[3]);
        SLVec3f nB       = (v2[4] - v2[0]).cross(v2[0] - v2[1]);
        bool    visibleN = nN.dot(nZ) >= 0; //near
        bool    visibleF = nF.dot(nZ) >= 0; //far
        bool    visibleL = nL.dot(nZ) >= 0; //left
        bool    visibleR = nR.dot(nZ) >= 0; //right
        bool    visibleT = nT.dot(nZ) >= 0; //top
        bool    visibleB = nB.dot(nZ) >= 0; //bottom

        Graphics g = e.Graphics;

        g.SmoothingMode = SmoothingMode.AntiAlias;

        // draw front square
        if (visibleN)
        {
            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
        if (visibleF)
        {
            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 left square
        if (visibleL)
        {
            g.DrawLine(Pens.Blue, v2[0].x, v2[0].y, v2[4].x, v2[4].y);
            g.DrawLine(Pens.Blue, v2[3].x, v2[3].y, v2[7].x, v2[7].y);
            g.DrawLine(Pens.Red, v2[3].x, v2[3].y, v2[0].x, v2[0].y);
            g.DrawLine(Pens.Green, v2[7].x, v2[7].y, v2[4].x, v2[4].y);
        }
        // draw right square
        if (visibleR)
        {
            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.Red, v2[1].x, v2[1].y, v2[2].x, v2[2].y);
            g.DrawLine(Pens.Green, v2[6].x, v2[6].y, v2[5].x, v2[5].y);
        }
        // draw top square
        if (visibleT)
        {
            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);
            g.DrawLine(Pens.Red, v2[3].x, v2[3].y, v2[2].x, v2[2].y);
            g.DrawLine(Pens.Green, v2[6].x, v2[6].y, v2[7].x, v2[7].y);
        }
        // draw bottom square
        if (visibleB)
        {
            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.Red, v2[0].x, v2[0].y, v2[1].x, v2[1].y);
            g.DrawLine(Pens.Green, v2[4].x, v2[4].y, v2[5].x, v2[5].y);
        }

        // Tell the system that the window should be repaint again
        this.Invalidate();
    }
Exemple #23
0
    /// <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();
    }