Jump to content

  • Log In with Google      Sign In   
  • Create Account


Can't draw a simple cube


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 blacken   Members   -  Reputation: 126

Like
0Likes
Like

Posted 09 December 2012 - 01:09 AM

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]

Sponsor:

#2 hossainiir   Members   -  Reputation: 912

Like
1Likes
Like

Posted 10 December 2012 - 02:28 AM

Hi,
at first use PIX to capture a frame and see the vertex data is correct or not?

خلیج تا ابد فارس

Persian Gulf


#3 adt7   Members   -  Reputation: 421

Like
1Likes
Like

Posted 10 December 2012 - 07:24 AM

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, 10 December 2012 - 07:25 AM.


#4 blacken   Members   -  Reputation: 126

Like
0Likes
Like

Posted 11 December 2012 - 12:28 AM

Hossainiir,

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, 11 December 2012 - 12:33 AM.


#5 hossainiir   Members   -  Reputation: 912

Like
1Likes
Like

Posted 11 December 2012 - 04:12 AM

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.

خلیج تا ابد فارس

Persian Gulf


#6 unbird   Crossbones+   -  Reputation: 4009

Like
0Likes
Like

Posted 11 December 2012 - 04:51 AM

Yeah, you want to get familiar with PIX...

Very important before you go PIX:
Direct3D Programming Tip #5: Use The Debug Runtime

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.

#7 Gavin Williams   Members   -  Reputation: 586

Like
0Likes
Like

Posted 11 December 2012 - 12:03 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS