article

Beginers Tutorial For DirectX 8.x

Email
Submitted on: 1/23/2015 1:04:00 PM
By: Nick Ridley (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1944
 
     

Beginers Tutorial For DirectX 8.x

WILL Teach You:

How to initialise DirectX, DirectD3D and a Direct3DDevice
How to use and initialise a Vertex Buffer
How to
Render a 3D Pyramid
How to use
Matrix's to rotate your 3D objects
How to use
Z-Buffering (Draw-Orders/Draw-Buffering)



This article has accompanying files
 
				

Beginers Tutorial For DirectX 8.x

WILL Teach You:

How to initialise DirectX, DirectD3D and a Direct3DDevice
How to use and initialise a Vertex Buffer
How to
Render a 3D Pyramid
How to use
Matrix's to rotate your 3D objects
How to use
Z-Buffering (Draw-Orders/Draw-Buffering)

By Nick Ridley

Mail Me!
"Web Site

spydernetlogo.gif (6511 bytes)

I just started to leard DirectX and ive been founding it very hard so I thought I'd put what I know into this tutorial so that hopefully some others would start to earn it too

Just copy and paste the code into a new VB form that has:

1- A picturebox Name: Picture1
2- A Timer Name: Timer1 Interval: 40

Then goto Project > References and check DirectX 8 For Visual Basic Type Library. If you do not have this goto Microsoft to get it.

<-- BEGIN CODE -->

'--------------------'
'My DirectX8 Tutorial'
'--------------------'
'I decided to make this tutorial after downloading the DirectX SDK
'and realising how hard DirectX is to learn
'This tutorial demonstates how to make a 3D cube, use a Z-Buffer and some other things

Option Explicit

'DirectX Objects
Dim g_DX As New DirectX8 'The main DirectX thingy
Dim g_D3D As Direct3D8 'Used to create the D3DDevice
Dim g_D3DDevice As Direct3DDevice8 'Our rendering device
Dim g_VB(3) As Direct3DVertexBuffer8 'Vertex Buffer, stores our shapes

' A structure for our custom vertex type
' representing a point on the screen
Private Type CUSTOMVERTEX
x As Single 'x in screen space
y As Single 'y in screen space
z As Single 'normalized z
color As Long 'vertex color
End Type

' Our custom FVF, which describes our custom vertex structure
Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE)

'Pi
Const g_pi = 3.1415


Private Sub Form_Load()
Dim b As Boolean

' Allow the form to become visible
Me.Show
DoEvents

' Initialize D3D and D3DDevice
'Uses picture1 as the 'canvas' for DX
b = InitD3D(Picture1.hWnd)

If Not b Then
'If we cant get D3D then tell user and exit
MsgBox "Unable to CreateDevice!", vbCritical, "Error:"
End
End If


' Initialize Vertex Buffer with Geometry
b = InitGeometry()
If Not b Then
'If the vertex buffer stuff failed then tell user and exit
MsgBox "Unable to Create VertexBuffer!", vbCritical, "Error:"
End
End If


' Enable Timer to render the scene
Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()
'call the rendering function
Render
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Well duh! makes sure directX doesnt keep the app going with no forms
End
End Sub

Function InitD3D(hWnd As Long) As Boolean
'Means that if an error occours in VB we carry on, if it happens in
'DX then the object will stay as nothing so we make sure that the error
'was in DX when we say we couldnt initialise D3D
On Local Error Resume Next

' Create the D3D object
Set g_D3D = g_DX.Direct3DCreate()
If g_D3D Is Nothing Then Exit Function

' Get the current display mode
Dim mode As D3DDISPLAYMODE
g_D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, mode

' Fill in the type structure used to create the device
Dim d3dpp As D3DPRESENT_PARAMETERS
d3dpp.Windowed = 1
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
d3dpp.BackBufferFormat = mode.Format
'Z-Buffering (were we make sure that we cant see sides of the object
'we shouldnt be able to
d3dpp.EnableAutoDepthStencil = 1
d3dpp.AutoDepthStencilFormat = D3DFMT_D16

' Create the D3DDevice (use hardware)
Set g_D3DDevice = g_D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, _
D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
If g_D3DDevice Is Nothing Then Exit Function

'Set stuff to do with our device
With g_D3DDevice

Call .SetRenderState(D3DRS_CULLMODE, D3DCULL_CW) 'Cull back faces with clockwise vertices
Call .SetRenderState(D3DRS_CLIPPING, 1) 'Turn Clipping On
Call .SetRenderState(D3DRS_LIGHTING, 0) 'Turn Lighting Off
Call .SetRenderState(D3DRS_ZENABLE, 1) 'Use Z-Buffer

End With

'Tell the Form_Load bit that D3D was initialised successfully
InitD3D = True

End Function


Sub SetupMatrices()


'This position, orientates etc all the objects that we are drawing
'In other words its like moving the camera
Dim matWorld As D3DMATRIX
'Rotate around the Y-Axis
D3DXMatrixRotationY matWorld, Timer * 4
g_D3DDevice.SetTransform D3DTS_WORLD, matWorld

