CattleRustler
Regular
I have converted the following C# tutorial (Craig's Turorials) into vb.net, here is the C#:
The only part I can't seem to convert is the SetupMatricies bit...
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:
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 were 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...
C#:
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:
Visual Basic:
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