internal static void SaveDepthMapToBitmap(int texId) { Bitmap b = new Bitmap(KWEngine.ShadowMapSize, KWEngine.ShadowMapSize, System.Drawing.Imaging.PixelFormat.Format24bppRgb); float[] depthData = new float[KWEngine.ShadowMapSize * KWEngine.ShadowMapSize]; GL.BindTexture(TextureTarget.Texture2D, texId); GL.GetTexImage(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL4.PixelFormat.DepthComponent, PixelType.Float, depthData); GL.BindTexture(TextureTarget.Texture2D, 0); HelperGeneral.ScaleToRange(0, 255, depthData); int x = 0, y = KWEngine.ShadowMapSize - 1; for (int i = 0; i < depthData.Length; i++) { int rgb = (int)(depthData[i]); b.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb)); int prevX = x; x = (x + 1) % KWEngine.ShadowMapSize; if (prevX > 0 && x == 0) { y--; } } b.Save("texture2d_depth.bmp", ImageFormat.Bmp); b.Dispose(); }
/// <summary> /// Gibt die am nächsten liegende (vom Mauszeiger überlagerte) GameObject-Instanz inkl. der genauen Mausposition auf dem Objekt zurück /// </summary> /// <typeparam name="T">Beliebige Unterklasse von GameObject</typeparam> /// <param name="ms">Aktueller Mausstatus</param> /// <param name="intersectionPoint">Punkt, an dem der Mauszeiger auf das Objekt trifft</param> /// <param name="offsetX">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="offsetY">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="precision">Präzision der Messung (Standard: Box für genauere Messung)</param> /// <returns>Die GameObject-Instanz, die der Kamera am nächsten ist</returns> public static T IsMouseCursorOnAny <T>(MouseState ms, out Vector3 intersectionPoint, int offsetX = 0, int offsetY = 0, MouseIntersectionPrecision precision = MouseIntersectionPrecision.Box) where T : GameObject { intersectionPoint = Vector3.Zero; GameObject[] list = KWEngine.CurrentWorld._gameObjects.FindAll(go => go is T).ToArray(); if (list.Length == 0) { return(null); } Vector2 mc = HelperGeneral.GetNormalizedMouseCoords(ms.X + offsetX, ms.Y + offsetY); Vector3 worldRay = KWEngine.CurrentWindow.Get3DMouseCoords(mc.X, mc.Y); Vector3 origin; if (KWEngine.CurrentWorld != null && KWEngine.CurrentWorld.IsFirstPersonMode) { if (KWEngine.Projection == ProjectionType.Perspective) { origin = KWEngine.CurrentWorld.GetFirstPersonObject().Position; origin.Y += KWEngine.CurrentWorld.GetFirstPersonObject().FPSEyeOffset; } else { origin = HelperGeneral.GetRayOriginForOrthographicProjection(mc); } } else { if (KWEngine.Projection == ProjectionType.Perspective) { origin = KWEngine.CurrentWorld.GetCameraPosition(); } else { origin = HelperGeneral.GetRayOriginForOrthographicProjection(mc); } } float minDistance = float.MaxValue; int minIndex = -1; for (int i = 0; i < list.Length; i++) { bool rayHitGameObject = GetRayIntersectionPointOnHitbox(list[i], origin, worldRay, out Vector3 iPoint, precision); if (rayHitGameObject) { float currentDistance = (origin - iPoint).LengthSquared; if (currentDistance < minDistance) { intersectionPoint = iPoint; minDistance = currentDistance; minIndex = i; } } } if (minIndex >= 0) { return(list[minIndex] as T); } return(null); }
/// <summary> /// Gibt eine Liste von GameObject-Instanzen zurück, die unter dem Mauszeiger liegen (Instanzen müssen mit IsPickable = true gesetzt haben) /// </summary> /// <param name="ms">Mausinformationen</param> /// <param name="offsetX">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="offsetY">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <returns>Liste betroffener GameObject-Instanzen</returns> public static List <GameObject> PickGameObjects(MouseState ms, int offsetX = 0, int offsetY = 0) { List <GameObject> pickedObjects = new List <GameObject>(); GLWindow w = GLWindow.CurrentWindow; if (w == null || w.CurrentWorld == null || !w.Focused) { return(pickedObjects); } Vector2 mouseCoords = HelperGeneral.GetNormalizedMouseCoords(ms.X + offsetX, ms.Y + offsetY); Vector3 ray = KWEngine.CurrentWindow.Get3DMouseCoords(mouseCoords.X, mouseCoords.Y); Vector3 pos; if (KWEngine.Projection == ProjectionType.Perspective) { pos = w.CurrentWorld.GetCameraPosition() + ray; } else { pos = HelperGeneral.GetRayOriginForOrthographicProjection(mouseCoords) + ray; } foreach (GameObject go in w.CurrentWorld.GetGameObjects()) { if (go.IsPickable && go.IsInsideScreenSpace) { if (GameObject.IntersectRaySphere(pos, ray, go.GetCenterPointForAllHitboxes(), go.GetMaxDiameter() / 2)) { pickedObjects.Add(go); } } } return(pickedObjects); }
public static int LoadTextureForModelGLB(byte[] rawTextureData) { int texID; if (rawTextureData[0] == 0x44 && rawTextureData[1] == 0x44 && rawTextureData[2] == 0x53) { HelperDDS2.TryLoadDDS(rawTextureData, false, out texID, out int width, out int height); return(texID); } try { using (MemoryStream ms = new MemoryStream(rawTextureData)) { Bitmap image = new Bitmap(ms); if (image == null) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelGLB()", "Could not load image file from GLB!"); return(-1); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } } catch (Exception ex) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelGLB()", "Could not load image file! Make sure to copy it to the correct output directory. " + "[" + ex.Message + "]"); return(-1); } return(texID); }
public static int LoadTextureForModelExternal(string filename) { if (!File.Exists(filename)) { return(-1); } if (filename.ToLower().EndsWith("dds")) { return(LoadTextureCompressedWithMipMaps(filename)); } int texID; try { Bitmap image = new Bitmap(filename); if (image == null) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelExternal()", "File " + filename + " is not a valid image file."); return(-1); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } catch (Exception ex) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelExternal()", "Could not load image file " + filename + "! Make sure to copy it to the correct output directory. " + "[" + ex.Message + "]"); return(-1); } return(texID); }
/// <summary> /// Berechnet die Position eines Punkts, der um einen angegeben Punkt entlang einer Achse rotiert wird /// </summary> /// <param name="point">Mittelpunkt der Rotation</param> /// <param name="distance">Distanz zum Mittelpunkt</param> /// <param name="degrees">Grad der Rotation</param> /// <param name="plane">Achse der Rotation (Standard: Y)</param> /// <returns>Position des rotierten Punkts</returns> public static Vector3 CalculateRotationAroundPointOnAxis(Vector3 point, float distance, float degrees, Plane plane = Plane.Y) { float radians = MathHelper.DegreesToRadians(degrees % 360); Matrix4.CreateTranslation(ref point, out translationPointMatrix); if (plane == Plane.X) { Matrix4.CreateRotationX(radians, out rotationMatrix); Matrix4.CreateTranslation(0, 0, distance, out translationMatrix); } else if (plane == Plane.Y) { Matrix4.CreateRotationY(radians, out rotationMatrix); Matrix4.CreateTranslation(0, 0, distance, out translationMatrix); } else if (plane == Plane.Z) { Matrix4.CreateRotationZ(radians, out rotationMatrix); Matrix4.CreateTranslation(0, distance, 0, out translationMatrix); } else if (plane == Plane.Camera) { if (KWEngine.CurrentWorld != null) { Vector3 camLookAt; if (KWEngine.CurrentWorld.IsFirstPersonMode) { camLookAt = KWEngine.CurrentWorld.GetFirstPersonObject().GetLookAtVector(); } else { camLookAt = KWEngine.CurrentWorld.GetCameraLookAtVector(); } rotationMatrix = HelperMatrix.CreateRotationMatrixForAxisAngle(ref camLookAt, ref radians); Vector3 cross = Vector3.Cross(camLookAt, KWEngine.WorldUp); Matrix4.CreateTranslation(-cross.X, -cross.Y, -cross.Z, out translationMatrix); } else { HelperGeneral.ShowErrorAndQuit("HelperRotation::CalculateRotationAroundPointOnAxis()", "CurrentWorld is not set. Cannot rotate around a camera axis that does not exist."); return(Vector3.Zero); } } Matrix4.Mult(ref translationMatrix, ref rotationMatrix, out tempMatrix); Matrix4.Mult(ref tempMatrix, ref translationPointMatrix, out spinMatrix); Vector3.TransformPosition(ref zeroVector, ref spinMatrix, out finalTranslationPoint); return(finalTranslationPoint); }
public static int LoadTextureCompressedWithMipMaps(Stream stream) { bool success = HelperDDS2.TryLoadDDS(stream, false, out int texID, out int width, out int height); if (!success) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureCompressedWithMipMaps()", "Unsupported compressed texture format: only DXT1, DXT3 and DXT5 are supported."); texID = -1; throw new Exception("Unsupported compressed texture format: only DXT1, DXT3 and DXT5 are supported."); } return(texID); }
/// <summary> /// Konvertiert 2D-Mauskoordinaten in 3D-Koordinaten /// </summary> /// <param name="ms">Mausinformationen</param> /// <param name="planeNormal">Kollisionsebene (Standard: Camera)</param> /// <param name="planeHeight">Höhe der Kollisionsebene</param> /// <param name="offsetX">(optionale) Seitenkorrektur in Pixeln</param> /// <param name="offsetY">(optionale) Höhenkorrektur in Pixeln</param> /// <returns>3D-Mauskoordinaten</returns> public static Vector3 GetMouseIntersectionPoint(MouseState ms, Plane planeNormal, float planeHeight, int offsetX = 0, int offsetY = 0) { Vector3 normal; if (planeNormal == Plane.Y) { normal = new Vector3(0, 1, 0.000001f); } else if (planeNormal == Plane.X) { normal = new Vector3(1, 0, 0); } else if (planeNormal == Plane.Z) { normal = new Vector3(0, 0.000001f, 1); } else { if (KWEngine.CurrentWorld != null) { normal = -KWEngine.CurrentWorld.GetCameraLookAtVector(); } else { normal = new Vector3(0, 1, 0.000001f); } } Vector2 mc = HelperGeneral.GetNormalizedMouseCoords(ms.X + offsetX, ms.Y + offsetY); Vector3 worldRay = KWEngine.CurrentWindow.Get3DMouseCoords(mc.X, mc.Y); bool result; Vector3 intersection; if (KWEngine.Projection == ProjectionType.Perspective) { result = GameObject.LinePlaneIntersection(out intersection, worldRay, KWEngine.CurrentWindow.CurrentWorld.GetCameraPosition(), normal, normal * planeHeight); } else { Vector3 rayOrigin = HelperGeneral.GetRayOriginForOrthographicProjection(mc); result = GameObject.LinePlaneIntersection(out intersection, worldRay, rayOrigin, normal, normal * planeHeight); } if (result) { return(intersection); } else { return(normal * planeHeight); } }
internal static int LoadTextureFromAssembly(string resourceName, Assembly assembly) { int texID = -1; using (Stream s = assembly.GetManifestResourceStream(resourceName)) { Bitmap image = new Bitmap(s); if (image == null) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureFromAssembly()", "File " + resourceName + " is not a valid image file."); return(-1); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } return(texID); }
internal static void UpdateDeltaTime() { float currTimePick_ms = Watch.ElapsedMilliseconds; float realTimeElapsed_ms; if (lastRealTimeMeasurement_ms > 0) { realTimeElapsed_ms = (currTimePick_ms - lastRealTimeMeasurement_ms); } else { realTimeElapsed_ms = GLWindow.CurrentWindow._vSync ? 16.66667f : smoothedDeltaRealTime_ms; // just the first time } movAverageDeltaTime_ms = (realTimeElapsed_ms + movAverageDeltaTime_ms * (movAveragePeriod - 1)) / movAveragePeriod; smoothedDeltaRealTime_ms = smoothedDeltaRealTime_ms + (movAverageDeltaTime_ms - smoothedDeltaRealTime_ms) * smoothFactor; deltaTimeFactor = HelperGeneral.Clamp(smoothedDeltaRealTime_ms / TargetFrameTime, 0.00001f, 10); lastRealTimeMeasurement_ms = currTimePick_ms; }
/// <summary> /// Gibt das GameObject zurück, das unter dem Mauszeiger liegt (Instanzen müssen mit IsPickable = true gesetzt haben) /// </summary> /// <param name="ms">Mausinformationen</param> /// <param name="offsetX">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="offsetY">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <returns>Gewähltes GameObject</returns> public static GameObject PickGameObject(MouseState ms, int offsetX = 0, int offsetY = 0) { GLWindow w = GLWindow.CurrentWindow; if (w == null || w.CurrentWorld == null || !w.Focused) { return(null); } Vector2 mouseCoords = HelperGeneral.GetNormalizedMouseCoords(ms.X + offsetX, ms.Y + offsetY); Vector3 ray = KWEngine.CurrentWindow.Get3DMouseCoords(mouseCoords.X, mouseCoords.Y); Vector3 pos; if (KWEngine.Projection == ProjectionType.Perspective) { pos = w.CurrentWorld.GetCameraPosition() + ray; } else { pos = HelperGeneral.GetRayOriginForOrthographicProjection(mouseCoords) + ray; } GameObject pickedObject = null; float pickedDistance = float.MaxValue; foreach (GameObject go in w.CurrentWorld.GetGameObjects()) { if (go.IsPickable && go.IsInsideScreenSpace) { if (GameObject.IntersectRaySphere(pos, ray, go.GetCenterPointForAllHitboxes(), go.GetMaxDiameter() / 2)) { float distance = (go.GetCenterPointForAllHitboxes() - pos).LengthSquared; if (distance < pickedDistance) { pickedDistance = distance; pickedObject = go; } } } } return(pickedObject); }
public static Matrix3 GetViewMatrixInversed() { if (mCurrentGameObject != null) { Vector3 pos = mCurrentGameObject.Position; Vector3 lookat = new Vector3(); lookat.X = (float)(Math.Sin((float)mOrientation.X) * Math.Cos((float)mOrientation.Y)); lookat.Y = (float)Math.Sin((float)mOrientation.Y); lookat.Z = (float)(Math.Cos((float)mOrientation.X) * Math.Cos((float)mOrientation.Y)); pos.Y = pos.Y + mCurrentGameObject.FPSEyeOffset; return(new Matrix3(Matrix4.LookAt(pos, pos - lookat, KWEngine.WorldUp))); } else { HelperGeneral.ShowErrorAndQuit("HelperCamera::GetViewMatrix()", "No first person object available."); return(Matrix3.Identity); } }
internal static void SaveTextureToBitmap16(int texId, string name = null) { GL.Flush(); GL.Finish(); GL.BindTexture(TextureTarget.Texture2D, texId); GL.GetTextureLevelParameter(texId, 0, GetTextureParameter.TextureWidth, out int width); GL.GetTextureLevelParameter(texId, 0, GetTextureParameter.TextureHeight, out int height); Bitmap b = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); float[] data = new float[width * height * 4]; GL.GetTexImage(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.Float, data); GL.BindTexture(TextureTarget.Texture2D, 0); int x = 0, y = height - 1; for (int i = 0; i < data.Length; i += 4) { float rd = HelperGeneral.Clamp(data[i] * byte.MaxValue, 0, 255); float gn = HelperGeneral.Clamp(data[i + 1] * byte.MaxValue, 0, 255); float bl = HelperGeneral.Clamp(data[i + 2] * byte.MaxValue, 0, 255); float al = HelperGeneral.Clamp(data[i + 3] * byte.MaxValue, 0, 255); byte red = (byte)rd; byte green = (byte)gn; byte blue = (byte)bl; byte alpha = (byte)al; b.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue)); int prevX = x; x = (x + 1) % width; if (prevX > 0 && x == 0) { y--; } } b.Save(name == null ? "texture2d_16_tonemapped_rgba.png" : name, ImageFormat.Png); b.Dispose(); }
internal static void InitFramebufferBloom() { int framebufferTempId; int renderedTextureTemp; // =========== TEMP =========== //Init des frame buffer: framebufferTempId = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebufferTempId); // Init der Textur auf die gerendet wird: renderedTextureTemp = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, renderedTextureTemp); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); //Konfig. des frame buffer: GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, renderedTextureTemp, 0); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); FramebufferErrorCode code = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (code != FramebufferErrorCode.FramebufferComplete) { HelperGeneral.ShowErrorAndQuit("HelperSkybox::InitFramebufferBloom()", "GL_FRAMEBUFFER_COMPLETE failed. Cannot use FrameBuffer object."); return; } else { _fbBloom1 = framebufferTempId; _fbBloomTex0 = renderedTextureTemp; } // =========== TEMP 2 =========== //Init des frame buffer: int framebufferId = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebufferId); // Init der Textur auf die gerendet wird: int renderedTextureTemp2 = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, renderedTextureTemp2); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); //Konfig. des frame buffer: GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, renderedTextureTemp2, 0); GL.DrawBuffer(DrawBufferMode.ColorAttachment0); code = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (code != FramebufferErrorCode.FramebufferComplete) { HelperGeneral.ShowErrorAndQuit("HelperSkybox::InitFramebufferBloom()", "GL_FRAMEBUFFER_COMPLETE failed. Cannot use FrameBuffer object."); return; } else { _fbBloom2 = framebufferId; _fbBloomTex1 = renderedTextureTemp2; } }
/// <summary> /// Erfragt, ob der Mauszeiger (näherungsweise) auf dem Objekt liegt /// </summary> /// <param name="g">Zu untersuchendes GameObject</param> /// <param name="ms">Mausinformationen</param> /// <param name="offsetX">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="offsetY">optionale Verschiebung des Cursors auf der X-Achse in Pixeln (Standard: 0)</param> /// <param name="precision">Genauigkeit der Prüfung (Standard: Box für eine genauere Prüfung)</param> /// <returns>true, wenn der Mauszeiger auf dem Objekt liegt</returns> public static bool IsMouseCursorInsideHitbox(GameObject g, MouseState ms, int offsetX = 0, int offsetY = 0, MouseIntersectionPrecision precision = MouseIntersectionPrecision.Box) { Vector2 mc = HelperGeneral.GetNormalizedMouseCoords(ms.X + offsetX, ms.Y + offsetY); Vector3 worldRay = KWEngine.CurrentWindow.Get3DMouseCoords(mc.X, mc.Y); Vector3 normal; bool result; Vector3 intersection; if (KWEngine.CurrentWindow.CurrentWorld != null && KWEngine.CurrentWindow.CurrentWorld.IsFirstPersonMode) { normal = HelperCamera.GetLookAtVector(); normal.Y += 0.000001f; normal.Z += 0.000001f; if (KWEngine.Projection == ProjectionType.Perspective) { Vector3 fpPos = KWEngine.CurrentWindow.CurrentWorld.GetFirstPersonObject().Position; fpPos.Y += KWEngine.CurrentWindow.CurrentWorld.GetFirstPersonObject().FPSEyeOffset; result = GameObject.LinePlaneIntersection(out intersection, worldRay, fpPos, normal, g.GetCenterPointForAllHitboxes()); } else { Vector3 fpPos = HelperGeneral.GetRayOriginForOrthographicProjection(mc); result = GameObject.LinePlaneIntersection(out intersection, worldRay, fpPos, normal, g.GetCenterPointForAllHitboxes()); } } else { normal = -KWEngine.CurrentWindow.CurrentWorld.GetCameraLookAtVector(); normal.Y += 0.000001f; normal.Z += 0.000001f; if (KWEngine.Projection == ProjectionType.Perspective) { result = GameObject.LinePlaneIntersection(out intersection, worldRay, KWEngine.CurrentWindow.CurrentWorld.GetCameraPosition(), normal, g.GetCenterPointForAllHitboxes()); } else { Vector3 rayOrigin = HelperGeneral.GetRayOriginForOrthographicProjection(mc); result = GameObject.LinePlaneIntersection(out intersection, worldRay, rayOrigin, normal, g.GetCenterPointForAllHitboxes()); } } if (result) { foreach (Hitbox hb in g.Hitboxes) { if (precision == MouseIntersectionPrecision.Box) { if (IsPointInsideBox(ref intersection, hb)) { return(true); } } else { if (IsPointInsideSphere(ref intersection, hb.GetCenter(), hb.DiameterAveraged)) { return(true); } } } return(false); } else { return(false); } }
public static int LoadTextureForModelInternal(string filename, bool convertRoughnessToSpecular = false) { Assembly a = Assembly.GetEntryAssembly(); int texID; if (filename.ToLower().EndsWith("dds")) { string assPath = a.GetName().Name + "." + filename; using (Stream s = a.GetManifestResourceStream(assPath)) texID = LoadTextureCompressedWithMipMaps(s); return(texID); } try { string assPath = a.GetName().Name + "." + filename; using (Stream s = a.GetManifestResourceStream(assPath)) { Bitmap image = new Bitmap(s); if (image == null) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModelInternal()", "Could not load image file! Make sure to copy it to the correct output directory."); return(-1); } texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = null; if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } else { data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); } GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.OpenGL.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, GLWindow.CurrentWindow.AnisotropicFiltering); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); int mipMapCount = GetMaxMipMapLevels(data.Width, data.Height); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); image.UnlockBits(data); image.Dispose(); GL.BindTexture(TextureTarget.Texture2D, 0); } } catch (Exception) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureForModel()", "Could not load image file from assembly: " + filename); return(-1); } return(texID); }
internal static int LoadTextureSkybox(string filename, bool isInAssembly = false) { Assembly a = Assembly.GetEntryAssembly(); if (!filename.ToLower().EndsWith("jpg") && !filename.ToLower().EndsWith("jpeg") && !filename.ToLower().EndsWith("png") && !filename.ToLower().EndsWith("dds")) { HelperGeneral.ShowErrorAndQuit("HelperTexture::LoadTextureSkybox()", "Only JPG, PNG and DDS (DXT1/3/5) files are supported."); return(-1); } if (!KWEngine.CustomTextures[KWEngine.CurrentWorld].ContainsKey(filename)) { int mipMapCount = 0; try { using (Stream s = isInAssembly ? a.GetManifestResourceStream(a.GetName().Name + "." + filename) : File.Open(filename, FileMode.Open)) { if (filename.ToLower().EndsWith(".dds")) { int textureId = -1; HelperDDS2.TryLoadDDSCubeMap(s, false, out textureId, out mipMapCount); return(textureId); } Bitmap image = new Bitmap(s); int width = image.Width; int height = image.Height; int height_onethird = height / 3; int width_onequarter = width / 4; Bitmap image_front = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_back = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_up = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_down = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_left = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Bitmap image_right = new Bitmap(width_onequarter, height_onethird, image.PixelFormat); Graphics g = null; //front g = Graphics.FromImage(image_front); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(2 * width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //back g = Graphics.FromImage(image_back); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(0, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //up g = Graphics.FromImage(image_up); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, 0, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //down g = Graphics.FromImage(image_down); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, 2 * height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //left g = Graphics.FromImage(image_left); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); //right g = Graphics.FromImage(image_right); g.DrawImage(image, new Rectangle(0, 0, width_onequarter, height_onethird), new Rectangle(3 * width_onequarter, height_onethird, width_onequarter, height_onethird), GraphicsUnit.Pixel ); g.Dispose(); int newTexture = GL.GenTexture(); GL.BindTexture(TextureTarget.TextureCubeMap, newTexture); BitmapData data = null; PixelInternalFormat iFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? PixelInternalFormat.Rgb : PixelInternalFormat.Rgba; OpenTK.Graphics.OpenGL4.PixelFormat pxFormat = image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? OpenTK.Graphics.OpenGL4.PixelFormat.Bgr : OpenTK.Graphics.OpenGL4.PixelFormat.Bgra; // front data = image_front.LockBits(new Rectangle(0, 0, image_front.Width, image_front.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_front.UnlockBits(data); // back data = image_back.LockBits(new Rectangle(0, 0, image_back.Width, image_back.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeX, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_back.UnlockBits(data); // up data = image_up.LockBits(new Rectangle(0, 0, image_up.Width, image_up.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_up.UnlockBits(data); // down data = image_down.LockBits(new Rectangle(0, 0, image_down.Width, image_down.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeY, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_down.UnlockBits(data); // left data = image_left.LockBits(new Rectangle(0, 0, image_left.Width, image_left.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_left.UnlockBits(data); // right data = image_right.LockBits(new Rectangle(0, 0, image_right.Width, image_right.Height), ImageLockMode.ReadOnly, image.PixelFormat); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, 0, iFormat, data.Width, data.Height, 0, pxFormat, PixelType.UnsignedByte, data.Scan0); image_right.UnlockBits(data); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge); GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap); mipMapCount = GetMaxMipMapLevels(width_onequarter, height_onethird); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMaxLevel, Math.Max(0, mipMapCount - 2)); KWEngine.CustomTextures[KWEngine.CurrentWorld].Add(filename, newTexture); image.Dispose(); image_front.Dispose(); image_back.Dispose(); image_up.Dispose(); image_down.Dispose(); image_left.Dispose(); image_right.Dispose(); GL.BindTexture(TextureTarget.TextureCubeMap, 0); return(newTexture); } } catch (Exception ex) { Debug.WriteLine("Error loading skybox texture: " + filename + " (" + ex.Message + ")"); return(-1); } } else { int id = -1; KWEngine.CustomTextures[KWEngine.CurrentWorld].TryGetValue(filename, out id); return(id); } }
/// <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(); }
internal static void CreateFramebuffer() { _fbFull = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, _fbFull); int renderedTexture = GL.GenTexture(); int renderedTextureAttachment = GL.GenTexture(); int depthTexId = GL.GenTexture(); GL.DrawBuffers(2, new DrawBuffersEnum[2] { DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1 }); GL.BindTexture(TextureTarget.Texture2D, renderedTexture); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); //render buffer fsaa: int renderbufferFSAA = GL.GenRenderbuffer(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbufferFSAA); GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, 8, RenderbufferStorage.Rgba8, _resolution, _resolution); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbufferFSAA); GL.BindTexture(TextureTarget.Texture2D, renderedTextureAttachment); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); int renderbufferFSAA2 = GL.GenRenderbuffer(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbufferFSAA2); GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, 8, RenderbufferStorage.Rgba8, _resolution, _resolution); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment1, RenderbufferTarget.Renderbuffer, renderbufferFSAA2); // depth buffer fsaa: int depthRenderBuffer = GL.GenRenderbuffer(); GL.BindTexture(TextureTarget.Texture2D, depthTexId); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent32, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureCompareMode, new int[] { (int)TextureCompareMode.CompareRefToTexture }); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureCompareFunc, new int[] { (int)DepthFunction.Lequal }); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, depthTexId, 0); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthRenderBuffer); GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, 8, RenderbufferStorage.DepthComponent32, _resolution, _resolution); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depthRenderBuffer); FramebufferErrorCode code = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (code != FramebufferErrorCode.FramebufferComplete) { HelperGeneral.ShowErrorAndQuit("HelperSkybox::CreateFramebuffer()", "GL_FRAMEBUFFER_COMPLETE failed. Cannot use FrameBuffer object."); return; } else { _tex0 = renderedTexture; _tex1 = renderedTextureAttachment; _texDepth = depthTexId; } GL.BindTexture(TextureTarget.Texture2D, 0); // Downsample fb: _fbDownsample = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, _fbDownsample); // Init der Textur auf die gerendet wird: renderedTexture = GL.GenTexture(); renderedTextureAttachment = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, renderedTexture); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); GL.BindTexture(TextureTarget.Texture2D, renderedTextureAttachment); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _resolution, _resolution, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureParameterName.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureParameterName.ClampToEdge); //Konfig. des frame buffer: GL.DrawBuffers(2, new DrawBuffersEnum[2] { DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1 }); GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, renderedTexture, 0); GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment1, renderedTextureAttachment, 0); code = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (code != FramebufferErrorCode.FramebufferComplete) { HelperGeneral.ShowErrorAndQuit("HelperSkybox::CreateFramebuffer()", "GL_FRAMEBUFFER_COMPLETE failed. Cannot use FrameBuffer object."); return; } else { _tex0d = renderedTexture; _tex1d = renderedTextureAttachment; } }