private async Task <WebGLProgram> InitProgramAsync(WebGLContext gl, string vsSource, string fsSource) { var vertexShader = await this.LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource); var fragmentShader = await this.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); } u_matrix_location = await _context.GetUniformLocationAsync(program, "u_matrix"); return(program); }
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)); }
internal Texture(WebGLContext gl, WebGLTexture texture, int width, int height) { _gl = gl; _texture = texture; _width = width; _height = height; }
protected override void OnAfterRender() { WebGLContext context = this.canvasReference.CreateWebGL(new WebGLContextAttributes { PowerPreference = WebGLContextAttributes.POWER_PREFERENCE_HIGH_PERFORMANCE }); context.ClearColor(0, 0, 0, 1); context.Clear(BufferBits.COLOR_BUFFER_BIT); var program = this.InitProgram(context, VS_SOURCE, FS_SOURCE); var vertexBuffer = context.CreateBuffer(); context.BindBuffer(BufferType.ARRAY_BUFFER, vertexBuffer); var vertices = new[] { -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }; context.BufferData(BufferType.ARRAY_BUFFER, vertices, BufferUsageHint.STATIC_DRAW); context.VertexAttribPointer(0, 3, DataType.FLOAT, false, 6 * sizeof(float), 0); context.VertexAttribPointer(1, 3, DataType.FLOAT, false, 6 * sizeof(float), 3 * sizeof(float)); context.EnableVertexAttribArray(0); context.EnableVertexAttribArray(1); context.UseProgram(program); context.DrawArrays(Primitive.TRIANGLES, 0, 3); }
public WebGLBuffer(WebGLContext context, int id, BufferType type, BufferUsage usage) { this.context = context; this.Id = id; this.Type = type; this.Usage = usage; }
private ShaderProgram(WebGLProgram program, WebGLContext gl, Dictionary <string, int> attributes, Dictionary <string, WebGLUniformLocation> uniforms) { Program = program; GlContext = gl; Attributes = attributes; Uniforms = uniforms; }
public static async Task AfterDraw(WebGLContext gl) { var error = await gl.GetErrorAsync(); Console.WriteLine($"gl.GetErrorAsync : {error}"); //Debug.Assert(error == Error.NO_ERROR,"DEBUG : OpenGL got error after rendering. ERROR = " + error); }
private async Task InitProgramAsync(WebGLContext gl, string vsSource, string fsSource) { 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); } Console.WriteLine($"shader program : id = {program.Id} , type = {program.WebGLType}"); ShaderProgram = program; }
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); }
private VBO(WebGLContext gl, int count, float[] data, WebGLBuffer dataBuffer) { _gl = gl; _count = count; _size = data.Length / count; _dataBuffer = dataBuffer; }
protected GLBuffer(WebGLContext context, WebGLProgram program, BufferType bufferType, BufferUsageHint bufferUsageHint = BufferUsageHint.STATIC_DRAW) { _context = context; _bufferUsageHint = bufferUsageHint; _program = program; _bufferType = bufferType; }
private static async void ChangeAdditiveStatus(WebGLContext gl, bool isAdditiveBlend) { if (additiveTrigger == isAdditiveBlend) { return; } additiveTrigger = isAdditiveBlend; await gl.BlendFuncAsync(BlendingMode.SRC_ALPHA, additiveTrigger?BlendingMode.ONE : BlendingMode.ONE_MINUS_SRC_ALPHA); }
protected override async Task OnAfterRenderAsync(bool firstRender) { glContext = await RefCanvas.CreateWebGLAsync(new WebGLContextAttributes { PowerPreference = WebGLContextAttributes.POWER_PREFERENCE_HIGH_PERFORMANCE }); RenderKernel.Init(glContext, 800, 600); }
public async static Task <VBO> BuildAsync(WebGLContext gl, int count, float[] data) { var dataBuffer = await gl.CreateBufferAsync(); await gl.BindBufferAsync(BufferType.ARRAY_BUFFER, dataBuffer); await gl.BufferDataAsync(BufferType.ARRAY_BUFFER, data, BufferUsageHint.STATIC_DRAW); return(new VBO(gl, count, data, dataBuffer)); }
Mesh(VBO positions, VBO normals, VBO uvs, Texture texture, int vertexCount, float[] position, WebGLContext gl) { _positions = positions; _normals = normals; _uvs = uvs; _texture = texture; _vertexCount = vertexCount; _position = position; _gl = gl; }
public static void Init(WebGLContext gl, int viewWidth, int viewHeight) { ProjectionMatrix = Matrix4.Identity * Matrix4.CreateOrthographic(viewWidth, viewHeight, -1, 1); //todo 对渲染进行初始化,比如说初始化着色器,顶点等。 shader.Build(gl); InitStaticBuffer(gl); init = true; }
public static async void ApplyRenderResource(WebGLContext gl, Dictionary <string, TextureResource> textureResourceMap) { if (RenderKernel.textureResourceMap != null) { foreach (var resource in RenderKernel.textureResourceMap.Values.Where(x => x.IsValid)) { await gl.DeleteTextureAsync(resource.Texture); } } RenderKernel.textureResourceMap = textureResourceMap; }
public static async Task DrawObject(WebGLContext gl, StoryboardObject obj) { if (!textureResourceMap.TryGetValue(obj.ImageFilePath, out var textureResource)) { return; } ChangeAdditiveStatus(gl, obj.IsAdditive); var is_xflip = Math.Sign(obj.Scale.X); var is_yflip = Math.Sign(obj.Scale.Y); //adjust scale transform which value is negative var horizon_flip = obj.IsHorizonFlip | (is_xflip < 0); var vertical_flip = obj.IsHorizonFlip | (is_yflip < 0); float scalex = is_xflip * obj.Scale.X * textureResource.Size.Width; float scaley = is_yflip * obj.Scale.Y * textureResource.Size.Height; await shader.UpdateColor(gl, obj.Color.X, obj.Color.Y, obj.Color.Z, obj.Color.W); await shader.UpdateFlip(gl, horizon_flip? -1 : 1, vertical_flip? -1 : 1); //anchor await shader.UpdateAnchor(gl, obj.OriginOffset.X, obj.OriginOffset.Y); //Create ModelMatrix Matrix3 model = Matrix3.Zero; float cosa = (float)Math.Cos(obj.Rotate); float sina = (float)Math.Sin(obj.Rotate); model.Row0.X = cosa * scalex; model.Row0.Y = -sina * scalex; model.Row1.X = sina * scaley; model.Row1.Y = cosa * scaley; model.Row2.X = obj.Postion.X - SB_WIDTH / 2f; model.Row2.Y = -obj.Postion.Y + SB_HEIGHT / 2f; unsafe { fixed(float *ptr = &martrix3Buffer[0]) { Unsafe.CopyBlock(ptr, &model.Row0.X, 9 * sizeof(float)); } } await shader.UpdateModel(gl, false, martrix3Buffer); await shader.UpdateTexture(gl, textureResource.Texture); await gl.DrawArraysAsync(Primitive.TRIANGLE_FAN, 0, 4); }
public async static Task <Mesh> BuildAsync(WebGLContext gl, Geometry geometry, Texture texture, float[] initialPosition = null) { int vertexCount = geometry.GetVertexCount(); return(new Mesh( await VBO.BuildAsync(gl, vertexCount, geometry.GetPositions()), await VBO.BuildAsync(gl, vertexCount, geometry.GetNormals()), await VBO.BuildAsync(gl, vertexCount, geometry.GetUvs()), texture, vertexCount, initialPosition ?? Mat4.Create(), gl)); }
private WebGLShader LoadShader(WebGLContext gl, ShaderType type, string source) { var shader = gl.CreateShader(type); gl.ShaderSource(shader, source); gl.CompileShader(shader); if (!gl.GetShaderParameter <bool>(shader, ShaderParameter.COMPILE_STATUS)) { string info = gl.GetShaderInfoLog(shader); gl.DeleteShader(shader); throw new Exception("An error occured while compiling the shader: " + info); } return(shader); }
public static async Task BeforeDraw(WebGLContext gl) { await shader.UseProgramAsync(gl); var VP = CameraViewMatrix * ProjectionMatrix; unsafe { fixed(float *ptr = &vpBuffer[0]) { Unsafe.CopyBlock(ptr, &VP.Row0.X, 16 * sizeof(float)); } } await shader.UpdateViewProjection(gl, false, vpBuffer); }
public static async Task Init(WebGLContext baseContext) { GL = baseContext; Console.WriteLine("Initializing WebGL"); await InitWebGL(); Console.WriteLine("WebGL initialized"); // Initialize systems. Systems.Init(); // Intialize events. // Events.Init(); await Task.Run(Update); }
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"); }
private static GLShader GetShader(WebGLContext gl, string id) { var shaderScript = Element.GetById(id); var str = shaderScript.TextContent; if (str == "" || str == null) { var k = shaderScript.FirstChild; while (k != null) { if (k.NodeType == 3) { str += k.TextContent; } k = k.NextSibling; } } GLShader shader; if (shaderScript.GetAttributeValue("type") == "x-shader/x-fragment") { shader = gl.CreateShader(gl.FragmentShader); } else if (shaderScript.GetAttributeValue("type") == "x-shader/x-vertex") { shader = gl.CreateShader(gl.VertexShader); } else { return(null); } gl.ShaderSource(shader, str); gl.CompileShader(shader); if (!gl.GetShaderParameter(shader, gl.CompileStatus)) { throw new Exception("Shader compilation failed"); } return(shader); }
public RenderBatch(WebGLContext context, int elementCount) { this.context = context; this.descriptor = new TDescriptor(); itemPool = new DefaultVertexFormat[elementCount * descriptor.ElementVertexCount]; modelBuffer = context.CreateBuffer(BufferType.ARRAY_BUFFER, BufferUsage.STREAM_DRAW); modelBuffer.Bind(); modelBuffer.SetDataSize(itemPool.Length * descriptor.ByteSize); System.Console.WriteLine("Buffer size: " + itemPool.Length * descriptor.ByteSize); indicesBuffer = context.CreateBuffer(BufferType.ELEMENT_ARRAY_BUFFER, BufferUsage.STATIC_DRAW); indicesBuffer.Bind(); indicesBuffer.SetData(descriptor.CreateIndices(elementCount)); textures = new Texture2D[10]; }
private async Task <WebGLShader> LoadShaderAsync(WebGLContext gl, ShaderType type, string source) { var shader = await gl.CreateShaderAsync(type); await gl.ShaderSourceAsync(shader, source); await gl.CompileShaderAsync(shader); if (!await gl.GetShaderParameterAsync <bool>(shader, ShaderParameter.COMPILE_STATUS)) { string info = await gl.GetShaderInfoLogAsync(shader); await gl.DeleteShaderAsync(shader); throw new Exception("An error occured while compiling the shader: " + info); } return(shader); }
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)); }
protected override async Task OnAfterRenderAsync() { this._context = await this._canvasReference.CreateWebGLAsync(new WebGLContextAttributes { PowerPreference = WebGLContextAttributes.POWER_PREFERENCE_HIGH_PERFORMANCE }); await this._context.ClearColorAsync(0, 0, 0, 1); var program = await this.InitProgramAsync(this._context, VS_SOURCE, FS_SOURCE); var vertexBuffer = await this._context.CreateBufferAsync(); await this._context.BindBufferAsync(BufferType.ARRAY_BUFFER, vertexBuffer); var vertices = new[] { -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }; await this._context.BufferDataAsync(BufferType.ARRAY_BUFFER, vertices, BufferUsageHint.STATIC_DRAW); await this._context.VertexAttribPointerAsync(0, 3, DataType.FLOAT, false, 6 *sizeof(float), 0); await this._context.VertexAttribPointerAsync(1, 3, DataType.FLOAT, false, 6 *sizeof(float), 3 *sizeof(float)); await this._context.EnableVertexAttribArrayAsync(0); await this._context.EnableVertexAttribArrayAsync(1); await this._context.UseProgramAsync(program); await this._context.BeginBatchAsync(); await this._context.ClearAsync(BufferBits.COLOR_BUFFER_BIT); await this._context.DrawArraysAsync(Primitive.TRIANGLES, 0, 3); await this._context.EndBatchAsync(); }
private static async void InitStaticBuffer(WebGLContext gl) { vertexBuffer = await gl.CreateBufferAsync(); await gl.BindBufferAsync(BufferType.ARRAY_BUFFER, vertexBuffer); { var vertexArrayDef = new[] { -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, }; await gl.BufferDataAsync(BufferType.ARRAY_BUFFER, vertexArrayDef, BufferUsageHint.STATIC_DRAW); await gl.EnableVertexAttribArrayAsync(shader.PositionAttributeLocaltion); await gl.VertexAttribPointerAsync(shader.PositionAttributeLocaltion, 2, DataType.FLOAT, false, sizeof(float) * 2, 0); } texBuffer = await gl.CreateBufferAsync(); await gl.BindBufferAsync(BufferType.ARRAY_BUFFER, texBuffer); { var texArrayDef = new[] { 0, 0, 1, 0, 1, 1, 0, 1 }; await gl.BufferDataAsync(BufferType.ARRAY_BUFFER, texArrayDef, BufferUsageHint.STATIC_DRAW); await gl.EnableVertexAttribArrayAsync(shader.TexturePositionAttributeLocaltion); await gl.VertexAttribPointerAsync(shader.TexturePositionAttributeLocaltion, 2, DataType.FLOAT, false, sizeof(float) * 2, 0); } }
private WebGLProgram InitProgram(WebGLContext gl, string vsSource, string fsSource) { var vertexShader = this.LoadShader(gl, ShaderType.VERTEX_SHADER, vsSource); var fragmentShader = this.LoadShader(gl, ShaderType.FRAGMENT_SHADER, fsSource); var program = gl.CreateProgram(); gl.AttachShader(program, vertexShader); gl.AttachShader(program, fragmentShader); gl.LinkProgram(program); gl.DeleteShader(vertexShader); gl.DeleteShader(fragmentShader); if (!gl.GetProgramParameter <bool>(program, ProgramParameter.LINK_STATUS)) { string info = gl.GetProgramInfoLog(program); throw new Exception("An error occured while linking the program: " + info); } return(program); }