Beginers Tutorial For DirectX 8.x
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)
AI
Ringkasan AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Kode Sumber
<p align="center"><strong><font face="Tahoma" size="2"><font color="#FF0000">Beginers</font> Tutorial For <font color="#FF0000">DirectX 8.x</font></font></strong></p> <p align="center"><font color="#FF0000" face="Tahoma" size="2"><strong>WILL Teach You:</strong></font></p> <p align="center"><strong><font face="Tahoma" size="2" color="#000000">How to initialise </font><font color="#FF0000" face="Tahoma" size="2">DirectX</font><font face="Tahoma" size="2" color="#000000">, </font><font color="#FF0000" face="Tahoma" size="2">DirectD3D</font><font face="Tahoma" size="2" color="#000000"> and a </font><font color="#FF0000" face="Tahoma" size="2">Direct3DDevice<br> </font><font face="Tahoma" size="2" color="#000000">How to use and initialise a </font><font face="Tahoma" size="2" color="#FF0000">Vertex Buffer</font><font face="Tahoma" size="2" color="#000000"><br> How to </font><font face="Tahoma" size="2" color="#FF0000">Render</font><font face="Tahoma" size="2" color="#000000"> a 3D Pyramid<br> How to use </font><font face="Tahoma" size="2" color="#FF0000">Matrix</font><font face="Tahoma" size="2" color="#000000">'s to rotate your 3D objects<br> How to use </font><font face="Tahoma" size="2" color="#FF0000">Z-Buffering</font><font face="Tahoma" size="2" color="#000000"> (Draw-Orders/Draw-Buffering)</p> <p><font face="Tahoma" size="2" color="#000000"><strong>By Nick Ridley</strong></font></p> <font face="Webdings" SIZE="1"> <p></font><font face="Webdings" size="4">š</font><a href="mailto:time_to_die_@excite.com">Mail Me!</a><br> <font face="Webdings">"</font><a href="http://www.spyder.tk">Web Site</a></p> <p align="center"><img src="http://www.geocities.com/nike+guy/spydernetlogo.gif" width="200" height="200" alt="spydernetlogo.gif (6511 bytes)"></p> <p><font face="Tahoma" size="2">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</font></p> <p><font face="Tahoma" size="2">Just copy and paste the code into a new VB form that has:</font></p> <p><font face="Tahoma" size="2">1- A picturebox <strong>Name:</strong> Picture1<br> 2- A Timer <strong> Name:</strong> Timer1 <strong>Interval:</strong> 40</font></p> <p><font face="Tahoma" size="2">Then goto <strong><font color="#FF0000">Project > References</font></strong> and check <font color="#0080FF"><strong>DirectX 8 For Visual Basic Type Library</strong></font>. If you do not have this goto <a href="http://www.microsoft.com">Microsoft</a> to get it.</font></p> <h1><font face="Tahoma" size="2"><-- BEGIN CODE --></font></h1> <p><font face="Tahoma" size="2">'--------------------'<br> 'My DirectX8 Tutorial'<br> '--------------------'<br> 'I decided to make this tutorial after downloading the DirectX SDK<br> 'and realising how hard DirectX is to learn<br> 'This tutorial demonstates how to make a 3D cube, use a Z-Buffer and some other things<br> <br> Option Explicit<br> <br> 'DirectX Objects<br> Dim g_DX As New DirectX8 'The main DirectX thingy<br> Dim g_D3D As Direct3D8 'Used to create the D3DDevice<br> Dim g_D3DDevice As Direct3DDevice8 'Our rendering device<br> Dim g_VB(3) As Direct3DVertexBuffer8 'Vertex Buffer, stores our shapes<br> <br> ' A structure for our custom vertex type<br> ' representing a point on the screen<br> Private Type CUSTOMVERTEX<br> x As Single 'x in screen space<br> y As Single 'y in screen space<br> z As Single 'normalized z<br> color As Long 'vertex color<br> End Type<br> <br> ' Our custom FVF, which describes our custom vertex structure<br> Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE)<br> <br> 'Pi<br> Const g_pi = 3.1415<br> <br> <br> Private Sub Form_Load()<br> Dim b As Boolean<br> <br> ' Allow the form to become visible<br> Me.Show<br> DoEvents<br> <br> ' Initialize D3D and D3DDevice<br> 'Uses picture1 as the 'canvas' for DX<br> b = InitD3D(Picture1.hWnd)<br> <br> If Not b Then<br> 'If we cant get D3D then tell user and exit<br> MsgBox "Unable to CreateDevice!", vbCritical, "Error:"<br> End<br> End If<br> <br> <br> ' Initialize Vertex Buffer with Geometry<br> b = InitGeometry()<br> If Not b Then<br> 'If the vertex buffer stuff failed then tell user and exit<br> MsgBox "Unable to Create VertexBuffer!", vbCritical, "Error:"<br> End<br> End If<br> <br> <br> ' Enable Timer to render the scene<br> Timer1.Enabled = True<br> <br> End Sub<br> <br> Private Sub Timer1_Timer()<br> 'call the rendering function<br> Render<br> End Sub<br> <br> Private Sub Form_Unload(Cancel As Integer)<br> 'Well duh! makes sure directX doesnt keep the app going with no forms<br> End<br> End Sub<br> <br> Function InitD3D(hWnd As Long) As Boolean<br> 'Means that if an error occours in VB we carry on, if it happens in<br> 'DX then the object will stay as nothing so we make sure that the error<br> 'was in DX when we say we couldnt initialise D3D<br> On Local Error Resume Next<br> <br> ' Create the D3D object<br> Set g_D3D = g_DX.Direct3DCreate()<br> If g_D3D Is Nothing Then Exit Function<br> <br> ' Get the current display mode<br> Dim mode As D3DDISPLAYMODE<br> g_D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, mode<br> <br> ' Fill in the type structure used to create the device<br> Dim d3dpp As D3DPRESENT_PARAMETERS<br> d3dpp.Windowed = 1<br> d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC<br> d3dpp.BackBufferFormat = mode.Format<br> 'Z-Buffering (were we make sure that we cant see sides of the object<br> 'we shouldnt be able to<br> d3dpp.EnableAutoDepthStencil = 1<br> d3dpp.AutoDepthStencilFormat = D3DFMT_D16<br> <br> ' Create the D3DDevice (use hardware)<br> Set g_D3DDevice = g_D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, _<br> D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)<br> If g_D3DDevice Is Nothing Then Exit Function<br> <br> 'Set stuff to do with our device<br> With g_D3DDevice<br> <br> Call .SetRenderState(D3DRS_CULLMODE, D3DCULL_CW) 'Cull back faces with clockwise vertices<br> Call .SetRenderState(D3DRS_CLIPPING, 1) 'Turn Clipping On<br> Call .SetRenderState(D3DRS_LIGHTING, 0) 'Turn Lighting Off<br> Call .SetRenderState(D3DRS_ZENABLE, 1) 'Use Z-Buffer<br> <br> End With<br> <br> 'Tell the Form_Load bit that D3D was initialised successfully<br> InitD3D = True<br> <br> End Function<br> <br> <br> Sub SetupMatrices()<br> <br> <br> 'This position, orientates etc all the objects that we are drawing<br> 'In other words its like moving the camera<br> Dim matWorld As D3DMATRIX<br> 'Rotate around the Y-Axis<br> D3DXMatrixRotationY matWorld, Timer * 4<br> g_D3DDevice.SetTransform D3DTS_WORLD, matWorld<br> <br> 'Here we set were the camera is, were its pointing and which<br> 'is treated as up (Y-Axis in this case)<br> Dim matView As D3DMATRIX<br> D3DXMatrixLookAtLH matView, vec3(0#, 3#, -5#), _<br> vec3(0#, 0#, 0#), _<br> vec3(0#, 1#, 0#)<br> <br> g_D3DDevice.SetTransform D3DTS_VIEW, matView<br> <br> 'This bit sets the perspective that means objects will apear smaller<br> 'the further away they are and the near/far clipping planes (how far/close)<br> 'objects can be to the camera for rendering<br> Dim matProj As D3DMATRIX<br> D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, 1, 1, 1000<br> g_D3DDevice.SetTransform D3DTS_PROJECTION, matProj<br> <br> End Sub<br> <br> Function InitGeometry() As Boolean<br> <br> 'Three vertices (3d singularitys that will make up our triangle sides<br> Dim Vertices(2) As CUSTOMVERTEX<br> Dim VertexSizeInBytes As Long<br> <br> VertexSizeInBytes = Len(Vertices(0))<br> <br> 'Set the side<br> With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(2): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With<br> <br> ' Create the vertex buffer.<br> Set g_VB(0) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _<br> 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)<br> If g_VB(0) Is Nothing Then Exit Function<br> <br> ' fill the vertex buffer from our array<br> D3DVertexBuffer8SetData g_VB(0), 0, VertexSizeInBytes * 3, 0, Vertices(0)<br> <br> 'Below is not commented because it is the same as ^above^<br> 'just for the other sides<br> '-----------------------------------------------------------------------------<br> <br> With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFFFFFF: End With<br> <br> Set g_VB(1) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _<br> 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)<br> If g_VB(1) Is Nothing Then Exit Function<br> <br> D3DVertexBuffer8SetData g_VB(1), 0, VertexSizeInBytes * 3, 0, Vertices(0)<br> <br> '-----------------------------------------------------------------------------<br> <br> With Vertices(0): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With<br> With Vertices(1): .x = 1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFF0000: End With<br> <br> Set g_VB(2) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _<br> 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)<br> If g_VB(2) Is Nothing Then Exit Function<br> <br> D3DVertexBuffer8SetData g_VB(2), 0, VertexSizeInBytes * 3, 0, Vertices(0)<br> <br> '-----------------------------------------------------------------------------<br> <br> With Vertices(0): .x = -1: .y = -1: .z = 0: .color = &HFFFF0000: End With<br> With Vertices(1): .x = 0: .y = 1: .z = 0: .color = &HFF00FFFF: End With<br> With Vertices(2): .x = 0: .y = -1: .z = 1: .color = &HFFFF0000: End With<br> <br> Set g_VB(3) = g_D3DDevice.CreateVertexBuffer(VertexSizeInBytes * 3, _<br> 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)<br> If g_VB(3) Is Nothing Then Exit Function<br> <br> D3DVertexBuffer8SetData g_VB(3), 0, VertexSizeInBytes * 3, 0, Vertices(0)<br> <br> '-----------------------------------------------------------------------------<br> <br> 'Tell Form_load we succeeded<br> InitGeometry = True<br> <br> End Function<br> <br> Sub Cleanup()<br> 'Sets all our DX stuff to nowt to avoid keeping the stuff in memory when<br> 'we unload<br> Set g_VB = Nothing<br> Set g_D3DDevice = Nothing<br> Set g_D3D = Nothing<br> End Sub<br> <br> Sub Render()<br> <br> <br> 'Draw, rotate etc our scene<br> Dim v As CUSTOMVERTEX<br> Dim sizeOfVertex As Long<br> <br> <br> If g_D3DDevice Is Nothing Then Exit Sub<br> <br> ' Clear the backbuffer to a blue color, and clear the Z-Buffer<br> g_D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF&, 1, 0<br> <br> ' Begin the scene<br> g_D3DDevice.BeginScene<br> <br> <br> ' Setup the view/rotation etc<br> SetupMatrices<br> <br> 'Draw the triangles in the vertex buffer<br> sizeOfVertex = Len(v)<br> <br> 'Set what triangle we will use<br> g_D3DDevice.SetStreamSource 0, g_VB(0), sizeOfVertex<br> 'Tell DX this is our custom vertex type<br> g_D3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX<br> 'draw it...<br> g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1<br> <br> '---------------------------------------------------<br> <br> g_D3DDevice.SetStreamSource 0, g_VB(1), sizeOfVertex<br> g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1<br> <br> '---------------------------------------------------<br> <br> g_D3DDevice.SetStreamSource 0, g_VB(2), sizeOfVertex<br> g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1<br> <br> '---------------------------------------------------<br> <br> g_D3DDevice.SetStreamSource 0, g_VB(3), sizeOfVertex<br> g_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1<br> <br> '---------------------------------------------------<br> <br> ' End the scene<br> g_D3DDevice.EndScene<br> <br> 'Transfer the stuff from the backbuffer to the front were we can see it<br> g_D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0<br> <br> End Sub<br> <br> Function vec3(x As Single, y As Single, z As Single) As D3DVECTOR<br> 'helps us create the vertex's easier<br> vec3.x = x<br> vec3.y = y<br> vec3.z = z<br> End Function</font></p> <p> </p> <h1><font face="Tahoma" size="2"><-- END CODE --></font></h1> <p> </p> <p><font face="Tahoma" size="2">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</font></p> <p><font face="Tahoma" size="2">Ok so now what else is there to do for you after you get my code:</font></p> <p><font face="Tahoma" size="5"><strong>*</strong></font><font face="Tahoma" size="2"> Read it (comments mainly) and understand<br> </font><font face="Tahoma" size="5"><strong>*</strong> </font><font face="Tahoma" size="2">Adapt it to make a cube for example to make a cube (remember only to use tri-angles)<br> </font><font face="Tahoma" size="5"><strong>*</strong></font><font face="Tahoma" size="2"> Once you understand my code understand other peoples code on this site<br> </font><font face="Tahoma" size="5"><strong>*</strong></font><font face="Tahoma" size="2"> And once youve done that goto <a href="http://www.microsoft.com">www.microsoft.com</a> and download the DirectX 8 VB SDK and learn from that</font></p> <p><font face="Tahoma" size="2" color="#000000">Ok, so i hope this helped all of you reading this, if anyone has a </font><strong><font face="Tahoma" size="2" color="#FF0000">DirectX 8 Collision Detection</font></strong><font face="Tahoma" size="2" color="#000000"> thing or knows how to do it <strong>mail me or leave a message here</strong></font></p> <p><font face="Tahoma" size="2" color="#000000">If you <strong>liked/understand/want to use this code</strong> PLEASE </font><font face="Tahoma" size="2" color="#FF0000"><strong>vote</strong></font><font face="Tahoma" size="2" color="#000000"> and leave comments. Theres nothing worse than people who dont appreciate others work. Thanks.....</font></p> <p><strong><font face="Tahoma" size="2" color="#000000">By Nick Ridley</font></strong></p> <p><img src="http://www.geocities.com/nike+guy/spydernetlogo.gif" width="200" height="200" alt="spydernetlogo.gif (6511 bytes)"></p> <font face="Webdings" SIZE="1"> <p></font><font face="Webdings" size="4">š</font><a href="mailto:time_to_die_@excite.com">Mail Me!</a><br> <font face="Webdings">"</font><a href="http://www.spyder.tk">Web Site</a></p> <p><strong><font face="Tahoma" size="2">If you find any bugs in my code please leave details of your computer and any error messages below :)</font></strong></p> </font></strong>
Komentar Asli (3)
Dipulihkan dari Wayback Machine