コード例 #1
0
 public Dimensions GetFramebufferSize()
 {
     Glfw.GetFramebufferSize(window, out var width, out var height);
     return(new Dimensions {
         Width = width, Height = height
     });
 }
コード例 #2
0
        public static void ImGui_ImplGlfw_NewFrame()
        {
            ImGuiIOPtr io = ImGui.GetIO();

            System.Diagnostics.Debug.Assert(io.Fonts.IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()." != null);

            // Setup display size (every frame to accommodate for window resizing)
            int w, h;
            int display_w, display_h;

            Glfw.GetWindowSize(windowHandle, out w, out h);
            Glfw.GetFramebufferSize(windowHandle, out display_w, out display_h);
            io.DisplaySize = new Vector2((float)w, (float)h);
            if (w > 0 && h > 0)
            {
                io.DisplayFramebufferScale = new Vector2((float)display_w / w, (float)display_h / h);
            }

            // Setup time step
            double current_time = Glfw.Time;

            io.DeltaTime = time > 0.0 ? (float)(current_time - time) : (float)(1.0f / 60.0f);
            time         = current_time;

            ImGui_ImplGlfw_UpdateMousePosAndButtons();
            ImGui_ImplGlfw_UpdateMouseCursor();

            // Update game controllers (if enabled and available)
            //ImGui_ImplGlfw_UpdateGamepads();
        }
コード例 #3
0
ファイル: TestSharing.cs プロジェクト: jpalcala/Glfw.Net
        static void DrawQuad(uint texture)
        {
            int width, height;

            Glfw.GetFramebufferSize(Glfw.GetCurrentContext(), out width, out height);

            Gl.Viewport(0, 0, width, height);

            Gl.MatrixMode(MatrixMode.Projection);
            Gl.LoadIdentity();
            Gl.Ortho(0f, 1f, 0f, 1f, 0f, 1f);

            Gl.Enable(EnableCap.Texture2d);
            Gl.BindTexture(TextureTarget.Texture2d, texture);
            Gl.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, Gl.MODULATE);

            Gl.Begin(PrimitiveType.Quads);

            Gl.TexCoord2(0f, 0f);
            Gl.Vertex2(0f, 0f);

            Gl.TexCoord2(1f, 0f);
            Gl.Vertex2(1f, 0f);

            Gl.TexCoord2(1f, 1f);
            Gl.Vertex2(1f, 1f);

            Gl.TexCoord2(0f, 1f);
            Gl.Vertex2(0f, 1f);

            Gl.End();
        }
コード例 #4
0
ファイル: Window.cs プロジェクト: The-Noah/CSEngine
        public void UpdateViewportSize()
        {
            int width  = 0;
            int height = 0;

            Glfw.GetFramebufferSize(window, ref width, ref height);

            Gl.Viewport(0, 0, width, height);
        }
コード例 #5
0
ファイル: MainProgram.cs プロジェクト: PlumpMath/LED_Engine
        static void RescaleToWindowSize()
        {
            Glfw.GetFramebufferSize(Window, out Width, out Height);

            if (Width > 0 && Height > 0)
            {
                GL.Viewport(0, 0, Width, Height);
                QFontProjectionMatrix = Matrix4.CreateOrthographicOffCenter(0, Width, 0, Height, -1.0f, 1.0f);
            }
        }
コード例 #6
0
        private Vector2i GetSurfaceSize()
        {
            // - If not initialized do nothing
            if (!IsValid)
            {
                throw new InvalidOperationException("Window not initialized");
            }

            Glfw.GetFramebufferSize(GLFWHAndle, out int w, out int h);

            return(new Vector2i(w, h));
        }
コード例 #7
0
ファイル: GameFunc.cs プロジェクト: PlumpMath/LED_Engine
        static void RescaleToWindowSize()
        {
            int FB_Width, FB_Height;

            Glfw.GetFramebufferSize(Window, out FB_Width, out FB_Height);

            if (FB_Width > 0 && FB_Height > 0)
            {
                FBO.Init(FB_Width, FB_Height);
                MainCamera.SetProjectionMatrix(ProjectionTypes.Perspective, FB_Width, FB_Height, MainCamera.zNear, MainCamera.zFar, MainCamera.FOV);
                GL.Viewport(0, 0, FB_Width, FB_Height);
                FPS.Font_Init();
                FPS.FPS_Font_ProjectionMatrix = Matrix4.CreateOrthographicOffCenter(0, FB_Width, 0, FB_Height, -1.0f, 1.0f);
            }
        }
