• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Can't draw a simple cube

6 posts in this topic

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 [url="http://www.vbforums.com/showthread.php?625045-draw-3D-cube-Direct3D"]here[/url], 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.

[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()

private void Form1_Load(object sender, EventArgs e)

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);

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.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.

//Render the scene.

d.Clear(ClearFlags.Target, Color.DarkGray, 1, 0);
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);


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
public struct TransformedColored
public Vector4 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.PositionRhw;
public struct PositionColored
public Vector3 Position;
public int Color;
public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position;
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;

Share this post

Link to post
Share on other sites
At no point are you drawing anything. Unless I've missed something there are no calls to any of the Device.Draw....() methods in your code.

EDIT: Just noticed you have your DrawPrimitives call commented out. Edited by adt7

Share this post

Link to post
Share on other sites

not sure what you mean by PIX? could you elaborate?

thanks adt7, but not sure what i should be doing for drawing.

i uncommented the drawprimitives section and it still just shows a blank screen.

Is there something i am missing? Edited by blacken

Share this post

Link to post
Share on other sites
before using Pix you must uncomment DrawPrimitives.
also i think is better to use DrawIndexedPrimitives and create IndexBuffer .

Pix is a good program for debugging Directx app.

1-run pix from directx sdk.
2-Create a new experiment and in program path enter app path or drag app and drop it to pix the experiment window will create with path.
3-select "A single-frame capture of ..." radio button and also set check "Enable draw timing" .
4-now press "start Experiment" by this work your app will start.
5-now press "F12" key and close your app.
6-after closing a window with name of Run will show that it has 4 sub windows.
7-in events window you in find "Frame #" which # is the number of frame that you captured!
8-find DrawPrimitives or DrawIndexedPrimitives and select it.
9-in details window select Mesh tab.
10-in this tab you can see wireframe of you model in 3 states 1:Pre-vertex Shader 2:Post-Vertex shader 3:View port.
11-in bottom we have two tabs PreVS and PostVS .
12-in PreVS you can see vertex data that you created.

try this.

excuse for bad english.

Share this post

Link to post
Share on other sites
Yeah, you want to get familiar with PIX...

Very important [i]before[/i] you go PIX:
[url="http://legalizeadulthood.wordpress.com/2009/06/28/direct3d-programming-tip-5-use-the-debug-runtime/"]Direct3D Programming Tip #5: Use The Debug Runtime[/url]

Also: Additional care must be taken when you work from Managed DirectX code. You cannot simply transliterate code (which, by the way, crashes on my machine) to SlimDX.
Managed DirectX behaves differently since it has some automatics and defaults. Rather work from the SDK samples or any other sample that actually works.

Share this post

Link to post
Share on other sites
Yeah, I agree with unbird, you really need to find some SlimDX or SharpDX examples or even just Direct X examples. You don't want to be translating as well as learning, that's just making it harder for yourself.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0