diff options
-rw-r--r-- | ConsoleApp/ConsoleApp.csproj | 5 | ||||
-rw-r--r-- | ConsoleApp/Main.cs | 3 | ||||
-rw-r--r-- | ConsoleApp/ModelCommands.cs | 26 | ||||
-rw-r--r-- | ConsoleApp/RenderWindow.cs | 77 |
4 files changed, 111 insertions, 0 deletions
diff --git a/ConsoleApp/ConsoleApp.csproj b/ConsoleApp/ConsoleApp.csproj index 451221d..69836fa 100644 --- a/ConsoleApp/ConsoleApp.csproj +++ b/ConsoleApp/ConsoleApp.csproj @@ -32,6 +32,10 @@ <ItemGroup> <Reference Include="System" /> <Reference Include="System.Drawing" /> + <Reference Include="OpenTK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\CSharp\opentk\Binaries\OpenTK\Release\OpenTK.dll</HintPath> + </Reference> </ItemGroup> <ItemGroup> <Compile Include="Main.cs" /> @@ -40,6 +44,7 @@ <Compile Include="ResFileCommands.cs" /> <Compile Include="ModelCommands.cs" /> <Compile Include="TextureCommands.cs" /> + <Compile Include="RenderWindow.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/ConsoleApp/Main.cs b/ConsoleApp/Main.cs index 56f2a8d..580d398 100644 --- a/ConsoleApp/Main.cs +++ b/ConsoleApp/Main.cs @@ -16,6 +16,7 @@ namespace ConsoleApp { CommandLookup.Add("export-collada", new ExportColladaCommand()); CommandLookup.Add("export-textures", new ExportTexturesCommand()); CommandLookup.Add("replace-image", new ReplaceImageCommand()); + CommandLookup.Add("render-model", new RenderModelCommand()); } public static void Main(string[] args) { @@ -23,6 +24,8 @@ namespace ConsoleApp { //args = new string[] { "list", "/home/me/Dropbox/NEWERsmbw/bigbrick/big_renga_block.brres" }; //System.IO.Directory.SetCurrentDirectory("/home/me/Games/Newer/CGround"); //args = new string[] { "export-collada", "regular.brres", "large.dae", "circle_ground_L" }; + //System.IO.Directory.SetCurrentDirectory("/home/me/Games/Newer/ModelRev"); + //args = new string[] { "import-obj", "MMFullWorld.brres", "fullworld-Z19nvrc.obj", "WorldBase", "map" }; Console.WriteLine("NW4RTools by Treeki"); diff --git a/ConsoleApp/ModelCommands.cs b/ConsoleApp/ModelCommands.cs index 9bbf1f3..4cd406c 100644 --- a/ConsoleApp/ModelCommands.cs +++ b/ConsoleApp/ModelCommands.cs @@ -114,5 +114,31 @@ namespace ConsoleApp { + " more settings will be added later."; } } + + + + public class RenderModelCommand : ResFileCommand { + public RenderModelCommand() : base("render a model in a window using OpenGL") { + } + + protected override void OperateOnFile(string[] args) { + if (args.Length != 1) { + Console.WriteLine("invalid syntax.\n" + GetHelp(null)); + return; + } + + using (var rwin = new RenderWindow()) { + rwin.Title = args[0]; + rwin.SetModel(TargetFile, args[0]); + rwin.Run(1, 1); + } + } + + public override string GetHelp(string[] args) { + return "syntax: render-model <brres-filename> <model-name>\n" + + " renders a model in a window using OpenGL.\n\n" + + " quite limited at the moment, will be improved later"; + } + } } diff --git a/ConsoleApp/RenderWindow.cs b/ConsoleApp/RenderWindow.cs new file mode 100644 index 0000000..f6f5951 --- /dev/null +++ b/ConsoleApp/RenderWindow.cs @@ -0,0 +1,77 @@ +using System; +using NW4RTools; +using OpenTK; +using OpenTK.Graphics; +using OpenTK.Graphics.OpenGL; +using OpenTK.Input; + +namespace ConsoleApp { + public class RenderWindow : GameWindow { + public RenderWindow() : base(800, 600, GraphicsMode.Default, "Test Model") { + + } + + private NW4RTools.Models.OpenGL.GLModel m_glModel; + + public void SetModel(ResFile rf, string modelName) { + m_glModel = new NW4RTools.Models.OpenGL.GLModel(rf, modelName); + } + + + protected override void OnLoad(EventArgs e) { + base.OnLoad(e); + + GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f); + GL.Enable(EnableCap.DepthTest); + + GL.Enable(EnableCap.Blend); + GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); + + GL.Disable(EnableCap.CullFace); + + GL.Enable(EnableCap.Lighting); + + GL.ShadeModel(ShadingModel.Smooth); + + GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0, 500, 1000, 0)); + GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, new Color4(1, 1, 1, 1)); + GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, 50); + GL.Enable(EnableCap.Light0); + + GL.Light(LightName.Light1, LightParameter.Position, new Vector4(1000, 500, 0, 0)); + GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, new Color4(1, 1, 1, 1)); + GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, 50); + GL.Enable(EnableCap.Light1); + + m_glModel.Prepare(Context); + } + + protected override void OnResize(EventArgs e) { + base.OnResize(e); + + GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); + + Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 2000.0f); + GL.MatrixMode(MatrixMode.Projection); + GL.LoadMatrix(ref projection); + } + + protected override void OnUpdateFrame(FrameEventArgs e) { + base.OnUpdateFrame(e); + + GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + + Matrix4 modelview = Matrix4.LookAt(new Vector3(0, 400, 1000), new Vector3(0, 0, 0), Vector3.UnitY); + //Matrix4 modelview = Matrix4.LookAt(new Vector3(-50, 20, 50), new Vector3(0, 0, 0), Vector3.UnitY); + GL.MatrixMode(MatrixMode.Modelview); + GL.LoadMatrix(ref modelview); + + m_glModel.Render(Context); + + SwapBuffers(); + } + + + } +} + |