コード例 #8
0
        static void DrawJoysticks(Glfw.Window window)
        {
            Glfw.GetFramebufferSize(window, out int width, out int height);

            Gl.MatrixMode(MatrixMode.Projection);
            Gl.LoadIdentity();
            Gl.Ortho(0.0, width, height, 0.0, 1.0, -1.0);
            Gl.MatrixMode(MatrixMode.Modelview);

            for (int i = 0; i < m_Joysticks.Count; i++)
            {
                DrawJoystick(i,
                             0, i * height / m_Joysticks.Count,
                             width, height / m_Joysticks.Count);
            }
        }
コード例 #9
0
        static void Main(string[] args)
        {
            /*Generator.Generate();
             * Console.WriteLine("Done!");
             * Console.ReadLine();*/

            NVG.SetLibraryDirectory();
            Glfw.Init();

            Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 3);
            Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 2);
            Glfw.WindowHint(Glfw.Hint.OpenglForwardCompat, true);
            Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);

            Glfw.WindowHint(Glfw.Hint.Samples, 16);

            Glfw.Window Wnd = Glfw.CreateWindow(800, 600, "NanoVG.NET");
            Glfw.MakeContextCurrent(Wnd);
            Glfw.GetFramebufferSize(Wnd, out int FbW, out int FbH);

            NanoVGContext Ctx = NVG.CreateGL3Glew(3);

            Glfw.SwapInterval(0);

            int Icons    = Ctx.CreateFont("icons", "data/fonts/entypo.ttf");
            int Sans     = Ctx.CreateFont("sans", "data/fonts/Roboto-Regular.ttf");
            int SansBold = Ctx.CreateFont("sans-bold", "data/fonts/Roboto-Bold.ttf");
            int Emoji    = Ctx.CreateFont("emoji", "data/fonts/NotoEmoji-Regular.ttf");

            Ctx.AddFallbackFontId(Sans, Emoji);
            Ctx.AddFallbackFontId(SansBold, Emoji);

            while (!Glfw.WindowShouldClose(Wnd))
            {
                glClear();
                Ctx.BeginFrame(800, 600, (float)FbW / FbH);

                Demo(Ctx);

                Ctx.EndFrame();
                Glfw.SwapBuffers(Wnd);
                Glfw.PollEvents();
            }
        }
コード例 #10
0
ファイル: TestTimeout.cs プロジェクト: masums/NetCoreGLFW
        public void Main(string[] args)
        {
            Init();

            var rand = new Random();

            Glfw.Window window;

            if (!Glfw.Init())
            {
                Environment.Exit(1);
            }

            window = Glfw.CreateWindow(640, 480, "Event Wait Timeout Test");
            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.MakeContextCurrent(window);
            Glfw.SetKeyCallback(window, KeyCallback);

            while (!Glfw.WindowShouldClose(window))
            {
                int    width, height;
                double r = rand.NextDouble(), g = rand.NextDouble(), b = rand.NextDouble();
                double l = Math.Sqrt(r * r + g * g + b * b);

                Glfw.GetFramebufferSize(window, out width, out height);

                Gl.Viewport(0, 0, width, height);
                Gl.ClearColor((float)(r / l), (float)(g / l), (float)(b / l), 1f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);
                Glfw.SwapBuffers(window);

                Glfw.WaitEventsTimeout(1.0);
            }

            Glfw.DestroyWindow(window);

            Glfw.Terminate();
        }
コード例 #11
0
        static void WindowRefreshCallback(Glfw.Window window)
        {
            Log("{0} Window refresh\n", Glfw.GetTime());

            Glfw.GetFramebufferSize(window, out int width, out int height);

            Glfw.MakeContextCurrent(window);

            Gl.Enable(EnableCap.ScissorTest);

            Gl.Scissor(0, 0, width, height);
            Gl.ClearColor(0, 0, 0, 0);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            Gl.Scissor(0, 0, 640, 480);
            Gl.ClearColor(1, 1, 1, 0);
            Gl.Clear(ClearBufferMask.ColorBufferBit);

            Glfw.SwapBuffers(window);
        }
