|
-
Mar 7th, 2013, 06:55 PM
#1
Thread Starter
New Member
MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newbies
Although years rolled by, there is still pretty much low support for beginners on this area, especially as for VB.NET, so I've decided to start a thread here and would like to ask everyone who has stuff to share in the topic to add it here. My initial contribution to the thread is a very simple and basic SharpDX VB.NET (VS 2010) keyboard reading example, here it is:
vb Code:
Imports SharpDX.DirectInput
Imports System.Windows.Forms
Module Module1
Public ApplicationWindow As New Form1
Public UserQuitted As Boolean
Sub Main()
Dim SharpDXDirectInput As New DirectInput
Dim SharpDXKeyboard As Object
Dim SharpDXKeyboardState As KeyboardState
Dim PreviousScrollLockStatePressed As Boolean = False
Dim ScrollLockStatePressed As Boolean
ApplicationWindow.Show()
SharpDXKeyboard = New Keyboard(SharpDXDirectInput)
With SharpDXKeyboard
.SetCooperativeLevel _
(ApplicationWindow, _
CooperativeLevel.Background Or CooperativeLevel.NonExclusive)
.Acquire()
End With
Console.WriteLine("Current State of the Scroll Lock key: Released")
Console.Write(StrDup(46, "-") & vbCrLf & "Press 'Q' to quit.")
Do
SharpDXKeyboardState = SharpDXKeyboard.GetCurrentState()
ScrollLockStatePressed = SharpDXKeyboardState.IsPressed(Key.ScrollLock)
If ScrollLockStatePressed <> PreviousScrollLockStatePressed Then
Console.SetCursorPosition(38, 0)
If ScrollLockStatePressed Then
Console.Write("Pressed ")
Else
Console.Write("Released")
End If
PreviousScrollLockStatePressed = ScrollLockStatePressed
End If
Application.DoEvents()
Loop Until SharpDXKeyboardState.IsPressed(Key.Q) Or UserQuitted
ApplicationWindow.Close()
SharpDXKeyboard.Unacquire()
End Sub
End Module
You need to dl and extract SharpDX to a local folder and compile the code above as a Console Application (VS 2010) in order to run it.
The following .NET references are required to be added to this project :
SharpDX.dll (SharpDXInstallFolder\Bin\Standard-net40\SharpDX.dll)
SharpDX.DirectInput.dll (SharpDXInstallFolder\Bin\Standard-net40\SharpDX.DirectInput.dll)
System.Windows.Forms - can be added from .NET reference list
Additionally, you need to add a Form named Form1 to the project with the following Form code:
vb Code:
Public Class Form1
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UserQuitted = True
End Sub
End Class
This code demonstrates 3 things:
1.) Receiving keyboard input using SharpDX
2.) Using Console Applications for test purposes (coz this example could be written as a simple Windows Application, it intentionally isn't)
3.) Don't be scared of this "VS 2010 is for object oriented coding" thing 
The code itself does only 2 things:
1.) Detects the state of the ScrollLock key (pressed or released) and displays it on the Console.
2.) Quits when the user closes the appwindow (not the Console window but the Form) or presses the Q key.
To create this simple code, I used the following:
- SharpDX C# sample codes (only Joystick DirectInput Sample is present in the currently distributed pack)
- "Managed DirectX 9 Kick Start: Graphics and Game Programming" - a book by Tom Miller, also in C#
- Visual Studio 2010 Objectbrowser
Another Initial Contribution To The Topic
There's a small but pretty much interesting 'lil sample set for SlimDX, downloadable from here :
http://www.ventsim.com/files/samplesvb.zip
P.s: I know about tutorials and samples available on the official sites of SlimDX and SharpDX.
Last edited by Visualizer; Mar 7th, 2013 at 07:39 PM.
-
Mar 12th, 2013, 04:35 PM
#2
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
My understanding is that these are all DirectX wrappers of one sort or another for managed languages. Do any of them allow you to use DirectX to draw individual Windows controls in a Windows forms app?
My usual boring signature: Nothing
 
-
Mar 13th, 2013, 02:22 AM
#3
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
Yea they are wrappers and theres really no point of learning them in my opinion because there already is...DirectX itself. They say that the libraries make it to where you can focus on the code in general, but in reality, theres not much DX code to type, and you focus on the game code itself anyways. Once you initialize DirectX, its all easy street from there.
-
Mar 13th, 2013, 12:07 PM
#4
Thread Starter
New Member
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
 Originally Posted by Jacob Roman
