Esempio n. 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();
    }
Esempio n. 2
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_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();
    }
Esempio n. 3
0
    /// <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);
    }
Esempio n. 4
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();
    }