protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); windowInfo = OpenTK.Platform.Utilities.CreateWindowsWindowInfo(Handle); graphicsContext = new OpenTK.Graphics.GraphicsContext(graphicsMode, windowInfo, major, minor, graphicsContextFlags); graphicsContext.MakeCurrent(windowInfo); graphicsContext.LoadAll(); graphicsContext.SwapInterval = VSync ? 1 : 0; if (platformRenderContext == null) { platformRenderContext = new Graphics.Platform.OpenGL.PlatformRenderContext(); PlatformRenderer.Initialize(platformRenderContext); } graphicsContext.MakeCurrent(null); }
public static skybox loadFromShader(shader_p surface, shader_gl shader) { skybox sky = new skybox(); var context = new OpenTK.Graphics.GraphicsContext(AlethaApplication.GraphicsMode, AlethaApplication.NativeWindowContext.WindowInfo); context.MakeCurrent(AlethaApplication.NativeWindowContext.WindowInfo); // determine type of skybox: number of textures to load //sky.type = skybox_type.one_tex; buildSkyboxbuffers(); loadSkyTexture(shader, surface, (int texture) => { //.skymap = texture; }, (int texture) => { sky.skymap = texture; //GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap); }); return(sky); }
public int createSolidTexture(Vector4 color) { byte[] data; List <byte> pixels = new List <byte>(); pixels.Add((byte)color.X); pixels.Add((byte)color.Y); pixels.Add((byte)color.Z); pixels.Add((byte)color.W); data = pixels.ToArray(); var context = new OpenTK.Graphics.GraphicsContext(AlethaApplication.GraphicsMode, AlethaApplication.NativeWindowContext.WindowInfo); context.MakeCurrent(AlethaApplication.NativeWindowContext.WindowInfo); int texture = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 1, 1, 0, OpenTK.Graphics.OpenGL4.PixelFormat.Rgba, PixelType.UnsignedByte, data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.BindTexture(TextureTarget.Texture2D, 0); return(texture); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { var time = new TimeMonitor(1000); // Request a 32-bits depth buffer when creating the window var contextSettings = new ContextSettings { DepthBits = 32 }; // Create the main window var window = new RenderWindow(new VideoMode(1600, 900), "TenSeconds", Styles.Close | Styles.Titlebar, contextSettings); // Make it the active window for OpenGL calls window.SetActive(); var windowInfo = OpenTK.Platform.Utilities.CreateWindowsWindowInfo(window.SystemHandle); var graphicsContext = new OpenTK.Graphics.GraphicsContext(OpenTK.Graphics.GraphicsMode.Default, windowInfo); graphicsContext.MakeCurrent(windowInfo); graphicsContext.LoadAll(); // Setup event handlers window.Closed += OnClosed; window.KeyPressed += OnKeyPressed; window.Resized += OnResized; var inputManager = new InputManager(); inputManager.Init(window); var game = new Game(); game.Init(window); while (window.IsOpen() && game.IsRunning) { time.Update(); // Process events window.DispatchEvents(); inputManager.Update(window); game.HandleInput(inputManager); game.Update(time); // Clear color and depth buffer GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); game.Draw(window); // Finally, display the rendered frame on screen window.Display(); } }
public static void LoadComplete(int index, TextureTarget target, OnTextueLoad onloadComplete) // OnSkymapLoadComplete { var context = new OpenTK.Graphics.GraphicsContext(AlethaApplication.GraphicsMode, AlethaApplication.NativeWindowContext.WindowInfo); context.MakeCurrent(AlethaApplication.NativeWindowContext.WindowInfo); int skybox = GL.GenTexture(); index = skybox; skymap = skybox; System.Drawing.Imaging.BitmapData xposd, yposd, zposd, xnegd, ynegd, znegd; //gl.enable(RenderingContext.TEXTURE_CUBE_MAP); GL.BindTexture(TextureTarget.TextureCubeMap, skybox); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); //gl.TexParameter(RenderingContext.TEXTURE_CUBE_MAP, RenderingContext.TEXTURE_WRAP_R, RenderingContext.CLAMP_TO_EDGE); int level = 0; PixelInternalFormat format = PixelInternalFormat.Rgba; PixelFormat pformat = PixelFormat.Rgba; PixelType type = PixelType.UnsignedByte; xposd = texture.UnlockBitmap(xpos); yposd = texture.UnlockBitmap(ypos); zposd = texture.UnlockBitmap(zpos); xnegd = texture.UnlockBitmap(xneg); ynegd = texture.UnlockBitmap(yneg); znegd = texture.UnlockBitmap(zneg); // gl.texImage2D(RenderingContext.TEXTURE_2D, 0, RenderingContext.RGBA, RenderingContext.RGBA, RenderingContext.UNSIGNED_BYTE, image); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX, level, format, xpos.Width, xpos.Height, 0, pformat, type, xposd.Scan0); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeX, level, format, xneg.Width, xneg.Height, 0, pformat, type, xnegd.Scan0); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveY, level, format, ypos.Width, ypos.Height, 0, pformat, type, yposd.Scan0); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeY, level, format, yneg.Width, yneg.Height, 0, pformat, type, ynegd.Scan0); GL.TexImage2D(TextureTarget.TextureCubeMapPositiveZ, level, format, zpos.Width, zpos.Height, 0, pformat, type, zposd.Scan0); GL.TexImage2D(TextureTarget.TextureCubeMapNegativeZ, level, format, zneg.Width, zneg.Height, 0, pformat, type, znegd.Scan0); onloadComplete(skybox); }
private void MakeCurrent() { if (RenderingContext != null) { if (!RenderingContext.IsCurrent) { try { RenderingContext.MakeCurrent(WindowsInfo); //if (Shader != null) // Shader.UpdateUniforms(this); } catch (Exception) { } } } }
public override Gk3Main.Graphics.IRenderer CreateRenderer() { if (_renderer != null) { throw new InvalidOperationException("A renderer has already been created"); } OpenTK.Graphics.GraphicsMode mode = new OpenTK.Graphics.GraphicsMode(new OpenTK.Graphics.ColorFormat(32), _depth, 0, 0); _window = new OpenTK.NativeWindow(_width, _height, "FreeGeeKayThree - OpenGL 3.3 renderer", _fullscreen ? OpenTK.GameWindowFlags.Fullscreen : OpenTK.GameWindowFlags.FixedWindow, mode, OpenTK.DisplayDevice.Default); _window.Visible = true; _window.Closed += (x, y) => _closed = true; _context = new OpenTK.Graphics.GraphicsContext(mode, _window.WindowInfo, 3, 3, OpenTK.Graphics.GraphicsContextFlags.ForwardCompatible | OpenTK.Graphics.GraphicsContextFlags.Debug); _context.MakeCurrent(_window.WindowInfo); _context.LoadAll(); _renderer = new Gk3Main.Graphics.OpenGl.OpenGLRenderer(this); return(_renderer); }
void TextureThread() { OpenTK.INativeWindow window = new OpenTK.NativeWindow(); OpenTK.Graphics.IGraphicsContext context = new OpenTK.Graphics.GraphicsContext(GLMode, window.WindowInfo); context.MakeCurrent(window.WindowInfo); TextureThreadContextReady.Set(); PendingTextures.Open(); Logger.DebugLog("Started Texture Thread"); while (window.Exists && TextureThreadRunning) { window.ProcessEvents(); TextureLoadItem item = null; if (!PendingTextures.Dequeue(Timeout.Infinite, ref item)) continue; // Already have this one loaded if (item.Data.TextureInfo.TexturePointer != 0) continue; byte[] imageBytes = null; if (item.TGAData != null) { imageBytes = item.TGAData; } else if (item.TextureData != null || item.LoadAssetFromCache) { if (item.LoadAssetFromCache) { item.TextureData = Client.Assets.Cache.GetCachedAssetBytes(item.Data.TextureInfo.TextureID); } ManagedImage mi; if (!OpenJPEG.DecodeToImage(item.TextureData, out mi)) continue; bool hasAlpha = false; bool fullAlpha = false; bool isMask = false; if ((mi.Channels & ManagedImage.ImageChannels.Alpha) != 0) { fullAlpha = true; isMask = true; // Do we really have alpha, is it all full alpha, or is it a mask for (int i = 0; i < mi.Alpha.Length; i++) { if (mi.Alpha[i] < 255) { hasAlpha = true; } if (mi.Alpha[i] != 0) { fullAlpha = false; } if (mi.Alpha[i] != 0 && mi.Alpha[i] != 255) { isMask = false; } } if (!hasAlpha) { mi.ConvertChannels(mi.Channels & ~ManagedImage.ImageChannels.Alpha); } } item.Data.TextureInfo.HasAlpha = hasAlpha; item.Data.TextureInfo.FullAlpha = fullAlpha; item.Data.TextureInfo.IsMask = isMask; imageBytes = mi.ExportTGA(); if (CacheDecodedTextures) { RHelp.SaveCachedImage(imageBytes, item.TeFace.TextureID, hasAlpha, fullAlpha, isMask); } } if (imageBytes != null) { Image img; using (MemoryStream byteData = new MemoryStream(imageBytes)) { img = OpenMetaverse.Imaging.LoadTGAClass.LoadTGA(byteData); } Bitmap bitmap = (Bitmap)img; bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); item.Data.TextureInfo.TexturePointer = RHelp.GLLoadImage(bitmap, item.Data.TextureInfo.HasAlpha); GL.Flush(); bitmap.Dispose(); } item.TextureData = null; item.TGAData = null; imageBytes = null; } context.MakeCurrent(window.WindowInfo); context.Dispose(); window.Dispose(); TextureThreadContextReady.Set(); Logger.DebugLog("Texture thread exited"); }
static void Main() { Console.Title = "Tetraquark Console"; Running = true; const string ConfigFile = "cfg.yaml"; if (File.Exists(ConfigFile)) { Console.WriteLine("Loading config file {0}", ConfigFile); Settings.Load(File.ReadAllText(ConfigFile)); } GameWatch = new Stopwatch(); GameWatch.Start(); PackMgr.Mount("Default.zip"); string[] Files = PackMgr.GetFiles(); for (int i = 0; i < Files.Length; i++) { if (Files[i].StartsWith("resources/fonts/")) ResourceMgr.Register<Font>(PackMgr.OpenFile(Files[i]), Path.GetFileNameWithoutExtension(Files[i])); else if (Files[i].StartsWith("resources/textures/")) ResourceMgr.Register<Texture>(PackMgr.OpenFile(Files[i]), Path.GetFileNameWithoutExtension(Files[i])); } Scales.Init(new Vector2f(ResX, ResY)); // OpenTK ToolkitOptions TOpt = ToolkitOptions.Default; TOpt.Backend = PlatformBackend.PreferNative; TOpt.EnableHighResolution = true; Toolkit.Init(TOpt); // SFML VideoMode VMode = new VideoMode((uint)Scales.XRes, (uint)Scales.YRes, (uint)BitsPerPixel); ContextSettings CSet = new ContextSettings((uint)DepthBits, (uint)StencilBits); CSet.MajorVersion = 4; CSet.MinorVersion = 2; CSet.AntialiasingLevel = (uint)Samples; Styles S = Styles.None; if (Border) S |= Styles.Titlebar | Styles.Close; RenderWindow RWind = new RenderWindow(VMode, "Tetraquark", S, CSet); RWind.Closed += (Snd, E) => Running = false; RWind.SetKeyRepeatEnabled(false); // OpenTK IWindowInfo WindInfo = Utilities.CreateWindowsWindowInfo(RWind.SystemHandle); var GfxMode = new GraphicsMode(new ColorFormat(BitsPerPixel), DepthBits, StencilBits, Samples, new ColorFormat(0)); var GfxCtx = new GraphicsContext(GfxMode, WindInfo, (int)CSet.MajorVersion, (int)CSet.MinorVersion, GfxCtxFlags.Default); GfxCtx.MakeCurrent(WindInfo); GfxCtx.LoadAll(); RWind.ResetGLStates(); //GL.Enable(EnableCap.FramebufferSrgb); Renderer.Init(RWind); Stopwatch Clock = new Stopwatch(); Clock.Start(); while (Running) { RWind.DispatchEvents(); while (Clock.ElapsedMilliseconds < 10) ; float Dt = (float)Clock.ElapsedMilliseconds / 1000; Clock.Restart(); Renderer.Update(Dt); Renderer.Draw(RWind); RWind.Display(); } RWind.Close(); RWind.Dispose(); Console.WriteLine("Flushing configs"); File.WriteAllText(ConfigFile, Settings.Save()); Environment.Exit(0); }
protected override void OnVSyncChanged() { graphicsContext.MakeCurrent(windowInfo); graphicsContext.SwapInterval = VSync ? 1 : 0; graphicsContext.MakeCurrent(null); }
void TextureThread() { OpenTK.INativeWindow window = new OpenTK.NativeWindow(); OpenTK.Graphics.IGraphicsContext context = new OpenTK.Graphics.GraphicsContext(GLMode, window.WindowInfo); context.MakeCurrent(window.WindowInfo); TextureThreadContextReady.Set(); PendingTextures.Open(); Logger.DebugLog("Started Texture Thread"); while (window.Exists && TextureThreadRunning) { window.ProcessEvents(); TextureLoadItem item = null; if (!PendingTextures.Dequeue(Timeout.Infinite, ref item)) continue; if (LoadTexture(item.TeFace.TextureID, ref item.Data.Texture, false)) { GL.GenTextures(1, out item.Data.TexturePointer); GL.BindTexture(TextureTarget.Texture2D, item.Data.TexturePointer); Bitmap bitmap = new Bitmap(item.Data.Texture); bool hasAlpha; if (item.Data.Texture.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { hasAlpha = true; } else { hasAlpha = false; } item.Data.IsAlpha = hasAlpha; bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height); BitmapData bitmapData = bitmap.LockBits( rectangle, ImageLockMode.ReadOnly, hasAlpha ? System.Drawing.Imaging.PixelFormat.Format32bppArgb : System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.TexImage2D( TextureTarget.Texture2D, 0, hasAlpha ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb8, bitmap.Width, bitmap.Height, 0, hasAlpha ? OpenTK.Graphics.OpenGL.PixelFormat.Bgra : OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, bitmapData.Scan0); 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); if (hasMipmap) { GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); } else { GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); } bitmap.UnlockBits(bitmapData); bitmap.Dispose(); GL.Flush(); SafeInvalidate(); } } Logger.DebugLog("Texture thread exited"); }
public override void Begin() { graphicsContext.MakeCurrent(windowInfo); platformRenderContext.Begin(0); }
private static void processSurfacesThread() { shader_p surface; bool processSurfaces; // Any method called within this thread that accesses GL must update the context like this before GL calls can be made: var context = new OpenTK.Graphics.GraphicsContext(AlethaApplication.GraphicsMode, AlethaApplication.NativeWindowContext.WindowInfo); context.MakeCurrent(AlethaApplication.NativeWindowContext.WindowInfo); processSurfaces = true; // PROCESS SURFACE SHADERS // as they come in until there are none left while (processSurfaces) { // Sort any surfaces found in unshadedSurfaces in correct model, effect, or default bins as discovered if (unshadedSurfaces.Count == 0) { // Have we processed all surfaces? // Sort to ensure correct order of transparent objects effectSurfaces.Sort((shader_p a, shader_p b) => { int order = a.shader.sort - b.shader.sort; // TODO: Sort by state here to cut down on changes? return(order); //(order == 0 ? 1 : order); }); processSurfaces = false; } { String shader_name; shader_gl shader; if (unshadedSurfaces.Count != 0) { surface = unshadedSurfaces.Last(); unshadedSurfaces.RemoveAt(unshadedSurfaces.Count - 1); //shader_p surface = unshadedSurfaces.RemoveAt(0); // var surface = unshadedSurfaces.shift(); shader_name = surface.shaderName; //shader_name = shader_name.startsWith('"')?shader_name.substring(1):shader_name; // BUG if (q3bsp.shaders.ContainsKey(shader_name)) { shader = q3bsp.shaders[shader_name]; } else { shader = null; } //shader_gl skyshader = q3bsp.shaders['textures/atcs/skybox_s']; if (shader == null) { surface.shader = glshading.buildDefault(surface); if (surface.geomType == 3) { surface.shader.model = true; modelSurfaces.Add(surface); } else { defaultSurfaces.Add(surface); } } else { glshading.loadShaderMaps(surface, shader); surface.shader = shader; if (shader.sky == true) { skybox.skyShader = shader; // Sky does not get pushed into effectSurfaces. It's a separate pass } else { effectSurfaces.Add(surface); } } } } } Console.WriteLine("Processed surfaces"); }