'Here we set were the camera is, were its pointing and which
'is treated as up (Y-Axis in this case)
Dim matView As D3DMATRIX
D3DXMatrixLookAtLH matView, vec3(0#, 3#, -5#), _
vec3(0#, 0#, 0#), _
vec3(0#, 1#, 0#)

g_D3DDevice.SetTransform D3DTS_VIEW, matView

'This bit sets the perspective that means objects will apear smaller
'the further away they are and the near/far clipping planes (how far/close)
'objects can be to the camera for rendering
Dim matProj As D3DMATRIX
D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, 1, 1, 1000
g_D3DDevice.SetTransform D3DTS_PROJECTION, matProj

End Sub

Function InitGeometry() As Boolean

'Three vertices (3d singularitys that will make up our triangle sides
Dim Vertices(2) As CUSTOMVERTEX
Dim VertexSizeInBytes As Long

VertexSizeInBytes = Len(Vertices(0))

'Set the side
With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(2): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With

' Create the vertex buffer.
Set g_VB(0) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
If g_VB(0) Is Nothing Then Exit Function

' fill the vertex buffer from our array
D3DVertexBuffer8SetData g_VB(0), 0, VertexSizeInBytes * 3, 0, Vertices(0)

'Below is not commented because it is the same as ^above^
'just for the other sides
'-----------------------------------------------------------------------------

With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFFFFFF: End With

Set g_VB(1) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
If g_VB(1) Is Nothing Then Exit Function

D3DVertexBuffer8SetData g_VB(1), 0, VertexSizeInBytes * 3, 0, Vertices(0)

'-----------------------------------------------------------------------------

With Vertices(0): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With
With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFF0000: End With

Set g_VB(2) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
If g_VB(2) Is Nothing Then Exit Function

D3DVertexBuffer8SetData g_VB(2), 0, VertexSizeInBytes * 3, 0, Vertices(0)

'-----------------------------------------------------------------------------

With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With
With Vertices(1): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With
With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFF0000: End With

Set g_VB(3) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
If g_VB(3) Is Nothing Then Exit Function

D3DVertexBuffer8SetData g_VB(3), 0, VertexSizeInBytes * 3, 0, Vertices(0)

'-----------------------------------------------------------------------------

'Tell Form_load we succeeded
InitGeometry = True

End Function

Sub Cleanup()
'Sets all our DX stuff to nowt to avoid keeping the stuff in memory when
'we unload
Set g_VB = Nothing
Set g_D3DDevice = Nothing
Set g_D3D = Nothing
End Sub

Sub Render()


'Draw, rotate etc our scene
Dim v As CUSTOMVERTEX
Dim sizeOfVertex As Long


If g_D3DDevice Is Nothing Then Exit Sub

' Clear the backbuffer to a blue color, and clear the Z-Buffer
g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF&, 1, 0

' Begin the scene
g_D3DDevice.BeginScene


' Setup the view/rotation etc
SetupMatrices

'Draw the triangles in the vertex buffer
sizeOfVertex = Len(v)

'Set what triangle we will use
g_D3DDevice.SetStreamSource 0, g_VB(0), sizeOfVertex
'Tell DX this is our custom vertex type
g_D3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX
'draw it...
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1

'---------------------------------------------------

g_D3DDevice.SetStreamSource 0, g_VB(1), sizeOfVertex
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1

'---------------------------------------------------

g_D3DDevice.SetStreamSource 0, g_VB(2), sizeOfVertex
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1

'---------------------------------------------------

g_D3DDevice.SetStreamSource 0, g_VB(3), sizeOfVertex
g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1

'---------------------------------------------------

' End the scene
g_D3DDevice.EndScene

'Transfer the stuff from the backbuffer to the front were we can see it
g_D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0

End Sub

Function vec3(x As Single, y As Single, z As Single) As D3DVECTOR
'helps us create the vertex's easier
vec3.x = x
vec3.y = y
vec3.z = z
End Function

<-- END CODE -->

Ok so this doesnt teach you every aspect of DirectX8 but it is the basics that most people will give up on so I hope this helps you people to get started in DirectX8

Ok so now what else is there to do for you after you get my code:

* Read it (comments mainly) and understand
* Adapt it to make a cube for example to make a cube (remember only to use tri-angles)
* Once you understand my code understand other peoples code on this site
* And once youve done that goto www.microsoft.com and download the DirectX 8 VB SDK and learn from that

Ok, so i hope this helped all of you reading this, if anyone has a DirectX 8 Collision Detection thing or knows how to do it mail me or leave a message here

If you liked/understand/want to use this code PLEASE vote and leave comments. Theres nothing worse than people who dont appreciate others work. Thanks.....

By Nick Ridley

spydernetlogo.gif (6511 bytes)

Mail Me!
"Web Site

If you find any bugs in my code please leave details of your computer and any error messages below :)

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.