コード例 #12
0
        private void HandleRefreshCallback(Glfw.Window window)
        {
            Glfw.MakeContextCurrent(window);

            Glfw.GetFramebufferSize(window, out var fbWidth, out var fbHeight);

            /*Gl.Viewport(0, 0, fbWidth, fbHeight);
             *
             * Gl.ClearColor(0.3f, 0.3f, 0.32f, 1.0f);
             * Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
             *
             * var w = Width;
             * var h = Height;
             *
             * _context.BeginFrame(w, h, (float)fbWidth / (float)w);
             *
             * Render(_context);
             *
             * _context.EndFrame();*/

            Glfw.SwapBuffers(window);
        }
コード例 #13
0
 protected override Vector2 GetSize()
 {
     Glfw.GetFramebufferSize(_win, out int width, out int height);
     return(new Vector2(width, height));
 }
コード例 #14
0
ファイル: TestCursor.cs プロジェクト: jpalcala/Glfw.Net
        static void Main()
        {
            Init();

            Glfw.Window window;
            var         starCursors  = new Glfw.Cursor[CURSOR_FRAME_COUNT];
            var         currentFrame = Glfw.Cursor.None;

            Gl.Initialize();

            if (!Glfw.Init())
            {
                Environment.Exit(1);
            }

            for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
            {
                starCursors[i] = CreateCursorFrame(i / (float)CURSOR_FRAME_COUNT);
                if (!starCursors[i])
                {
                    Glfw.Terminate();
                    Environment.Exit(1);
                }
            }

            Glfw.CursorType[] shapes =
            {
                Glfw.CursorType.Arrow,
                Glfw.CursorType.Beam,
                Glfw.CursorType.Crosshair,
                Glfw.CursorType.Hand,
                Glfw.CursorType.ResizeX,
                Glfw.CursorType.ResizeY
            };

            standardCursors = new Glfw.Cursor[6];

            for (int i = 0; i < standardCursors.Length; i++)
            {
                standardCursors[i] = Glfw.CreateStandardCursor(shapes[i]);

                if (!standardCursors[i])
                {
                    Glfw.Terminate();
                    Environment.Exit(1);
                }
            }

            window = Glfw.CreateWindow(640, 480, "Cursor Test");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.MakeContextCurrent(window);

            Glfw.GetCursorPos(window, out cursorX, out cursorY);
            Log("Cursor position: {0} {1}", cursorX, cursorY);

            Glfw.SetCursorPosCallback(window, CursorPositionCallback);
            Glfw.SetKeyCallback(window, KeyCallback);

            while (!Glfw.WindowShouldClose(window))
            {
                Gl.Clear(ClearBufferMask.ColorBufferBit);

                if (trackCursor)
                {
                    int   wnd_width, wnd_height, fb_width, fb_height;
                    float scale;

                    Glfw.GetWindowSize(window, out wnd_width, out wnd_height);
                    Glfw.GetFramebufferSize(window, out fb_width, out fb_height);

                    scale = (float)fb_width / (float)wnd_width;

                    Gl.Viewport(0, 0, fb_width, fb_height);

                    Gl.MatrixMode(MatrixMode.Projection);
                    Gl.LoadIdentity();
                    Gl.Ortho(0f, fb_width, 0f, fb_height, 0f, 1f);

                    Gl.Begin(PrimitiveType.Lines);
                    Gl.Vertex2(0f, (float)(fb_height - cursorY * scale));
                    Gl.Vertex2((float)fb_width, (float)(fb_height - cursorY * scale));
                    Gl.Vertex2((float)cursorX * scale, 0f);
                    Gl.Vertex2((float)cursorX * scale, (float)fb_height);
                    Gl.End();
                }

                Glfw.SwapBuffers(window);

                if (animateCursor)
                {
                    var i = (int)(Glfw.GetTime() * 30.0) % CURSOR_FRAME_COUNT;

                    if (currentFrame != starCursors[i])
                    {
                        Glfw.SetCursor(window, starCursors[i]);
                        currentFrame = starCursors[i];
                    }
                }
                else
                {
                    currentFrame = Glfw.Cursor.None;
                }

                if (waitEvents)
                {
                    if (animateCursor)
                    {
                        Glfw.WaitEventsTimeout(1.0 / 30.0);
                    }
                    else
                    {
                        Glfw.WaitEvents();
                    }
                }
                else
                {
                    Glfw.PollEvents();
                }
            }

            Glfw.DestroyWindow(window);

            for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
            {
                Glfw.DestroyCursor(starCursors[i]);
            }

            for (int i = 0; i < standardCursors.Length; i++)
            {
                Glfw.DestroyCursor(standardCursors[i]);
            }

            Glfw.Terminate();
        }
