Exemple #1
0
        public void Run()
        {
            var vertices = new float[]
            {
                -1, -1, -1,
                1, -1, -1,
                1, 1, -1,

                -1, 1, -1,
                -1, -1, 1,
                1, -1, 1,

                1, 1, 1,
                -1, 1, 1,
                -1, -1, -1,

                -1, 1, -1,
                -1, 1, 1,
                -1, -1, 1,

                1, -1, -1,
                1, 1, -1,
                1, 1, 1,

                1, -1, 1,
                -1, -1, -1,
                -1, -1, 1,

                1, -1, 1,
                1, -1, -1,
                -1, 1, -1,

                -1, 1, 1,
                1, 1, 1,
                1, 1, -1
            };

            vertexBuffer = gl.CreateArrayBuffer(vertices);

            indices = new ushort[]
            {
                0, 1, 2,
                0, 2, 3,

                4, 5, 6,
                4, 6, 7,

                8, 9, 10,
                8, 10, 11,

                12, 13, 14,
                12, 14, 15,

                16, 17, 18,
                16, 18, 19,

                20, 21, 22,
                20, 22, 23
            };
            indexBuffer = gl.CreateElementArrayBuffer(indices);

            var colors = new float[]
            {
                1, 0, 0,
                1, 0, 0,
                1, 0, 0,
                1, 0, 0,

                0, 1, 0,
                0, 1, 0,
                0, 1, 0,
                0, 1, 0,

                0, 0, 1,
                0, 0, 1,
                0, 0, 1,
                0, 0, 1,

                1, 1, 0,
                1, 1, 0,
                1, 1, 0,
                1, 1, 0,

                0, 1, 1,
                0, 1, 1,
                0, 1, 1,
                0, 1, 1,

                1, 1, 1,
                1, 1, 1,
                1, 1, 1,
                1, 1, 1
            };

            colorBuffer = gl.CreateArrayBuffer(colors);

            var shaderProgram = InitShaders();

            pMatrixUniform = gl.GetUniformLocation(shaderProgram, "pMatrix");
            vMatrixUniform = gl.GetUniformLocation(shaderProgram, "vMatrix");
            wMatrixUniform = gl.GetUniformLocation(shaderProgram, "wMatrix");

            gl.BindBuffer(WebGLRenderingContextBase.ARRAY_BUFFER, vertexBuffer);
            var positionAttribute = (uint)gl.GetAttribLocation(shaderProgram, "position");

            gl.VertexAttribPointer(positionAttribute, 3, WebGLRenderingContextBase.FLOAT, false, 0, 0);
            gl.EnableVertexAttribArray(positionAttribute);

            gl.BindBuffer(WebGLRenderingContextBase.ARRAY_BUFFER, colorBuffer);
            var colorAttribute = (uint)gl.GetAttribLocation(shaderProgram, "color");

            gl.VertexAttribPointer(colorAttribute, 3, WebGLRenderingContextBase.FLOAT, false, 0, 0);
            gl.EnableVertexAttribArray(colorAttribute);

            gl.BindBuffer(WebGLRenderingContextBase.ELEMENT_ARRAY_BUFFER, indexBuffer);
            worldMatrix = Matrix.Identity;
        }