From b726fbe95fa850f1e51dbc6a4a76f701dbb1db6b Mon Sep 17 00:00:00 2001 From: Treeki Date: Sat, 7 May 2011 03:50:42 +0200 Subject: added render-model command to ConsoleApp --- ConsoleApp/ConsoleApp.csproj | 5 +++ ConsoleApp/Main.cs | 3 ++ ConsoleApp/ModelCommands.cs | 26 +++++++++++++++ ConsoleApp/RenderWindow.cs | 77 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 ConsoleApp/RenderWindow.cs (limited to 'ConsoleApp') 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 @@ + + False + ..\..\CSharp\opentk\Binaries\OpenTK\Release\OpenTK.dll + @@ -40,6 +44,7 @@ + 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 \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(); + } + + + } +} + -- cgit v1.2.3