.NET example #2

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
the_bob
Posts: 37
Joined: Fri Dec 09, 2005 6:49 pm
Location: Michigan

.NET example #2

Post by the_bob »

When I went through example 1 in C#, I was impressed. I looked for more, but only found C++. So, I went ahead and converted example 2 to c#, and have a VS.NET 8 project working for that example. I tried to keep everything as similar as possible to the c++ version.

One of the things I ran into is that you must have both Irrlicht.dll and Irrlicht.Net.dll in the same folder as your .exe file.

Is there somewhere anyone would like me to post this example or has this already been done?
Last edited by the_bob on Thu Dec 15, 2005 4:04 am, edited 1 time in total.
3D in .NET - Who would've guessed!
the_bob
Posts: 37
Joined: Fri Dec 09, 2005 6:49 pm
Location: Michigan

Example 2 source

Post by the_bob »

OK, well, nobody really responded, but here's the source anyway. I'll probably go through all the examples I can just to get familiar with Irrlicht. If anyone wants me to continue posting the code here or some other place, let me know. I'd be happy to share.

Code: Select all

/*
This Tutorial shows how to load a Quake 3 map into the
engine, create a SceneNode for optimizing the speed of
rendering and how to create a user controlled camera.

Lets start like the HelloWorld example: We include
the irrlicht header files and an additional file to be able
to ask the user for a driver type using the console.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Irrlicht;
using Irrlicht.Video;
using Irrlicht.Core;
using Irrlicht.Scene;

namespace _02.Quake3Map
{
    class Program
    {
        /// <summary>
        /// Main entry point for the program.
        /// </summary>
        /// <param name="args">Arguments to pass the software.</param>
        [STAThread]
        static void Main(string[] args)
        {
            /*
	        Like in the HelloWorld example, we create an IrrlichtDevice with
	        createDevice(). The difference now is that we ask the user to select 
	        which hardware accelerated driver to use. The Software device would be
	        too slow to draw a huge Quake 3 map, but just for the fun of it, we make
	        this decision possible too.
	        */

            // ask user for driver
            DriverType driverType;

            // Ask user to select driver:
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Please select the driver you want for this example:");
            sb.AppendLine("(a) Direct3D 9.0c\n(b) Direct3D 8.1\n(c) OpenGL 1.5");
            sb.AppendLine("(d) Software Renderer\nApfelbaum Software Renderer");
            sb.AppendLine("(f) Null Device\n(otherKey) exit\n\n");

            // Get the user's input:
            TextReader tIn = Console.In;
            TextWriter tOut = Console.Out;
            tOut.Write(sb.ToString());
            string input = tIn.ReadLine();

            // Select device based on user's input:
            switch (input) {
                case "a":
                    driverType = DriverType.DIRECT3D9;
                    break;
                case "b":
                    driverType = DriverType.DIRECT3D8;
                    break;
                case "c":
                    driverType = DriverType.OPENGL;
                    break;
                case "d":
                    driverType = DriverType.SOFTWARE;
                    break;
                case "e":
                    driverType = DriverType.SOFTWARE2;
                    break;
                case "f":
                    driverType = DriverType.NULL_DRIVER;
                    break;
                default:
                    return;
            }

            // Create device and exit if creation fails:
            IrrlichtDevice device = new IrrlichtDevice(driverType, new Dimension2D(1024, 768), 32, true, true, true);

            if (device == null) {
                tOut.Write("Device creation failed.");
                return;
            }

            /*
            Get a pointer to the video driver and the SceneManager so that
            we do not always have to write device->getVideoDriver() and
            device->getSceneManager().
            */
            // I just left these lines here for example purposes:
            //irrv.IVideoDriver driver = device.VideoDriver;
            //irrs.ISceneManager smgr = device.SceneManager;

            /*
            To display the Quake 3 map, we first need to load it. Quake 3 maps
            are packed into .pk3 files wich are nothing other than .zip files.
            So we add the .pk3 file to our FileSystem. After it was added,
            we are able to read from the files in that archive as they would
            directly be stored on disk.
            */
            // I changed this to make it more obvious where to put the media files.
            device.FileSystem.AddZipFileArchive(Application.StartupPath + "\\map-20kdm2.pk3");

            /* 
            Now we can load the mesh by calling getMesh(). We get a pointer returned
            to a IAnimatedMesh. As you know, Quake 3 maps are not really animated,
            they are only a huge chunk of static geometry with some materials
            attached. Hence the IAnimated mesh consists of only one frame,
            so we get the "first frame" of the "animation", which is our quake level
            and create an OctTree scene node with it, using addOctTreeSceneNode().
            The OctTree optimizes the scene a little bit, trying to draw only geometry
            which is currently visible. An alternative to the OctTree would be a 
            AnimatedMeshSceneNode, which would draw always the complete geometry of 
            the mesh, without optimization. Try it out: Write addAnimatedMeshSceneNode
            instead of addOctTreeSceneNode and compare the primitives drawed by the
            video driver. (There is a getPrimitiveCountDrawed() method in the 
            IVideoDriver class). Note that this optimization with the Octree is only
            useful when drawing huge meshes consiting of lots of geometry.
            */
            // I changed this to make it more obvious where to put the media files.
            IAnimatedMesh mesh = device.SceneManager.GetMesh("20kdm2.bsp");
            ISceneNode node = null;

            if (mesh != null)
                node = device.SceneManager.AddOctTreeSceneNode(mesh, null, 0);

            /*
            Because the level was modelled not around the origin (0,0,0), we translate
            the whole level a little bit.
            */
            if (node != null)
                node.Position = (new Vector3D(-1300, -144, -1249));

            /*
            Now we only need a Camera to look at the Quake 3 map.
            And we want to create a user controlled camera. There are some
            different cameras available in the Irrlicht engine. For example the 
            Maya Camera which can be controlled compareable to the camera in Maya:
            Rotate with left mouse button pressed, Zoom with both buttons pressed,
            translate with right mouse button pressed. This could be created with
            addCameraSceneNodeMaya(). But for this example, we want to create a 
            camera which behaves like the ones in first person shooter games (FPS).
            */
            device.SceneManager.AddCameraSceneNodeFPS();

            /*
            The mouse cursor needs not to be visible, so we make it invisible.
            */
            device.CursorControl.Visible = false;

            /*
            We have done everything, so lets draw it. We also write the current
            frames per second and the drawn primitives to the caption of the
            window. The 'if (device->isWindowActive())' line is optional, but 
            prevents the engine render to set the position of the mouse cursor 
            after task switching when other program are active.
            */
            int lastFPS = -1;

            while (device.Run()) {
                if (device.WindowActive) {
                    device.VideoDriver.BeginScene(true, true, new Color(0, 200, 200, 200));
                    device.SceneManager.DrawAll();
                    device.VideoDriver.EndScene();

                    int fps = device.VideoDriver.FPS;
                    if (lastFPS != fps) {
                        device.WindowCaption = "Irrlicht Engine - Quake 3 Map example [" +
                                               device.VideoDriver.Name + "] FPS:" + fps.ToString();
                        lastFPS = fps;
                    }
                }
            }


            /*
            In the end, delete the Irrlicht device.
            */
            // Instead of device->drop, we'll use:
            GC.Collect();
        	
        }
    }
}
3D in .NET - Who would've guessed!
Dasca.ITA