Yea they are wrappers and theres really no point of learning them in my opinion because there already is...DirectX itself. ...
No doubt that the items enumerated in the title are just wrappers for the actual DirectX. No doubt you can develope any DirectX code using VC++ and/or low level code or whatever, and codes written that way will prolly run more optimized and faster. The reason to start such thread is I'm hopefully not the only one who has more or less difficulties with understanding and/or writing DirectX code in VC++ (I meant the topic for newbies, casual and/or hobby programmers, check title), not to mention that coding in VB.NET/VC# is pretty much different comparing them to VC++ coding. Another good reason for starting such thread is these wrappers are even different from each other. For example, while MS MDX is an old .NET version library set and only supports DirectX 9.0c, SharpDX has stuff for the latest OS's with the latest DirectX versions including libraries for both .NET 2.0 and 4.0, and their namespaces, class libraries are also different. As you can see shaggy hiker already has a question which can just be one of the many that belong to this thread. I'm sure there is a need for such thread, the number of views on it is already proving it.
 Originally Posted by Jacob Roman
... They say that the libraries make it to where you can focus on the code in general, but in reality, theres not much DX code to type, and you focus on the game code itself anyways. Once you initialize DirectX, its all easy street from there. ...
If a DirectX newbie or casual/hobby programmer takes a look at the 2010 jun DirectX SDK or adds an MDX reference to his VB.NET project and takes a look at declarations in the object browser, he/she gets the shivers ... You could say, "learn DirectX first", then I would say "ok, should I do it in VC ++ ? ..." etc. the final conclusion will be the same, you will need easy samples to learn / casually write DirectX code in VB.NET and even VC#.
-
Mar 13th, 2013, 03:04 PM
#5
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
I actually have tons of samples I've written in all languages if you need me to post em.
-
Mar 14th, 2013, 10:31 AM
#6
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
Ok. So, can any of those be used to draw individual controls in a Windows Forms App? I'm not interested in full game loops, because I long since concluded that I suck at anything artistic, so I will confine my designs to square and mostly gray, where my lack of artististry won't make the user hurl chunder on their keyboard. However, I'm currently using XNA to draw some controls that can't be drawn fast enough using GDI. If XNA goes extinct, those controls can probably still work for quite some time (look at how long VB6 has been hanging on), but what is the future for faster controls in WinForms?
My usual boring signature: Nothing
 
-
Mar 14th, 2013, 09:57 PM
#7
Thread Starter
New Member
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
 Originally Posted by Jacob Roman
I actually have tons of samples I've written in all languages if you need me to post em.
I definitely expected samples from you, Jacob I would be interested in even a collection of them that would be upped in just a zip/rar somewhere with a link given here (as I did it in my initial post). Anything that belongs to the topic would be appreciated. I am mostly interested in examples written in VS 2010 VB.NET but VC# samples might also come.
 Originally Posted by Shaggy Hiker
