Esempio n. 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);
        }
Esempio n. 2
0
        public async void Build(WebGLContext gl)
        {
            await InitProgramAsync(gl, VertexProgramString, FragmentProgramString);

            ViewProjectionLocation = await gl.GetUniformLocationAsync(ShaderProgram, "ViewProjection");

            AnchorLocation = await gl.GetUniformLocationAsync(ShaderProgram, "in_anchor");

            ColorLocation = await gl.GetUniformLocationAsync(ShaderProgram, "in_color");

            FilpLocation = await gl.GetUniformLocationAsync(ShaderProgram, "in_flip");

            ModelLocation = await gl.GetUniformLocationAsync(ShaderProgram, "in_model");

            PositionAttributeLocaltion = (uint)await gl.GetAttribLocationAsync(ShaderProgram, "in_pos");

            TexturePositionAttributeLocaltion = (uint)await gl.GetAttribLocationAsync(ShaderProgram, "in_texPos");
        }
        public static async Task <ShaderProgram> InitShaderProgram(WebGLContext gl, string vsSource, string fsSource, List <string> attributesNames = null, List <string> uniformsNames = null)
        {
            var vertexShader = await LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource);

            var fragmentShader = await LoadShaderAsync(gl, ShaderType.FRAGMENT_SHADER, fsSource);

            var program = await gl.CreateProgramAsync();

            await gl.AttachShaderAsync(program, vertexShader);

            await gl.AttachShaderAsync(program, fragmentShader);

            await gl.LinkProgramAsync(program);

            await gl.DeleteShaderAsync(vertexShader);

            await gl.DeleteShaderAsync(fragmentShader);

            if (!await gl.GetProgramParameterAsync <bool>(program, ProgramParameter.LINK_STATUS))
            {
                string info = await gl.GetProgramInfoLogAsync(program);

                throw new Exception("An error occured while linking the program: " + info);
            }

            attributesNames ??= new List <string>();
            var attributesDict = new Dictionary <string, int>();

            foreach (var attribute in attributesNames)
            {
                attributesDict.Add(attribute, await gl.GetAttribLocationAsync(program, attribute));
            }

            uniformsNames ??= new List <string>();
            var uniformsDict = new Dictionary <string, WebGLUniformLocation>();

            foreach (var uniform in uniformsNames)
            {
                uniformsDict.Add(uniform, await gl.GetUniformLocationAsync(program, uniform));
            }

            return(new ShaderProgram(program, gl, attributesDict, uniformsDict));
        }