From 915c8f997eb9d077054381d8318785abe5ec71f1 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 15 Feb 2010 17:05:24 +1300 Subject: [PATCH] gl context works --- .gitignore | 5 ++ OpenRa.Game/Graphics/LineRenderer.cs | 2 +- OpenRa.Game/Graphics/MappedImage.cs | 2 +- OpenRa.Game/Graphics/Renderer.cs | 8 +-- OpenRa.Game/Graphics/Sheet.cs | 2 +- OpenRa.Game/Graphics/SpriteRenderer.cs | 2 +- OpenRa.Game/Graphics/TerrainRenderer.cs | 2 +- OpenRa.Game/Graphics/Vertex.cs | 2 +- OpenRa.Gl/GraphicsDevice.cs | 68 +++++++++++++++++++++---- OpenRa.Gl/OpenRa.Gl.csproj | 3 +- 10 files changed, 75 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 9d2ab70a49..c96b870526 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ sheet-*.png log.txt /replay.rep + +# dependency DLLs (different for every platform!) +cg.dll +cgGL.dll +glfw.dll diff --git a/OpenRa.Game/Graphics/LineRenderer.cs b/OpenRa.Game/Graphics/LineRenderer.cs index c1b6841b63..21a2e5fe01 100644 --- a/OpenRa.Game/Graphics/LineRenderer.cs +++ b/OpenRa.Game/Graphics/LineRenderer.cs @@ -1,5 +1,5 @@ using System.Drawing; -using OpenRa.Gl; +using OpenRa.GlRenderer; namespace OpenRa.Graphics { diff --git a/OpenRa.Game/Graphics/MappedImage.cs b/OpenRa.Game/Graphics/MappedImage.cs index 8296e0d0ff..548952911c 100644 --- a/OpenRa.Game/Graphics/MappedImage.cs +++ b/OpenRa.Game/Graphics/MappedImage.cs @@ -1,6 +1,6 @@ using System.Xml; using System.Drawing; -using OpenRa.Gl; +using OpenRa.GlRenderer; using System.IO; namespace OpenRa.Graphics { diff --git a/OpenRa.Game/Graphics/Renderer.cs b/OpenRa.Game/Graphics/Renderer.cs index f391af9dc9..31cf83fe8d 100644 --- a/OpenRa.Game/Graphics/Renderer.cs +++ b/OpenRa.Game/Graphics/Renderer.cs @@ -1,6 +1,6 @@ using System.Drawing; using System.Windows.Forms; -using OpenRa.Gl; +using OpenRa.GlRenderer; using OpenRa.FileFormats; using OpenRa.Support; @@ -22,10 +22,10 @@ namespace OpenRa.Graphics //readonly SpriteHelper sh; //readonly FontHelper fhDebug, fhTitle; - public Renderer(Control host, Size resolution, bool windowed) + public Renderer(Control control, Size resolution, bool windowed) { - host.ClientSize = resolution; - device = new GraphicsDevice(host, resolution.Width, resolution.Height, windowed, false); + control.ClientSize = resolution; + device = new GraphicsDevice(control, resolution.Width, resolution.Height, windowed, false); SpriteShader = new Shader(device, FileSystem.Open("world-shp.fx")); SpriteShader.Quality = ShaderQuality.Low; diff --git a/OpenRa.Game/Graphics/Sheet.cs b/OpenRa.Game/Graphics/Sheet.cs index 64348c0e99..0734550084 100644 --- a/OpenRa.Game/Graphics/Sheet.cs +++ b/OpenRa.Game/Graphics/Sheet.cs @@ -1,5 +1,5 @@ using System.Drawing; -using OpenRa.Gl; +using OpenRa.GlRenderer; using OpenRa.FileFormats; namespace OpenRa.Graphics diff --git a/OpenRa.Game/Graphics/SpriteRenderer.cs b/OpenRa.Game/Graphics/SpriteRenderer.cs index 47a03f6b49..06bf302d2b 100644 --- a/OpenRa.Game/Graphics/SpriteRenderer.cs +++ b/OpenRa.Game/Graphics/SpriteRenderer.cs @@ -1,4 +1,4 @@ -using OpenRa.Gl; +using OpenRa.GlRenderer; namespace OpenRa.Graphics { diff --git a/OpenRa.Game/Graphics/TerrainRenderer.cs b/OpenRa.Game/Graphics/TerrainRenderer.cs index cc90444efc..2ff9fe440d 100644 --- a/OpenRa.Game/Graphics/TerrainRenderer.cs +++ b/OpenRa.Game/Graphics/TerrainRenderer.cs @@ -1,5 +1,5 @@ using System.Drawing; -using OpenRa.Gl; +using OpenRa.GlRenderer; using IjwFramework.Collections; using OpenRa.FileFormats; diff --git a/OpenRa.Game/Graphics/Vertex.cs b/OpenRa.Game/Graphics/Vertex.cs index 72ca506750..aba78e887d 100644 --- a/OpenRa.Game/Graphics/Vertex.cs +++ b/OpenRa.Game/Graphics/Vertex.cs @@ -1,5 +1,5 @@ using System.Runtime.InteropServices; -using OpenRa.Gl; +using OpenRa.GlRenderer; namespace OpenRa.Graphics { diff --git a/OpenRa.Gl/GraphicsDevice.cs b/OpenRa.Gl/GraphicsDevice.cs index beb9aadc06..7d9469a8e6 100644 --- a/OpenRa.Gl/GraphicsDevice.cs +++ b/OpenRa.Gl/GraphicsDevice.cs @@ -1,22 +1,70 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; using System.Drawing; using System.IO; +using System.Windows.Forms; -namespace OpenRa.Gl +using Tao.OpenGl; +using Tao.Cg; +using Tao.Platform.Windows; +using System.Runtime.InteropServices; + +namespace OpenRa.GlRenderer { public class GraphicsDevice { - public GraphicsDevice(Control host, int width, int height, bool fullscreen, bool vsync) { } - public void EnableScissor(int left, int top, int width, int height) { } - public void DisableScissor() { } + Graphics g; + IntPtr dc; + IntPtr rc; + + public GraphicsDevice(Control control, int width, int height, bool fullscreen, bool vsync) + { + g = control.CreateGraphics(); + dc = g.GetHdc(); + + var pfd = new Gdi.PIXELFORMATDESCRIPTOR + { + nSize = (short)Marshal.SizeOf(typeof(Gdi.PIXELFORMATDESCRIPTOR)), + nVersion = 1, + dwFlags = Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DRAW_TO_BITMAP | Gdi.PFD_DOUBLEBUFFER, + iPixelType = Gdi.PFD_TYPE_RGBA, + cColorBits = 24, + iLayerType = Gdi.PFD_MAIN_PLANE + }; + + var iFormat = Gdi.ChoosePixelFormat(dc, ref pfd); + Gdi.SetPixelFormat(dc, iFormat, ref pfd); + + rc = Wgl.wglCreateContext(dc); + if (rc == IntPtr.Zero) + throw new InvalidOperationException("can't create wglcontext"); + Wgl.wglMakeCurrent(dc, rc); + } + + public void EnableScissor(int left, int top, int width, int height) + { + Gl.glScissor(left, top, width, height); + Gl.glEnable(Gl.GL_SCISSOR_TEST); + } + + public void DisableScissor() + { + Gl.glDisable(Gl.GL_SCISSOR_TEST); + } + public void Begin() { } public void End() { } - public void Clear(Color c) { } - public void Present() { } + + public void Clear(Color c) + { + Gl.glClearColor(1, 1, 1, 1); + Gl.glClear(Gl.GL_COLOR_BUFFER_BIT); + } + + public void Present() + { + Wgl.wglSwapBuffers(dc); + } + public void DrawIndexedPrimitives(PrimitiveType pt, Range vertices, Range indices) { } public void DrawIndexedPrimitives(PrimitiveType pt, int numVerts, int numPrimitives) { } } diff --git a/OpenRa.Gl/OpenRa.Gl.csproj b/OpenRa.Gl/OpenRa.Gl.csproj index 2e6920740f..8706185596 100644 --- a/OpenRa.Gl/OpenRa.Gl.csproj +++ b/OpenRa.Gl/OpenRa.Gl.csproj @@ -8,7 +8,7 @@ {67CF1A10-C5F6-48FA-B1A7-FE83BE4CE2CC} Library Properties - OpenRa.Gl + OpenRa.GlRenderer OpenRa.Gl v3.5 512 @@ -48,6 +48,7 @@ +