-Due to spam, wiki editing has been restricted to the dev team only. Sorry for any inconvenience-
Custom ScreenNode
From IrrlichtSharp
using System;
using System.Drawing;
using System.Windows.Forms;
using IrrlichtSharp;
using IrrlichtSharp.Scene;
using IrrlichtSharp.Core;
using IrrlichtSharp.Video;
namespace CustomSceneNodeTest
{
public class PyramidSceneNode : SceneNode // We're making a pyramid. All scene nodes
{ // inherit from the SceneNode base class.
protected SizeF nodeSize; // Determines the height of our pyramid.
S3DVertex[] Vertices; // Will contain all of the vertices of our pyramid.
int[] Indices; // Will contain the order to use when
// drawing the triangles for our pyramid.
public Material Material; // Will contain whatever texture our
// pyramid is rendered with.
public PyramidSceneNode(SizeF size, Vector3Df position, SceneNode parent, SceneManager mgr, int id)
: base(parent, mgr, id, position) // Don't forget to pass information on to the base class.
{
this.Material = new Material(); // Time to initialize the class with the information needed
this.Size = size; // to build the pyramid.
// A pyramid is made of 4 triangles.
// Triangle #1 , #2 , #3 , #4
Indices = new int[12] { 0,1,2, 3,4,5, 6,7,8, 9,10,11 };
Vector3Df[] vectors = new Vector3Df[4]; // Vectors containing the coordinates of the
// 4 corners of our pyramid.
// A little geometry and poof!
vectors[0] = new Vector3Df(-Size.Height * (float)Math.Sqrt(3) / 3, 0, -Size.Height / 3);
vectors[1] = new Vector3Df(0, Size.Height, 0);
vectors[2] = new Vector3Df(Size.Height * (float)Math.Sqrt(3) / 3, 0, -Size.Height / 3);
vectors[3] = new Vector3Df(0, 0, 2 * Size.Height / 3);
Vector3Df normals = new Vector3Df(0, 0, 0); // We're not too concerned with normals at the moment.
Color color = Color.FromArgb(255, 255, 255, 255); // This color will be hidden by our texture.
Vector2Df[] tcoords = new Vector2Df[3]; // These 2D vectors contain the texture mapping coordinates.
tcoords[0] = new Vector2Df(0, 1); // Bottom left corner of the image.
tcoords[1] = new Vector2Df(0.5f, 0); // Top center point of the image.
tcoords[2] = new Vector2Df(1, 1); // Bottom right corner of the image.
Vertices = new S3DVertex[12]; // Time to put it all together in our vertex buffer.
// Triangle #
Vertices[0] = new S3DVertex(vectors[0], normals, color, tcoords[0]);
Vertices[1] = new S3DVertex(vectors[1], normals, color, tcoords[1]); // 1 (Front)
Vertices[2] = new S3DVertex(vectors[2], normals, color, tcoords[2]);
Vertices[3] = new S3DVertex(vectors[2], normals, color, tcoords[0]);
Vertices[4] = new S3DVertex(vectors[1], normals, color, tcoords[1]); // 2 (Back Right)
Vertices[5] = new S3DVertex(vectors[3], normals, color, tcoords[2]);
Vertices[6] = new S3DVertex(vectors[3], normals, color, tcoords[0]);
Vertices[7] = new S3DVertex(vectors[1], normals, color, tcoords[1]); // 3 (Back Left)
Vertices[8] = new S3DVertex(vectors[0], normals, color, tcoords[2]);
Vertices[9] = new S3DVertex(vectors[0], normals, color, tcoords[0]);
Vertices[10] = new S3DVertex(vectors[2], normals, color, tcoords[1]); // 4 (Bottom)
Vertices[11] = new S3DVertex(vectors[3], normals, color, tcoords[2]);
}
public override void OnPreRender() // Before rendering, the node must be registered for rendering in
{ // the appropriate scene manager.
if (IsVisible) // If the scene is visible
mSceneManager.RegisterNodeForRendering(this); // let's register our node for rendering.
base.OnPreRender(); // Takes care of the rest of the backend prerendering processes.
}
public override void Render() // Time to define what needs to be done to put our pyramid together.
{
IVideoDriver driver = mSceneManager.VideoDriver; // Let's make this easier for our busy fingers.
if (driver == null) // If the driver is null we can't draw.
return;
driver.SetMaterial(Material); // Set the material used to draw our scene node.
driver.SetTransform(TransformationState.World, AbsoluteTransformation); // We're switching to the
// world viewpoint.
driver.DrawIndexedTriangleList(Vertices, Indices, 12); // We use a triangle list to draw our pyramid.
}
public override Material GetMaterial(int i) // We need this property for the rendering process
{
return Material;
}
public SizeF Size
{
get
{
return nodeSize;
}
set // If a bounding box was defined in this scene node, this would be a good place to calculate it.
{
nodeSize = value;
}
}
public override int MaterialCount // SceneNode has a default of 0 Materials, this property needs to define
{ // the number of materials used to render our pyramid.
get
{
return 1;
}
}
}
public class IrrMain : IrrlichtSharp.IEventReceiver
{
IlsDevice device = null;
public IrrMain()
{
CreationParameters myParameters = new CreationParameters();
myParameters.DriverType = DriverType.DirectX9;
myParameters.Bits = 32;
myParameters.EventReceiver = this;
device = new IlsDevice(myParameters);
}
public bool OnEvent(IrrEvent e)
{
if (e.EventType == EventTypes.KeyInput)
{
if (e.Key.Key == Keys.Escape && e.Key.PressedDown)
{
device.Quit = true;
return true;
}
}
return false;
}
[STAThread]
static void Main() // Some of this part is covered in a previous example.
{ // I will only comment on the changed area.
Logger.LoggingLevel = Logger.Level.Information;
IrrMain main = new IrrMain();
IlsDevice device = main.device;
IVideoDriver driver = device.VideoDriver;
SceneManager smgr = device.SceneManager;
ICameraSceneNode camera =
smgr.AddCameraSceneNodeFPS(null, -1, 250f, 100f);
device.CursorControl.Visible = false;
// Here's our pyramid. Currently, custom scene nodes are not added to the scenemanager in the same
// way that predefined scene nodes are. Instead they're added directly through the constructor.
PyramidSceneNode csnode =
new PyramidSceneNode(new SizeF(0f, 5f), // The second number in the size represents the height.
new Vector3Df(0f, -2.5f, 15f), // This represents the position.
smgr, // The parent scene node in this case is the root scene node of the scenemanager.
smgr, // The scene manager.
0); // Our scene node's ID number.
csnode.SetMaterialFlag(MaterialFlags.Lighting, false); // We're not using lighting.
csnode.SetMaterialTexture(0, driver.GetTexture("rockwall.bmp")); // Time to load our texture and
// apply it to our scene node.
SceneNodeAnimator rotationAnim =
smgr.CreateRotationAnimator(new Vector3Df(0f, 0.5f, 0f));
csnode.AddAnimator(rotationAnim); // Rotates our scene node around the Y axis.
while (device.Run())
{
driver.BeginScene(Color.Blue);
smgr.DrawAll();
driver.EndScene();
}
}
}
}
