VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Beginers Tutorial For DirectX 8.x

by Nick Ridley (8 Submissions)
Category: DirectX
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)

Beginers
Tutorial For DirectX 8.x


WILL Teach You:


How to initialise color="#FF0000" face="Tahoma" size="2">DirectXcolor="#000000">, DirectD3Dface="Tahoma" size="2" color="#000000"> and a size="2">Direct3DDevice

How to use and initialise a face="Tahoma" size="2" color="#FF0000">Vertex Buffercolor="#000000">

How to
Renderface="Tahoma" size="2" color="#000000"> a 3D Pyramid

How to use Matrixface="Tahoma" size="2" color="#000000">'s to rotate your 3D objects

How to use Z-Bufferingface="Tahoma" size="2" color="#000000"> (Draw-Orders/Draw-Buffering)

Rate Beginers Tutorial For DirectX 8.x

Beginers
Tutorial For DirectX 8.x


WILL Teach You:


How to initialise color="#FF0000" face="Tahoma" size="2">DirectXcolor="#000000">, DirectD3Dface="Tahoma" size="2" color="#000000"> and a size="2">Direct3DDevice

How to use and initialise a face="Tahoma" size="2" color="#FF0000">Vertex Buffercolor="#000000">

How to Renderface="Tahoma" size="2" color="#000000"> a 3D Pyramid

How to use Matrixface="Tahoma" size="2" color="#000000">'s to rotate your 3D objects

How to use Z-Bufferingface="Tahoma" size="2" color="#000000"> (Draw-Orders/Draw-Buffering)


By Nick Ridley



šhref="mailto:[email protected]">Mail Me!

"Web Site


width="200" height="200" alt="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 href="http://www.microsoft.com">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 voteface="Tahoma" size="2" color="#000000"> and leave comments. Theres nothing worse than
people who dont appreciate others work. Thanks.....


By Nick Ridley


alt="spydernetlogo.gif (6511 bytes)">



šhref="mailto:[email protected]">Mail Me!

"Web Site


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


Download this snippet    Add to My Saved Code

Beginers Tutorial For DirectX 8.x Comments

No comments have been posted about Beginers Tutorial For DirectX 8.x. Why not be the first to post a comment about Beginers Tutorial For DirectX 8.x.

Post your comment

Subject:
Message:
0/1000 characters