protected override void OnGLRender(object sender, EventArgs args) { //------------------------------------------------------------------------------------------------ float[] vertices = { 50,100f, //2d coord 1, 0, 0, 0.5f, //r 200f,100f, //2d coord 0, 1, 0, 0.5f, //g 150f,200, //2d corrd 0, 0, 1, 0.5f, //b }; //--------------------------------------------------------- GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- //triangle shape //x,y,r,g,b,a u_useSolidColor.SetValue(0); unsafe { fixed(float *head = &vertices[0]) { a_position.UnsafeLoadMixedV2f(head, 5); a_color.UnsafeLoadMixedV3f(head + 3, 5); } } GL.DrawArrays(BeginMode.Triangles, 0, 6); //--------------------------------------------------------- //rect shape //float[] quadVertices3 = CreateRectCoords(250f, 450f, 100f, 100f); //FillPolygonWithSolidColor(quadVertices3, 6, PixelFarm.Drawing.Color.Red); //float[] quadVertices2 = CreateRectCoords(250f, 450f, 100f, 100f); float[] quadVertices2 = CreateRectCoords(260f, 160f, 100f, 100f); float[] textureCoords = CreateRectTextureCoords(); //FillPolygonWithSolidColor2(quadVertices2, textureCoords, 6, PixelFarm.Drawing.Color.Blue); FillPolygonWithSolidColor(quadVertices2, 6, PixelFarm.Drawing.Color.Blue); //--------------------------------------------------------- float[] quadVertices3 = CreateRectCoords(280f, 180f, 100f, 100f); float[] textureCoords3 = CreateRectTextureCoords(); FillPolygonWithSolidColor2(quadVertices3, textureCoords3, 6, PixelFarm.Drawing.Color.Blue); //--------------------------------------------------------- float[] quadVertices = CreateRectCoords(250f, 150f, 100f, 100f); FillPolygonWithSolidColor(quadVertices, 6, PixelFarm.Drawing.Color.Yellow); //--------------------------------------------------------- // DrawLines(0, 0, 300, 300); //--------------------------------------------------------- SwapBuffers(); }
protected override void OnGLRender(object sender, EventArgs args) { //------------------------------------------------------------------------------------------------ int width = this.Width; int height = this.Height; float[] vertices = { 0.0f,0.5f, //2d coord 1, 0, 0, 0.1f, //r -0.5f,-0.5f, //2d coord 0, 1, 0, 0.1f, //g 0.5f,-0.5f, //2d corrd 0, 0, 1, 0.1f, //b }; GL.Viewport(0, 0, width, height); // Set the viewport GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); // Load the vertex data unsafe { fixed(float *head = &vertices[0]) { a_position.UnsafeLoadMixedV2f(head, 5); a_color.UnsafeLoadMixedV3f(head + 2, 5); } } GL.DrawArrays(BeginMode.Triangles, 0, 3); SwapBuffers(); }
protected override void OnGLRender(object sender, EventArgs args) { float[] vertices = new float[] { -0.5f, 0.5f, 0.0f, // Position 0 0.0f, 0.0f, // TexCoord 0 -0.5f, -0.5f, 0.0f, // Position 1 0.0f, 1.0f, // TexCoord 1 0.5f, -0.5f, 0.0f, // Position 2 1.0f, 1.0f, // TexCoord 2 0.5f, 0.5f, 0.0f, // Position 3 1.0f, 0.0f // TexCoord 3 }; ushort[] indices = new ushort[] { 0, 1, 2, 0, 2, 3 }; GL.Viewport(0, 0, this.Width, this.Height); GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); a_position.LoadV3f(vertices, 5, 0); a_textCoord.LoadV2f(vertices, 5, 3); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, mTexture); s_texture.SetValue(0); GL.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedShort, indices); miniGLControl.SwapBuffers(); }
protected override void OnGLRender(object sender, EventArgs args) { //------------------------------------------------------------------------------------------------ int width = miniGLControl.Width; int height = miniGLControl.Height; float[] vertices = { 0.0f,0.5f, //2d coord 1, 0, 0, 0.1f, //r -0.5f,-0.5f, //2d coord 0, 1, 0, 0.1f, //g 0.5f,-0.5f, //2d corrd 0, 0, 1, 0.1f, //b }; GL.Viewport(0, 0, width, height); // Set the viewport GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); // Load the vertex data a_position.LoadV2f(vertices, 6, 0); a_color.LoadV3f(vertices, 6, 2); //GL.DrawArrays(BeginMode.Triangles, 0, 3); GL.DrawArrays(BeginMode.Points, 0, 3); miniGLControl.SwapBuffers(); }
/// <summary> /// set as current shader /// </summary> protected void SetCurrent() { if (_canvasShareResource._currentShader != this) { shaderProgram.UseProgram(); _canvasShareResource._currentShader = this; this.OnSwithToThisShader(); } }
/// <summary> /// set as current shader /// </summary> internal void SetCurrent() { if (_shareRes._currentShader != this) { _shareRes._currentShader = this; _shaderProgram.UseProgram(); this.OnSwitchToThisShader(); } }
protected override void OnGLRender(object sender, EventArgs args) { //------------------------------------------------------------------------------------------------ float[] vertices = { 50,100f, //2d coord 1, 0, 0, 0.5f, //r 200f,100f, //2d coord 0, 1, 0, 0.5f, //g 150f,200, //2d corrd 0, 0, 1, 0.5f, //b }; //--------------------------------------------------------- GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- //triangle shape FillPolygonWithVertexColor(vertices, 6); //--------------------------------------------------------- //rect shape //float[] quadVertices3 = CreateRectCoords(250f, 450f, 100f, 100f); //FillPolygonWithSolidColor(quadVertices3, 6, PixelFarm.Drawing.Color.Red); //float[] quadVertices2 = CreateRectCoords(250f, 450f, 100f, 100f); float[] quadVertices2 = CreateRectCoords(260f, 160f, 100f, 100f); float[] textureCoords = CreateRectTextureCoords(); //FillPolygonWithSolidColor2(quadVertices2, textureCoords, 6, PixelFarm.Drawing.Color.Blue); FillPolygonWithSolidColor(quadVertices2, 6, PixelFarm.Drawing.Color.Blue); //--------------------------------------------------------- float[] quadVertices3 = CreateRectCoords(280f, 180f, 100f, 100f); float[] textureCoords3 = CreateRectTextureCoords(); FillPolygonWithSolidColor2(quadVertices3, textureCoords3, 6, PixelFarm.Drawing.Color.Blue); //--------------------------------------------------------- float[] quadVertices = CreateRectCoords(250f, 150f, 100f, 100f); FillPolygonWithSolidColor(quadVertices, 6, PixelFarm.Drawing.Color.Yellow); //--------------------------------------------------------- // DrawLines(0, 0, 300, 300); //--------------------------------------------------------- miniGLControl.SwapBuffers(); }
protected override void OnGLRender(object sender, EventArgs args) { GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- //DrawLines(50, 0, 50, 150); DrawLine(50, 50, 300, 200); DrawLine(300, 200, 100, 150); DrawLine(100, 150, 110, 100); //--------------------------------------------------------- miniGLControl.SwapBuffers(); }
protected override void OnGLRender(object sender, EventArgs args) { //------------------------------------------------------------------------------------------------ GL.Clear(ClearBufferMask.ColorBufferBit); shaderProgram.UseProgram(); //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- DrawLines(0, 0, 300, 150); float[] rect = CreateRectCoords2(100, 100, 50, 50); FillPolygonWithSolidColor(rect, rect.Length / 2, PixelFarm.Drawing.Color.Black); //--------------------------------------------------------- SwapBuffers(); }
void SetupGL() { //EAGLContext.SetCurrentContext(context); MakeCurrent(); LoadShaders(); //-------------------------------------------------------------------------------- GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.ClearColor(1, 1, 1, 1); //setup viewport size int ww_w = view_width; int ww_h = view_height; int max = Math.Max(ww_w, ww_h); //square viewport GL.Viewport(0, 0, max, max); orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //-------------------------------------------------------------------------------- shaderProgram.UseProgram(); }
public void InitShader() { if (isInited) { return; } //---------------- //vertex shader source string vs = @" attribute vec3 a_position; attribute vec4 a_color; attribute vec2 a_texcoord; uniform mat4 u_mvpMatrix; uniform vec4 u_solidColor; uniform int u_useSolidColor; uniform int u_useAggColor; varying vec4 v_color; varying vec2 v_texcoord; void main() { float a= a_position[2]; //before matrix op gl_Position = u_mvpMatrix* vec4(a_position[0],a_position[1],0,1); if(u_useAggColor !=0) { v_color= vec4(u_solidColor.r /255.0,u_solidColor.g /255.0,u_solidColor.b/255.0, a/255.0); } else if(u_useSolidColor !=0) { v_color= u_solidColor; } else { v_color = a_color; } v_texcoord= a_texcoord; } "; //fragment source string fs = @" precision mediump float; varying vec4 v_color; varying vec2 v_texcoord; void main() { gl_FragColor= v_color; } "; if (!shaderProgram.Build(vs, fs)) { throw new NotSupportedException(); } a_position = shaderProgram.GetVtxAttrib("a_position"); a_color = shaderProgram.GetVtxAttrib("a_color"); a_textureCoord = shaderProgram.GetVtxAttrib("a_texcoord"); u_matrix = shaderProgram.GetUniformMat4("u_mvpMatrix"); u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor"); u_useAggColor = shaderProgram.GetUniform1("u_useAggColor"); u_solidColor = shaderProgram.GetUniform4("u_solidColor"); shaderProgram.UseProgram(); isInited = true; }
public static void Start() { if (!Glfw.Init()) { Console.WriteLine("can't init glfw"); return; } //--------------------------------------------------- //at this, point no opengl func binding //--------------------------------------------------- //1. we specific which verision we want, //here => OpenGLES 2.0 Glfw.WindowHint(WindowHint.GLFW_CLIENT_API, (int)OpenGLAPI.OpenGLESAPI); Glfw.WindowHint(WindowHint.GLFW_CONTEXT_CREATION_API, (int)OpenGLContextCreationAPI.GLFW_EGL_CONTEXT_API); Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MAJOR, 3); Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MINOR, 0); //--------------------------------------------------- OpenTK.Platform.Factory.GetCustomPlatformFactory = () => OpenTK.Platform.Egl.EglAngle.NewFactory(); OpenTK.Toolkit.Init(new OpenTK.ToolkitOptions { Backend = OpenTK.PlatformBackend.PreferNative, }); OpenTK.Graphics.PlatformAddressPortal.GetAddressDelegate = OpenTK.Platform.Utilities.CreateGetAddress(); GlfwMonitorPtr monitor = new GlfwMonitorPtr(); //default monitor GlfwWindowPtr winPtr = new GlfwWindowPtr(); //default window GlfwWindowPtr glWindow = Glfw.CreateWindow(800, 600, "Test Glfw", monitor, winPtr); /* Make the window's context current */ Glfw.MakeContextCurrent(glWindow); Glfw.SwapInterval(1); GlfwWindowPtr currentContext = Glfw.GetCurrentContext(); var contextHandler = new OpenTK.ContextHandle(currentContext.inner_ptr); //faster: create external context var glfwContext = new GLFWContextForOpenTK(contextHandler); var context = OpenTK.Graphics.GraphicsContext.CreateExternalContext(glfwContext); //--------------------------- //slow ... use create dummy context //var context = OpenTK.Graphics.GraphicsContext.CreateDummyContext(contextHandler); //and you need to load gles2 binding points manaually (below) bool isCurrent = context.IsCurrent; PixelFarm.GlfwWinInfo winInfo = new PixelFarm.GlfwWinInfo(glWindow); context.MakeCurrent(winInfo); //-------------------------------------- //bind open gl funcs here.. //this not need if we use glfwcontext for opentk //new OpenTK.Graphics.ES20.GL().LoadEntryPoints(); //-------------------------------------- //create shader program var shaderProgram = new MiniShaderProgram(); //-------------------------------------- ShaderVtxAttrib3f a_position; ShaderVtxAttrib4f a_color; ShaderUniformMatrix4 u_matrix; ShaderUniformVar1 u_useSolidColor; ShaderUniformVar4 u_solidColor; MyMat4 orthoView; string vs = @" attribute vec3 a_position; attribute vec4 a_color; uniform mat4 u_mvpMatrix; uniform vec4 u_solidColor; uniform int u_useSolidColor; varying vec4 v_color; varying vec4 a_position_output; void main() { a_position_output = u_mvpMatrix* vec4(a_position[0],a_position[1],0,1); gl_Position = a_position_output; v_color= vec4(1,0,0,1); } "; //fragment source // string fs = @"void main() // { // gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); // } // "; string fs = @" precision mediump float; varying vec4 v_color; varying vec4 a_position_output; void main() { if(a_position_output[1]>0.5){ gl_FragColor = vec4(0,1,1,1); }else{ gl_FragColor= vec4(0,1,0,1); } } "; if (!shaderProgram.Build(vs, fs)) { throw new NotSupportedException(); } a_position = shaderProgram.GetAttrV3f("a_position"); a_color = shaderProgram.GetAttrV4f("a_color"); u_matrix = shaderProgram.GetUniformMat4("u_mvpMatrix"); u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor"); u_solidColor = shaderProgram.GetUniform4("u_solidColor"); //-------------------------------------------------------------------------------- GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.ClearColor(1, 1, 1, 1); //setup viewport size int ww_w = 800; int ww_h = 600; int max = Math.Max(ww_w, ww_h); //square viewport GL.Viewport(0, 0, max, max); orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //-------------------------------------------------------------------------------- //load image /* Loop until the user closes the window */ //int width, height; //Glfw.GetFramebufferSize(glWindow, out width, out height); //float ratio = (float)width / (float)height; GL.Viewport(0, 0, 800, 600); shaderProgram.UseProgram(); while (!Glfw.WindowShouldClose(glWindow)) { //set clear color to white GL.ClearColor(1f, 1, 1, 1); GL.Clear(ClearBufferMask.ColorBufferBit); //--------------------------------- //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- //DrawLines(0, 0, 300, 300); float x1 = 50, y1 = 20, x2 = 300, y2 = 20; float[] vtxs = new float[] { x1, y1, 1, x2, y2, 1, 50, 300, 1 }; u_useSolidColor.SetValue(1); u_solidColor.SetValue(1f, 0f, 0f, 1f);//use solid color a_position.LoadPureV3f(vtxs); GL.DrawArrays(BeginMode.Triangles, 0, 3); //--------------------------------------------------------- //GL.MatrixMode(MatrixMode.Modelview); //GL.LoadIdentity(); //GL.Begin(BeginMode.Triangles); //GL.Color3(1f, 0, 0); //GL.Vertex3(-0.6f, -0.4f, 0f); //GL.Color3(0f, 1f, 0); //GL.Vertex3(0.6f, -0.4f, 0f); //GL.Color3(0f, 0, 1f); //GL.Vertex3(0.0f, 0.6f, 0f); //GL.End(); //--------------------------------- /* Render here */ /* Swap front and back buffers */ Glfw.SwapBuffers(glWindow); /* Poll for and process events */ Glfw.PollEvents(); } Glfw.Terminate(); }
public static void Start() { if (!Glfw.Init()) { Console.WriteLine("can't init glfw"); return; } //--------------------------------------------------- //specific OpenGLES //Glfw.WindowHint(WindowHint.GLFW_CLIENT_API, (int)OpenGLAPI.OpenGLESAPI); //Glfw.WindowHint(WindowHint.GLFW_CONTEXT_CREATION_API, (int)OpenGLContextCreationAPI.GLFW_EGL_CONTEXT_API); //Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MAJOR, 2); //Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MINOR, 0); //--------------------------------------------------- GlfwMonitorPtr monitor = new GlfwMonitorPtr(); GlfwWindowPtr winPtr = new GlfwWindowPtr(); GlfwWindowPtr glWindow = Glfw.CreateWindow(800, 600, "Test Glfw", monitor, winPtr); /* Make the window's context current */ Glfw.MakeContextCurrent(glWindow); Glfw.SwapInterval(1); GL.Hello(); //-------------------------------------- //create shader program var shaderProgram = new MiniShaderProgram(); ShaderVtxAttrib3f a_position; ShaderVtxAttrib4f a_color; ShaderUniformMatrix4 u_matrix; ShaderUniformVar1 u_useSolidColor; ShaderUniformVar4 u_solidColor; MyMat4 orthoView; string vs = @" attribute vec3 a_position; attribute vec4 a_color; uniform mat4 u_mvpMatrix; uniform vec4 u_solidColor; uniform int u_useSolidColor; varying vec4 v_color; varying vec4 a_position_output; void main() { a_position_output = u_mvpMatrix* vec4(a_position[0],a_position[1],0,1); gl_Position = a_position_output; v_color= vec4(1,0,0,1); } "; //fragment source // string fs = @"void main() // { // gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); // } // "; string fs = @" //precision mediump float; //need for OpenGLES2 varying vec4 v_color; varying vec4 a_position_output; void main() { if(a_position_output[1]>0.5){ gl_FragColor = vec4(0,1,1,1); }else{ gl_FragColor= vec4(0,1,0,1); } } "; if (!shaderProgram.Build(vs, fs)) { throw new NotSupportedException(); } a_position = shaderProgram.GetAttrV3f("a_position"); a_color = shaderProgram.GetAttrV4f("a_color"); u_matrix = shaderProgram.GetUniformMat4("u_mvpMatrix"); u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor"); u_solidColor = shaderProgram.GetUniform4("u_solidColor"); //-------------------------------------------------------------------------------- GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.ClearColor(1, 1, 1, 1); //setup viewport size int ww_w = 800; int ww_h = 600; int max = Math.Max(ww_w, ww_h); //square viewport GL.Viewport(0, 0, max, max); orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //-------------------------------------------------------------------------------- //load image /* Loop until the user closes the window */ int width, height; Glfw.GetFramebufferSize(glWindow, out width, out height); float ratio = (float)width / (float)height; GL.Viewport(0, 0, 800, 600); shaderProgram.UseProgram(); while (!Glfw.WindowShouldClose(glWindow)) { //set clear color to white GL.ClearColor(1f, 1, 1, 1); GL.Clear(ClearBufferMask.ColorBufferBit); //--------------------------------- //--------------------------------------------------------- u_matrix.SetData(orthoView.data); //--------------------------------------------------------- //DrawLines(0, 0, 300, 300); float x1 = 50, y1 = 20, x2 = 300, y2 = 20; float[] vtxs = new float[] { x1, y1, 1, x2, y2, 1, 50, 300, 1 }; u_useSolidColor.SetValue(1); u_solidColor.SetValue(1f, 0f, 0f, 1f);//use solid color a_position.LoadPureV3f(vtxs); GL.DrawArrays(BeginMode.Triangles, 0, 3); //--------------------------------------------------------- //GL.MatrixMode(MatrixMode.Modelview); //GL.LoadIdentity(); //GL.Begin(BeginMode.Triangles); //GL.Color3(1f, 0, 0); //GL.Vertex3(-0.6f, -0.4f, 0f); //GL.Color3(0f, 1f, 0); //GL.Vertex3(0.6f, -0.4f, 0f); //GL.Color3(0f, 0, 1f); //GL.Vertex3(0.0f, 0.6f, 0f); //GL.End(); //--------------------------------- /* Render here */ /* Swap front and back buffers */ Glfw.SwapBuffers(glWindow); /* Poll for and process events */ Glfw.PollEvents(); } Glfw.Terminate(); }