Exemple #1
0
        public static int GenerateTexture(string resourceName, Assembly a)
        {
            Stream resourceStream = a.GetManifestResourceStream(resourceName);

            return(HelperTexture.LoadTextureCompressedNoMipMap(resourceStream));
        }
Exemple #2
0
        /// <summary>
        /// Erstellt eine Skybox (oder Environment Map) für die aktuelle Szene als Bitmap
        /// </summary>
        /// <param name="resolution">Auflösung der Skybox (Standard: 4096)</param>
        public static void CreateSkyboxFromCurrentSceneAndExit(int resolution = 4096)
        {
            _resolution = HelperTexture.RoundDownToPowerOf2(resolution);
            KWEngine.CurrentWorld.AddRemoveObjects();

            CreateFramebuffer();
            InitFramebufferBloom();

            Matrix4 modelViewProjectionMatrixBloom = Matrix4.CreateScale(_resolution, _resolution, 1) * Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) * Matrix4.CreateOrthographic(_resolution, _resolution, 0.1f, 100f);

            Vector3 camPosition = KWEngine.CurrentWorld.GetCameraPositionEitherWay();

            _viewMatrixSkybox[0] = Matrix4.LookAt(camPosition, camPosition + new Vector3(1, 0, 0), new Vector3(0, -1, 0));  // right
            _viewMatrixSkybox[1] = Matrix4.LookAt(camPosition, camPosition + new Vector3(-1, 0, 0), new Vector3(0, -1, 0)); // left
            _viewMatrixSkybox[2] = Matrix4.LookAt(camPosition, camPosition + new Vector3(0, 1, 0), new Vector3(0, 0, 1));   // top
            _viewMatrixSkybox[3] = Matrix4.LookAt(camPosition, camPosition + new Vector3(0, -1, 0), new Vector3(0, 0, -1)); // bottom
            _viewMatrixSkybox[4] = Matrix4.LookAt(camPosition, camPosition + new Vector3(0, 0, 1), new Vector3(0, -1, 0));  // front
            _viewMatrixSkybox[5] = Matrix4.LookAt(camPosition, camPosition + new Vector3(0, 0, -1), new Vector3(0, -1, 0)); // back

            _projectionMatrixSkybox = Matrix4.CreatePerspectiveFieldOfView(
                MathHelper.DegreesToRadians(90),
                1,
                0.1f,
                10000);
            Bitmap[] bitmaps = new Bitmap[6];

            for (int s = 0; s < _viewMatrixSkybox.Length; s++)
            {
                KWEngine.CurrentWindow.DrawSceneForSkybox(_viewMatrixSkybox[s], _projectionMatrixSkybox, s);

                // Downsample framebuffer:
                GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _fbFull);
                GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, _fbDownsample);

                GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
                GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
                GL.BlitFramebuffer(0, 0, _resolution, _resolution, 0, 0, _resolution, _resolution, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);

                GL.ReadBuffer(ReadBufferMode.ColorAttachment1);
                GL.DrawBuffer(DrawBufferMode.ColorAttachment1);
                GL.BlitFramebuffer(0, 0, _resolution, _resolution, 0, 0, _resolution, _resolution, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear);

                // Draw bloom:
                GL.UseProgram(KWEngine.RendererBloomOld.GetProgramId());
                GL.Viewport(0, 0, _resolution, _resolution);
                int sourceTex; // this is the texture that the bloom will be read from
                for (int i = 0; i < 4; i++)
                {
                    if (i % 2 == 0)
                    {
                        if (i == 0)
                        {
                            HelperGeneral.SwitchToBufferAndClear(_fbBloom1);
                        }
                        else
                        {
                            GL.BindFramebuffer(FramebufferTarget.Framebuffer, _fbBloom1);
                        }
                        if (i == 0)
                        {
                            sourceTex = _tex1d;
                        }
                        else
                        {
                            sourceTex = _fbBloomTex1;
                        }
                    }
                    else
                    {
                        sourceTex = _fbBloomTex0;
                        GL.BindFramebuffer(FramebufferTarget.Framebuffer, _fbBloom2);
                    }

                    KWEngine.RendererBloomOld.DrawBloom(
                        KWEngine.KWRect,
                        ref modelViewProjectionMatrixBloom,
                        i % 2 == 0,
                        sourceTex
                        );
                }

                HelperGeneral.SwitchToBufferAndClear(_fbDownsample);
                GL.UseProgram(KWEngine.RendererMerge.GetProgramId());
                GL.Viewport(0, 0, _resolution, _resolution);
                KWEngine.RendererMerge.DrawMerge(KWEngine.KWRect, ref modelViewProjectionMatrixBloom, _tex0d, _fbBloomTex1);
                GL.UseProgram(0); // unload bloom shader program


                GL.BindFramebuffer(FramebufferTarget.Framebuffer, _fbDownsample);
                GL.ReadBuffer(ReadBufferMode.ColorAttachment0);
                bitmaps[s] = new Bitmap(_resolution, _resolution);
                BitmapData bd = bitmaps[s].LockBits(new Rectangle(0, 0, _resolution, _resolution), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                GL.ReadPixels(0, 0, _resolution, _resolution, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, bd.Scan0);
                bitmaps[s].UnlockBits(bd);
            }

            // make 6img:
            Rectangle srcRect     = new Rectangle(0, 0, _resolution, _resolution);
            Bitmap    finalBitmap = new Bitmap(_resolution * 4, _resolution * 3);

            // left?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[1], new Rectangle(0, _resolution, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            // front?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[4], new Rectangle(_resolution, _resolution, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            // right?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[0], new Rectangle(_resolution * 2, _resolution, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            // back?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[5], new Rectangle(_resolution * 3, _resolution, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            // top?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[2], new Rectangle(_resolution, 0, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            // bottom?:
            using (Graphics grD = Graphics.FromImage(finalBitmap))
            {
                grD.DrawImage(bitmaps[3], new Rectangle(_resolution, _resolution * 2, _resolution, _resolution), srcRect, GraphicsUnit.Pixel);
            }
            string fileaffix = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");

            finalBitmap.Save(fileaffix + "-environmentmap.png", ImageFormat.Png);
            finalBitmap.Dispose();


            DeleteFramebuffer();
            KWEngine.CurrentWindow.ForceClose();
        }