public unsafe GLTexture(string name, SDL2.SDL.SDL_Surface source) { Name = name; Width = source.w; Height = source.h; GLHelper.Texture(TextureTarget.Texture2D, name, out int gt); GL.BindTexture(TextureTarget.Texture2D, gt); GL.TextureStorage2D(gt, 1, SizedInternalFormat.Rgba16, Width, Height); var fmt = PixelInternalFormat.Rgb; var surfmt = (SDL2.SDL.SDL_PixelFormat *)(source.format.ToPointer()); if (surfmt->BytesPerPixel == 4) { // We're in RGBA land fmt = PixelInternalFormat.Rgba; } GL.TexImage2D(TextureTarget.Texture2D, 0, fmt, Width, Height, 0, (PixelFormat)fmt, PixelType.UnsignedByte, source.pixels); GLObject = gt; SetMinFilter(TextureMinFilter.Nearest); SetMagFilter(TextureMagFilter.Nearest); }
public void Rotate(Vector3 eulers) { var eul = GLHelper.V3DegRad(eulers); var q = Quaternion.FromEulerAngles(eul); Quat *= q; }
public void RenderToDepth(System.Numerics.Vector3 LightPos) { GL.Viewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); var aspect = (float)SHADOW_WIDTH / SHADOW_HEIGHT; GL.BindFramebuffer(FramebufferTarget.Framebuffer, DepthMapFBO); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); var near = 1.0f; var far = 25.0f; Matrix4 shadProj = Matrix4.CreatePerspectiveFieldOfView( GLHelper.DegRad(90.0f), aspect, near, far); // THIS NEEDS TO BE 90! I might make it a var anyway for shits and giggles List <Matrix4> Transforms = new(); Vector3 right = new(1.0f, 0.0f, 0.0f); Vector3 left = new(-1.0f, 0.0f, 0.0f); Vector3 up = new(0.0f, 1.0f, 0.0f); Vector3 down = new(0.0f, -1.0f, 0.0f); Vector3 forward = new(0.0f, 0.0f, 1.0f); Vector3 back = new(0.0f, 0.0f, -1.0f); var lptk = LightPos.ToOpenTK(); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + right, down)); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + left, down)); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + up, forward)); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + down, back)); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + forward, down)); Transforms.Add( shadProj * Matrix4.LookAt( lptk, lptk + back, down)); GL.BindTexture(TextureTarget.TextureCubeMap, Cubemap); ECSScene.ExplicitRender(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); }
public GLTexture(string name, int w, int h, IntPtr data) { Name = name; Width = w; Height = h; GLHelper.Texture(TextureTarget.Texture2D, name, out int gt); GL.TextureStorage2D(gt, 1, SizedInternalFormat.Rgba16, w, h); GL.TextureSubImage2D(gt, 0, 0, 0, w, h, PixelFormat.Bgra, PixelType.UnsignedByte, data); GLObject = gt; SetMinFilter(TextureMinFilter.Nearest); SetMagFilter(TextureMagFilter.Nearest); }
public GLTexture(string name, Bitmap bmp) { GLHelper.Texture(TextureTarget.Texture2D, name, out int obj); GLObject = obj; Bind(); var ir = new Rectangle(0, 0, bmp.Width, bmp.Height); var data = bmp.LockBits(ir, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var fmt = PixelInternalFormat.Rgba; GL.TexImage2D(TextureTarget.Texture2D, 0, fmt, bmp.Width, bmp.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); SetMinFilter(TextureMinFilter.Linear); SetMagFilter(TextureMagFilter.Linear); SetWrappingRules(TextureWrapMode.Repeat); }
public GLVertexArrayObject(string name) { GLObject = GL.GenVertexArray(); GLHelper.LabelObj(ObjectLabelIdentifier.VertexArray, GLObject, $"VAO: {name}"); }
public Matrix4 Projection() { return(Matrix4.CreatePerspectiveFieldOfView(GLHelper.DegRad(FieldOfView), Engine.Width / Engine.Height, 0.1f, 1000f)); }
public GLShaderProgram Label(string name) { GLHelper.LabelObj(ObjectLabelIdentifier.Program, GLObject, name); return(this); }
public GLUIntBuffer(string name) { GLObject = GL.GenBuffer(); GLHelper.LabelObj(ObjectLabelIdentifier.Buffer, GLObject, $"UIntBuffer: {name}"); }
public static unsafe void Initialise() { GL.LoadBindings(new SDLBindingsContext()); Context = ImGui.CreateContext(); ImGui.SetCurrentContext(Context); var io = ImGui.GetIO(); io.DisplaySize.X = Engine.Width; io.DisplaySize.Y = Engine.Height; io.KeyMap[(int)ImGuiKey.Tab] = (int)SC.SDL_SCANCODE_TAB; io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)SC.SDL_SCANCODE_LEFT; io.KeyMap[(int)ImGuiKey.RightArrow] = (int)SC.SDL_SCANCODE_RIGHT; io.KeyMap[(int)ImGuiKey.UpArrow] = (int)SC.SDL_SCANCODE_UP; io.KeyMap[(int)ImGuiKey.DownArrow] = (int)SC.SDL_SCANCODE_DOWN; io.KeyMap[(int)ImGuiKey.PageUp] = (int)SC.SDL_SCANCODE_PAGEUP; io.KeyMap[(int)ImGuiKey.PageDown] = (int)SC.SDL_SCANCODE_PAGEDOWN; io.KeyMap[(int)ImGuiKey.Home] = (int)SC.SDL_SCANCODE_HOME; io.KeyMap[(int)ImGuiKey.End] = (int)SC.SDL_SCANCODE_END; io.KeyMap[(int)ImGuiKey.Insert] = (int)SC.SDL_SCANCODE_INSERT; io.KeyMap[(int)ImGuiKey.Delete] = (int)SC.SDL_SCANCODE_DELETE; io.KeyMap[(int)ImGuiKey.Backspace] = (int)SC.SDL_SCANCODE_BACKSPACE; io.KeyMap[(int)ImGuiKey.Space] = (int)SC.SDL_SCANCODE_SPACE; io.KeyMap[(int)ImGuiKey.Enter] = (int)SC.SDL_SCANCODE_RETURN; io.KeyMap[(int)ImGuiKey.Escape] = (int)SC.SDL_SCANCODE_ESCAPE; io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SC.SDL_SCANCODE_KP_ENTER; io.KeyMap[(int)ImGuiKey.A] = (int)SC.SDL_SCANCODE_A; io.KeyMap[(int)ImGuiKey.C] = (int)SC.SDL_SCANCODE_C; io.KeyMap[(int)ImGuiKey.V] = (int)SC.SDL_SCANCODE_V; io.KeyMap[(int)ImGuiKey.X] = (int)SC.SDL_SCANCODE_X; io.KeyMap[(int)ImGuiKey.Y] = (int)SC.SDL_SCANCODE_Y; io.KeyMap[(int)ImGuiKey.Z] = (int)SC.SDL_SCANCODE_Z; var maj = GL.GetInteger(GetPName.MajorVersion); var min = GL.GetInteger(GetPName.MinorVersion); Log.Info($"OpenGLManager: This appears to be OpenGL {maj}.{min}."); VtxBufSize = 10000; IdxBufSize = 2000; IG_VAO = new("ImGui"); IG_VAO.Bind(); VertexSource = File.ReadAllText("EngineResources/2D.vert"); FragSource = File.ReadAllText("EngineResources/standard.frag"); VS = new GLShader(VertexSource, GLShaderType.VERTEX); FS = new GLShader(FragSource, GLShaderType.FRAGMENT); VS.Compile(); FS.Compile(); ImGuiProgram = new GLShaderProgram().Attach(VS).Attach(FS).Link(); GLHelper.VertexBuffer("ImGui VBO", out VertexBuffer); GLHelper.ElementBuffer("ImGui EBO", out IndexBuffer); GL.NamedBufferData(VertexBuffer, VtxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.NamedBufferData(IndexBuffer, IdxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); var va = IG_VAO.GLObject; GL.VertexArrayVertexBuffer(va, 0, VertexBuffer, IntPtr.Zero, Unsafe.SizeOf <ImDrawVert>()); GL.VertexArrayElementBuffer(va, IndexBuffer); GL.EnableVertexArrayAttrib(va, 0); GL.VertexArrayAttribBinding(va, 0, 0); GL.VertexArrayAttribFormat(va, 0, 2, VertexAttribType.Float, false, 0); GL.EnableVertexArrayAttrib(va, 1); GL.VertexArrayAttribBinding(va, 1, 0); GL.VertexArrayAttribFormat(va, 1, 2, VertexAttribType.Float, false, 8); GL.EnableVertexArrayAttrib(va, 2); GL.VertexArrayAttribBinding(va, 2, 0); GL.VertexArrayAttribFormat(va, 2, 4, VertexAttribType.UnsignedByte, true, 16); Initialised = true; OnInitGL?.Invoke(); }
public static unsafe void Initialise() { GL.LoadBindings(new SDLBindingsContext()); Context = ImGui.CreateContext(); ImGui.SetCurrentContext(Context); ImGuizmo.SetImGuiContext(Context); var io = ImGui.GetIO(); GL.Enable(EnableCap.DebugOutput); GL.Enable(EnableCap.DebugOutputSynchronous); GL.DebugMessageCallback(_DebugCb, IntPtr.Zero); io.DisplaySize.X = Engine.Width; io.DisplaySize.Y = Engine.Height; io.KeyMap[(int)ImGuiKey.Tab] = (int)SC.SDL_SCANCODE_TAB; io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)SC.SDL_SCANCODE_LEFT; io.KeyMap[(int)ImGuiKey.RightArrow] = (int)SC.SDL_SCANCODE_RIGHT; io.KeyMap[(int)ImGuiKey.UpArrow] = (int)SC.SDL_SCANCODE_UP; io.KeyMap[(int)ImGuiKey.DownArrow] = (int)SC.SDL_SCANCODE_DOWN; io.KeyMap[(int)ImGuiKey.PageUp] = (int)SC.SDL_SCANCODE_PAGEUP; io.KeyMap[(int)ImGuiKey.PageDown] = (int)SC.SDL_SCANCODE_PAGEDOWN; io.KeyMap[(int)ImGuiKey.Home] = (int)SC.SDL_SCANCODE_HOME; io.KeyMap[(int)ImGuiKey.End] = (int)SC.SDL_SCANCODE_END; io.KeyMap[(int)ImGuiKey.Insert] = (int)SC.SDL_SCANCODE_INSERT; io.KeyMap[(int)ImGuiKey.Delete] = (int)SC.SDL_SCANCODE_DELETE; io.KeyMap[(int)ImGuiKey.Backspace] = (int)SC.SDL_SCANCODE_BACKSPACE; io.KeyMap[(int)ImGuiKey.Space] = (int)SC.SDL_SCANCODE_SPACE; io.KeyMap[(int)ImGuiKey.Enter] = (int)SC.SDL_SCANCODE_RETURN; io.KeyMap[(int)ImGuiKey.Escape] = (int)SC.SDL_SCANCODE_ESCAPE; io.KeyMap[(int)ImGuiKey.KeyPadEnter] = (int)SC.SDL_SCANCODE_KP_ENTER; io.KeyMap[(int)ImGuiKey.A] = (int)SC.SDL_SCANCODE_A; io.KeyMap[(int)ImGuiKey.C] = (int)SC.SDL_SCANCODE_C; io.KeyMap[(int)ImGuiKey.V] = (int)SC.SDL_SCANCODE_V; io.KeyMap[(int)ImGuiKey.X] = (int)SC.SDL_SCANCODE_X; io.KeyMap[(int)ImGuiKey.Y] = (int)SC.SDL_SCANCODE_Y; io.KeyMap[(int)ImGuiKey.Z] = (int)SC.SDL_SCANCODE_Z; var maj = GL.GetInteger(GetPName.MajorVersion); var min = GL.GetInteger(GetPName.MinorVersion); Log.Info($"OpenGLManager: This appears to be OpenGL {maj}.{min}."); var t = new GLVersion() { Major = maj, Minor = min, VersionString = $"{maj}.{min}" }; Version = t; if (Version.Major < 4 || (Version.Major == 4 && Version.Minor < 5)) { // This is not OpenGL 4.5 or higher. Log.Fatal("This version of OpenGL is too old! Luminal applications require at least GL 4.5."); Log.Fatal("Showing message box and exiting."); MessageBox.Error("Sorry, applications built with Luminal cannot run on your graphics card.\n" + $"Your graphics card only supports OpenGL {Version.VersionString}, and at least OpenGL 4.5 is required.", "Engine incompatible: OpenGL version too old"); Engine.Quit(1); } VtxBufSize = 10000; IdxBufSize = 2000; IG_VAO = new("ImGui"); IG_VAO.Bind(); VertexSource = File.ReadAllText("EngineResources/Shaders/2D/2D.vert"); FragSource = File.ReadAllText("EngineResources/Shaders/2D/2D.frag"); VS = new GLShader(VertexSource, GLShaderType.Vertex); FS = new GLShader(FragSource, GLShaderType.Fragment); VS.Compile(); FS.Compile(); ImGuiProgram = new GLShaderProgram().Attach(VS).Attach(FS).Link(); GLHelper.VertexBuffer("ImGui VBO", out VertexBuffer); GLHelper.ElementBuffer("ImGui EBO", out IndexBuffer); GL.NamedBufferData(VertexBuffer, VtxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.NamedBufferData(IndexBuffer, IdxBufSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); var va = IG_VAO.GLObject; GL.VertexArrayVertexBuffer(va, 0, VertexBuffer, IntPtr.Zero, Unsafe.SizeOf <ImDrawVert>()); GL.VertexArrayElementBuffer(va, IndexBuffer); GL.EnableVertexArrayAttrib(va, 0); GL.VertexArrayAttribBinding(va, 0, 0); GL.VertexArrayAttribFormat(va, 0, 2, VertexAttribType.Float, false, 0); GL.EnableVertexArrayAttrib(va, 1); GL.VertexArrayAttribBinding(va, 1, 0); GL.VertexArrayAttribFormat(va, 1, 2, VertexAttribType.Float, false, 8); GL.EnableVertexArrayAttrib(va, 2); GL.VertexArrayAttribBinding(va, 2, 0); GL.VertexArrayAttribFormat(va, 2, 4, VertexAttribType.UnsignedByte, true, 16); _SetClipboard = SetClipboard; io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_SetClipboard); _GetClipboard = GetClipboard; io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(_GetClipboard); Initialised = true; OnInitGL?.Invoke(); }
public GLFloatBuffer(string name) { Name = name; GLObject = GL.GenBuffer(); GLHelper.LabelObj(ObjectLabelIdentifier.Buffer, GLObject, $"FloatBuffer: {name}"); }