コード例 #15
0
ファイル: MainProgram.cs プロジェクト: PlumpMath/LED_Engine
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; //For fix parsing values like "0.5" and "0,5"

            Glfw.SetErrorCallback(OnError);

            if (!Glfw.Init())
            {
                Log.WriteLineRed("ERROR: Could not initialize GLFW, shutting down.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            Engine.LoadConfig();
            Engine.ApplyConfig();

            while (Settings.Window.NeedReinitWindow)
            {
                ApplySettingsAndCreateWindow();
                Engine.GetGLSettings();
                Engine.LoadContentLists();

                int FB_Width, FB_Height;
                Glfw.GetFramebufferSize(Window, out FB_Width, out FB_Height);
                FBO.Init(FB_Width, FB_Height);
                RescaleToWindowSize(); // This function call FBO.Init()!!! That's is needed! (mb GLFW bug...)

                Settings.Window.NeedReinitWindow = false;

                if (!ContextIsLoaded)
                {
                    Engine.LoadEngineContent();

                    //Load Map
                    if (args.Length > 0)
                    {
                        Maps.LoadMap(args[0]);
                    }
                    else
                    {
                        Maps.LoadMap("Materials_Test_Map"); //Sponza //SampleMap //Materials_Test_Map
                    }
                    ContextIsLoaded = true;
                }

                // On...DoSomething functions
                Glfw.SetCursorPosCallback(Window, OnMouseMove);
                Glfw.SetMouseButtonCallback(Window, OnMouseClick);
                Glfw.SetFramebufferSizeCallback(Window, OnFramebufferResize);
                Glfw.SetKeyCallback(Window, OnKeyPress);
                Glfw.SetWindowFocusCallback(Window, OnFocusChanged);
                Glfw.SetWindowPosCallback(Window, OnWindowPositionChange);
                Glfw.SetWindowSizeCallback(Window, OnWindowResize);

                #region Main Loop
                while (!(Glfw.WindowShouldClose(Window) || Settings.Window.NeedReinitWindow))
                {
                    OnUpdateFrame();
                    OnRenderFrame();

                    if (FPS.ShowFPS)
                    {
                        FPS.CalcFPS();        //FPS Counter, must be at the END of the Render LOOP!
                    }
                    Glfw.SwapBuffers(Window); // Swap the front and back buffer, displaying the scene
                    Glfw.PollEvents();        // Poll GLFW window events
                }
                #endregion
            }
            Maps.Free(true); // Free all: Map -> Meshes, Shaders, Textures...
            FBO.Free();
            FPS.Font_Free();
            Engine.ClearLists();
            try
            {
                Game.EditorMainForm.Close(); // Editor
            }
            catch
            {
            }
            Glfw.DestroyWindow(Window);
            Glfw.Terminate();

            if (Settings.Debug.Enabled)
            {
                //Console.ReadKey();
                string log = Log.GetLog();
                if (log.Length > 256)
                {
                    System.Windows.Forms.Clipboard.SetText(log);
                }
            }
        }
コード例 #16
0
        public GlfwPlatform(int width, int height, bool fullscreen = false)
        {
            midi = new MidiConsoleOut();

            // Handle Glfw errors:
            Glfw.SetErrorCallback((code, desc) => throw new Exception(String.Format("GLFW error code {0}: {1}", code, desc)));

            Debug.WriteLine("glfw.Init()");
            Glfw.Init();

            // Disable window resizing
            Glfw.WindowHint(Glfw.Hint.Resizable, false);
            // Enable multi-sampling
            Glfw.WindowHint(Glfw.Hint.Samples, 8);

            Glfw.Monitor monitor = fullscreen ? Glfw.GetPrimaryMonitor() : Glfw.Monitor.None;

            Debug.WriteLine("window = glfw.CreateWindow()");
            window = Glfw.CreateWindow(
                width,
                height,
                "e-sharp-minor",
                monitor,
                Glfw.Window.None
                );

            // Fetch the real window size:
            Glfw.GetWindowSize(window, out width, out height);

            // Window dimensions (aspect ratio) in VG coordinate space (non-retina):
            this.Width  = width;
            this.Height = height;

            // Get the real framebuffer size for OpenGL pixels; should work with Retina:
            int fbWidth, fbHeight;

            Glfw.GetFramebufferSize(window, out fbWidth, out fbHeight);

            // These are needed for vgClear since it works in framebuffer pixels.
            FramebufferWidth  = fbWidth;
            FramebufferHeight = fbHeight;

            Debug.WriteLine("glfw.MakeContextCurrent(window)");
            Glfw.MakeContextCurrent(window);

            // create an OpenVG context
            Debug.WriteLine("vgContext = vgPrivContextCreateAM(0)");
            vgContext = vgPrivContextCreateAM(IntPtr.Zero);

            // create a drawing surface (sRGBA premultiplied color space)
            //vgSurface = vgPrivSurfaceCreateAM(fbWidth, fbHeight, 0, 1, 1);
            vgSurface = vgPrivSurfaceCreateAM(fbWidth, fbHeight, 0, 1, 0);

            // bind context and surface
            vgPrivMakeCurrentAM(vgContext, vgSurface);

            // Create OpenVGContext:
            vg = new OpenVGContext();

            // Apply scale for retina display:
            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_PATH_USER_TO_SURFACE);
            vg.LoadIdentity();
            vg.Scale((float)fbWidth / (float)width, (float)fbHeight / (float)height);
            vg.Translate(0.5f, 0.5f);

            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_GLYPH_USER_TO_SURFACE);
            vg.LoadIdentity();
            vg.Scale((float)fbWidth / (float)width, (float)fbHeight / (float)height);
            vg.Translate(0.5f, 0.5f);

            vg.Seti(ParamType.VG_MATRIX_MODE, (int)MatrixMode.VG_MATRIX_PATH_USER_TO_SURFACE);

            // Disable vsync and show frame immediately after render:
            Glfw.SwapInterval(0);

            Debug.WriteLine("glfw.ShowWindow(window)");
            Glfw.ShowWindow(window);

            Glfw.SetKeyCallback(window, handleKeys);
            Glfw.SetMouseButtonCallback(window, handleMouseButton);
            Glfw.SetCursorPosCallback(window, handleMousePos);
        }
