Irrlicht C# Port

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Irrlicht C# Port

Post by rz950 »

I'm willing to do a official C# port as long as long as people don't complain when there are bugs or compiling problems. :lol:
I think that a C# wrapper on top of C++ won't show the power of C# cause C# is fast and much easier to learn then C++,
trust me I learned how to make a DirectX demo in a week. In C++ that took me three week cause i had some stupid header problem.

If anyone is looking forward for a .net port, then try contributing by creating a website and such.
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Irrlicht C# Port

Post by rz950 »

After look over some of the C++ code again, so Dependence isn't a problem since DirectX has C# support and Opengl does too and Ode, sdl has support as well. Not sure about Libpng, Zlib, and libjpeg but it should but if it doesn't then I'll either try telling the author to work on a C# version or I'll port it myself.

I would like comments and suggestions on this port.

Features Planned for Irrlicht# 0.1:
Window working and loads textures
DirectX Support
Opengl Support
Sdl Support
Linux Support via Mono
dracflamloc
Posts: 142
Joined: Sat Dec 11, 2004 8:13 am
Contact:

Post by dracflamloc »

Cool a C# port of Irrlicht! :wink:
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Post by rz950 »

Wow, Someone Replyed :shock:
I thought nobody wanted a C# port
since no one said anyhthing about it
hehe :roll:
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Post by rz950 »

Well, I can't find Libpng and Zlib anywhere for C# :(
and System.Drawing.Bitmap supports loading png files
but not as good as libpng. I'll try to port it or just use
System.Drawing.Bitmap for now even though Mono
doesn't support it.
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Post by rz950 »

This is the C# source on creating a device and loading a scene. :D

Code: Select all

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using D3D = Microsoft.DirectX.Direct3D;

namespace Irrlicht.DirectX9
{
    public class CreateDevice : Form
    {
        // Our global variables for this project
        Device device = null; // Our rendering device

        public CreateDevice()
       {
            // Set the initial size of our form
            this.ClientSize = new System.Drawing.Size(640, 800);
        }


        public bool InitializeGraphics()
        {
            try
            {
                // Now let's setup our D3D stuff
                PresentParameters presentParams = new PresentParameters();
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
                return true;
            }
            catch (DirectXException)
            {
                return false;
            }
        }
        private void Render()
        {
            if (device == null)
                return;

            //Clear the backbuffer to a blue color 
            device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();

            // Rendering of scene objects can happen here

            //End the scene
            device.EndScene();
            device.Present();
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render(); // Render on painting
        }
        
        static void Main()
        {

            using (CreateDevice frm = new CreateDevice())
            {
                if (!frm.InitializeGraphics()) // Initialize Direct3D
                {
                    MessageBox.Show("Could not initialize Direct3D.");
                    return;
                }
                frm.Show();

                // While the form is still valid, render and process messages
                while (frm.Created)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}
I'm working on texture and meshes but most of this code comes from tutorials or samples since I'm not too good with C# yet :o

Oh, if anyone knows C# and has a better way of creating a device just
go ahead and post code :)
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Post by rz950 »

The texture code is here, it took me a while to copy and paste from websites 8) .... Just Kidding

Code: Select all

