示例#1
0
        public async Task Initialize()
        {
            _context = await _canvasReference.CreateWebGLAsync(new WebGLContextAttributes
            {
                PowerPreference = WebGLContextAttributes.POWER_PREFERENCE_HIGH_PERFORMANCE
            });

            vertexBuffer = await _context.CreateBufferAsync();

            await _context.BindBufferAsync(BufferType.ARRAY_BUFFER, vertexBuffer);

            var program = await this.InitProgramAsync(this._context, VS_SOURCE, FS_SOURCE);

            var positionLocation = await _context.GetAttribLocationAsync(program, "aPos");

            var texcoordLocation = await _context.GetAttribLocationAsync(program, "aTex");

            await _context.VertexAttribPointerAsync((uint)positionLocation, 3, DataType.FLOAT, false, 6 *sizeof(float), 0);

            await _context.VertexAttribPointerAsync((uint)texcoordLocation, 2, DataType.FLOAT, false, 6 *sizeof(float), 3 *sizeof(float));

            await _context.EnableVertexAttribArrayAsync((uint)positionLocation);

            await _context.EnableVertexAttribArrayAsync((uint)texcoordLocation);

            await _context.UseProgramAsync(program);

            var texture = await _context.CreateTextureAsync();

            await _context.BindTextureAsync(TextureType.TEXTURE_2D, texture);
        }
        public static async Task <Texture> BuildAsync(WebGLContext gl, int[] image, int width = 100, int height = 100)
        {
            var texture = await gl.CreateTextureAsync();

            await gl.BindTextureAsync(TextureType.TEXTURE_2D, texture);

            //TODO: Create a new version without width&height
            await gl.TexImage2DAsync(Texture2DType.TEXTURE_2D, 0, PixelFormat.RGBA, width, height, 0, PixelFormat.RGBA, PixelType.UNSIGNED_BYTE, image);

            // WebGL1 has different requirements for power of 2 images vs non power of 2 images
            //if ((width & (width - 1)) == 0 && (height & (height - 1)) == 0)
            //{
            //    await gl.GenerateMipmapAsync(TextureType.TEXTURE_2D);
            //}
            //else
            //{
            //await gl.TexParameterAsync(TextureType.TEXTURE_2D, TextureParameter.TEXTURE_MAG_FILTER, (int)TextureParameterValue.LINEAR);
            await gl.TexParameterAsync(TextureType.TEXTURE_2D, TextureParameter.TEXTURE_MIN_FILTER, (int)TextureParameterValue.LINEAR);

            await gl.TexParameterAsync(TextureType.TEXTURE_2D, TextureParameter.TEXTURE_WRAP_S, (int)TextureParameterValue.CLAMP_TO_EDGE);

            await gl.TexParameterAsync(TextureType.TEXTURE_2D, TextureParameter.TEXTURE_WRAP_T, (int)TextureParameterValue.CLAMP_TO_EDGE);

            return(new Texture(gl, texture, width, height));
        }