コード例 #17
0
        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();
        }
コード例 #18
0
        public void Main(string[] args)
        {
            Init();

            var rand = new Random();

            if (!Glfw.Init())
            {
                Environment.Exit(1);
            }

            var window = Glfw.CreateWindow(640, 480, "Empty Event Test");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.MakeContextCurrent(window);
            Glfw.SetKeyCallback(window, KeyCallback);

            var thread = new Thread(new ThreadStart(ThreadMain));

            if (thread == null)
            {
                Log("Failed to create secondary thread");

                Glfw.Terminate();
                Environment.Exit(1);
            }

            thread.Start();

            while (running)
            {
                int   width, height;
                float r = rand.Next(), g = rand.Next(), b = rand.Next();
                float l = (float)Math.Sqrt(r * r + g * g + b * b);

                Glfw.GetFramebufferSize(window, out width, out height);

                Gl.Viewport(0, 0, width, height);
                Gl.ClearColor(r / l, g / l, b / l, 1f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);
                Glfw.SwapBuffers(window);

                Glfw.WaitEvents();

                if (Glfw.WindowShouldClose(window))
                {
                    running = false;
                }
            }

            Glfw.HideWindow(window);
            thread.Join();
            Glfw.DestroyWindow(window);

            Glfw.Terminate();
        }
コード例 #19
0
ファイル: GLFWProgram.cs プロジェクト: cs-phillips/PixelFarm
        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, 2);
            Glfw.WindowHint(WindowHint.GLFW_CONTEXT_VERSION_MINOR, 0);
            //---------------------------------------------------
            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();
        }