more c# example

Post by Dasca.ITA »

Hi BOB,

I would like very much to see and understand some other c# example of irrlicht.

Cheers

Davide
the_bob
Posts: 37
Joined: Fri Dec 09, 2005 6:49 pm
Location: Michigan

c# examples

Post by the_bob »

I did this example so I could better learn Irrlicht myself. I've been pretty busy with Christmas coming up, but Zitzu seems to be on a roll getting through the rest of the examples.

Unfortunately, it's not possible to do example 3 yet because the .NET interface isn't quite complete yet. However, I see that Zitzu has done examples 4,5,6,7. When I get a chance, I want to try his code, but that might not be until next week some time. I'll be busy through the weekend with lots of travel and visting family, but after that, I hope to get back into the swing of things.
3D in .NET - Who would've guessed!
Zitzu
Posts: 28
Joined: Sun Jul 03, 2005 9:18 am

Re: c# examples

Post by Zitzu »

the_bob wrote:I did this example so I could better learn Irrlicht myself. I've been pretty busy with Christmas coming up, but Zitzu seems to be on a roll getting through the rest of the examples.

Unfortunately, it's not possible to do example 3 yet because the .NET interface isn't quite complete yet. However, I see that Zitzu has done examples 4,5,6,7. When I get a chance, I want to try his code, but that might not be until next week some time. I'll be busy through the weekend with lots of travel and visting family, but after that, I hope to get back into the swing of things.

