C#. How do i draw a cube?

I want to draw a simple cube, that i can move after that, on one axis. I don't need mapping and stuff.

Thanks.
LVL 1
drcyrus3dAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...  =)
0
 
drcyrus3dAuthor Commented:
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.

Thanks
0
 
rivusglobalCommented:
drcyrus,

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.
0
 
drcyrus3dAuthor Commented:
Ok! Show me what you've got.
0
 
rivusglobalCommented:
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()
            {
                InitializeComponent();

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

                SetupCamera();

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

                device.EndScene();

                device.Present();

                this.Invalidate();
            }

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

            #region Windows Form Designer generated code

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

            /// <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
                    frm.Show();
                    frm.InitializeGraphics();
                    Application.Run(frm);
                }
            }
        }
    }
}
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.