internal void InitializeOpenGL() { SetThreadAffinity(); if (SDL.SDL_GL_MakeCurrent(window.Window, context) < 0) { throw new InvalidOperationException($"Can not bind OpenGL context. (Error: {SDL.SDL_GetError()})"); } OpenGL.Initialize(window.GLProfile == GLProfile.Legacy); OpenGL.CheckGLError(); if (OpenGL.Profile != GLProfile.Legacy) { OpenGL.glGenVertexArrays(1, out var vao); OpenGL.CheckGLError(); OpenGL.glBindVertexArray(vao); OpenGL.CheckGLError(); } OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TintAttributeIndex); OpenGL.CheckGLError(); }
public virtual void ApplyFormatOnVertexBuffer() { List <VertexLayout> vl = new List <VertexLayout>(); vl.Add(new VertexLayout() { SlotCount = 3, SlotDataType = OpenGL.GL_FLOAT }); //aVertexPosition vl.Add(new VertexLayout() { SlotCount = 2, SlotDataType = OpenGL.GL_FLOAT }); //aVertexTexCoord vl.Add(new VertexLayout() { SlotCount = 2, SlotDataType = OpenGL.GL_FLOAT }); //aVertexTexCoordSecond vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aVertexPaletteIndex vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aVertexTexMetadata vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aVertexDrawmode vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aVertexTexMetadata2 vl.Add(new VertexLayout() { SlotCount = 4, SlotDataType = OpenGL.GL_FLOAT }); //aVertexColorInfo vl.Add(new VertexLayout() { SlotCount = 4, SlotDataType = OpenGL.GL_FLOAT }); //aVertexUVFillRect vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aFlipX vl.Add(new VertexLayout() { SlotCount = 1, SlotDataType = OpenGL.GL_FLOAT }); //aFlipY //SlotCount байтов в слоте 4=vec4 float=1 - ед.измерения слота в байтах int slotbytesize = 4; int offset = 0; for (int i = 0; i < vl.Count; i++) { OpenGL.glVertexAttribPointer(i, vl[i].SlotCount, vl[i].SlotDataType, false, VertexSize, new IntPtr(offset)); OpenGL.glEnableVertexAttribArray(i); offset += vl[i].SlotCount * slotbytesize; } }
internal void InitializeOpenGL() { SetThreadAffinity(); context = SDL.SDL_GL_CreateContext(window.Window); if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window.Window, context) < 0) { throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError())); } OpenGL.Initialize(window.GLProfile == GLProfile.Legacy); OpenGL.CheckGLError(); if (OpenGL.Profile != GLProfile.Legacy) { uint vao; OpenGL.glGenVertexArrays(1, out vao); OpenGL.CheckGLError(); OpenGL.glBindVertexArray(vao); OpenGL.CheckGLError(); } OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); OpenGL.CheckGLError(); }
public Sdl2GraphicsDevice(Size windowSize, WindowMode windowMode) { Console.WriteLine("Using SDL 2 with OpenGL renderer"); WindowSize = windowSize; SDL.SDL_Init(SDL.SDL_INIT_NOPARACHUTE | SDL.SDL_INIT_VIDEO); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, 0); SDL.SDL_DisplayMode display; SDL.SDL_GetCurrentDisplayMode(0, out display); Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h); if (WindowSize.Width == 0 && WindowSize.Height == 0) { Console.WriteLine("No custom resolution provided, using desktop resolution"); WindowSize = new Size(display.w, display.h); } Console.WriteLine("Using resolution: {0}x{1}", WindowSize.Width, WindowSize.Height); window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, WindowSize.Width, WindowSize.Height, SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL); if (Game.Settings.Game.LockMouseWindow) { GrabWindowMouseFocus(); } else { ReleaseWindowMouseFocus(); } if (windowMode == WindowMode.Fullscreen) { SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN); } else if (windowMode == WindowMode.PseudoFullscreen) { // Work around a visual glitch in OSX: the window is offset // partially offscreen if the dock is at the left of the screen if (Platform.CurrentPlatform == PlatformType.OSX) { SDL.SDL_SetWindowPosition(window, 0, 0); } SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); } context = SDL.SDL_GL_CreateContext(window); if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0) { throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError())); } OpenGL.Initialize(); OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); OpenGL.CheckGLError(); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); input = new Sdl2Input(); }
public Sdl2GraphicsDevice(Size windowSize, WindowMode windowMode) { Console.WriteLine("Using SDL 2 with OpenGL renderer"); WindowSize = windowSize; // Disable legacy scaling on Windows if (Platform.CurrentPlatform == PlatformType.Windows && !Game.Settings.Graphics.DisableWindowsDPIScaling) { SetProcessDPIAware(); } SDL.SDL_Init(SDL.SDL_INIT_NOPARACHUTE | SDL.SDL_INIT_VIDEO); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8); SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, 0); SDL.SDL_DisplayMode display; SDL.SDL_GetCurrentDisplayMode(0, out display); Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h); if (WindowSize.Width == 0 && WindowSize.Height == 0) { Console.WriteLine("No custom resolution provided, using desktop resolution"); WindowSize = new Size(display.w, display.h); } Console.WriteLine("Using resolution: {0}x{1}", WindowSize.Width, WindowSize.Height); var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI; window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, WindowSize.Width, WindowSize.Height, windowFlags); SurfaceSize = WindowSize; WindowScale = 1; // Enable high resolution rendering for Retina displays if (Platform.CurrentPlatform == PlatformType.OSX) { // OSX defines the window size in "points", with a device-dependent number of pixels per point. // The window scale is simply the ratio of GL pixels / window points. int width, height; SDL.SDL_GL_GetDrawableSize(window, out width, out height); SurfaceSize = new Size(width, height); WindowScale = width * 1f / WindowSize.Width; } else if (Platform.CurrentPlatform == PlatformType.Windows) { float ddpi, hdpi, vdpi; if (!Game.Settings.Graphics.DisableWindowsDPIScaling && SDL.SDL_GetDisplayDPI(0, out ddpi, out hdpi, out vdpi) == 0) { WindowScale = ddpi / 96; WindowSize = new Size((int)(SurfaceSize.Width / WindowScale), (int)(SurfaceSize.Height / WindowScale)); } } else { float scale = 1; var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE"); if (scaleVariable != null && float.TryParse(scaleVariable, out scale)) { WindowScale = scale; WindowSize = new Size((int)(SurfaceSize.Width / WindowScale), (int)(SurfaceSize.Height / WindowScale)); } } Console.WriteLine("Using window scale {0:F2}", WindowScale); if (Game.Settings.Game.LockMouseWindow) { GrabWindowMouseFocus(); } else { ReleaseWindowMouseFocus(); } if (windowMode == WindowMode.Fullscreen) { SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN); } else if (windowMode == WindowMode.PseudoFullscreen) { // Work around a visual glitch in OSX: the window is offset // partially offscreen if the dock is at the left of the screen if (Platform.CurrentPlatform == PlatformType.OSX) { SDL.SDL_SetWindowPosition(window, 0, 0); } SDL.SDL_SetWindowFullscreen(window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP); SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); } context = SDL.SDL_GL_CreateContext(window); if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0) { throw new InvalidOperationException("Can not create OpenGL context. (Error: {0})".F(SDL.SDL_GetError())); } OpenGL.Initialize(); OpenGL.glEnableVertexAttribArray(Shader.VertexPosAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexCoordAttributeIndex); OpenGL.CheckGLError(); OpenGL.glEnableVertexAttribArray(Shader.TexMetadataAttributeIndex); OpenGL.CheckGLError(); SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); input = new Sdl2Input(); }