Пример #1
0
 private void CloseWindow()
 {
     if (window.Equals(GlfwWindowPtr.Null))
     {
         return;
     }
     Glfw.SetWindowShouldClose(window, true);
     onWindowClosed.Fire(this);
 }
Пример #2
0
        // the pragram starts here
        private static void AppMain()
        {
            // initialize GLFW
            if (!Glfw.Init())
            {
                throw new Exception("glfwInit failed");
            }

            // open a window with GLFW
            Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
            Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
            Glfw.WindowHint(WindowHint.ContextVersionMinor, 2);
            _window = Glfw.CreateWindow(ScreenSize.X, ScreenSize.Y, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            if (_window.Equals(GlfwWindowPtr.Null))
            {
                throw new Exception("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
            }
            Glfw.MakeContextCurrent(_window);

            // TODO: GLEW in C#

            // print out some info about the graphics drivers
            Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version));
            Console.WriteLine("GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
            Console.WriteLine("Vendor: {0}", GL.GetString(StringName.Vendor));
            Console.WriteLine("Renderer: {0}", GL.GetString(StringName.Renderer));

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // load vertex and fragment shaders into opengl
            LoadShaders();

            // load the texture
            LoadTexture();

            // create buffer and fill it with the points of the triangle
            LoadCube();

            double lastTime = Glfw.GetTime();

            // run while window is open
            while (!Glfw.WindowShouldClose(_window))
            {
                // update the scene based on the time elapsed since last update
                double thisTime = Glfw.GetTime();
                Update(thisTime - lastTime);
                lastTime = thisTime;

                // draw one frame
                Render();
            }

            // clean up and exit
            Glfw.Terminate();
        }
Пример #3
0
        // the pragram starts here
        private static void AppMain()
        {
            // initialize GLFW
            if (!Glfw.Init())
            {
                throw new Exception("glfwInit failed");
            }

            // open a window with GLFW
            Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
            Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
            Glfw.WindowHint(WindowHint.ContextVersionMinor, 2);
            _window = Glfw.CreateWindow(ScreenSize.X, ScreenSize.Y, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            if (_window.Equals(GlfwWindowPtr.Null))
            {
                throw new Exception("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
            }
            Glfw.MakeContextCurrent(_window);

            // GLFW settings
            Glfw.SetInputMode(_window, InputMode.CursorMode, CursorMode.CursorHidden | CursorMode.CursorCaptured);
            Glfw.SetCursorPos(_window, 0, 0);
            Glfw.SetScrollCallback(_window, new GlfwScrollFun((win, x, y) => {
                //increase or decrease field of view based on mouse wheel
                float zoomSensitivity = -0.2f;
                float fieldOfView     = _gCamera.FieldOfView + zoomSensitivity * (float)y;
                if (fieldOfView < 5.0f)
                {
                    fieldOfView = 5.0f;
                }
                if (fieldOfView > 130.0f)
                {
                    fieldOfView = 130.0f;
                }
                _gCamera.FieldOfView = fieldOfView;
            }));

            // TODO: GLEW in C#

            // print out some info about the graphics drivers
            Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version));
            Console.WriteLine("GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
            Console.WriteLine("Vendor: {0}", GL.GetString(StringName.Vendor));
            Console.WriteLine("Renderer: {0}", GL.GetString(StringName.Renderer));

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // load vertex and fragment shaders into opengl
            LoadShaders();

            // load the texture
            LoadTexture();

            // create buffer and fill it with the points of the triangle
            LoadCube();

            _gCamera.Position            = new Vector3(0, 0, 4);
            _gCamera.ViewportAspectRatio = (float)ScreenSize.X / (float)ScreenSize.Y;

            double lastTime = Glfw.GetTime();

            // run while window is open
            while (!Glfw.WindowShouldClose(_window))
            {
                // update the scene based on the time elapsed since last update
                double thisTime = Glfw.GetTime();
                Update(thisTime - lastTime);
                lastTime = thisTime;

                // draw one frame
                Render();
                //exit program if escape key is pressed
                if (Glfw.GetKey(_window, Key.Escape))
                {
                    Glfw.SetWindowShouldClose(_window, true);
                }
            }

            // clean up and exit
            Glfw.Terminate();
        }
Пример #4
0
        // the pragram starts here
        private static void AppMain()
        {
            // initialize GLFW
            if (!Glfw.Init())
            {
                throw new Exception("glfwInit failed");
            }

            // open a window with GLFW
            Glfw.WindowHint(WindowHint.OpenGLProfile, (int)OpenGLProfile.Core);
            Glfw.WindowHint(WindowHint.ContextVersionMajor, 3);
            Glfw.WindowHint(WindowHint.ContextVersionMinor, 2);
            _window = Glfw.CreateWindow(ScreenSize.X, ScreenSize.Y, "", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
            if (_window.Equals(GlfwWindowPtr.Null))
            {
                throw new Exception("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?");
            }
            Glfw.MakeContextCurrent(_window);

            // GLFW settings
            Glfw.SetInputMode(_window, InputMode.CursorMode, CursorMode.CursorHidden | CursorMode.CursorCaptured);
            Glfw.SetCursorPos(_window, 0, 0);
            Glfw.SetScrollCallback(_window, new GlfwScrollFun((win, x, y) => {
                //increase or decrease field of view based on mouse wheel
                float zoomSensitivity = -0.2f;
                float fieldOfView     = _gCamera.FieldOfView + zoomSensitivity * (float)y;
                if (fieldOfView < 5.0f)
                {
                    fieldOfView = 5.0f;
                }
                if (fieldOfView > 130.0f)
                {
                    fieldOfView = 130.0f;
                }
                _gCamera.FieldOfView = fieldOfView;
            }));

            // TODO: GLEW in C#

            // print out some info about the graphics drivers
            Console.WriteLine("OpenGL version: {0}", GL.GetString(StringName.Version));
            Console.WriteLine("GLSL version: {0}", GL.GetString(StringName.ShadingLanguageVersion));
            Console.WriteLine("Vendor: {0}", GL.GetString(StringName.Vendor));
            Console.WriteLine("Renderer: {0}", GL.GetString(StringName.Renderer));

            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);

            // initialise the gWoodenCrate asset
            LoadWoodenCrateAsset();

            // create all the instances in the 3D scene based on the gWoodenCrate asset
            CreateInstances();

            _gCamera.Position            = new Vector3(-4, 0, 17);
            _gCamera.ViewportAspectRatio = (float)ScreenSize.X / (float)ScreenSize.Y;
            _gCamera.SetNearAndFarPlanes(0.5f, 100.0f);

            // setup light
            _gLight = new Light()
            {
                Position           = _gCamera.Position,
                Intensities        = new Vector3(1, 1, 1), // white
                Attenuation        = 0.2f,
                AmbientCoefficient = 0.005f
            };

            float lastTime = (float)Glfw.GetTime();

            // run while window is open
            while (!Glfw.WindowShouldClose(_window))
            {
                // update the scene based on the time elapsed since last update
                float thisTime = (float)Glfw.GetTime();
                Update(thisTime - lastTime);
                lastTime = thisTime;

                // draw one frame
                Render();
                //exit program if escape key is pressed
                if (Glfw.GetKey(_window, Key.Escape))
                {
                    Glfw.SetWindowShouldClose(_window, true);
                }
            }

            // clean up and exit
            Glfw.Terminate();
        }
Пример #5
0
        public static void Main(string[] args)
        {
            try
            {
                if (Glfw.Init() == false)
                {
                    Console.Error.WriteLine("Failed to initialize GLFW!");
                    Environment.Exit(1);
                }

                try
                {
                    Glfw.SetErrorCallback((code, des) =>
                    {
                        Console.Error.WriteLine("ERROR ({0}): {1}", code, des);
                    });

                    // Create GLFW window
                    GlfwWindowPtr window = Glfw.CreateWindow(width, height, "FontStash.net-Pencil.Gaming.GLFW3", GlfwMonitorPtr.Null, GlfwWindowPtr.Null);
                    if (window.Equals(GlfwWindowPtr.Null))
                    {                     // Does this line actually work???
                        Console.Error.WriteLine("ERROR: Failed to create GLFW window, shutting down");
                        Environment.Exit(1);
                    }

                    Glfw.SetKeyCallback(window, key);
                    // Enable the OpenGL context for the current window
                    Glfw.MakeContextCurrent(window);

                    //Glfw.SetWindowTitle(window, "This is a GLFW window!");

                    Glfw.SwapInterval(1);
                    Glfw.WindowHint(WindowHint.Samples, 2);                     // Turns on 2× mutlisampling

                    fontNormal = FontStash.FONS_INVALID;

                    fs = GlFontStash.glfonsCreate(512, 512, FONSflags.FONS_ZERO_TOPLEFT);

                    fontNormal = FontStash.fonsAddFont(fs, "sans", "DroidSerif-Regular.ttf");
                    if (fontNormal == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font normal.\n");
                    }

                    fontItalic = FontStash.fonsAddFont(fs, "sans-italic", "DroidSerif-Italic.ttf");
                    if (fontItalic == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font italic.\n");
                    }
                    fontBold = FontStash.fonsAddFont(fs, "sans-bold", "DroidSerif-Bold.ttf");
                    if (fontBold == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font bold.\n");
                    }
                    fontJapanese = FontStash.fonsAddFont(fs, "sans-jp", "DroidSansJapanese.ttf");
                    if (fontJapanese == FontStash.FONS_INVALID)
                    {
                        throw new Exception("Could not add font japanese.\n");
                    }


                    Glfw.PollEvents();                     // Get events
                    while (!Glfw.WindowShouldClose(window))
                    {
                        // Poll GLFW window events
                        Glfw.PollEvents();

                        float sx, sy, dx, dy, lh = 0, pf1 = 0, pf2 = 0;
                        uint  white, black, brown, blue;

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

                        // Update and render
                        GL.Viewport(0, 0, width, height);
                        GL.ClearColor(0.3f, 0.3f, 0.32f, 1.0f);
                        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                        GL.Enable(EnableCap.Blend);
                        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                        GL.Disable(EnableCap.Texture2D);
                        GL.MatrixMode(MatrixMode.Projection);
                        GL.LoadIdentity();
                        GL.Ortho(0, width, height, 0, -1, 1);

                        GL.MatrixMode(MatrixMode.Modelview);
                        GL.LoadIdentity();
                        GL.Disable(EnableCap.DepthTest);
                        GL.Color4(255f, 255f, 255f, 255f);

                        GL.Enable(EnableCap.Blend);
                        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                        GL.Enable(EnableCap.CullFace);

                        white = GlFontStash.glfonsRGBA(255, 255, 255, 255);
                        brown = GlFontStash.glfonsRGBA(192, 128, 0, 128);
                        blue  = GlFontStash.glfonsRGBA(0, 192, 255, 255);
                        black = GlFontStash.glfonsRGBA(0, 0, 0, 255);

                        sx = 50;
                        sy = 50;

                        dx = sx;
                        dy = sy;

                        dash(dx, dy);

                        FontStash.fonsClearState(ref fs);

                        FontStash.fonsSetSize(ref fs, 124.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        pf1 = 0;
                        pf2 = 0;
                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh;
                        dash(dx, dy);

                        FontStash.fonsSetSize(ref fs, 124.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "The quick ");

                        FontStash.fonsSetSize(ref fs, 48.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, brown);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "brown ");

                        FontStash.fonsSetSize(ref fs, 24.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "fox ");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "jumps over ");
                        FontStash.fonsSetFont(ref fs, fontBold);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "the lazy ");
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        dx = FontStash.fonsDrawText(ref fs, dx, dy, "dog.");

                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetSize(ref fs, 12.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, blue);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Now is the time for all good men to come to the aid of the party.");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f * 2;
                        dash(dx, dy);
                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Ég get etið gler án þess að meiða mig.");

                        FontStash.fonsVertMetrics(ref fs, ref pf1, ref pf2, ref lh);
                        dx  = sx;
                        dy += lh * 1.2f;
                        dash(dx, dy);
                        FontStash.fonsSetFont(ref fs, fontJapanese);
                        FontStash.fonsDrawText(ref fs, dx, dy, "私はガラスを食べられます。それは私を傷つけません。");

                        // Font alignment
                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontNormal);
                        FontStash.fonsSetColor(ref fs, white);

                        dx = 50;
                        dy = 350;
                        line(dx - 10, dy, dx + 250, dy);
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_TOP);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Top");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_MIDDLE);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Middle");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);
                        dx  = FontStash.fonsDrawText(ref fs, dx, dy, "Baseline");
                        dx += 10;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BOTTOM);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Bottom");

                        dx = 150;
                        dy = 400;
                        line(dx, dy - 30, dx, dy + 80.0f);
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Left");
                        dy += 30;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_CENTER | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Center");
                        dy += 30;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_RIGHT | FONSalign.FONS_ALIGN_BASELINE);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Right");

                        // Blur
                        dx = 500;
                        dy = 350;
                        FontStash.fonsSetAlign(fs, FONSalign.FONS_ALIGN_LEFT | FONSalign.FONS_ALIGN_BASELINE);

                        FontStash.fonsSetSize(ref fs, 60.0f);
                        FontStash.fonsSetFont(ref fs, fontItalic);
                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsSetSpacing(ref fs, 5.0f);
                        FontStash.fonsSetBlur(ref fs, 10.0f);
                        FontStash.fonsDrawText(ref fs, dx, dy, "Blurry...");

                        dy += 50.0f;

                        FontStash.fonsSetSize(ref fs, 18.0f);
                        FontStash.fonsSetFont(ref fs, fontBold);
                        FontStash.fonsSetColor(ref fs, black);
                        FontStash.fonsSetSpacing(ref fs, 0.0f);
                        FontStash.fonsSetBlur(ref fs, 3.0f);
                        FontStash.fonsDrawText(ref fs, dx, dy + 2, "DROP THAT SHADOW");

                        FontStash.fonsSetColor(ref fs, white);
                        FontStash.fonsSetBlur(ref fs, 0);
                        FontStash.fonsDrawText(ref fs, dx, dy, "DROP THAT SHADOW");

                        if (debug)
                        {
                            FontStash.fonsDrawDebug(fs, 800.0f, 50.0f);
                        }


                        GL.Enable(EnableCap.DepthTest);

                        Glfw.SwapBuffers(window);
                    }
                }
                finally
                {
                    GlFontStash.glfonsDelete(fs);
                }
            }
            finally
            {
                Glfw.Terminate();
            }
        }