Esempio n. 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);
        }
Esempio n. 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);
        }
        public WebGL15ContextImpl()
            : base(100, 100)
        {
            gl = null;
#endif
            if (gl == null)
                throw new Exception("UnsupportedOperationException: WebGL N/A");
            InitShaders(); CheckError("initShader");
            _elementBuffer = gl.CreateBuffer(); CheckError("createBuffer f. elements");
            for (int index = 0; index < _buffers.Length; index++)
            {
                WebGLBufferData b = new WebGLBufferData();
                b.ToBind = null;
                b.Buffer = gl.CreateBuffer(); CheckError("createBuffer" + index);
                b.Stride = 0;
                b.Size = 0;
                b.Type = 0;
                b.ByteSize = 0;
                b.Normalized = false;
                _buffers[index] = b;
            }
        }
        public WebGL15ContextImpl()
            : base(100, 100)
        {
            gl = null;
#endif
            if (gl == null)
            {
                throw new Exception("UnsupportedOperationException: WebGL N/A");
            }
            InitShaders(); CheckError("initShader");
            _elementBuffer = gl.CreateBuffer(); CheckError("createBuffer f. elements");
            for (int index = 0; index < _buffers.Length; index++)
            {
                WebGLBufferData b = new WebGLBufferData();
                b.ToBind        = null;
                b.Buffer        = gl.CreateBuffer(); CheckError("createBuffer" + index);
                b.Stride        = 0;
                b.Size          = 0;
                b.Type          = 0;
                b.ByteSize      = 0;
                b.Normalized    = false;
                _buffers[index] = b;
            }
        }
        private void VertexAttribStaticDrawPointer(uint indx, int size, uint type, bool normalized, int stride, long offset, Stream pointer, int staticDrawIndex)
        {
            WebGLBuffer b = _staticDrawBuffers[staticDrawIndex];

            if (b == null)
            {
                _staticDrawBuffers[staticDrawIndex] = b = gl.CreateBuffer();
                gl.BindBuffer(GLES20.ARRAY_BUFFER, b);
                gl.BufferData(GLES20.ARRAY_BUFFER, GetTypedArray(pointer, type), GLES20.STATIC_DRAW); CheckError("bufferData");
                //Log("static buffer created; id: " + staticDrawIndex + " remaining: " + pointer.Remaining());
            }
            gl.BindBuffer(GLES20.ARRAY_BUFFER, b);
            gl.VertexAttribPointer(indx, size, type, normalized, stride, offset); CheckError("vertexAttribPointer");
            _buffers[indx].ToBind = null;
        }
Esempio n. 6
0
        public spriteBatcher(WebGLRenderingContext webgl, shaderParser shaderparser)
        {
            this.webgl        = webgl;
            this.shaderparser = shaderparser;
            this.vbo          = webgl.CreateBuffer();
            var asp = (this.webgl.DrawingBufferWidth / this.webgl.DrawingBufferHeight);

            //this.matrix=
            float[] array =
            {
                1.0f / asp, 0, 0, 0,//去掉asp的影响
                0,          1, 0, 0,
                0,          0, 1, 0,
                0,          0, 0, 1
            };//ness
            this.matrix = new Float32Array(array);

            this.recorder = new stateRecorder(webgl);//ness
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }