예제 #1
0
        public void BufferSubDataRegressionTest()
        {
            var buffer = gl.CreateBuffer();

            gl.BindBuffer(WebGLRenderingContextBase.ARRAY_BUFFER, buffer);
            gl.BufferData(WebGLRenderingContextBase.ARRAY_BUFFER, 1024, WebGLRenderingContextBase.STATIC_DRAW);
            var data = new float[] { 0 };

            gl.BufferSubData(WebGLRenderingContextBase.ARRAY_BUFFER, 512, data);
        }
예제 #2
0
        public static void DrawLine(WebGLRenderingContext gl, WebGLProgram program, RGBAColour colour,
                                    int x0, int y0, int x1, int y1)
        {
            gl.UseProgram(program);

            var positionCoords = new float[]
            {
                x0, y0, x1, y1
            };

            /* Add the components of the colour twice (one for each vertex). */

            var colours = new float[]
            {
                colour.R, colour.G, colour.B, colour.A,
                colour.R, colour.G, colour.B, colour.A
            };

            var matrixLocation = gl.GetUniformLocation(program, "u_matrix");

            /* Load the colour into a buffer and bind to shader attribute. */

            var colourBuffer   = gl.CreateBuffer();
            var colourLocation = gl.GetAttribLocation(program, "a_color");

            gl.BindBuffer(gl.ARRAY_BUFFER, colourBuffer);
            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(colours), gl.STATIC_DRAW);
            gl.VertexAttribPointer(colourLocation, 4, gl.FLOAT, false, 0, 0);
            gl.EnableVertexAttribArray(colourLocation);

            /* Load the line endpoints into a buffer and bind to shader attribute. */

            var vertexBuffer     = gl.CreateBuffer();
            var positionLocation = gl.GetAttribLocation(program, "a_position");

            gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(positionCoords), gl.STATIC_DRAW);
            gl.VertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
            gl.EnableVertexAttribArray(positionLocation);

            /* Set up projection matrix and translate to screen origin. */

            var matrix = CameraHelpers.Orthographic(0, gl.Canvas.Width, gl.Canvas.Height, 0, -1, 1);

            gl.UniformMatrix4fv(matrixLocation, false, matrix.ToArray());

            matrix = matrix.Translate(x0 / (float)gl.Canvas.Width * 2, -y0 / (float)gl.Canvas.Height * 2, 0);

            gl.DrawArrays(gl.LINES, 0, 2);
            gl.BindBuffer(gl.ARRAY_BUFFER, null);
        }
        private void UpdatTCBuffer(Stream s, int offset, int count)
        {
            WebGLBufferData b = _buffers[ARRAY_TEXCOORD_0];

            gl.BindBuffer(GLES20.ARRAY_BUFFER, b.Buffer);
            long p = s.Position;
            long l = s.Length;

            s.Position = p + offset;
            s.SetLength(p + offset + count);
            gl.BufferSubData(GLES20.ARRAY_BUFFER, offset * 4, GetTypedArray(s, GLES20.FLOAT));
            s.Position = p;
            s.SetLength(l);
        }