 using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace Irrlicht.DirectX9
{
    public class Textures : Form
    {
        // Our global variables
        Device device = null; // Our rendering device
        VertexBuffer vertexBuffer = null;
        Texture texture = null;
        PresentParameters presentParams = new PresentParameters();
        bool pause = false;


        public Textures()
        {
            // Set the initial size of our form
            this.ClientSize = new System.Drawing.Size(400, 300);
           }

        public bool InitializeGraphics()
        {
            try
            {
                presentParams.Windowed = true; // We don't want to run fullscreen
                presentParams.SwapEffect = SwapEffect.Discard; // Discard the frames 
                presentParams.EnableAutoDepthStencil = true; // Turn on a Depth stencil
                presentParams.AutoDepthStencilFormat = DepthFormat.D16; // And the stencil format
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //Create a device
                device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnCreateDevice(device, null);
                this.OnResetDevice(device, null);
                pause = false;

                return true;
            }
            catch (DirectXException)
            {
                // Catch any errors and return a failure
                return false;
            }
        }
        public void OnCreateDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
            // Now Create the VB
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 100, dev, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default);
            vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
            this.OnCreateVertexBuffer(vertexBuffer, null);
        }
        public void OnResetDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
            // Turn off culling, so we see the front and back of the triangle
            dev.RenderState.CullMode = Cull.None;
            // Turn off D3D lighting
            dev.RenderState.Lighting = false;
            // Turn on the ZBuffer
            dev.RenderState.ZBufferEnable = true;
            // Now create our texture
            texture = TextureLoader.FromFile(dev, Application.StartupPath + @"\..\..\banana.bmp");
        }
        public void OnCreateVertexBuffer(object sender, EventArgs e)
        {
            VertexBuffer vb = (VertexBuffer)sender;
            // Create a vertex buffer (100 customervertex)
            CustomVertex.PositionNormalTextured[] verts = (CustomVertex.PositionNormalTextured[])vb.Lock(0, 0); // Lock the buffer (which will return our structs)
            for (int i = 0; i < 50; i++)
            {
                // Fill up our structs
                float theta = (float)(2 * Math.PI * i) / 49;
                verts[2 * i].Position = new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta));
                verts[2 * i].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));
                verts[2 * i].Tu = ((float)i) / (50 - 1);
                verts[2 * i].Tv = 1.0f;
                verts[2 * i + 1].Position = new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta));
                verts[2 * i + 1].Normal = new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta));
                verts[2 * i + 1].Tu = ((float)i) / (50 - 1);
                verts[2 * i + 1].Tv = 0.0f;
            }
            // Unlock (and copy) the data
            vb.Unlock();
        }
        private void SetupMatrices()
        {
            // For our world matrix, we will just rotate the object about the y-axis.
            device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 1000.0f);

            // Set up our view matrix. A view matrix can be defined given an eye point,
            // a point to lookat, and a direction for which way is up. Here, we set the
            // eye five units back along the z-axis and up three units, look at the
            // origin, and define "up" to be in the y-direction.
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            // For the projection matrix, we set up a perspective transform (which
            // transforms geometry from 3D view space to 2D viewport space, with
            // a perspective divide making objects smaller in the distance). To build
            // a perpsective transform, we need the field of view (1/4 pi is common),
            // the aspect ratio, and the near and far clipping planes (which define at
            // what distances geometry should be no longer be rendered).
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1.0f, 1.0f, 100.0f);
        }

        private void Render()
        {
            if (pause)
                return;

            //Clear the backbuffer to a blue color 
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            // Setup the world, view, and projection matrices
            SetupMatrices();
            // Setup our texture. Using textures introduces the texture stage states,
            // which govern how textures get blended together (in the case of multiple
            // textures) and lighting information. In this case, we are modulating
            // (blending) our texture with the diffuse color of the vertices.
            device.SetTexture(0, texture);
            device.TextureState[0].ColorOperation = TextureOperation.Modulate;
            device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
            device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
            device.TextureState[0].AlphaOperation = TextureOperation.Disable;

            device.SetStreamSource(0, vertexBuffer, 0);
            device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4 * 25) - 2);
            //End the scene
            device.EndScene();
            // Update the screen
            device.Present();
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render(); // Render on painting
        }
        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
                this.Dispose(); // Esc was pressed
        }
        protected override void OnResize(System.EventArgs e)
        {
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
        }

        static void Main()
        {

            using (Textures frm = new Textures())
            {
                if (!frm.InitializeGraphics()) // Initialize Direct3D
                {
                    MessageBox.Show("Could not initialize Direct3D.");
                    return;
                }
                frm.Show();

                // While the form is still valid, render and process messages
                while (frm.Created)
                {
                    frm.Render();
                    Application.DoEvents();
                }
            }
        }

    }
}


Need to add better texture support
but that requires me reading tutorials :)
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Try to keep most functions and their arguments right. Like pass the same values to the createDevice() function etc. Then it'll look more like a real port. Gj.
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
rz950
Posts: 9
Joined: Thu Dec 30, 2004 1:57 pm

Post by rz950 »

Yea, I know, I'm trying to keep it like irrlicht but in C#. Problem is that it isn't that easy,:( and also I will be changing this code to make it more like irrlicht so that if your a C++ developer and would like to try C# you can make the change with almost no problems. 8)


It isn't easy to port something if you don't know the language :lol:
Just kidding
pxp
Posts: 1
Joined: Sat Feb 19, 2005 3:16 pm

C# port status

Post by pxp »

Hi,

i'm curious as what the status of the port is.
pennharris
Posts: 1
Joined: Thu Jul 03, 2014 8:23 am

Re: Irrlicht C# Port

Post by pennharris »

Why not, i also interested in C# port, like howto work with C# serial port communication.


Penn
Life is getting better
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Re: Irrlicht C# Port

Post by Darktib »

dat necro...
kevingreen
Posts: 1
Joined: Mon Dec 08, 2014 6:32 am

Re: Irrlicht C# Port

Post by kevingreen »

I have one question regarding serial port
My product which i developed in .net 2.0 [c#.net], i need to used serial port class and functionality to connect to remote machine or device i.e my has to connect and remote deivce and load the data and also i need UI Desing some interactive
did any have tried serial port please complete overview with UI support
thank a lot
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irrlicht C# Port

Post by CuteAlien »

bot or not bot ... that is the question
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply