C#. How do i draw a cube?

Posted on 2006-05-09
Last Modified: 2007-12-19
I want to draw a simple cube, that i can move after that, on one axis. I don't need mapping and stuff.

Question by:drcyrus3d
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Can we have a little more info here?

    You want to move on one axis...with or without "perspective"?

    Or do you want to rotate on one axis?

    Should this be fullscreen or windowed?

    You can certainly brute force this using a little coding but do you really want to?

    DirectX and/or OpenGL may be a better approach.

    Can you give us a bigger picture?  It may save you some heartache down the line...  =)
    LVL 1

    Author Comment

    It's about an aplication that simulates a thing moving in a space. So i woul need a simple cube in perspective, it wont be affected with rotation only linear traslation.

    I would like to keep away from DirectX or OpenGL. Don't want to get to areas that i don't know, right now.

    LVL 10

    Expert Comment


    If not DirectX or OpenGL, what are you looking to draw your cube with?

    If you are looking for something done in MDX (DirectX) with C#, I would certainly be willing to post some code for you.
    LVL 1

    Author Comment

    Ok! Show me what you've got.
    LVL 10

    Accepted Solution

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

    namespace CubeExample
        static class Program
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
            public class Form1 : System.Windows.Forms.Form
                private Device device = null;
                private VertexBuffer vb = null;

                private System.ComponentModel.Container components = null;
                private float angle = 0.0f;

                public Form1()

                    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

                /// <summary>
                /// Initialize the graphics device
                /// </summary>
                public void InitializeGraphics()
                    // Set our presentation parameters
                    PresentParameters presentParams = new PresentParameters();

                    presentParams.Windowed = true;
                    presentParams.SwapEffect = SwapEffect.Discard;

                    // Create our device
                    device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

                    vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 36, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format,        Pool.Default);
                    vb.Created += new EventHandler(this.OnVertexBufferCreate);
                    OnVertexBufferCreate(vb, null);

                private void OnVertexBufferCreate(object sender, EventArgs e)
                    VertexBuffer buffer = (VertexBuffer)sender;

                    // Here is where we define the cube
                    CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[36];

                    // Front face
                    verts[0] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Red.ToArgb());
                    verts[1] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
                    verts[2] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Red.ToArgb());
                    verts[3] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
                    verts[4] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Red.ToArgb());
                    verts[5] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Red.ToArgb());

                    // Back face (facing away from the camera, so vertices should be clockwise order)
                    verts[6] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
                    verts[7] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
                    verts[8] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
                    verts[9] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
                    verts[10] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Blue.ToArgb());
                    verts[11] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());

                    // Top face
                    verts[12] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
                    verts[13] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());
                    verts[14] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());
                    verts[15] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
                    verts[16] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
                    verts[17] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Yellow.ToArgb());

                    // Bottom face (facing away from the camera, so vertices should be clockwise order)
                    verts[18] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, System.Drawing.Color.Black.ToArgb());
                    verts[19] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
                    verts[20] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
                    verts[21] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Black.ToArgb());
                    verts[22] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Black.ToArgb());
                    verts[23] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Black.ToArgb());

                    // Left face
                    verts[24] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Gray.ToArgb());
                    verts[25] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Gray.ToArgb());
                    verts[26] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.Gray.ToArgb());
                    verts[27] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Gray.ToArgb());
                    verts[28] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Gray.ToArgb());
                    verts[29] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Gray.ToArgb());

                    // Right face (facing away from the camera, so vertices should be clockwise order)
                    verts[30] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Green.ToArgb());
                    verts[31] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Green.ToArgb());
                    verts[32] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Green.ToArgb());
                    verts[33] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Green.ToArgb());
                    verts[34] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Green.ToArgb());
                    verts[35] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Green.ToArgb());

                    buffer.SetData(verts, 0, LockFlags.None);

                // Setup the Camera to view cube in World Space
                private void SetupCamera()
                    // Rotate the cube
                    device.Transform.World = Matrix.RotationYawPitchRoll(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f);
                    angle += 0.1f;

                    // Define FovLH & LookAtLH
                    device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
                    device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 18.0f), new Vector3(), new Vector3(0, 1, 0));
                    // Disable lighting
                    device.RenderState.Lighting = false;

                // This is our render loop
                protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
                    // Background Color
                    device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);


                    device.VertexFormat = CustomVertex.PositionColored.Format;
                    device.SetStreamSource(0, vb, 0);
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);




                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose(bool disposing)
                    if (disposing)
                        if (components != null)

                #region Windows Form Designer generated code

                private void InitializeComponent()
                    this.components = new System.ComponentModel.Container();
                    this.Size = new Size(800, 600);
                    this.Text = "Form1";

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                static void Main()
                    using (Form1 frm = new Form1())
                        // Show our form and initialize our graphics engine

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now