public SimpleCanvas(int view_width, int view_height) { FillColor = Color.Black; StrokeColor = Color.Black; //dimension _view_width = view_width; _view_height = view_height; _max = Math.Max(view_width, view_height); //------------ //matrix ////square viewport _orthoView = MyMat4.ortho(0, _max, 0, _max, 0, 1); _flipVerticalView = MyMat4.scale(1, -1) * MyMat4.translate(new OpenTK.Vector3(0, -_max, 0)); _orthoAndFlip = _orthoView * _flipVerticalView; //----------------------------------------------------------------------- //shader _shaderRes = new CanvasToShaderSharedResource(); _shaderRes.OrthoView = _orthoView; // _fillShader = new GlyphFillShader(_shaderRes); //------------ //tools Tesselate.Tesselator tt = new Tesselate.Tesselator(); _tessTool = new TessTool(tt); _curveFlattener = new SimpleCurveFlattener(); ClearColor = Color.White; //-------- //set blend mode GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); }
protected override void OnTimerTick(object sender, EventArgs e) { if (!isGLInit) { return; } float dt = 0.5f; mRotation = (float)fmod(mRotation + (dt * 40.0f), 360.0f); //Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(), // 1.0f, 20.0f); MyMat4 perspectiveMat = MyMat4.perspective(60.0f, (float)Width / (float)Height, 1.0f, 20.0f); //Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) * // Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f)); MyMat4 modelMat = MyMat4.translate(new Vector3(0f, 0f, -2f)) * MyMat4.rotate(mRotation, new Vector3(1, 0, 1)); MyMat4 viewMatrix = MyMat4.GetIdentityMat(); MyMat4 mvpMatrix = perspectiveMat * viewMatrix * modelMat; //// Load the matrices //glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data); GL.UniformMatrix4(mMVPMatrixLoc, 1, false, mvpMatrix.data); }
void SetupGL() { EAGLContext.SetCurrentContext(context); 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(); }
protected override void OnInitGLProgram(object sender, EventArgs args) { string vs = @" attribute vec4 a_position; attribute vec4 a_color; uniform mat4 u_mvpMatrix; uniform vec4 u_solidColor; uniform int u_useSolidColor; uniform float u_linewidth; varying vec4 v_color; varying float v_distance; varying float p0; void main() { float rad = a_position[3]; v_distance= a_position[2]; float n_x = sin(rad); float n_y = cos(rad); vec4 delta; if(v_distance <1.0){ delta = vec4(-n_x * u_linewidth,n_y * u_linewidth,0,0); }else{ delta = vec4(n_x * u_linewidth,-n_y * u_linewidth,0,0); } if(u_linewidth <= 0.5){ p0 = 0.5; }else if(u_linewidth <=1.0){ p0 = 0.45; }else if(u_linewidth>1.0 && u_linewidth<3.0){ p0 = 0.25; }else{ p0= 0.1; } vec4 pos = vec4(a_position[0],a_position[1],0,1) + delta; gl_Position = u_mvpMatrix* pos; if(u_useSolidColor !=0) { v_color= u_solidColor; } else { v_color = a_color; } } "; //fragment source string fs = @" precision mediump float; varying vec4 v_color; varying float v_distance; varying float p0; void main() { float d0= v_distance; float p1= 1.0-p0; float factor= 1.0 /p0; if(d0 < p0){ gl_FragColor =vec4(v_color[0],v_color[1],v_color[2], v_color[3] *(d0 * factor)); }else if(d0> p1){ gl_FragColor =vec4(v_color[0],v_color[1],v_color[2], v_color[3] *((1.0-d0)* factor)); } else{ gl_FragColor =v_color; } } "; if (!shaderProgram.Build(vs, fs)) { throw new NotSupportedException(); } a_position = shaderProgram.GetVtxAttrib("a_position"); a_color = shaderProgram.GetVtxAttrib("a_color"); u_matrix = shaderProgram.GetUniformMat4("u_mvpMatrix"); u_useSolidColor = shaderProgram.GetUniform1("u_useSolidColor"); u_solidColor = shaderProgram.GetUniform4("u_solidColor"); u_linewidth = shaderProgram.GetUniform1("u_linewidth"); //-------------------------------------------------------------------------------- GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.ClearColor(1, 1, 1, 1); //setup viewport size int max = Math.Max(this.Width, this.Height); //square viewport GL.Viewport(0, 0, max, max); orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //-------------------------------------------------------------------------------- //load image }
protected override void OnReadyForInitGLShaderProgram() { //---------------- //vertex shader source string vs = @" precision mediump float; attribute vec2 a_position; attribute vec3 a_color; attribute vec2 a_texcoord; uniform mat4 u_mvpMatrix; uniform vec4 u_solidColor; uniform int u_useSolidColor; varying vec4 v_color; varying vec2 v_texcoord; void main() { gl_Position = u_mvpMatrix* vec4(a_position[0],a_position[1],0.0,1.0); if(u_useSolidColor !=0) { v_color= u_solidColor; } else { v_color = vec4(a_color,1.0); } v_texcoord= a_texcoord; } "; //fragment source string fs = @" precision mediump float; varying vec4 v_color; varying vec2 v_texcoord; void main() { //gl_FragColor = vec4(1,v_texcoord.y,0,1); gl_FragColor= v_color; } "; if (!shaderProgram.Build(vs, fs)) { throw new NotSupportedException(); } a_position = shaderProgram.GetAttrV2f("a_position"); a_color = shaderProgram.GetAttrV3f("a_color"); a_textureCoord = shaderProgram.GetAttrV2f("a_texcoord"); 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 max = Math.Max(this.Width, this.Height); //square viewport GL.Viewport(0, 0, max, max); orthoView = MyMat4.ortho(0, max, 0, max, 0, 1); //-------------------------------------------------------------------------------- //load image }
protected override void OnInitGLProgram(object sender, EventArgs handler) { //-------------------------------------------------------------------------- string vs = @" attribute vec4 a_position; attribute vec2 a_texCoord; uniform mat4 u_mvpMatrix; varying vec2 v_texCoord; void main() { gl_Position = u_mvpMatrix* a_position; v_texCoord = a_texCoord; } "; //in fs, angle on windows //we need to switch color component //because we store value in memory as BGRA //and gl expect input in RGBA string fs = @" precision mediump float; varying vec2 v_texCoord; uniform sampler2D s_texture; void main() { vec4 c = texture2D(s_texture, v_texCoord); gl_FragColor = vec4(c[2],c[1],c[0],c[3]); } "; mProgram = ES2Utils.CompileProgram(vs, fs); if (mProgram == 0) { //return false } // Get the attribute locations mPositionLoc = GL.GetAttribLocation(mProgram, "a_position"); mTexCoordLoc = GL.GetAttribLocation(mProgram, "a_texCoord"); u_matrix = GL.GetUniformLocation(mProgram, "u_mvpMatrix"); // Get the sampler location mSamplerLoc = GL.GetUniformLocation(mProgram, "s_texture"); //// Load the texture //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("d:\\WImageTest\\test001.png"); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("d:\\WImageTest\\test001.png"); int bmpW = bmp.Width; int bmpH = bmp.Height; mTexture = LoadTexture(bmp); GL.ClearColor(0, 0, 0, 0); //================================================================================ GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.ClearColor(1, 1, 1, 1); //setup viewport size int max = Math.Max(this.Width, this.Height); orthoViewMat = MyMat4.ortho(0, max, 0, max, 0, 1).data; //square viewport GL.Viewport(0, 0, max, max); imgVertices = new float[] { 0, bmpH, 0, 0, 0, //--------------------- 0, 0, 0, 0, 1, //--------------------- bmpW, bmpH, 0, 1, 0, //--------------------- bmpW, 0, 0, 1, 1 }; }
internal void SetViewMatrix(MyMat4 mat) { scanlineShader.ViewMatrix = mat; }
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(); }