Original Post
Hi Guys,
Just wondering if someone out there would be able to help me with what i thought would be an extremely simple thing.
I have been searching the web trying to find some example code on how to draw a simple 3d cube.
I found some here, which i have translated from vb to c# and from managed directx to slimdx.
However, the cube does not want to draw and i am at a wits end as to why. The background is fine but just cant seem to get the cube to show up.
If anyone out there can help me out it would be greatly appreciated.
Cheers
[source lang="csharp"]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Device d = null;
VertexBuffer vb = null;
float rotX = 0, rotY = 0, initX = 0, initY = 0, finX = 0, finY = 0;
Boolean booMovingMouse = false;
//Next 6 numbers handle the click-drag movement
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeD3D();
}
private void InitializeD3D()
{
//Create my device and set it up.
PresentParameters para = new PresentParameters();
para.Windowed = true;
para.SwapEffect = SwapEffect.Discard;
d = new Device(new Direct3D(),0,DeviceType.Hardware,this.Handle,CreateFlags.HardwareVertexProcessing,para);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.Opaque, true);
//Set up and stock the vertex buffer with the vertexes of my cube.
Cube Cube = new Cube();
Cube.New(2, 2, 2, Color.SkyBlue);
vb = new VertexBuffer(d, 36, Usage.Dynamic, CustomVertex.PositionColored.format, Pool.Default);
vb.Lock(0,36,LockFlags.None).WriteRange(Cube.GetVerts());
vb.Unlock();
}
private void SetupCamera()
{
d.SetTransform(TransformState.Projection, Matrix.PerspectiveFovLH((float)(Math.PI / 4), (float)(this.Width / this.Height), 1f, 100f));
d.SetTransform(TransformState.View, Matrix.LookAtLH(new Vector3(0, 0, 5.0F), new Vector3(), new Vector3(0, 1, 0)));
d.SetRenderState(RenderState.Lighting, true);
//My single light
Light Light = new Light();
Light.Type = LightType.Point;
Light.Position = new Vector3(0,0,10);
Light.Ambient = Color.White;
Light.Attenuation0 = 0.8f;
Light.Attenuation1 = 0.02f;
Light.Range = 10000.0f;
d.SetLight(0,Light);
d.EnableLight(0, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (booMovingMouse == true)
{
rotX = (initX - MousePosition.X) + finX;
rotY = (initY - MousePosition.Y) + finY;
}
//Set up my camera and lighting. Since in most scenes, this changes constantly,
//it is here and not in the Initialize.
SetupCamera();
//Render the scene.
d.Clear(ClearFlags.Target, Color.DarkGray, 1, 0);
d.BeginScene();
d.VertexFormat = CustomVertex.PositionNormalColored.format;
// draw the cube
d.SetStreamSource(0, vb, 0, 0);
//Scale the mouse movement by 100 because 1:1 is too sensitive. Y = negative so
//the cube moves the way you'd expect on initial movement. A true "grab and move
//as expected" routine would be a little too complex for this example but all you'd
//need is a routine to determine the orientation of the world to the camera and select
//axis of rotation and direction accordingly.
d.SetTransform(TransformState.World, Matrix.RotationYawPitchRoll(rotX / 100.0F, -rotY / 100.0F, 0));
//d.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
d.EndScene();
d.Present();
this.Invalidate();
}
private void Form1_MouseDown(object sender, EventArgs e)
{
booMovingMouse = true;
initX = MousePosition.X;
initY = MousePosition.Y;
}
private void Form1_MouseUp(object sender, EventArgs e)
{
booMovingMouse = false;
finX += (initX - MousePosition.X);
finY += (initY - MousePosition.Y);
}
}
class CustomVertex
{
//[StructLayout(LayoutKind.Sequential)]
public struct TransformedColored
{
public Vector4 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw;
}
//[StructLayout(LayoutKind.Sequential)]
public struct PositionColored
{
public Vector3 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position;
}
//[StructLayout(LayoutKind.Sequential)]
public struct PositionNormalColored
{
public Vector3 Position;
public Vector3 Normal;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal;
}
}
class Cube
{
float X,Y,Z;
Color Color;
public void New(float Length, float Width, float Height, Color CubeColor)
{
X = Length;
Y = Width;
Z = Height;
Color = CubeColor;
}
public CustomVertex.PositionNormalColored[] GetVerts()
{
//This gets the vertexes of the cube. Each face is composed of 2 triangles.
//Each triangle has 3 vertexes, so each face needs 6 vertexes; i.e. some vertexes
//are written twice. The order in which you build the vertexes determine which
//side gets filled. You plot vertexes counter-clockwise for sides facing the camera
//and clockwise for ones facing "away".
//Also, in order for each side to be lit, you need to specify the
//normal for each vertex. This is the same number for all vertexes on each side.
//Setting the normal improperly can give some interesting effects in lighting.
float X2 = X/2;
float Y2 = Y/2;
float Z2 = Z/2;
CustomVertex.PositionNormalColored[] verts = new CustomVertex.PositionNormalColored[36];
//Front face.
verts[0].Position = new Vector3(-X2,Y2,Z2);
verts[0].Normal = new Vector3(0,0,1);
verts[0].Color = Color.ToArgb();
verts[1].Position = new Vector3(-X2,-Y2,Z2);
verts[1].Normal = new Vector3(0,0,1);
verts[1].Color = Color.ToArgb();
verts[2].Position = new Vector3(X2,Y2,Z2);
verts[2].Normal = new Vector3(0,0,1);
verts[2].Color = Color.ToArgb();
verts[3].Position = new Vector3(-X2,-Y2,Z2);
verts[3].Normal = new Vector3(0,0,1);
verts[3].Color = Color.ToArgb();
verts[4].Position = new Vector3(X2,-Y2,Z2);
verts[4].Normal = new Vector3(0,0,1);
verts[4].Color = Color.ToArgb();
verts[5].Position = new Vector3(X2,Y2,Z2);
verts[5].Normal = new Vector3(0,0,1);
verts[5].Color = Color.ToArgb();
//Back face. This is facing away from the camera, so vertices should be clockwise order.
verts[6].Position = new Vector3(-X2,Y2,-Z2);
verts[6].Normal = new Vector3(0,0,-1);
verts[6].Color = Color.ToArgb();
verts[7].Position = new Vector3(X2,Y2,-Z2);
verts[7].Normal = new Vector3(0,0,-1);
verts[7].Color = Color.ToArgb();
verts[8].Position = new Vector3(-X2,-Y2,-Z2);
verts[8].Normal = new Vector3(0,0,-1);
verts[8].Color = Color.ToArgb();
verts[9].Position = new Vector3(-X2,Y2,-Z2);
verts[9].Normal = new Vector3(0,0,-1);
verts[9].Color = Color.ToArgb();
verts[10].Position = new Vector3(X2,Y2,-Z2);
verts[10].Normal = new Vector3(0,0,-1);
verts[10].Color = Color.ToArgb();
verts[11].Position = new Vector3(X2,-Y2,-Z2);
verts[11].Normal = new Vector3(0,0,-1);
verts[11].Color = Color.ToArgb();
//Top face.
verts[12].Position = new Vector3(-X2,Y2,Z2);
verts[12].Normal = new Vector3(0,1,0);
verts[12].Color = Color.ToArgb();
verts[13].Position = new Vector3(X2,Y2,-Z2);
verts[13].Normal = new Vector3(0,1,0);
verts[13].Color = Color.ToArgb();
verts[14].Position = new Vector3(-X2,Y2,-Z2);
verts[14].Normal = new Vector3(0,1,0);
verts[14].Color = Color.ToArgb();
verts[15].Position = new Vector3(-X2,Y2,Z2);
verts[15].Normal = new Vector3(0,1,0);
verts[15].Color = Color.ToArgb();
verts[16].Position = new Vector3(X2,Y2,Z2);
verts[16].Normal = new Vector3(0,1,0);
verts[16].Color = Color.ToArgb();
verts[17].Position = new Vector3(X2,Y2,-Z2);
verts[17].Normal = new Vector3(0,1,0);
verts[17].Color = Color.ToArgb();
//Bottom face. This is facing away from the camera, so vertices should be clockwise order.
verts[18].Position = new Vector3(-X2,-Y2,Z2);
verts[18].Normal = new Vector3(0,-1,0);
verts[18].Color = Color.ToArgb();
verts[19].Position = new Vector3(-X2,-Y2,-Z2);
verts[19].Normal = new Vector3(0,-1,0);
verts[19].Color = Color.ToArgb();
verts[20].Position = new Vector3(X2,-Y2,-Z2);
verts[20].Normal = new Vector3(0,-1,0);
verts[20].Color = Color.ToArgb();
verts[21].Position = new Vector3(-X2,-Y2,Z2);
verts[21].Normal = new Vector3(0,-1,0);
verts[21].Color = Color.ToArgb();
verts[22].Position = new Vector3(X2,-Y2,-Z2);
verts[22].Normal = new Vector3(0,-1,0);
verts[22].Color = Color.ToArgb();
verts[23].Position = new Vector3(X2,-Y2,Z2);
verts[23].Normal = new Vector3(0,-1,0);
verts[23].Color = Color.ToArgb();
//Left face.
verts[24].Position = new Vector3(-X2,Y2,Z2);
verts[24].Normal = new Vector3(-1,0,0);
verts[24].Color = Color.ToArgb();
verts[25].Position = new Vector3(-X2,-Y2,-Z2);
verts[25].Normal = new Vector3(-1,0,0);
verts[25].Color = Color.ToArgb();
verts[26].Position = new Vector3(-X2,-Y2,Z2);
verts[26].Normal = new Vector3(-1,0,0);
verts[26].Color = Color.ToArgb();
verts[27].Position = new Vector3(-X2,Y2,-Z2);
verts[27].Normal = new Vector3(-1,0,0);
verts[27].Color = Color.ToArgb();
verts[28].Position = new Vector3(-X2,-Y2,-Z2);
verts[28].Normal = new Vector3(-1,0,0);
verts[28].Color = Color.ToArgb();
verts[29].Position = new Vector3(-X2,Y2,Z2);
verts[29].Normal = new Vector3(-1,0,0);
verts[29].Color = Color.ToArgb();
//Right face. This is facing away from the camera, so vertices should be clockwise order.
verts[30].Position = new Vector3(X2,Y2,Z2);
verts[30].Normal = new Vector3(1,0,0);
verts[30].Color = Color.ToArgb();
verts[31].Position = new Vector3(X2,-Y2,Z2);
verts[31].Normal = new Vector3(1,0,0);
verts[31].Color = Color.ToArgb();
verts[32].Position = new Vector3(X2,-Y2,-Z2);
verts[32].Normal = new Vector3(1,0,0);
verts[32].Color = Color.ToArgb();
verts[33].Position = new Vector3(X2,Y2,-Z2);
verts[33].Normal = new Vector3(1,0,0);
verts[33].Color = Color.ToArgb();
verts[34].Position = new Vector3(X2,Y2,Z2);
verts[34].Normal = new Vector3(1,0,0);
verts[34].Color = Color.ToArgb();
verts[35].Position = new Vector3(X2,-Y2,-Z2);
verts[35].Normal = new Vector3(1,0,0);
verts[35].Color = Color.ToArgb();
return verts;
}
}
}
[/source]
Just wondering if someone out there would be able to help me with what i thought would be an extremely simple thing.
I have been searching the web trying to find some example code on how to draw a simple 3d cube.
I found some here, which i have translated from vb to c# and from managed directx to slimdx.
However, the cube does not want to draw and i am at a wits end as to why. The background is fine but just cant seem to get the cube to show up.
If anyone out there can help me out it would be greatly appreciated.
Cheers
[source lang="csharp"]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Device d = null;
VertexBuffer vb = null;
float rotX = 0, rotY = 0, initX = 0, initY = 0, finX = 0, finY = 0;
Boolean booMovingMouse = false;
//Next 6 numbers handle the click-drag movement
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeD3D();
}
private void InitializeD3D()
{
//Create my device and set it up.
PresentParameters para = new PresentParameters();
para.Windowed = true;
para.SwapEffect = SwapEffect.Discard;
d = new Device(new Direct3D(),0,DeviceType.Hardware,this.Handle,CreateFlags.HardwareVertexProcessing,para);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.Opaque, true);
//Set up and stock the vertex buffer with the vertexes of my cube.
Cube Cube = new Cube();
Cube.New(2, 2, 2, Color.SkyBlue);
vb = new VertexBuffer(d, 36, Usage.Dynamic, CustomVertex.PositionColored.format, Pool.Default);
vb.Lock(0,36,LockFlags.None).WriteRange(Cube.GetVerts());
vb.Unlock();
}
private void SetupCamera()
{
d.SetTransform(TransformState.Projection, Matrix.PerspectiveFovLH((float)(Math.PI / 4), (float)(this.Width / this.Height), 1f, 100f));
d.SetTransform(TransformState.View, Matrix.LookAtLH(new Vector3(0, 0, 5.0F), new Vector3(), new Vector3(0, 1, 0)));
d.SetRenderState(RenderState.Lighting, true);
//My single light
Light Light = new Light();
Light.Type = LightType.Point;
Light.Position = new Vector3(0,0,10);
Light.Ambient = Color.White;
Light.Attenuation0 = 0.8f;
Light.Attenuation1 = 0.02f;
Light.Range = 10000.0f;
d.SetLight(0,Light);
d.EnableLight(0, true);
}
protected override void OnPaint(PaintEventArgs e)
{
if (booMovingMouse == true)
{
rotX = (initX - MousePosition.X) + finX;
rotY = (initY - MousePosition.Y) + finY;
}
//Set up my camera and lighting. Since in most scenes, this changes constantly,
//it is here and not in the Initialize.
SetupCamera();
//Render the scene.
d.Clear(ClearFlags.Target, Color.DarkGray, 1, 0);
d.BeginScene();
d.VertexFormat = CustomVertex.PositionNormalColored.format;
// draw the cube
d.SetStreamSource(0, vb, 0, 0);
//Scale the mouse movement by 100 because 1:1 is too sensitive. Y = negative so
//the cube moves the way you'd expect on initial movement. A true "grab and move
//as expected" routine would be a little too complex for this example but all you'd
//need is a routine to determine the orientation of the world to the camera and select
//axis of rotation and direction accordingly.
d.SetTransform(TransformState.World, Matrix.RotationYawPitchRoll(rotX / 100.0F, -rotY / 100.0F, 0));
//d.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
d.EndScene();
d.Present();
this.Invalidate();
}
private void Form1_MouseDown(object sender, EventArgs e)
{
booMovingMouse = true;
initX = MousePosition.X;
initY = MousePosition.Y;
}
private void Form1_MouseUp(object sender, EventArgs e)
{
booMovingMouse = false;
finX += (initX - MousePosition.X);
finY += (initY - MousePosition.Y);
}
}
class CustomVertex
{
//[StructLayout(LayoutKind.Sequential)]
public struct TransformedColored
{
public Vector4 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw;
}
//[StructLayout(LayoutKind.Sequential)]
public struct PositionColored
{
public Vector3 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position;
}
//[StructLayout(LayoutKind.Sequential)]
public struct PositionNormalColored
{
public Vector3 Position;
public Vector3 Normal;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal;
}
}
class Cube
{
float X,Y,Z;
Color Color;
public void New(float Length, float Width, float Height, Color CubeColor)
{
X = Length;
Y = Width;
Z = Height;
Color = CubeColor;
}
public CustomVertex.PositionNormalColored[] GetVerts()
{
//This gets the vertexes of the cube. Each face is composed of 2 triangles.
//Each triangle has 3 vertexes, so each face needs 6 vertexes; i.e. some vertexes
//are written twice. The order in which you build the vertexes determine which
//side gets filled. You plot vertexes counter-clockwise for sides facing the camera
//and clockwise for ones facing "away".
//Also, in order for each side to be lit, you need to specify the
//normal for each vertex. This is the same number for all vertexes on each side.
//Setting the normal improperly can give some interesting effects in lighting.
float X2 = X/2;
float Y2 = Y/2;
float Z2 = Z/2;
CustomVertex.PositionNormalColored[] verts = new CustomVertex.PositionNormalColored[36];
//Front face.
verts[0].Position = new Vector3(-X2,Y2,Z2);
verts[0].Normal = new Vector3(0,0,1);
verts[0].Color = Color.ToArgb();
verts[1].Position = new Vector3(-X2,-Y2,Z2);
verts[1].Normal = new Vector3(0,0,1);
verts[1].Color = Color.ToArgb();
verts[2].Position = new Vector3(X2,Y2,Z2);
verts[2].Normal = new Vector3(0,0,1);
verts[2].Color = Color.ToArgb();
verts[3].Position = new Vector3(-X2,-Y2,Z2);
verts[3].Normal = new Vector3(0,0,1);
verts[3].Color = Color.ToArgb();
verts[4].Position = new Vector3(X2,-Y2,Z2);
verts[4].Normal = new Vector3(0,0,1);
verts[4].Color = Color.ToArgb();
verts[5].Position = new Vector3(X2,Y2,Z2);
verts[5].Normal = new Vector3(0,0,1);
verts[5].Color = Color.ToArgb();
//Back face. This is facing away from the camera, so vertices should be clockwise order.
verts[6].Position = new Vector3(-X2,Y2,-Z2);
verts[6].Normal = new Vector3(0,0,-1);
verts[6].Color = Color.ToArgb();
verts[7].Position = new Vector3(X2,Y2,-Z2);
verts[7].Normal = new Vector3(0,0,-1);
verts[7].Color = Color.ToArgb();
verts[8].Position = new Vector3(-X2,-Y2,-Z2);
verts[8].Normal = new Vector3(0,0,-1);
verts[8].Color = Color.ToArgb();
verts[9].Position = new Vector3(-X2,Y2,-Z2);
verts[9].Normal = new Vector3(0,0,-1);
verts[9].Color = Color.ToArgb();
verts[10].Position = new Vector3(X2,Y2,-Z2);
verts[10].Normal = new Vector3(0,0,-1);
verts[10].Color = Color.ToArgb();
verts[11].Position = new Vector3(X2,-Y2,-Z2);
verts[11].Normal = new Vector3(0,0,-1);
verts[11].Color = Color.ToArgb();
//Top face.
verts[12].Position = new Vector3(-X2,Y2,Z2);
verts[12].Normal = new Vector3(0,1,0);
verts[12].Color = Color.ToArgb();
verts[13].Position = new Vector3(X2,Y2,-Z2);
verts[13].Normal = new Vector3(0,1,0);
verts[13].Color = Color.ToArgb();
verts[14].Position = new Vector3(-X2,Y2,-Z2);
verts[14].Normal = new Vector3(0,1,0);
verts[14].Color = Color.ToArgb();
verts[15].Position = new Vector3(-X2,Y2,Z2);
verts[15].Normal = new Vector3(0,1,0);
verts[15].Color = Color.ToArgb();
verts[16].Position = new Vector3(X2,Y2,Z2);
verts[16].Normal = new Vector3(0,1,0);
verts[16].Color = Color.ToArgb();
verts[17].Position = new Vector3(X2,Y2,-Z2);
verts[17].Normal = new Vector3(0,1,0);
verts[17].Color = Color.ToArgb();
//Bottom face. This is facing away from the camera, so vertices should be clockwise order.
verts[18].Position = new Vector3(-X2,-Y2,Z2);
verts[18].Normal = new Vector3(0,-1,0);
verts[18].Color = Color.ToArgb();
verts[19].Position = new Vector3(-X2,-Y2,-Z2);
verts[19].Normal = new Vector3(0,-1,0);
verts[19].Color = Color.ToArgb();
verts[20].Position = new Vector3(X2,-Y2,-Z2);
verts[20].Normal = new Vector3(0,-1,0);
verts[20].Color = Color.ToArgb();
verts[21].Position = new Vector3(-X2,-Y2,Z2);
verts[21].Normal = new Vector3(0,-1,0);
verts[21].Color = Color.ToArgb();
verts[22].Position = new Vector3(X2,-Y2,-Z2);
verts[22].Normal = new Vector3(0,-1,0);
verts[22].Color = Color.ToArgb();
verts[23].Position = new Vector3(X2,-Y2,Z2);
verts[23].Normal = new Vector3(0,-1,0);
verts[23].Color = Color.ToArgb();
//Left face.
verts[24].Position = new Vector3(-X2,Y2,Z2);
verts[24].Normal = new Vector3(-1,0,0);
verts[24].Color = Color.ToArgb();
verts[25].Position = new Vector3(-X2,-Y2,-Z2);
verts[25].Normal = new Vector3(-1,0,0);
verts[25].Color = Color.ToArgb();
verts[26].Position = new Vector3(-X2,-Y2,Z2);
verts[26].Normal = new Vector3(-1,0,0);
verts[26].Color = Color.ToArgb();
verts[27].Position = new Vector3(-X2,Y2,-Z2);
verts[27].Normal = new Vector3(-1,0,0);
verts[27].Color = Color.ToArgb();
verts[28].Position = new Vector3(-X2,-Y2,-Z2);
verts[28].Normal = new Vector3(-1,0,0);
verts[28].Color = Color.ToArgb();
verts[29].Position = new Vector3(-X2,Y2,Z2);
verts[29].Normal = new Vector3(-1,0,0);
verts[29].Color = Color.ToArgb();
//Right face. This is facing away from the camera, so vertices should be clockwise order.
verts[30].Position = new Vector3(X2,Y2,Z2);
verts[30].Normal = new Vector3(1,0,0);
verts[30].Color = Color.ToArgb();
verts[31].Position = new Vector3(X2,-Y2,Z2);
verts[31].Normal = new Vector3(1,0,0);
verts[31].Color = Color.ToArgb();
verts[32].Position = new Vector3(X2,-Y2,-Z2);
verts[32].Normal = new Vector3(1,0,0);
verts[32].Color = Color.ToArgb();
verts[33].Position = new Vector3(X2,Y2,-Z2);
verts[33].Normal = new Vector3(1,0,0);
verts[33].Color = Color.ToArgb();
verts[34].Position = new Vector3(X2,Y2,Z2);
verts[34].Normal = new Vector3(1,0,0);
verts[34].Color = Color.ToArgb();
verts[35].Position = new Vector3(X2,-Y2,-Z2);
verts[35].Normal = new Vector3(1,0,0);
verts[35].Color = Color.ToArgb();
return verts;
}
}
}
[/source]