I added 2 more examples today.
My code is by no mean intended to be perfect! :) but the examples work as intended and if you, the_bob, of someone else helps me to tidy them up, we could publish the examples in the wiki where they could be useful to some other people ;)
Valnarus
Posts: 24
Joined: Sun Jan 22, 2006 11:53 am
Location: Kansas

Post by Valnarus »

I know I'm a little behind but before I saw this converion I converted it myself and my solution is nearly identical (I took out the console and made it a straight windows app) but for some reason it won't load the map file 20kdm2.bsp. I've tried moving the pk3 file into the directory of the app but it still doesn't load the map. Using SharpDevelop 2.0. I've analyzed the tutorial posted here and I can't see anything that helps :(
coolsummer
Posts: 24
Joined: Fri Nov 04, 2005 1:44 pm
Location: Grafenwoehr, Germany

Post by coolsummer »

hi,

maybe if you post your error messages, and your code. this could help.

Andi
Valnarus
Posts: 24
Joined: Sun Jan 22, 2006 11:53 am
Location: Kansas

Post by Valnarus »

Code: Select all

using System;
using System.Windows.Forms;
using Irrlicht;
using Irrlicht.Video;
using Irrlicht.Core;
using Irrlicht.Scene;

namespace Quake3Map
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		
		private IrrlichtDevice device;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent()
		{
			this.cBoxDriverType = new System.Windows.Forms.ComboBox();
			this.labelRDriver = new System.Windows.Forms.Label();
			this.btnBegin = new System.Windows.Forms.Button();
			this.btnQuit = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// cBoxDriverType
			// 
			this.cBoxDriverType.FormattingEnabled = true;
			this.cBoxDriverType.Items.AddRange(new object[] {
									"Direct3D 9",
									"Direct3D 8"});
			this.cBoxDriverType.Location = new System.Drawing.Point(127, 8);
			this.cBoxDriverType.Name = "cBoxDriverType";
			this.cBoxDriverType.Size = new System.Drawing.Size(88, 21);
			this.cBoxDriverType.TabIndex = 0;
			this.cBoxDriverType.Text = "OpenGL";
			// 
			// labelRDriver
			// 
			this.labelRDriver.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.labelRDriver.Location = new System.Drawing.Point(12, 9);
			this.labelRDriver.Name = "labelRDriver";
			this.labelRDriver.Size = new System.Drawing.Size(109, 23);
			this.labelRDriver.TabIndex = 1;
			this.labelRDriver.Text = "Rendering Driver:";
			this.labelRDriver.UseCompatibleTextRendering = true;
			// 
			// btnBegin
			// 
			this.btnBegin.Location = new System.Drawing.Point(12, 35);
			this.btnBegin.Name = "btnBegin";
			this.btnBegin.Size = new System.Drawing.Size(75, 23);
			this.btnBegin.TabIndex = 2;
			this.btnBegin.Text = "Begin";
			this.btnBegin.UseCompatibleTextRendering = true;
			this.btnBegin.UseVisualStyleBackColor = true;
			this.btnBegin.MouseClick += new System.Windows.Forms.MouseEventHandler(this.BeginMouseClick);
			// 
			// btnQuit
			// 
			this.btnQuit.Location = new System.Drawing.Point(140, 35);
			this.btnQuit.Name = "btnQuit";
			this.btnQuit.Size = new System.Drawing.Size(75, 23);
			this.btnQuit.TabIndex = 3;
			this.btnQuit.Text = "Quit";
			this.btnQuit.UseCompatibleTextRendering = true;
			this.btnQuit.UseVisualStyleBackColor = true;
			this.btnQuit.Click += new System.EventHandler(this.QuitMouseClick);
			// 
			// MainForm
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(229, 70);
			this.Controls.Add(this.btnQuit);
			this.Controls.Add(this.btnBegin);
			this.Controls.Add(this.labelRDriver);
			this.Controls.Add(this.cBoxDriverType);
			this.Name = "MainForm";
			this.Text = "Quake 3 Map Tutorial";
			this.ResumeLayout(false);
		}
		private System.Windows.Forms.Label labelRDriver;
		private System.Windows.Forms.Button btnBegin;
		private System.Windows.Forms.Button btnQuit;
		private System.Windows.Forms.ComboBox cBoxDriverType;
		#endregion
		
		private void InitIrrlicht()
		{
			// Ask user for driver type
			DriverType driverType = DriverType.OPENGL;
			switch (cBoxDriverType.Text)
			{
				case "OpenGL":
					break;
				case "Direct3D 9":
					driverType = DriverType.DIRECT3D9;					
					break;
				case "Direct3D 8":
					driverType = DriverType.DIRECT3D8;
					break;
			}
			
			device = new IrrlichtDevice(driverType, new Dimension2D(640, 480), 32, false, true, false, true, this.Handle);
			
			device.FileSystem.AddZipFileArchive(@"map20kdm2.pk3");
			IAnimatedMesh mesh = device.SceneManager.GetMesh("20kdm2.bsp");
			ISceneNode node = null;
			try
			{
				node = device.SceneManager.AddOctTreeSceneNode(mesh, null, 0);
				node.Position.Set(new Vector3D(-1300, -144, -1249));
			}
			catch(NullReferenceException)
			{
				Application.Exit();
			}
			
			device.SceneManager.AddCameraSceneNodeFPS();
			device.CursorControl.Visible = false;
			
		}
		
		private void DrawScene()
		{
			int lastFPS = -1;
			
			while(device.Run())
			{
				if (this.Enabled)
				{
					device.VideoDriver.BeginScene(true, false, new Color(0, 0, 0, 255));
					device.SceneManager.DrawAll();
					device.VideoDriver.EndScene();
					
					int fps = device.VideoDriver.FPS;
					
					if (lastFPS != fps)
					{
						string str = "Irrlicht Engine - Quake 3 Map example [" + 
							device.VideoDriver.Name + "] FPS:" + fps;
						this.Text = str;
						lastFPS = fps;
					}
				}
			}
			
			device.CloseDevice();
		}
		
		
		void BeginMouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.labelRDriver.Visible = false;
			this.cBoxDriverType.Visible = false;
			this.btnBegin.Visible = false;
			this.btnQuit.Visible = false;
			this.Cursor.Dispose();
			this.Width = 648;
			this.Height = 514;
			InitIrrlicht();
			DrawScene();
		}
		
		void QuitMouseClick(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
	}
}