... what is the future for faster controls in WinForms?
Well it can be read here and there that Mircosoft kinda "tends to replace" conventional GDI with DirectX, a part of this process is including latest DirectX SDK into Windows 8 SDK ... As for current chances of drawing UI Windows and Controls using DirectX, as a DirectX newbie I can't really add anything to this except that you can obviously do anything using MDX wrappers that you would be able to do using DirectX itself. If someone would have actual coding practice on this area, any VB.NET/VC# samples would be appreciated.
-
Mar 14th, 2013, 11:43 PM
#8
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
In the past I was trying to make a Massive DirectX Tutorial targeting all languages, but its incomplete and the fact I no longer have time to complete it due to work, spending time with my gf, working on my Nintoaster project (Nintendo inside a toaster), working on my Nintendo Emulator, enjoing life, etc etc, put the whole tutorial on hold. But heres just a taste of what I have to offer. This is an example of a first person camera in 3D. I wrote it in all languages with similar structure.
Visual Basic 6.0
vb Code:
Option Explicit Private Type CUSTOM_VERTEX X As Single Y As Single Z As Single Color As Long End Type Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZ Or D3DFVF_DIFFUSE Private Const PI As Single = 3.141592654 Private Const FOV As Single = PI / 4 Private Const ASPECT_RATIO As Single = 3 / 4 Private Const NEAR_Z As Single = 1 Private Const FAR_Z As Single = 10000 Private DX As DirectX8 Private D3D As Direct3D8 Private Device As Direct3DDevice8 Private Display_Mode As D3DDISPLAYMODE Private Screen As D3DPRESENT_PARAMETERS Private DI As DirectInput8 Private Mouse_Device As DirectInputDevice8 Private Mouse_State As DIMOUSESTATE Private Mouse As D3DVECTOR2 Private Keyboard_Device As DirectInputDevice8 Private Keyboard_State As DIKEYBOARDSTATE Private Vertex_List(3) As CUSTOM_VERTEX Private View_Matrix As D3DMATRIX Private Projection_Matrix As D3DMATRIX Private Camera_Position As D3DVECTOR Private Camera_Angle As D3DVECTOR Private Fullscreen_Enabled As Boolean Private Running As Boolean Private Sub DirectX_Initialize() Set DX = New DirectX8 Set D3D = DX.Direct3DCreate() If Fullscreen_Enabled = True Then Display_Mode.Width = 800 Display_Mode.Height = 600 Display_Mode.Format = D3DFMT_R5G6B5 Screen.Windowed = False Screen.BackBufferCount = 1 Screen.BackBufferWidth = Display_Mode.Width Screen.BackBufferHeight = Display_Mode.Height Screen.hDeviceWindow = frmMain.hWnd Else D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode Screen.Windowed = True End If Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC Screen.BackBufferFormat = Display_Mode.Format Screen.AutoDepthStencilFormat = D3DFMT_D16 Screen.EnableAutoDepthStencil = 1 Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen) End Sub Private Sub DirectInput_Initialize() Set DI = DX.DirectInputCreate End Sub Private Sub DirectInput_Initialize_Mouse() Set Mouse_Device = DI.CreateDevice("GUID_SYSMOUSE") Mouse_Device.SetCommonDataFormat DIFORMAT_MOUSE Mouse_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE Mouse_Device.Acquire End Sub Private Sub Mouse_Controls() Mouse_Device.GetDeviceStateMouse Mouse_State Mouse.X = Mouse.X + Mouse_State.lX Mouse.Y = Mouse.Y + Mouse_State.lY If Mouse.X > frmMain.ScaleWidth Then Mouse.X = frmMain.ScaleWidth If Mouse.X < 0 Then Mouse.X = 0 If Mouse.Y > frmMain.ScaleHeight Then Mouse.Y = frmMain.ScaleHeight If Mouse.Y < 0 Then Mouse.Y = 0 Camera_Angle.X = Camera_Angle.X + Mouse_State.lY Camera_Angle.Y = Camera_Angle.Y + Mouse_State.lX End Sub Private Sub DirectInput_Initialize_Keyboard() Set Keyboard_Device = DI.CreateDevice("GUID_SysKeyboard") Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD Keyboard_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE Keyboard_Device.Acquire End Sub Private Function DirectInput_Key_State(Key_Code As Long) As Boolean Keyboard_Device.GetDeviceStateKeyboard Keyboard_State DirectInput_Key_State = Keyboard_State.Key(Key_Code) And &H80 End Function Private Sub Keyboard_Controls() Const Camera_Speed As Long = 5 If DirectInput_Key_State(DIK_W) Then 'Move Forward Camera_Position.X = Camera_Position.X - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_S) Then 'Move Backward Camera_Position.X = Camera_Position.X + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_A) Then 'Move Left Camera_Position.X = Camera_Position.X + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_D) Then 'Move Right Camera_Position.X = Camera_Position.X - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_Q) Then 'Look Left Camera_Angle.Y = Camera_Angle.Y - 1 End If If DirectInput_Key_State(DIK_E) Then 'Look Right Camera_Angle.Y = Camera_Angle.Y + 1 End If If DirectInput_Key_State(DIK_R) Then 'Look Up Camera_Angle.X = Camera_Angle.X - 1 End If If DirectInput_Key_State(DIK_F) Then 'Look Down Camera_Angle.X = Camera_Angle.X + 1 End If If DirectInput_Key_State(DIK_C) Then 'Move Up Camera_Position.Y = Camera_Position.Y - Camera_Speed End If If DirectInput_Key_State(DIK_Z) Then 'Move Down Camera_Position.Y = Camera_Position.Y + Camera_Speed End If End Sub Private Sub Settings() Device.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE Device.SetRenderState D3DRS_ZWRITEENABLE, 1 Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE Device.SetRenderState D3DRS_LIGHTING, 0 End Sub Private Function Create_Vertex(X As Single, Y As Single, Z As Single) As D3DVECTOR Create_Vertex.X = X Create_Vertex.Y = Y Create_Vertex.Z = Z End Function Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Long) As CUSTOM_VERTEX Create_Custom_Vertex.X = X Create_Custom_Vertex.Y = Y Create_Custom_Vertex.Z = Z Create_Custom_Vertex.Color = Color End Function Private Sub Setup_Matrices() D3DXMatrixLookAtRH View_Matrix, Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0) Device.SetTransform D3DTS_VIEW, View_Matrix D3DXMatrixPerspectiveFovRH Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z Device.SetTransform D3DTS_PROJECTION, Projection_Matrix End Sub Public Sub Camera_Control() Dim Camera_Translation_Matrix As D3DMATRIX Dim Camera_Angle_Matrix_X As D3DMATRIX Dim Camera_Angle_Matrix_Y As D3DMATRIX D3DXMatrixIdentity View_Matrix D3DXMatrixIdentity Camera_Translation_Matrix D3DXMatrixTranslation Camera_Translation_Matrix, Camera_Position.X, Camera_Position.Y, -Camera_Position.Z D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Translation_Matrix D3DXMatrixRotationY Camera_Angle_Matrix_Y, (PI * Camera_Angle.Y) / 180 D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_Y D3DXMatrixRotationX Camera_Angle_Matrix_X, (PI * Camera_Angle.X) / 180 D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_X Device.SetTransform D3DTS_VIEW, View_Matrix End Sub Private Sub Create_Polygon() Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, D3DColorXRGB(255, 255, 255)) End Sub Private Sub Draw_Polygon() Device.SetVertexShader CUSTOM_VERTEX_FORMAT Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0)) End Sub Private Sub Create_Platform() Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, D3DColorXRGB(255, 0, 0)) Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, D3DColorXRGB(0, 255, 0)) Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, D3DColorXRGB(0, 0, 255)) Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, D3DColorXRGB(255, 0, 255)) End Sub Private Sub Render() Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(0, 0, 0), 1, 0 Device.BeginScene 'Rendering code goes here Create_Platform Draw_Polygon Create_Polygon Draw_Polygon Device.EndScene Device.Present ByVal 0, ByVal 0, 0, ByVal 0 End Sub Private Sub Game_Loop() Do While Running = True Mouse_Controls Keyboard_Controls Camera_Control Render If DirectInput_Key_State(DIK_ESCAPE) <> 0 Then Shutdown DoEvents Loop End Sub Private Sub Main() If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True End If With Me .Show .ScaleMode = vbPixels .ScaleWidth = 312 .ScaleHeight = 213 .Caption = "DirectX Tutorial" If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone End With Camera_Position.X = 0 Camera_Position.Y = -50 Camera_Position.Z = 250 DirectX_Initialize DirectInput_Initialize DirectInput_Initialize_Mouse DirectInput_Initialize_Keyboard Settings Setup_Matrices Running = True Game_Loop End Sub Private Sub Shutdown() If Running = True Then Running = False Mouse_Device.Unacquire Set Mouse_Device = Nothing Keyboard_Device.Unacquire Set Keyboard_Device = Nothing Set DI = Nothing Set Device = Nothing Set D3D = Nothing Set DX = Nothing Unload Me End If End Sub Private Sub Form_Load() Main End Sub Private Sub Form_Unload(Cancel As Integer) Shutdown End Sub
...Continued Next Post
-
Mar 14th, 2013, 11:43 PM
#9
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
...Continued from last post
Visual Basic.NET
vb.net Code:
Option Explicit On Option Strict On Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports Microsoft.DirectX.DirectInput Public Class frmMain Private Const FOV As Single = Math.PI / 4 Private Const ASPECT_RATIO As Single = 4 / 3 Private Const NEAR_Z As Single = 1 Private Const FAR_Z As Single = 10000 Private Device As Direct3D.Device Private Display_Mode As DisplayMode Private Screen As New PresentParameters Private Mouse_Device As DirectInput.Device Private Mouse_State As MouseState Private Mouse As Vector2 Private Keyboard_Device As DirectInput.Device Private Keyboard_State As KeyboardState Private Vertex_List(3) As CustomVertex.PositionColored Private View_Matrix As Matrix Private Projection_Matrix As Matrix Private Camera_Position As Vector3 Private Camera_Angle As Vector3 Private Fullscreen_Enabled As Boolean Private Running As Boolean Private Sub DirectX_Initialize() If Fullscreen_Enabled = True Then Display_Mode.Width = 800 Display_Mode.Height = 600 Display_Mode.Format = Direct3D.Format.R5G6B5 Screen.Windowed = False Screen.BackBufferCount = 1 Screen.BackBufferWidth = Display_Mode.Width Screen.BackBufferHeight = Display_Mode.Height Else Screen.Windowed = True End If Screen.SwapEffect = SwapEffect.Copy Screen.BackBufferFormat = Display_Mode.Format Screen.AutoDepthStencilFormat = DepthFormat.D16 Screen.EnableAutoDepthStencil = True Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen) End Sub Private Sub DirectInput_Initialize_Mouse() Mouse_Device = New DirectInput.Device(SystemGuid.Mouse) Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse) Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive) Mouse_Device.Acquire() End Sub Private Sub Mouse_Controls() Mouse_State = Mouse_Device.CurrentMouseState Mouse.X = Mouse.X + Mouse_State.X Mouse.Y = Mouse.Y + Mouse_State.Y If Mouse.X > Me.Width Then Mouse.X = Me.Width If Mouse.X < 0 Then Mouse.X = 0 If Mouse.Y > Me.Height Then Mouse.Y = Me.Height If Mouse.Y < 0 Then Mouse.Y = 0 Camera_Angle.X += Mouse_State.Y Camera_Angle.Y += Mouse_State.X If Camera_Angle.X >= 90 Then Camera_Angle.X = 90 If Camera_Angle.X <= -90 Then Camera_Angle.X = -90 End Sub Private Sub DirectInput_Initialize_Keyboard() Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard) Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard) Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive) Keyboard_Device.Acquire() End Sub Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean Keyboard_State = Keyboard_Device.GetCurrentKeyboardState Return Keyboard_State.Item(Key_Code) End Function Private Sub Keyboard_Controls() Const Camera_Speed As Long = 5 If DirectInput_Key_State(Key.W) Then 'Move Forward Camera_Position.X -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.S) Then 'Move Backward Camera_Position.X += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.A) Then 'Move Left Camera_Position.X += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.D) Then 'Move Right Camera_Position.X -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.Q) Then 'Look Left Camera_Angle.Y -= 1 End If If DirectInput_Key_State(Key.E) Then 'Look Right Camera_Angle.Y += 1 End If If DirectInput_Key_State(Key.R) Then 'Look Up Camera_Angle.X -= 1 End If If DirectInput_Key_State(Key.F) Then 'Look Down Camera_Angle.X += 1 End If If DirectInput_Key_State(Key.C) Then 'Move Up Camera_Position.Y -= Camera_Speed End If If DirectInput_Key_State(Key.Z) Then 'Move Down Camera_Position.Y += Camera_Speed End If End Sub Private Sub Settings() Device.SetRenderState(RenderStates.ZEnable, True) Device.SetRenderState(RenderStates.ZBufferWriteEnable, True) Device.SetRenderState(RenderStates.CullMode, Cull.None) Device.SetRenderState(RenderStates.Lighting, False) End Sub Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3 Dim Vertex As Vector3 Vertex.X = X Vertex.Y = Y Vertex.Z = Z Return Vertex End Function Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored Vertex.Position = New Vector3(X, Y, Z) Vertex.Color = Color Return Vertex End Function Private Sub Setup_Matrices() View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0)) Device.Transform.View = View_Matrix Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z) Device.Transform.Projection = Projection_Matrix End Sub Private Sub Camera_Control() Dim Camera_Translation_Matrix As Matrix Dim Camera_Angle_Matrix_X As Matrix Dim Camera_Angle_Matrix_Y As Matrix View_Matrix = Matrix.Identity Camera_Translation_Matrix = Matrix.Identity Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix) Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y) Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X) Device.Transform.View = View_Matrix End Sub Private Sub Create_Polygon() Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb) End Sub Private Sub Draw_Polygon() Device.VertexFormat = CustomVertex.PositionColored.Format Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List) End Sub Private Sub Create_Platform() Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb) Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb) Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb) Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb) End Sub Private Sub Reset_Device() If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then Running = False Device.Reset(Screen) Settings() Setup_Matrices() Application.DoEvents() Running = True End If End Sub Private Sub Render() Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1, 0) Device.BeginScene() Create_Polygon() Draw_Polygon() Create_Platform() Draw_Polygon() Device.EndScene() Device.Present() End Sub Private Sub Game_Loop() Do While Running = True Mouse_Controls() Keyboard_Controls() Camera_Control() Render() If DirectInput_Key_State(Key.Escape) Then Shutdown() Application.DoEvents() Loop End Sub Private Sub Main() If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then Fullscreen_Enabled = True End If With Me .Show() .Width = 330 .Height = 250 .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) .Text = "DirectX Tutorial" If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None End With Camera_Position.X = 0 Camera_Position.Y = -50 Camera_Position.Z = 250 DirectX_Initialize() DirectInput_Initialize_Mouse() DirectInput_Initialize_Keyboard() Settings() Setup_Matrices() Running = True End Sub Private Sub Shutdown() If Running = True Then Running = False Mouse_Device.Unacquire() Mouse_Device.Dispose() Keyboard_Device.Unacquire() Keyboard_Device.Dispose() Device.Dispose() Application.Exit() End If End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Main() End Sub Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Shutdown() End Sub Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Game_Loop() End Sub Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize Reset_Device() End Sub End Class
-
Mar 14th, 2013, 11:45 PM
#10
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
...Continued from last post
C#
c# Code:
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 Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using Microsoft.DirectX.DirectInput; namespace DX_Tut { public partial class frmMain : Form { private const float FOV = (float)Math.PI / 4.0f; private const float ASPECT_RATIO = 4.0f / 3.0f; private const float NEAR_Z = 1.0f; private const float FAR_Z = 10000.0f; Microsoft.DirectX.Direct3D.Device Device; DisplayMode Display_Mode; PresentParameters Screen; Microsoft.DirectX.DirectInput.Device Mouse_Device; MouseState Mouse_State; Vector2 Mouse; Microsoft.DirectX.DirectInput.Device Keyboard_Device; KeyboardState Keyboard_State; CustomVertex.PositionColored[] Vertex_List = new CustomVertex.PositionColored[4]; Matrix View_Matrix; Matrix Projection_Matrix; Vector3 Camera_Position; Vector3 Camera_Angle; bool Fullscreen_Enabled; bool Running; private void DirectX_Initialize() { Screen = new PresentParameters(); if (Fullscreen_Enabled == true) { Display_Mode.Width = 800; Display_Mode.Height = 600; Display_Mode.Format = Format.R5G6B5; Screen.Windowed = false; Screen.BackBufferCount = 1; Screen.BackBufferWidth = Display_Mode.Width; Screen.BackBufferHeight = Display_Mode.Height; } else { Screen.Windowed = true; } Screen.SwapEffect = SwapEffect.Copy; Screen.BackBufferFormat = Display_Mode.Format; Screen.AutoDepthStencilFormat = DepthFormat.D16; Screen.EnableAutoDepthStencil = true; Device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen); } private void DirectInput_Initialize_Mouse() { Mouse_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse); Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse); Mouse_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); Mouse_Device.Acquire(); } private void Mouse_Controls() { Mouse_State = Mouse_Device.CurrentMouseState; Mouse.X += Mouse_State.X; Mouse.Y += Mouse_State.Y; if (Mouse.X > this.Width) Mouse.X = this.Width; if (Mouse.X < 0) Mouse.X = 0; if (Mouse.Y > this.Height) Mouse.Y = this.Height; if (Mouse.Y < 0) Mouse.Y = 0; Camera_Angle.X += Mouse_State.Y; Camera_Angle.Y += Mouse_State.X; if (Camera_Angle.X >= 90) Camera_Angle.X = 90; if (Camera_Angle.X <= -90) Camera_Angle.X = -90; } private void DirectInput_Initialize_Keyboard() { Keyboard_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard); Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard); Keyboard_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); Keyboard_Device.Acquire(); } private bool DirectInput_Key_State(Microsoft.DirectX.DirectInput.Key Key_Code) { Keyboard_State = Keyboard_Device.GetCurrentKeyboardState(); return Keyboard_State[Key_Code]; } private void Keyboard_Controls() { const int Camera_Speed = 5; if (DirectInput_Key_State(Key.W)) //Move Forward { Camera_Position.X -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.S)) //Move Backward { Camera_Position.X += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.A)) //Move Left { Camera_Position.X += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.D)) //Move Right { Camera_Position.X -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.Q)) //Look Left Camera_Angle.Y -= 1; if (DirectInput_Key_State(Key.E)) //Look Right Camera_Angle.Y += 1; if (DirectInput_Key_State(Key.R)) //Look Up Camera_Angle.X -= 1; if (DirectInput_Key_State(Key.F)) //Look Down Camera_Angle.X += 1; if (DirectInput_Key_State(Key.C)) //Move Up Camera_Position.Y -= Camera_Speed; if (DirectInput_Key_State(Key.Z)) //Move Down Camera_Position.Y += Camera_Speed; } private void Settings() { Device.SetRenderState(RenderStates.ZEnable, true); Device.SetRenderState(RenderStates.ZBufferWriteEnable, true); Device.SetRenderState(RenderStates.CullMode, (int)Cull.None); Device.SetRenderState(RenderStates.Lighting, false); } private Vector3 Create_Vertex(float X, float Y, float Z) { Vector3 Vertex; Vertex.X = X; Vertex.Y = Y; Vertex.Z = Z; return Vertex; } private CustomVertex.PositionColored Create_Custom_Vertex(float X, float Y, float Z, int Color) { CustomVertex.PositionColored Vertex = new CustomVertex.PositionColored(); Vertex.Position = new Vector3(X, Y, Z); Vertex.Color = Color; return Vertex; } private void Setup_Matrices() { View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0)); Device.Transform.View = View_Matrix; Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z); Device.Transform.Projection = Projection_Matrix; } private void Camera_Control() { Matrix Camera_Translation_Matrix; Matrix Camera_Angle_Matrix_X; Matrix Camera_Angle_Matrix_Y; View_Matrix = Matrix.Identity; Camera_Translation_Matrix = Matrix.Identity; Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix); Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y); Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X); Device.Transform.View = View_Matrix; } private void Create_Polygon() { Vertex_List[0] = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[1] = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[2] = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[3] = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb()); } private void Draw_Polygon() { Device.VertexFormat = CustomVertex.PositionColored.Format; Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List); } private void Create_Platform() { Vertex_List[0] = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb()); Vertex_List[1] = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb()); Vertex_List[2] = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb()); Vertex_List[3] = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb()); } private void Reset_Device() { if (this.WindowState != FormWindowState.Minimized && Fullscreen_Enabled == false) { Running = false; Device.Reset(Screen); Settings(); Setup_Matrices(); Application.DoEvents(); Running = true; } } private void Render() { Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1.0f, 0); Device.BeginScene(); Create_Platform(); Draw_Polygon(); Create_Polygon(); Draw_Polygon(); Device.EndScene(); Device.Present(); } private void Game_Loop() { do { Mouse_Controls(); Keyboard_Controls(); Camera_Control(); Render(); if (DirectInput_Key_State(Key.Escape)) Shutdown(); Application.DoEvents(); } while (Running == true); } private void Main() { if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) Fullscreen_Enabled = true; this.Show(); this.KeyPreview = true; this.Width = 330; this.Height = 250; this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); this.Text = "DirectX Tutorial"; if (Fullscreen_Enabled == true) this.FormBorderStyle = FormBorderStyle.None; Camera_Position.X = 0; Camera_Position.Y = -50; Camera_Position.Z = 250; DirectX_Initialize(); DirectInput_Initialize_Mouse(); DirectInput_Initialize_Keyboard(); Settings(); Setup_Matrices(); Running = true; } void Shutdown() { if (Running == true) { Running = false; Mouse_Device.Unacquire(); Mouse_Device.Dispose(); Keyboard_Device.Unacquire(); Keyboard_Device.Dispose(); Device.Dispose(); Application.Exit(); } } public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { Main(); } private void frmMain_Paint(object sender, PaintEventArgs e) { Game_Loop(); } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { Shutdown(); } private void frmMain_Resize(object sender, EventArgs e) { Reset_Device(); } } }
...Continued next post
-
Mar 14th, 2013, 11:46 PM
#11
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
...Continued from last post
C++
c++ Code:
#include <windows.h> #include <d3d9.h> #include <d3dx9.h> #include <dinput.h> #pragma comment (lib, "d3d9.lib") #pragma comment (lib, "d3dx9.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") struct CUSTOM_VERTEX { float X, Y, Z; DWORD Color; }; #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE) const float PI = 3.141592654f; const float FOV = PI / 4.0f; const float ASPECT_RATIO = 4.0f / 3.0f; const float NEAR_Z = 1.0f; const float FAR_Z = 10000.0f; HWND hWnd; MSG msg; HINSTANCE hInstance; int Width; int Height; LPDIRECT3D9 D3D; LPDIRECT3DDEVICE9 Device; D3DDISPLAYMODE Display_Mode; D3DPRESENT_PARAMETERS Screen; LPDIRECTINPUT8 DI; LPDIRECTINPUTDEVICE8 Mouse_Device; DIMOUSESTATE Mouse_State; D3DXVECTOR2 Mouse; LPDIRECTINPUTDEVICE8 Keyboard_Device; BYTE Keyboard_State[256]; CUSTOM_VERTEX Vertex_List[4]; D3DXMATRIX View_Matrix; D3DXMATRIX Projection_Matrix; D3DVECTOR Camera_Position; D3DVECTOR Camera_Angle; bool Fullscreen_Enabled; bool Running; void DirectX_Initialize(void); void DirectInput_Initialize(void); void DirectInput_Initialize_Mouse(void); void Mouse_Controls(void); void DirectInput_Initialize_Keyboard(void); bool DirectInput_Key_State(void); void Keyboard_Controls(void); void Settings(void); D3DXVECTOR3 Create_Vertex(void); CUSTOM_VERTEX Create_Custom_Vertex(void); void Setup_Matrices(void); void Camera_Control(void); void Create_Polygon(void); void Draw_Polygon(void); void Create_Platform(void); void Render(void); void Game_Loop(void); void Main(void); void Shutdown(void); LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void DirectX_Initialize() { D3D = Direct3DCreate9(D3D_SDK_VERSION); memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS)); if (Fullscreen_Enabled == true) { Display_Mode.Width = 800; Display_Mode.Height = 600; Display_Mode.Format = D3DFMT_R5G6B5; Screen.Windowed = FALSE; Screen.BackBufferCount = 1; Screen.BackBufferWidth = Display_Mode.Width; Screen.BackBufferHeight = Display_Mode.Height; Screen.hDeviceWindow = hWnd; } else { D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode); Screen.Windowed = TRUE; } Screen.SwapEffect = D3DSWAPEFFECT_DISCARD; Screen.BackBufferFormat = D3DFMT_R5G6B5; Screen.EnableAutoDepthStencil = TRUE; Screen.AutoDepthStencilFormat = D3DFMT_D16; D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device); } void DirectInput_Initialize() { DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL); } void DirectInput_Initialize_Mouse() { DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL); Mouse_Device->SetDataFormat(&c_dfDIMouse); Mouse_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); Mouse_Device->Acquire(); } void Mouse_Controls() { Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State); Mouse.x += Mouse_State.lX; Mouse.y += Mouse_State.lY; if (Mouse.x > Width) Mouse.x = (float)Width; if (Mouse.y < 0) Mouse.x = 0; if (Mouse.x > Height) Mouse.y = (float)Height; if (Mouse.y < 0) Mouse.y = 0; Camera_Angle.x += Mouse_State.lY; Camera_Angle.y += Mouse_State.lX; if (Camera_Angle.x >= 90) Camera_Angle.x = 90; if (Camera_Angle.x <= -90) Camera_Angle.x = -90; } void DirectInput_Initialize_Keyboard() { DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL); Keyboard_Device->SetDataFormat(&c_dfDIKeyboard); Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); Keyboard_Device->Acquire(); } bool DirectInput_Key_State(int Key_Code) { Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State); return Keyboard_State[Key_Code] && 0x80; } void Keyboard_Controls() { const int Camera_Speed = 5; if (DirectInput_Key_State(DIK_W)) { Camera_Position.x -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_S)) { Camera_Position.x += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_A)) { Camera_Position.x += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_D)) { Camera_Position.x -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_Q)) { Camera_Angle.y -= 1; } if (DirectInput_Key_State(DIK_E)) { Camera_Angle.y += 1; } if (DirectInput_Key_State(DIK_R)) { Camera_Angle.x -= 1; } if (DirectInput_Key_State(DIK_F)) { Camera_Angle.x += 1; } if (DirectInput_Key_State(DIK_C)) { Camera_Position.y -= Camera_Speed; } if (DirectInput_Key_State(DIK_Z)) { Camera_Position.y += Camera_Speed; } } void Settings() { Device->SetRenderState(D3DRS_ZENABLE, TRUE); Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); Device->SetRenderState(D3DRS_LIGHTING, FALSE); } D3DXVECTOR3 Create_Vertex(float X, float Y, float Z) { D3DXVECTOR3 Vertex; Vertex.x = X; Vertex.y = Y; Vertex.z = Z; return Vertex; } CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color) { CUSTOM_VERTEX Vertex; Vertex.X = X; Vertex.Y = Y; Vertex.Z = Z; Vertex.Color = Color; return Vertex; } void Setup_Matrices() { D3DXMatrixLookAtRH(&View_Matrix, &Create_Vertex(Camera_Position.x, Camera_Position.y, Camera_Position.z), &Create_Vertex(0, 0, 0), &Create_Vertex(0.0f, 1.0f, 0.0f)); Device->SetTransform(D3DTS_VIEW, &View_Matrix); D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z); Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix); } void Camera_Control() { D3DXMATRIX Camera_Translation_Matrix; D3DXMATRIX Camera_Angle_Matrix_X; D3DXMATRIX Camera_Angle_Matrix_Y; D3DXMatrixIdentity(&View_Matrix); D3DXMatrixIdentity(&Camera_Translation_Matrix); D3DXMatrixTranslation(&Camera_Translation_Matrix, Camera_Position.x, Camera_Position.y, -Camera_Position.z); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Translation_Matrix); D3DXMatrixRotationY(&Camera_Angle_Matrix_Y, (PI * Camera_Angle.y) / 180); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_Y); D3DXMatrixRotationX(&Camera_Angle_Matrix_X, (PI * Camera_Angle.x) / 180); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_X); Device->SetTransform(D3DTS_VIEW, &View_Matrix); } void Create_Polygon() { Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); } void Draw_Polygon() { Device->SetFVF(CUSTOM_VERTEX_FORMAT); Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX)); } void Create_Platform() { Vertex_List[0] = Create_Custom_Vertex(-1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(255, 0, 0)); Vertex_List[1] = Create_Custom_Vertex(1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(0, 255, 0)); Vertex_List[2] = Create_Custom_Vertex(-1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(0, 0, 255)); Vertex_List[3] = Create_Custom_Vertex(1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(255, 0, 255)); } void Render() { Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); Device->BeginScene(); //Rendering code goes here Create_Polygon(); Draw_Polygon(); Create_Platform(); Draw_Polygon(); Device->EndScene(); Device->Present(NULL, NULL, NULL, NULL); } void Game_Loop() { while (Running == true) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0) { if (WM_QUIT == msg.message) break; TranslateMessage (&msg); DispatchMessage (&msg); } else { Mouse_Controls(); Keyboard_Controls(); Camera_Control(); Render(); if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd); } } } void Main() { Camera_Position.x = 0.0f; Camera_Position.y = -50.0f; Camera_Position.z = 250.0f; DirectX_Initialize(); DirectInput_Initialize(); DirectInput_Initialize_Mouse(); DirectInput_Initialize_Keyboard(); Settings(); Setup_Matrices(); Running = true; } void Shutdown() { if (Running == true) { Running = false; Mouse_Device->Unacquire(); Mouse_Device->Release(); Keyboard_Device->Unacquire(); Keyboard_Device->Release(); Device->Release(); D3D->Release(); PostQuitMessage (0); HANDLE Process; Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId()); TerminateProcess(Process , 0); } } int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL}; RegisterClassEx(&wc); if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES) Fullscreen_Enabled = true; if (Fullscreen_Enabled == true) hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL); else hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL); hInstance = hinstance; ShowWindow (hWnd, nCmdShow); if (Fullscreen_Enabled == true) { Width = 800; Height = 600; } else { Width = 330; Height = 250; } Main(); Game_Loop(); return msg.wParam; } LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: Shutdown(); break; default: return DefWindowProc (hWnd, msg, wParam, lParam); } return 0; }
-
Mar 15th, 2013, 04:37 AM
#12
Thread Starter
New Member
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
Yea I got the VB.NET version of this code already from a previous post of you and it's a must for newbies, surely one that does belong to here
-
Feb 22nd, 2014, 07:06 PM
#13
New Member
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
Thank you guys, I wrote a piece of code using SlimDX 4, Directx 11 in Visual Studio 2013 Vb net.
I wuold like your thought about
to download the code go there
http://vbgraphic.altervista.org/directx11/directx11.htm
Fabio
-
Feb 23rd, 2014, 08:43 AM
#14
Thread Starter
New Member
Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb
Thanx bro, you're the 3rd one in almost a year Will check it out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|