Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have converted the following C# tutorial (Craig's Turorials) into vb.net, here is the 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�re done drawing 
			device.EndScene(); 
			// Copy the back buffer to the display 
			device.Present(); 
		} 
	} 
} 

 

The only part I can't seem to convert is the SetupMatricies bit...

 

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

 

The vb version has no concept of the Matrix Structure. I am not sure how this routine would get converted to VB.NET. Can someone lend a hand?

 

Thanks

 

by the way, here is the vb version:

 

Option Explicit On 
Option Strict On
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D


Namespace VerticesTutorial
   Public Class Vertices
       Inherits Form
       ' Our global variables for this project
       Private device As device = Nothing ' Our rendering device
       Private vertexBuffer As vertexBuffer = Nothing    

       Public Sub New()
           ' Set the initial size of our form
           Me.ClientSize = New System.Drawing.Size(500, 425)
           ' And its caption
           Me.Text = "Direct3D Tutorial 2 - Vertices"
       End Sub 'New


       Public Function InitializeGraphics() As Boolean
           Try
               ' Now let's setup our D3D stuff
               Dim presentParams As New PresentParameters()
               presentParams.Windowed = True
               presentParams.SwapEffect = SwapEffect.Discard
               device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams)
               Me.OnCreateDevice(device, Nothing)
               Return True
           Catch e As Exception
               Return False
           End Try
       End Function 'InitializeGraphics

       Public Sub OnCreateDevice(ByVal sender As Object, ByVal e As EventArgs)
           Dim dev As Device = CType(sender, Device)
           ' Now Create the VB
           vertexBuffer = New VertexBuffer(GetType(CustomVertex.PositionColored), 3, dev, 0, CustomVertex.TransformedColored.Format, Pool.Default)
           AddHandler vertexBuffer.Created, AddressOf Me.OnCreateVertexBuffer
           Me.OnCreateVertexBuffer(vertexBuffer, Nothing)
       End Sub 'OnCreateDevice

       Public Sub OnCreateVertexBuffer(ByVal sender As Object, ByVal e As EventArgs)
           Dim vb As VertexBuffer = CType(sender, VertexBuffer)
           Dim verts As CustomVertex.PositionColored() = CType(vb.Lock(0, 0), CustomVertex.PositionColored())
           'verts(0).X = 150
           'verts(0).Y = 50
           'verts(0).Z = 0.5F
           'verts(0).Rhw = 1
           'verts(0).Color = System.Drawing.Color.Red.ToArgb()
           'verts(1).X = 250
           'verts(1).Y = 250
           'verts(1).Z = 0.5F
           'verts(1).Rhw = 1
           'verts(1).Color = System.Drawing.Color.Green.ToArgb()
           'verts(2).X = 50
           'verts(2).Y = 250
           'verts(2).Z = 0.5F
           'verts(2).Rhw = 1
           'verts(2).Color = System.Drawing.Color.Blue.ToArgb()
           verts(0) = New CustomVertex.PositionColored(0, 1, 0, System.Drawing.Color.Red.ToArgb())
           verts(1) = New CustomVertex.PositionColored(-0.5F, 0, 0, System.Drawing.Color.Green.ToArgb())
           verts(2) = New CustomVertex.PositionColored(0.5F, 0, 0, System.Drawing.Color.Blue.ToArgb())

           vb.Unlock()
       End Sub 'OnCreateVertexBuffer

       Private Sub Render()
           If device Is Nothing Then
               Return
           End If
           'Clear the backbuffer to a blue color 
           device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0)
           'Begin the scene
           device.BeginScene()

           'SetupMatrices()

           device.SetStreamSource(0, vertexBuffer, 0)
           device.VertexFormat = CustomVertex.PositionColored.Format
           device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)

           'End the scene
           device.EndScene()
           device.Present()
       End Sub 'Render


       'Private Sub SetupMatrices()
       '    Dim sngAngle As Single
       '    sngAngle = Environment.TickCount / 500.0F
       '    device.Transform.World = Matrix.RotationY(sngAngle)
       '    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((sngAngle)math.PI/4.0F, 1.0F, 1.0F, 3.25F) 
       'End Sub 'SetupMatrices



       Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
           Me.Render() ' Render on painting
       End Sub 'OnPaint

       Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
           If Asc(e.KeyChar) = CInt(System.Windows.Forms.Keys.Escape) Then
               Me.Close() ' Esc was pressed
           End If
       End Sub 'OnKeyPress
       '/ <summary>
       '/ The main entry point for the application.
       '/ </summary>
       Shared Sub Main()

           Dim frm As New Vertices()
           If Not frm.InitializeGraphics() Then ' Initialize Direct3D
               MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.")
               Return
           End If
           frm.Show()

           While frm.Created
               frm.Render()
               Application.DoEvents()
           End While
       End Sub 'Main
   End Class 'Vertices 
End Namespace 'VerticesTutorial

mod2software

Home of the VB.NET Class Builder Utility - Demo and Full versions available now!

  • *Experts*
Posted

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.

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

Posted

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 couldn't 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. Don't 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

mod2software

Home of the VB.NET Class Builder Utility - Demo and Full versions available now!

  • 1 month later...
Posted

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:

 

Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

 

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

 

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:

 

m_device.Material = matBox

box.DrawSubset(0)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...