예제 #1
0
        public void Load_Texture2D(Bitmap Image)
        {
            try
            {
                if (Size != Size.Empty)
                {
                    Image = new Bitmap(Image, Size);
                }

                Free();

                ID = GL.GenTexture();
                GL.BindTexture(TextureTarget, ID);

                // Отображаем текстуру по Y
                Image.RotateFlip(RotateFlipType.RotateNoneFlipY);

                BitmapData data = Image.LockBits(new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
                                                 ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                GL.TexImage2D(TextureTarget, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                              PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

                Image.UnlockBits(data);
                data = null;

                //Filter parameters
                GL.TexParameter(TextureTarget, TextureParameterName.TextureMagFilter, (int)MagFilter);
                GL.TexParameter(TextureTarget, TextureParameterName.TextureMinFilter, (int)MinFilter);

                if (AnisotropicFiltering && Glfw.ExtensionSupported("GL_EXT_texture_filter_anisotropic"))
                {
                    int MaxAniso = GL.GetInteger((GetPName)ExtTextureFilterAnisotropic.MaxTextureMaxAnisotropyExt);
                    if (Settings.Graphics.AnisotropicFiltering != -1)
                    {
                        MaxAniso = Math.Min(MaxAniso, Settings.Graphics.AnisotropicFiltering);
                    }
                    GL.TexParameter(TextureTarget, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, MaxAniso);
                }

                GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapS, (int)WrapS);
                GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapT, (int)WrapT);
                GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapR, (int)WrapR);
                GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)EnvironmentMode);

                if (Mipmaps)
                {
                    GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                }
            }
            catch (Exception e)
            {
                Log.WriteLineRed("Error: Cannot load texture. Name: \"{0}\".", Name);
                Log.WriteLineYellow(e.Message);
                Free();
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            Init();

            var samples = 4;

            Parser.Default.ParseArguments <Options>(args).WithParsed(options =>
            {
                if (options.Samples > -1)
                {
                    samples = options.Samples;
                }
            });

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

            if (samples > 0)
            {
                Log("Requesting MSAA with {0} samples", samples);
            }
            else
            {
                Log("Requesting that MSAA not be available");
            }

            Glfw.WindowHint(Glfw.Hint.Samples, samples);
            Glfw.WindowHint(Glfw.Hint.Visible, false);

            var window = Glfw.CreateWindow(800, 400, "Aliasing Detector");

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

            Glfw.SetKeyCallback(window, KeyCallback);
            Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);

            Glfw.MakeContextCurrent(window);
            Glfw.SwapInterval(1);

            if (Glfw.ExtensionSupported("ARB_multisample"))
            {
                Log("Multisampling is not supported");
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.ShowWindow(window);

            Gl.Get(GetPName.Samples, out int s);

            if (s > 1)
            {
                Log("Context reports MSAA is available with {0} samples", samples);
            }
            else
            {
                Log("Context reports MSAA is unavailable");
            }

            Gl.MatrixMode(MatrixMode.Projection);
            Gl.Ortho(0.0, 1.0, 0.0, 0.5, 0.0, 1.0);
            Gl.MatrixMode(MatrixMode.Modelview);

            while (!Glfw.WindowShouldClose(window))
            {
                var time = (float)Glfw.GetTime();

                Gl.Clear(ClearBufferMask.ColorBufferBit);

                Gl.LoadIdentity();
                Gl.Translate(0.25f, 0.25f, 0f);
                Gl.Rotate(time, 0f, 0f, 1f);

                Gl.Disable(EnableCap.Multisample);
                Gl.Rect(-0.15f, -0.15f, 0.15f, 0.15f);

                Gl.LoadIdentity();
                Gl.Translate(0.75f, 0.25f, 0f);
                Gl.Rotate(time, 0f, 0f, 1f);

                Gl.Enable(EnableCap.Multisample);
                Gl.Rect(-0.15f, -0.15f, 0.15f, 0.15f);

                Glfw.SwapBuffers(window);
                Glfw.PollEvents();
            }

            Glfw.Terminate();
        }
