?
Solved

C#. How do i draw a cube?

Posted on 2006-05-09
7
Medium Priority
?
2,586 Views
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.

Thanks.
0
Comment
Question by:drcyrus3d
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16639699
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
 
LVL 1

Author Comment

by:drcyrus3d
ID: 16639730
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
 
LVL 10

Expert Comment

by:rivusglobal
ID: 16640199
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
 
LVL 1

Author Comment

by:drcyrus3d
ID: 16640339
Ok! Show me what you've got.
0
 
LVL 10

Accepted Solution

by:
rivusglobal earned 2000 total points
ID: 16640957
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

830 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