• Advertisement

Archived

This topic is now archived and is closed to further replies.

Trouble drawing primitives with Managed DirectX in C#

This topic is 5062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have been working at this for a while and I don't understand why my red square is not showing up. It seems to be working fine, the device is clearing the screen to black, but its not drawing my vertices. Here is my source:
public static void Main(string [] args)
		{
			TankTestWindow frm = new TankTestWindow();
			frm.Show();

			if(!frm.InitializeD3D())
			{
				MessageBox.Show("Error Initializing D3D");
			}

			while(frm.Created == true)
			{
				frm.Render();
				Application.DoEvents();
			}
		}


And here are the functions:
   
public bool InitializeD3D()
		{
			try
			{
				PresentParameters presentParams = new PresentParameters();
				presentParams.Windowed = true;
				presentParams.SwapEffect = SwapEffect.Discard;

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

				vb = new VertexBuffer(typeof(CustomVertex.TransformedColored),
					4,
					device,
					0,
					CustomVertex.TransformedColored.Format,
					Pool.Default);

				vb.Created += new System.EventHandler(this.OnCreateVertexBuffer);
				this.OnCreateVertexBuffer(vb, null);
		
				return true;
			}
			catch
			{
				return false;
			}
		}

		public void Render()
		{
			if (device == null)
				return;

			device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);
			device.BeginScene();

			device.SetStreamSource(0,vb,0);
			device.VertexFormat = CustomVertex.TransformedColored.Format;
			device.DrawPrimitives(PrimitiveType.TriangleStrip,0,2);

			device.EndScene();
			device.Present();
		}

		public void OnCreateVertexBuffer(object sender, EventArgs e)
		{
			VertexBuffer vb = (VertexBuffer)sender;

			GraphicsStream stm = vb.Lock(0, 0, 0);
			CustomVertex.TransformedColored[] verts =
				new CustomVertex.TransformedColored[4];

			verts[0].X=this.Width-20; verts[0].Y=this.Height-20; verts[0].Z=0.0f; verts[0].Rhw=1; 
			verts[0].Color = System.Drawing.Color.Red.ToArgb();

			verts[1].X=this.Width-20; verts[1].Y=this.Height+20; verts[1].Z=0.0f; verts[1].Rhw=1; 
			verts[1].Color = System.Drawing.Color.Red.ToArgb();

			verts[2].X=this.Width+20; verts[2].Y=this.Height+20; verts[2].Z=0.0f; verts[2].Rhw=1; 
			verts[2].Color = System.Drawing.Color.Red.ToArgb();

			verts[3].X=this.Width+20; verts[3].Y=this.Height-20; verts[3].Z=0.0f; verts[3].Rhw=1; 
			verts[3].Color = System.Drawing.Color.Red.ToArgb();

			stm.Write(verts);
			vb.Unlock();
		}
Thanks in advance, wakebrdkid12 [edited by - wakebrdkid12 on June 9, 2004 6:19:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
Always clear to (dark)blue when testing/developing. There are a number of reasons objects might turn black, and you won''t see it against a black background.

Share this post


Link to post
Share on other sites
You are drawing the vertices in the wrong order. This way only the last 3 vertices will be drawn to screen.

Since you are using the height and width of the current window, half of the rectangle will not be drawn inside the window.

Change

verts[0].X=this.Width-20; verts[0].Y=this.Height-20;

to something like

verts[0].X=this.Width-200; verts[0].Y=this.Height-200;

Here''s the code to draw a rectangle (centered in the screen)


int x = (this.Width / 2);
int y =(this.Height / 2);

verts[0].X=(x-20);
verts[0].Y=(y-20);
verts[0].Z=0.0f;
verts[0].Rhw=1;
verts[0].Color = System.Drawing.Color.Red.ToArgb();

verts[1].X=(x+20);
verts[1].Y=(y-20);
verts[1].Z=0.0f;
verts[1].Rhw=1;
verts[1].Color = System.Drawing.Color.Red.ToArgb();

verts[2].X=(x-20);
verts[2].Y=(y+20);
verts[2].Z=0.0f;
verts[2].Rhw=1;
verts[2].Color = System.Drawing.Color.Red.ToArgb();

verts[3].X=(x+20);
verts[3].Y=(y+20);
verts[3].Z=0.0f;
verts[3].Rhw=1;
verts[3].Color = System.Drawing.Color.Red.ToArgb();

Share this post


Link to post
Share on other sites
Thanks so much guys!

Fidelio - Thanks for the tip. I wondered why all the tutorials and stuff wanted me to set it to blue.

jstemmer - That was it exactly! I meant to do that in the first place but apparently I forgot. I just am wondering why it didn''t show me anything with my old code. Looking at it again it seems I should see A corner of the square in the lower right corner of the window, but I don''t see anything. Why would this be?


Thanks for the help guys!

Ciao

Share this post


Link to post
Share on other sites
quote:
Original post by wakebrdkid12
... I just am wondering why it didn't show me anything with my old code. Looking at it again it seems I should see A corner of the square in the lower right corner of the window, but I don't see anything. Why would this be?



take a look at this picture:


if you use triangle strip you should use the right order to setup your vertices, otherwise the rectangle wouldnt be drawn correctly.
The reason why you didnt see anything in your old code was because the part of the rectangle that would be visible (the 2nd triangle made up of vertices 2, 3 and 4) was outside the view.
(add device.RenderState.CullMode = Cull.None to your old code and you'll probably see a part of the rectangle)

[edited by - jstemmer on June 10, 2004 10:32:27 AM]

Share this post


Link to post
Share on other sites
Thank you very much for your explanation. I forgot triangle strips use the new vertex and the last 2 vertices, so my method was wrong. Thanks!

Share this post


Link to post
Share on other sites

  • Advertisement