C# tutorial(dx9) to VB.NET - small problem with Matrix

CattleRustler

Well-known member
Joined
Nov 8, 2003
Messages
76
Location
NYC - USA
I have converted the following C# tutorial (Craigs Turorials) into vb.net, here is the C#:

C#:
	using System; 
	using System.Drawing; 
	using System.Windows.Forms; 
	using Microsoft.DirectX; 
	using Microsoft.DirectX.Direct3D; 


	namespace DevelopMentor.Candera.Direct3D 
	{ 
		public class Game : System.Windows.Forms.Form 

		{ 
			static void Main() 
			{ 
				Game app = new Game(); 
				app.InitializeGraphics(); 
				app.Show(); 
				while (app.Created) 
				{ 
					app.Render(); 
					Application.DoEvents(); 
				} 
				//app.DisposeGraphics(); 
			} 

			private Device device; 
			private VertexBuffer vertices; 
			

			protected bool InitializeGraphics() 
			{ 
				PresentParameters pres = new PresentParameters(); 
				pres.Windowed = true ; 
				pres.SwapEffect = SwapEffect.Discard; 
				device = new Device(0, DeviceType.Hardware, this , 
				CreateFlags.SoftwareVertexProcessing, pres); 
				vertices = CreateVertexBuffer(device); 
				return true ;   
			} 

			protected VertexBuffer CreateVertexBuffer(Device device) 
			{ 
				device.VertexFormat = 
				CustomVertex.PositionColored.Format; 
			
				device.RenderState.CullMode = Cull.None;

				VertexBuffer buf = new VertexBuffer( 
					typeof (CustomVertex.PositionColored), // What type of vertices 
					3,                                    // How many 
					device,                               // The device 
					0,                                    // Default usage 
					CustomVertex.PositionColored.Format,  // Vertex format 
				Pool.Default);                        // Default pooling 

				CustomVertex.PositionColored[] verts = 
					(CustomVertex.PositionColored[]) buf.Lock(0, 0); 

				int i = 0; 
				verts[i++] = new CustomVertex.PositionColored( 
					0, 1, 0, Color.Red.ToArgb()); 
				verts[i++] = new CustomVertex.PositionColored( 
					-0.5F, 0, 0, Color.Green.ToArgb()); 
				verts[i++] = new CustomVertex.PositionColored( 
					0.5F,  0, 0, Color.Blue.ToArgb()); 

				buf.Unlock(); 
				return buf; 
			} 

			protected void SetupMatrices() 
			{ 

				float angle = Environment.TickCount / 500.0F; 
				device.Transform.World = Matrix.RotationY(angle); 
				device.Transform.View = Matrix.LookAtLH( new Vector3(0, 0.5F, -3), 
					new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0)); 
				device.Transform.Projection = 
					Matrix.PerspectiveFovLH(( float )Math.PI/4.0F, 1.0F, 1.0F, 5.0F); 
			} 

			protected void Render() 
			{ 
				// Clear the back buffer 
				device.Clear(ClearFlags.Target, Color.Bisque, 1.0F, 0); 
				// Ready Direct3D to begin drawing 
				device.BeginScene(); 
				// Set the Matrices 
				SetupMatrices(); 
				// Draw the scene - 3D Rendering calls go here 
				device.SetStreamSource(0, vertices, 0); 
				device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);   
				// Indicate to Direct3D that we
 
Code:
device.Transform.Projection =  Matrix.PerspectiveFovLH((sngAngle)math.PI/4.0F, 1.0F, 1.0F, 3.25F)
One problem, this is not how you do casting in VB.NET, and you cannot cast to a variable. To cast in VB.NET use the DirectCast keyword.
Here is an example of how that line would look.
Code:
device.Transform.Projection =  Matrix.PerspectiveFovLH(Convert.ToSingle(Math.PI) /4.0F, 1.0F, 1.0F, 3.25F)

If you want, you can download a sample like this here:
http://www.directx4.net/tutorials/15.zip
 
Mutant

Thanks, That syntax is the remmed C# code(or a mixture of partially recoded vb and c#). The problem I was having was I couldnt do anything that contained "Matrix..." as far as vb went. In C# I could open the tuts and run em ok and Matrix Structure was fine. Dont ask me why, i have no idea - all sdk was installed properly - all libraries were fine - but for some reason, now that I upgraded to VS 2003 (literally an hour ago) all of the vb tutorials that contain MATRIX work fine... Before in vs2002 I was hacking the .vb files out of the projects and making new projects in 2002, so I could see the code and some of them even ran ok - accept, like I said, for Matrix refs.

Oh well, all sorted out now--thanks!

Thanks very much for your help-It is greatly appreciated
 
Wowsers, the thing with the vertex buffer is very complex.
I rather use Meshes.

Import the D3DX dll and add these lines at the top of the class file for your rendering:

Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Then use the following for a box (for example):

Code:
dim box as Mesh
dim matBox as New Material
box = Mesh.Box(m_device,1.0f,1.0f,1.0f)
matBox.Diffuse = Color.Red
matBox.Ambient = Color.Red

In the render function add these lines:

Code:
m_device.Material = matBox

box.DrawSubset(0)
 
Back
Top