예제 #3
0
        static void Main(string[] args)
        {
            Init();

            var options = new Options();

            int    width, height;
            float  position;
            ulong  frameCount = 0;
            double lastTime, currentTime;
            bool   fullscreen = false;
            var    monitor    = Glfw.Monitor.None;

            Glfw.Window window;

            if (Parser.Default.ParseArguments(args, options))
            {
                fullscreen = options.Fullscreen;
            }

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

            if (fullscreen)
            {
                monitor = Glfw.GetPrimaryMonitor();
                var mode = Glfw.GetVideoMode(monitor);

                Glfw.WindowHint(Glfw.Hint.RedBits, mode.RedBits);
                Glfw.WindowHint(Glfw.Hint.GreenBits, mode.GreenBits);
                Glfw.WindowHint(Glfw.Hint.BlueBits, mode.BlueBits);
                Glfw.WindowHint(Glfw.Hint.RefreshRate, mode.RefreshRate);

                width  = mode.Width;
                height = mode.Height;
            }
            else
            {
                width  = 640;
                height = 480;
            }

            window = Glfw.CreateWindow(width, height, "", monitor, Glfw.Window.None);
            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(1);
            }

            Glfw.MakeContextCurrent(window);
            SetSwapInterval(window, 0);

            lastTime  = Glfw.GetTime();
            frameRate = 0.0;
            swapTear  = (Glfw.ExtensionSupported("WGL_EXT_swap_control_tear") ||
                         Glfw.ExtensionSupported("GLX_EXT_swap_control_tear"));

            Glfw.SetFramebufferSizeCallback(window, FramebufferSizeCallback);
            Glfw.SetKeyCallback(window, KeyCallback);

            Gl.MatrixMode(MatrixMode.Projection);
            Gl.Ortho(-1f, 1f, -1f, 1f, 1f, -1f);
            Gl.MatrixMode(MatrixMode.Modelview);

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

                position = (float)System.Math.Cos(Glfw.GetTime() * 4f) * 0.75f;
                Gl.Rect(position - 0.25f, -1f, position + 0.25f, 1f);

                Glfw.SwapBuffers(window);
                Glfw.PollEvents();

                frameCount++;

                currentTime = Glfw.GetTime();
                if (currentTime - lastTime > 1.0)
                {
                    frameRate  = frameCount / (currentTime - lastTime);
                    frameCount = 0;
                    lastTime   = currentTime;
                    UpdateWindowTitle(window);
                }
            }

            Glfw.Terminate();
        }
예제 #4
0
        public void Load_TextureCubemap(Bitmap[] CubemapTextures)
        {
            try
            {
                if (CubemapTextures.Length == 6)
                {
                    Free();

                    ID = GL.GenTexture();
                    GL.BindTexture(TextureTarget, ID);

                    // Изменяем, отображаем и вращаем текстуры
                    for (int i = 0; i < CubemapTextures.Length; i++)
                    {
                        if (Size != Size.Empty)
                        {
                            CubemapTextures[i] = new Bitmap(CubemapTextures[i], Size);
                        }

                        if (i < 2 || i > 3)
                        {
                            CubemapTextures[i].RotateFlip(RotateFlipType.RotateNoneFlipX);
                        }
                        if (i == 2 || i == 3)
                        {
                            CubemapTextures[i].RotateFlip(RotateFlipType.Rotate270FlipY);
                        }
                        if (i == 2)
                        {
                            CubemapTextures[i].RotateFlip(RotateFlipType.Rotate180FlipNone);
                        }
                    }

                    TextureTarget[] targets = new TextureTarget[]
                    {
                        TextureTarget.TextureCubeMapPositiveX, TextureTarget.TextureCubeMapNegativeX,
                        TextureTarget.TextureCubeMapNegativeY, TextureTarget.TextureCubeMapPositiveY,
                        TextureTarget.TextureCubeMapPositiveZ, TextureTarget.TextureCubeMapNegativeZ
                    };

                    BitmapData data;
                    // Загружаем все текстуры граней
                    for (int i = 0; i < CubemapTextures.Length; i++)
                    {
                        data = CubemapTextures[i].LockBits(new System.Drawing.Rectangle(0, 0, CubemapTextures[i].Width,
                                                                                        CubemapTextures[i].Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        GL.TexImage2D(targets[5 - i], 0, PixelInternalFormat.Rgba8, data.Width, data.Height, 0,
                                      PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
                        CubemapTextures[i].UnlockBits(data);
                    }
                    data = null;

                    //Filter parameters
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureMagFilter, (int)MagFilter);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureMinFilter, (int)MinFilter);

                    if (AnisotropicFiltering && Glfw.ExtensionSupported("GL_EXT_texture_filter_anisotropic"))
                    {
                        int MaxAniso = GL.GetInteger((GetPName)ExtTextureFilterAnisotropic.MaxTextureMaxAnisotropyExt);
                        if (Settings.Graphics.AnisotropicFiltering != -1)
                        {
                            MaxAniso = Math.Min(MaxAniso, Settings.Graphics.AnisotropicFiltering);
                        }
                        GL.TexParameter(TextureTarget, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, MaxAniso);
                    }

                    GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapS, (int)WrapS);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapT, (int)WrapT);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapR, (int)WrapR);
                    GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)EnvironmentMode);

                    if (Mipmaps)
                    {
                        GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);
                    }
                }
                else
                {
                    throw new Exception("CubemapTextures Count != 6");
                }
            }
            catch (Exception e)
            {
                Log.WriteLineRed("Error: Cannot load CubemapTexture. Name: \"{0}\".", Name);
                Log.WriteLineYellow(e.Message);
                Free();
            }
        }