Compile and Run with console and it says "Could not load mesh, because file could not be opened.: 20kdm2.bsp"

Other than that the only error I get is the exceptions thrown after closing the program when the OpenGL drivers are used. But that appears to be a known bug and as far as I can tell would be completely unrelated to my problem.
coolsummer
Posts: 24
Joined: Fri Nov 04, 2005 1:44 pm
Location: Grafenwoehr, Germany

Post by coolsummer »

hi,

don´t know exactly if you renamed the file, but there is a little typing error in your code

your code
device.FileSystem.AddZipFileArchive(@"map20kdm2.pk3");

must be
device.FileSystem.AddZipFileArchive(@"map-20kdm2.pk3");

If you didn´t renamed the file you forgot a "-".

Otherwise i see no problems. It compiles and runs without errors for me

Andi
Valnarus
Posts: 24
Joined: Sun Jan 22, 2006 11:53 am
Location: Kansas

Post by Valnarus »

Snicker... thanks. I actually caught that before I read yer post but I feel kinda dumb now. Anywho, there is still a severe lack of mapage but it does load that file now. I can see little blips of graphics here and there so this might be interresting.
Valnarus
Posts: 24
Joined: Sun Jan 22, 2006 11:53 am
Location: Kansas

Post by Valnarus »

And........ the zbuffer is ever so slightly important.
coolsummer
Posts: 24
Joined: Fri Nov 04, 2005 1:44 pm
Location: Grafenwoehr, Germany

Post by coolsummer »

And........ the zbuffer is ever so slightly important.
yes, i changed that, too. Forgot to wrote it in my last post, sorry
Locked