コード例 #1
0
ファイル: Framebuffer.cs プロジェクト: Lazzu/Hatzap
        void Initialize()
        {
            var textureQuality = new TextureQuality()
            {
                TextureWrapMode = TextureWrapMode.ClampToEdge,
                Filtering = TextureFiltering.Nearest,
            };

            TextureTarget target;

            int texID = GL.GenTexture();
            var msaa = GL.GetInteger(GetPName.MaxSamples);

            if (MSAA < 0)
                MSAA = 0;

            if (MSAA > msaa)
                MSAA = msaa;

            switch(MSAA)
            {
                case 1:
                case 2:
                case 4:
                case 8:
                case 16:
                case 32:
                case 64:
                case 128:
                case 256:
                    target = TextureTarget.Texture2DMultisample;

                    GL.BindTexture(target, texID);
                    GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, MSAA, PixelInternalFormat.Rgba, Width, Height, true);

                    break;
                default:
                    target = TextureTarget.Texture2D;

                    GL.BindTexture(target, texID);
                    GL.TexImage2D(target, 0, PixelInternalFormat.Rgba, Width, Height, 0, PixelFormat.Rgba, PixelType.Byte, IntPtr.Zero);

                    break;
            }

            GL.BindTexture(target, 0);

            Texture = new Texture(texID, Width, Height, target, PixelInternalFormat.Rgba);
            Texture.Quality = textureQuality;

            Texture.Bind();
            Texture.UpdateQuality();
            Texture.UnBind();

            /*texture.Bind();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); // automatic mipmap
            texture.UnBind();*/

            // create a renderbuffer object to store depth info
            GL.GenRenderbuffers(1, out rboId);
            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rboId);

            if(MSAA > 0)
            {
                GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, MSAA, RenderbufferStorage.DepthComponent32, Width, Height);
            }
            else
            {
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent32, Width, Height);
            }

            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);

            // create a framebuffer object
            GL.GenFramebuffers(1, out fbo);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);

            // attach the texture to FBO color attachment point
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,        // 1. fbo target: GL_FRAMEBUFFER
                                   FramebufferAttachment.ColorAttachment0,  // 2. attachment point
                                   Texture.TextureTarget,         // 3. tex target: GL_TEXTURE_2D
                                   Texture.ID,             // 4. tex ID
                                   0);                    // 5. mipmap level: 0(base)

            // attach the renderbuffer to depth attachment point
            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer,      // 1. fbo target: GL_FRAMEBUFFER
                                      FramebufferAttachment.DepthAttachment, // 2. attachment point
                                      RenderbufferTarget.Renderbuffer,     // 3. rbo target: GL_RENDERBUFFER
                                      rboId);              // 4. rbo ID

            // check FBO status
            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                Complete = false;
                Console.WriteLine("ERROR: Can not create framebuffer because " + status.ToString());
            }
            else
            {
                Complete = true;
            }

            // switch back to window-system-provided framebuffer
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            if (!Complete)
                return;

            var error = GL.GetError();

            while(error != ErrorCode.NoError)
            {
                Debug.WriteLine("Error creating FBO: " + error.ToString());
                error = GL.GetError();
            }

            batch = new VertexBatch();
            batch.StartBatch(PrimitiveType.Triangles);
            batch.Add(new Vector3(-1, -1, 0)); // Bottom left
            batch.Add(new Vector3(1, -1, 0));  // Bottom right
            batch.Add(new Vector3(-1, 1, 0));  // Top left
            batch.Add(new Vector3(1, -1, 0));  // Bottom right
            batch.Add(new Vector3(1, 1, 0)); // Top right
            batch.Add(new Vector3(-1, 1, 0));  // Top left
            batch.EndBatch();
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: Lazzu/Hatzap
        private void MenuImportTexture_Click(object sender, RoutedEventArgs e)
        {
            //workQueue.Enqueue((bw) =>
            //{
                // Configure open file dialog box
                Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
                openDlg.FileName = ""; // Default file name
                //openDlg.DefaultExt = "*.png,*.jpg,*.bmp"; // Default file extension
                openDlg.Filter = "All supported texture files |*.png;*.jpg;*.bmp"; // Filter files by extension

                // Show open file dialog box
                Nullable<bool> result = openDlg.ShowDialog();

                // Process open file dialog box results
                if (result != true)
                    return;

                var filename = openDlg.FileName;

                var bmp = new Bitmap(filename);

                //bw.ReportProgress(33);

                settingsDialog = new TextureSettingsWindow();
                settingsDialog.TextureWidth = bmp.Width;
                settingsDialog.TextureHeight = bmp.Height;

                settingsDialog.ShowDialog();

                if (!settingsDialog.OKClicked)
                    return;

                var mipmapped = settingsDialog.Mipmapped;
                var preMipmapped = mipmapped && settingsDialog.MipmapsSaved;
                var compressed = settingsDialog.Compressed;
                var compressedSaved = compressed && settingsDialog.CompressionSaved;
                var format = (PixelInternalFormat)settingsDialog.PixelInternalFormat;
                var filtering = settingsDialog.TextureFiltering;
                var pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                var anisolevel = settingsDialog.AnisoLevel;

                switch (format)
                {
                    case PixelInternalFormat.Rgba:
                        format = compressed ? PixelInternalFormat.CompressedRgbaS3tcDxt5Ext : format;
                        break;
                    case PixelInternalFormat.SrgbAlpha:
                        format = compressed ? PixelInternalFormat.CompressedSrgbAlphaS3tcDxt5Ext : format;
                        break;
                    case PixelInternalFormat.Rgb:
                        pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgb;
                        format = compressed ? PixelInternalFormat.CompressedRgbS3tcDxt1Ext : format;
                        break;
                    case PixelInternalFormat.Srgb:
                        pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rgb;
                        format = compressed ? PixelInternalFormat.CompressedSrgbS3tcDxt1Ext : format;
                        break;
                    case PixelInternalFormat.Rg8:
                        pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Rg;
                        format = compressed ? PixelInternalFormat.CompressedRg : format;
                        break;
                    case PixelInternalFormat.R8:
                        pixelFormat = OpenTK.Graphics.OpenGL.PixelFormat.Red;
                        format = compressed ? PixelInternalFormat.CompressedRedRgtc1 : format;
                        break;
                }

                // Load the texture temporarily in to GPU
                Texture texture = new Texture();

                texture.Quality = new TextureQuality()
                {
                    Filtering = filtering,
                    Mipmaps = mipmapped,
                    PregeneratedMipmaps = false,
                    TextureWrapMode = OpenTK.Graphics.OpenGL.TextureWrapMode.Clamp,
                    Anisotrophy = anisolevel,
                };

                // Set meta settings
                var meta = texture.Metadata;
                meta.PreMipmapped = false;
                meta.Precompressed = false;
                meta.PixelFormat = pixelFormat;
                meta.PixelInternalFormat = format;

                texture.Load(bmp, format);
                textures.Insert("tmp", texture);

                //bw.ReportProgress(33);

                // Apply settings (generate possible mipmaps etc)
                texture.UpdateQuality();
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                GL.Finish();

                meta.PreMipmapped = preMipmapped;
                meta.Precompressed = compressedSaved;
                meta.Quality.PregeneratedMipmaps = preMipmapped;

                //bw.ReportProgress(33);

                filename = System.IO.Path.GetFileNameWithoutExtension(filename);
                filename += ".tex";

                // Configure save file dialog box
                Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
                saveDlg.FileName = filename; // Default file name
                saveDlg.DefaultExt = ".tex"; // Default file extension
                saveDlg.Filter = "Hatzap texture files|*.tex"; // Filter files by extension

                // Show save file dialog box
                result = saveDlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    filename = saveDlg.FileName;

                    meta.FileName = filename;

                    textures.Save("tmp", filename);

                    texture.Release();
                    textures.Remove("tmp");
                }

                //bw.ReportProgress(1);

            //});

            //backgroundWorker.RunWorkerAsync();
        }