예제 #4
0
        public void InitBuffers()
        {
            this.cubeVertexPositionBuffer = gl.CreateBuffer();
            gl.BindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);

            var vertices = new double[] {
                // Front face
                -1.0, -1.0, 1.0,
                1.0, -1.0, 1.0,
                1.0, 1.0, 1.0,
                -1.0, 1.0, 1.0,

                // Back face
                -1.0, -1.0, -1.0,
                -1.0, 1.0, -1.0,
                1.0, 1.0, -1.0,
                1.0, -1.0, -1.0,

                // Top face
                -1.0, 1.0, -1.0,
                -1.0, 1.0, 1.0,
                1.0, 1.0, 1.0,
                1.0, 1.0, -1.0,

                // Bottom face
                -1.0, -1.0, -1.0,
                1.0, -1.0, -1.0,
                1.0, -1.0, 1.0,
                -1.0, -1.0, 1.0,

                // Right face
                1.0, -1.0, -1.0,
                1.0, 1.0, -1.0,
                1.0, 1.0, 1.0,
                1.0, -1.0, 1.0,

                // Left face
                -1.0, -1.0, -1.0,
                -1.0, -1.0, 1.0,
                -1.0, 1.0, 1.0,
                -1.0, 1.0, -1.0,
            };

            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

            this.cubeVertexNormalBuffer = gl.CreateBuffer();
            gl.BindBuffer(gl.ARRAY_BUFFER, cubeVertexNormalBuffer);

            var vertexNormals = new double[] {
                // Front face
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,

                // Back face
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,

                // Top face
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,

                // Bottom face
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,

                // Right face
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,

                // Left face
                -1.0, 0.0, 0.0,
                -1.0, 0.0, 0.0,
                -1.0, 0.0, 0.0,
                -1.0, 0.0, 0.0
            };

            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(vertexNormals), gl.STATIC_DRAW);

            this.cubeVertexTextureCoordBuffer = gl.CreateBuffer();
            gl.BindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);

            var textureCoords = new double[] {
                // Front face
                0.0, 0.0,
                1.0, 0.0,
                1.0, 1.0,
                0.0, 1.0,

                // Back face
                1.0, 0.0,
                1.0, 1.0,
                0.0, 1.0,
                0.0, 0.0,

                // Top face
                0.0, 1.0,
                0.0, 0.0,
                1.0, 0.0,
                1.0, 1.0,

                // Bottom face
                1.0, 1.0,
                0.0, 1.0,
                0.0, 0.0,
                1.0, 0.0,

                // Right face
                1.0, 0.0,
                1.0, 1.0,
                0.0, 1.0,
                0.0, 0.0,

                // Left face
                0.0, 0.0,
                1.0, 0.0,
                1.0, 1.0,
                0.0, 1.0
            };

            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);

            this.cubeVertexIndexBuffer = gl.CreateBuffer();
            gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);

            var cubeVertexIndices = new int[] {
                0, 1, 2, 0, 2, 3,          // Front face
                4, 5, 6, 4, 6, 7,          // Back face
                8, 9, 10, 8, 10, 11,       // Top face
                12, 13, 14, 12, 14, 15,    // Bottom face
                16, 17, 18, 16, 18, 19,    // Right face
                20, 21, 22, 20, 22, 23     // Left face
            };

            gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
        }
예제 #5
0
        public static void DrawImage(WebGLRenderingContext gl, TextureInfo textureInfo, WebGLProgram program,
                                     int xSrc, int ySrc, int srcWidth, int srcHeight,
                                     int xDest, int yDest, int destWidth, int destHeight)
        {
            gl.UseProgram(program);

            var positionLocation = gl.GetAttribLocation(program, "a_position");
            var texCoordLocation = gl.GetAttribLocation(program, "a_texcoord");

            var matrixLocation  = gl.GetUniformLocation(program, "u_matrix");
            var textureLocation = gl.GetUniformLocation(program, "u_texture");

            /* Create a buffer to hold the position coordinates. */

            var positionBuffer = gl.CreateBuffer();
            var positionCoords = new float[]
            {
                0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1
            };

            gl.BindBuffer(gl.ARRAY_BUFFER, positionBuffer);
            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(positionCoords), gl.STATIC_DRAW);

            /* Create a buffer to hold the texture coordinates. */

            var sx = xSrc / (float)textureInfo.Width.Value;
            var sy = ySrc / (float)textureInfo.Height.Value;

            var sw = srcWidth / (float)textureInfo.Width.Value;
            var sh = srcHeight / (float)textureInfo.Height.Value;

            var texBuffer = gl.CreateBuffer();
            var texCoords = new float[]
            {
                0 + sx, 0 + sy,
                0 + sx, sh + sy,
                sw + sx, 0 + sy,
                sw + sx, 0 + sy,
                0 + sx, sh + sy,
                sw + sx, sh + sy
            };

            gl.BindBuffer(gl.ARRAY_BUFFER, texBuffer);
            gl.BufferData(gl.ARRAY_BUFFER, new Float32Array(texCoords), gl.STATIC_DRAW);
            gl.BindTexture(gl.TEXTURE_2D, textureInfo.Texture);

            gl.BindBuffer(gl.ARRAY_BUFFER, positionBuffer);
            gl.EnableVertexAttribArray(positionLocation);
            gl.VertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);

            gl.BindBuffer(gl.ARRAY_BUFFER, texBuffer);
            gl.EnableVertexAttribArray(texCoordLocation);
            gl.VertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);

            /* Create an standard orthographic projection to transform our coordinates by and bind to our shader uniform. */

            var matrix = CameraHelpers.Orthographic(0, gl.Canvas.Width, gl.Canvas.Height, 0, -1, 1);

            matrix = matrix.Translate(xDest / (float)gl.Canvas.Width * 2, -yDest / (float)gl.Canvas.Height * 2, 0);
            matrix = matrix.Scale(destWidth, destHeight, 1);

            gl.UniformMatrix4fv(matrixLocation, false, matrix.ToArray());
            gl.Uniform1i(textureLocation, 0);

            /* 6 points per image rendered as two triangles. */

            gl.DrawArrays(gl.TRIANGLES, 0, 6);
            gl.BindBuffer(gl.ARRAY_BUFFER, null);
        }