|
| | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | An Introduction To DirectX8
Visit www.VBgames.co.uk
for more!
What you will learn
- How to use the DirectX libraries from Visual Basic
- How to get input from the most commonly used devices - keyboard and mouse
- using DirectInput
- How to load and play a wave file using DirectSound
- How to create a rendering device with Direct3D
- How to use the D3DX helper functions
- How to use vertex and index buffers to create simple geometry
- How to use view and projection matrices to set up a camera
- How to use world matrices to make animation and reuse the same geometry
- How to load textures from a bitmap file
- How to unload all of this safely
How you will learn it
- Overview of DirectX8
- Explanation of DirectX terms
- Explanation of sample program
- Full working demo program to download
with source code and comments
- Evaluation of what has been learnt
- Exercises to extend your knowledge
- Thoughts for future tutorials
Boring Intro
Back by popular demand is my DirectX tutorial series! Although I'm sort of starting
again for DirectX 8. So for complete newbies, this tutorial is great, and for
those who already know some DX7 or DX8, the tutorial includes some more complex
stuff than previous tutorials. In DirectX 8, the API has become simpler in
the initialization of objects and it also has many more maths functions to help
you. But it's still alot of work to do by yourself, so that's why you should
help spread the word by making free source demos and tutorials. At this point I acknowledge
Richard Hayden for his free source Direct3D8 world, it is a great example of
what I am talking about and helped me begin to learn the new API. Enough of the
chit chat...
Before you begin
If you don't already have DirectX 8 and the DirectX 8 Type Libraries for
Visual Basic then you've got some downloading to do! Sorry, but it is worth it.
You don't need all the SDK documentation, although I recommend getting it, and
you don't need the C++ SDK if you are a VB'er only, so your download might not
be as big as mine was. I managed to download 135 MB though my cheap 56 K phone
line though, and that was the full download including everything. So it is
possible, but you will need to get a program such as GetRight
to help you download such a big file. All developer information and downloads
can be found at www.microsoft.com/directx
. Once DirectX 8 is installed, and you have the DX VB Type Libs, read on.
Adding a reference to your project
Every time you start a new project that will use DirectX, you will need to do
the following:
- Click the Project menu
- Choose the References... submenu and the References dialog will pop up
- Scroll down the list of references until you find the "DirectX 8 Type
Library for Visual Basic" and check the box next to it
- Click OK
Now VB will know every class, type and enumeration that DirectX 8 contains,
so you're ready to begin coding!
DirectX and 3D terminology
This is the part which gets most people. I wish I had someone to explain all
the jargon to me when I was learning. After the language barrier, things get a
bit easier. Here's some terms you need to know. If you already know a bit about
DirectX, you should probably skip this whole section and only come back to it
when you see a word you don't understand. It is not in alphabetical order,
rather it is in logical order so that you can read the whole thing if you're new
and you want to. As you can see, there is alot of it, and this is only the basics.
- DirectX, DirectAudio (DirectSound, DirectMusic), DirectGraphics
(Direct3D, DirectDraw), DirectPlay, DirectSetup - These are all part of
the DirectX API. They are the main objects which deal with different jobs
e.g DirectAudio takes care of all audio input and output, and it contains
DirectSound and DirectMusic
- API - What does that stand for again? I think it was Advanced
Programming Interface (correct me please if I'm wrong). At least I know what
it means. It's the bunch of objects that give you a higher level view of a
task, so you don't need to think about writing low level code anymore
because people have already made functions to do that for you.
- Variable - If you don't know what a variable is, go away and learn
something simpler.
- Type - I hope you know this too. It's several variables group
together, in C++ it's a structure.
- Class - This is code that describes an object (see below).
- Object - An object can contain variables like a type, but it also
can have functions that can be called from code elsewhere. An object is
created from a class.
- Instance - When an object is created from a class, it is said to be
an instance of the object. Note there can be many instances of an object
created from the same class.
- Library - A whole group of objects are often grouped together into
one file, usually a DLL (Dynamic Link Library). DirectX is made of DLL's.
- Pointer - This is a variable that stores a memory address. In VB,
your don't use pointers directly, but if you use a object without creating a
new instance of the object, you are basically using a pointer to another
object.
- Buffer - A word given to a chunk of memory which has been assigned
a job, usually to temporarily store data which is moved around alot. There
are several types of buffer in DirectX.
- Backbuffer, Frontbuffer, Surface, Texture - These are used to store
graphics. The only visible graphics buffer is the front buffer. In DirectX
8, you never need to worry about this, just know what it is (erm, like, it's
what you see on the screen). A back buffer is where graphics go just before
the front buffer. You draw on the back buffer, and when your super duper
graphics are finished, you ask DirectX to move it to the front buffer. A
surface is just like a back buffer, it stores pictures, but it is more
general since it has nothing to do with a front buffer. A texture is a
surface used for texture mapping polygons (see later), and is usually of dimensions
that are square, a power of 2, typically 256 x 256.
- Copy, VSync, Blt, Flip, Discard - These are methods of copying from
one surface to another. Copying involves copying every single bit from on
surface to another. Flipping involves moving a pointer to s surface so that
the front buffer and back buffer surfaces switch roles (their pointers are
swapped) making for a very quick appearance of a new image. VSync means synchronizing
the copying or flipping of surfaces with the vertical refresh of the monitor
so that you can't see the graphics flicker. If you discard your surface when
you flip, it is a faster, but the contents of the back buffer are not
guaranteed to be still the same as before the flip.
- Z buffer - A piece of memory that store the z positions of objects
drawn onto a surface.
- Sound buffers (primary and secondary) - A sound buffer stores a
sound. A primary sound buffer can be heard out of the speakers, with DirectX
you can ignore it because it is managed for you. A secondary sound buffer is
where sounds can be stored before being mixed and sent to the primary
buffer.
- Mixing - The process of creating just one sound from several source
sounds.
- Static and streaming - A static buffer stores just a whole sound
and just sits there. A streaming buffer stores only part of the sound and
constantly is moving in the next part of the sound and and moving out the
already played sound. A static buffer is more CPU efficient and a streaming
buffer is more memory efficient.
- Input device - This is commonly a mouse or a keyboard, but can also
be a joy pad or a steering wheel etc.
- Device state - The state of the input device depends on what
buttons/rollers/wheels are being pressed/moved etc. For example, the state
of the keyboard is that the "X" key is down.
- Rendering device - This is something that draws graphics, it can be
a hardware graphics card or a software emulation device.
- Hardware and software emulation - Hardware is a physical unit on
your computer and is usually very fast at doing it's job. Software emulation
can do the same job as hardware, but at a slower rate and using up memory.
- System and video memory - System memory is the main memory where everything
else is stored - programs, Windows, anything and everything scattered
everywhere so it can be slow. Video memory is separately used for hardware
to store pictures and is usually alot faster. It can be a slow operation to
copy between these two types of memory.
- Polygon, Primitive - Polygons are a general term for shapes that
can be made with a number of straight edged sides and are used in 3D store
create shapes. In Direct3D, a primitive is usually a point, a line or a
triangle.
- Material - A polygon appears to be made of a material, in DirectX,
a material has several colors to describe it's appearance.
- Texture mapping - When a polygon has a picture put onto it it is
said that the polygon has been texture mapped.
- Texture management - Textures must be ordered and and moved around
so that the right textures are available when they are need. DirectX by
default can do this for you.
- Vector - A 3 dimensional value, having x, y and z components.
- Vertex - A primitive is made up of vertices (plural of vertex)
where edges end or meet. They can be just the same as vectors, or they can
have additional components such as color, direction (or normal), or texture
coordinates.
- Plane - A flat shape that goes on forever and splits space into 2.
For example, the ground is a horizontal plane.
- Normal - A normal to a plane or vertex or primitive is a vector
that describes where it is facing. Has a similar meaning as perpendicular or
orthogonal.
- Transformation - A formula that changes a vector to another
position.
- Matrix - Matrices (plural of matrix) describes any transformation
by storing 16 numbers.
- Translation, rotation, scaling - These are types of
transformations. A translation is a movement in the x, y or z direction (or
2 or all 3 directions), a rotation spins the vector around an origin,
scaling resizes the vector around a origin.
- Origin - A point or vector that is the center of something.
- World, View and Projection - The world transformation affects every
vector in the world, moving it moves everything. The view transformation
makes the camera or eye on the scene appear to be in the right place, and
the projection transformation describes how the 3D scene is conveyed onto
the 2D picture produced from it.
- Culling - When a polygon is not facing the camera, the process of
culling ensures it is not drawn.
- Z buffering and Z sorting - This process makes sure that when a
object is obscured by another, it cannot be seen.
- More... I've missed out loads so if you still don't understand a
word then just ask.
The sample program
You can download the sample program from here.
You will need a program like Winzip to
decompress it. It is written in Visual Basic 6, if you have another version of
VB then there is information on www.planet-source-code.com/vb
as to how to try to open the version 6 files.
The sample program uses hardware accelerated rasterization. If your computer does not have this, the request
will fail, so use D3DDEVTYPE_REF instead of D3DDEVTYPE_HAL if this happens. A real
program would be able to detect an error and automatically switch device. It also requests software vertex processing, which means the CPU has to
transform and light geometry, but if you have a good graphics card, you might be
able to use hardware vertex processing.
The sample program assumes your computer can render in 16 bit (R5G6B5) color
format, in 640 x 480 resolution. If this is not the case, it may fail but you
can change those values in the source code.
The sample program renders the same texture mapped 3D cube in different
positions. It uses the same cube but it makes it appear that there are 3, each
of different sizes, and they are all spinning and rotating around everywhere.
The camera can be zoomed in and out using the mouse and the program can be
exited using the escape key. The program plays a sound every time the animation
loop restarts. It attempts to show all the basic features of DirectX 8 simply.
It is not optimized so as to keep it as simple to understand as possible.
The main point to note is the that the animation is achieved by moving the
world transformation. Every single line is commented, an there are lengthy
explanations of each main function. Here is the full source code and comment to
view, but you can also download
it.
---***---SOURCE CODE STARTS HERE---***---
'-----------------------------------------------------------------
'
'DX8 INTRODUCTION - DIRECTGRAPHICS,DIRECTSOUND, DIRECTINPUT
'
' BY SIMON PRICE
'
'-----------------------------------------------------------------
' For this tutorial program you will need the DirectX8 for Visual
' Basic Type Library, from www.microsoft.com/directx
' You should also have the tutorial in HTML format, if you don't
' you can download it from my website www.VBgames.co.uk
' Any questions go to ihaveaquestionforsimonaboutdx8@VBgames.co.uk,
' or you could use a shorter address :) (si@VBgames.co.uk will do)
' Any bug reports go to the same address too please, as do comments
' feedback, suggestions, erm whatever you feel like
' Every time you start a project which will use DirectX8, you need
' to click on the menu Project -> References and a dialog box will
' pop up. Check the box which says "DirectX8 for Visual Basic Type
' Library" and click OK. Now VB will know all the types, classes
' enumerations and functions of DirectX8.
Option Explicit
' GLOBAL VARIABLE DECLARATIONS
' No matter what you do with DirectX8, you will need to start with
' the DirectX8 object. You will need to create a new instance of
' the object, using the New keyword, rather than just getting a
' pointer to it, since there's nowhere to get a pointer from yet (duh!).
Dim DX As New DirectX8
' The DirectInput8 object is used to get data from input devices
' such as the mouse and keyboard. This is what we will use it for
' in this tutorial, since they are the most common input devices.
' Notice how we don't create a new instance of the object, rather
' DirectX does that for us and we just get a pointer to it.
Dim DI As DirectInput8
' Now we need 2 devices - keyboard and mouse...
Dim Keyboard As DirectInputDevice8
Dim Mouse As DirectInputDevice8
' ...and a structure (type) to hold the data from each device. DI
' provides us a custom keyboard and mouse type, since they are
' commonly used
Dim KeyboardState As DIKEYBOARDSTATE
Dim MouseState As DIMOUSESTATE
' Next, we have DirectSound8, this can be used for many things, but
' for now we just play a sound from a .wav file
Dim DS As DirectSound8
' A sound buffer is a piece of memory in which the sound is stored.
' We use a secondary buffer, because a primary buffer can actually
' be heard though the speakers, and the sound needs to be mixed
' before we allow the user to hear that. In this tutorial, we let
' DirectSound worry about mixing and copying to the primary buffer
' to play the sound for us
Dim Sound As DirectSoundSecondaryBuffer8
' The DSBUFFER type holds a description of a sound buffer. We won't
' use any of the more advanced flags in this tutorial
Dim SoundDesc As DSBUFFERDESC
' The Direct3D8 object is responsible for all graphics, yes, even 2D
Dim D3D As Direct3D8
' The D3DX8 object contains lots of helper functions, mostly math
' to make Direct3D alot easier to use. Notice we create a new
' instance of the object using the New keyword.
Dim D3DX As New D3DX8
' The Direct3DDevice8 represents our rendering device, which could
' be a hardware or a software device. The great thing is we still
' use the same object no matter what it is
Dim D3Ddevice As Direct3DDevice8
' The D3DPRESENT_PARAMETERS type holds a description of the way
' in which DirectX will display it's rendering
Dim D3Dpp As D3DPRESENT_PARAMETERS
' The D3DMATERIAL8 type stores information on the material our
' polygons are rendered with, such as color
Dim Material As D3DMATERIAL8
' The Direct3DTexture8 object represents a piece of memory used to
' store a texture to be mapped onto our polygons
Dim Texture As Direct3DTexture8
' The Direct3DVertexBuffer8 object stores an array of vertices from which
' our polygons are made
Dim VertexBuffer As Direct3DVertexBuffer8
' The D3DVERTEX type stores vertices temporarily before we copy
' them into the vertex buffer
Dim Vertex(1 To 24) As D3DVERTEX
' The Direct3DIndexBuffer8 object stores the order in which our
' vertices are rendered
Dim IndexBuffer As Direct3DIndexBuffer8
' These integers are used to temporarily store indices before they
' are copied into the index buffer
Dim Index(1 To 36) As Integer
' This stores the rotation of the cubes
Dim Rotation As Single
' FORM_LOAD
' The whole program is started and controlled from here
Private Sub Form_Load()
On Error Resume Next
' initialize directx
If Init = False Then
' display error message
MsgBox "Error! Could not initialize DirectX!"
Else
' show form
Show
' do main program loop
MainLoop
End If
' unload form and clean up directx
Unload Me
End Sub
' FORM_UNLOAD
' Before the program ends, call the cleanup function
Private Sub Form_Unload(Cancel As Integer)
CleanUp
End Sub
' INITIALIZATION
' In this function we initialize all the global DirectX objects. We
' basically get the DirectInput, DirectSound, and DirectGraphics
' engines started up, and retrieve pointers so we can manipulate them
Function Init() As Boolean
'On Error GoTo InitFailed
' DIRECTINPUT
' Get a pointer to DirectInput
Set DI = DX.DirectInputCreate()
' Check to see if the pointer is valid
If DI Is Nothing Then GoTo InitFailed
' Get a pointer to keyboard and mouse device objects
Set Keyboard = DI.CreateDevice("GUID_SysKeyboard")
Set Mouse = DI.CreateDevice("guid_SysMouse")
' Check to see if pointers are valid
If Keyboard Is Nothing Then GoTo InitFailed
If Mouse Is Nothing Then GoTo InitFailed
' Set the data formats to the commmonly used keyboard and mouse
Keyboard.SetCommonDataFormat DIFORMAT_KEYBOARD
Mouse.SetCommonDataFormat DIFORMAT_MOUSE
' Set cooperative level, this tells DI how much control we need
Keyboard.SetCooperativeLevel hWnd, DISCL_NONEXCLUSIVE Or DISCL_BACKGROUND
Mouse.SetCooperativeLevel hWnd, DISCL_NONEXCLUSIVE Or DISCL_BACKGROUND
' Now we are ready to aquire (erm, get) our input devices
Keyboard.Acquire
Mouse.Acquire
' DIRECTSOUND
' Get a pointer to DirectSound
Set DS = DX.DirectSoundCreate("")
' Check the pointer is valid
If DS Is Nothing Then GoTo InitFailed
' Set cooperative level, we only need normal functionality
DS.SetCooperativeLevel hWnd, DSSCL_NORMAL
' Create a sound buffer from a .wav file. We provide a filename
' and a DSBUFFER type, which stores any special information
' about the buffer we might need to know (not used here)
Set Sound = DS.CreateSoundBufferFromFile(App.Path & "\sound.wav", SoundDesc)
' Check the pointer is valid
If Sound Is Nothing Then GoTo InitFailed
' DIRECT3D
' Get a pointer to Direct3D
Set D3D = DX.Direct3DCreate()
' Check the pointer is valid
If D3D Is Nothing Then GoTo InitFailed
' Fill the D3DPRESENT_PARAMETERS type, describing how DirectX should
' display it's renders
With D3Dpp
' set the most common fullscreen display mode
.Windowed = False ' the app is not in a window
.BackBufferWidth = 640 '
the size of the screen
.BackBufferHeight = 480
.BackBufferFormat = D3DFMT_R5G6B5 ' the color depth format (16 bit)
' the swap effect determines how the graphics get from
' the backbuffer to the screen - note : D3DSWAPEFFECT_DISCARD
' means that every time the render is presented, the backbuffer
' image is destroyed, so everything must be rendered again
.SwapEffect = D3DSWAPEFFECT_DISCARD
' request a 16 bit z-buffer - this depth sorts the scene
' so we can't see polygons that are behind other polygons
.EnableAutoDepthStencil = 1
.AutoDepthStencilFormat = D3DFMT_D16
' 1 backbuffer
.BackBufferCount = 1
End With
' Create the rendering device. Here we request a hardware rasterization.
' If your computer does not have this, the request may fail, so use
' D3DDEVTYPE_REF instead of D3DDEVTYPE_HAL if this happens. A real
' program would be able to detect an error and automatically switch device.
' We also request software vertex processing, which means the CPU has to
' transform and light our geometry
Set D3Ddevice = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3Dpp)
' check the pointer is valid
If D3Ddevice Is Nothing Then GoTo InitFailed
' Set rendering options
D3Ddevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
D3Ddevice.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE ' enable z buffering
D3Ddevice.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID ' render solid polygons
D3Ddevice.SetRenderState D3DRS_LIGHTING, True ' enable lighting
D3Ddevice.SetRenderState D3DRS_AMBIENT, vbWhite ' use ambient white light
' Set the material properties
With Material.Ambient
.a = 1: .r = 1: .g = 1: .b = 1
End With
' Create a texture surface from a file
Set Texture = D3DX.CreateTextureFromFile(D3Ddevice, App.Path & "\texture.bmp")
' Check the pointer is valid
If Texture Is Nothing Then GoTo InitFailed
' Set the material and texture as the current ones to render from
D3Ddevice.SetMaterial Material
D3Ddevice.SetTexture 0, Texture
' Create a vertex buffer, using default usage and specifying enough memory for 24 vertices of format
D3DFVF_VERTEX
Set VertexBuffer = D3Ddevice.CreateVertexBuffer(24 * Len(Vertex(1)), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT)
' Check pointer is valid
If VertexBuffer Is Nothing Then GoTo InitFailed
' Create an index buffer, using default uage and specifying enough memory for 36 16 bit integers
Set IndexBuffer = D3Ddevice.CreateIndexBuffer(36 * Len(Index(1)), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT)
' Check pointer is valid
If IndexBuffer Is Nothing Then GoTo InitFailed
' Now we make a cube shape out of our vetices
Vertex(1) = MakeVertex(-1, 1, -1, 0, 0, -1, 0, 0)
Vertex(2) = MakeVertex(1, 1, -1, 0, 0, -1, 1, 0)
Vertex(3) = MakeVertex(-1, -1, -1, 0, 0, -1, 0, 1)
Vertex(4) = MakeVertex(1, -1, -1, 0, 0, -1, 1, 1)
Vertex(5) = MakeVertex(1, 1, -1, 0, 0, 1, 0, 0)
Vertex(6) = MakeVertex(-1, 1, -1, 0, 0, 1, 1, 0)
Vertex(7) = MakeVertex(1, -1, -1, 0, 0, 1, 0, 1)
Vertex(8) = MakeVertex(-1, -1, -1, 0, 0, 1, 1, 1)
Vertex(9) = MakeVertex(-1, 1, 1, -1, 0, 0, 0, 0)
Vertex(10) = MakeVertex(-1, 1, -1, -1, 0, 0, 1, 0)
Vertex(11) = MakeVertex(-1, -1, 1, -1, 0, 0, 0, 1)
Vertex(12) = MakeVertex(-1, -1, -1, -1, 0, 0, 1, 1)
Vertex(13) = MakeVertex(1, 1, -1, 1, 0, 0, 0, 0)
Vertex(14) = MakeVertex(1, 1, 1, 1, 0, 0, 1, 0)
Vertex(15) = MakeVertex(1, -1, -1, 1, 0, 0, 0, 1)
Vertex(16) = MakeVertex(1, -1, 1, 1, 0, 0, 1, 1)
Vertex(17) = MakeVertex(-1, 1, -1, 0, 1, 0, 0, 0)
Vertex(18) = MakeVertex(1, 1, -1, 0, 1, 0, 1, 0)
Vertex(19) = MakeVertex(-1, 1, 1, 0, 1, 0, 0, 1)
Vertex(20) = MakeVertex(1, 1, 1, 0, 1, 0, 1, 1)
Vertex(21) = MakeVertex(-1, -1, -1, 0, -1, 0, 0, 0)
Vertex(22) = MakeVertex(1, -1, -1, 0, -1, 0, 1, 0)
Vertex(23) = MakeVertex(-1, -1, 1, 0, -1, 0, 0, 1)
Vertex(24) = MakeVertex(1, -1, 1, 0, -1, 0, 1, 1)
' Copy the vertices into the vertex buffer
D3DVertexBuffer8SetData VertexBuffer, 0, 24 * Len(Vertex(1)), 0, Vertex(1)
' Make a list which tells the order in which to render these vertices
MakeIndices 1, 2, 3, 3, 2, 4, 5, 6, 7, 7, 6, 8, 9, 10, 11, 11, 10, 12, 13, 14, 15, 15, 14, 16, 17, 18, 19, 19, 18, 20, 21, 22, 23, 23, 22, 24
' Copy the indices into the index buffer
D3DIndexBuffer8SetData IndexBuffer, 0, 36 * Len(Index(1)), 0, Index(1)
' Set the vertex format
D3Ddevice.SetVertexShader D3DFVF_VERTEX
' Set the vertex and index buffers as current ones to render from
D3Ddevice.SetStreamSource 0, VertexBuffer, Len(Vertex(1))
D3Ddevice.SetIndices IndexBuffer, -1
' Initializtion is complete!
Init = True
Exit Function
InitFailed: ' the initialization function has failed
Init = False
End Function
' MAKEVECTOR
' This function creates vectors
Function MakeVector(x As Single, y As Single, z As Single) As D3DVECTOR
With MakeVector
.x = x
.y = y
.z = z
End With
End Function
' MAKEVERTEX
' This function creates vertices
Function MakeVertex(x As Single, y As Single, z As Single, nx As Single, ny As Single, nz As Single, tu As Single, tv As Single) As D3DVERTEX
With MakeVertex
.x = x
.y = y
.z = z
.nx = nx
.ny = ny
.nz = nz
.tu = tu
.tv = tv
End With
End Function
' MAKEINDICES
' This function creates a list of indices
Function MakeIndices(ParamArray Indices()) As Integer()
Dim i As Integer
For i = LBound(Indices) To UBound(Indices)
Index(i + 1) = Indices(i)
Next
End Function
' MAINLOOP
' This sub animates the scene by moving the positions of the
' cubes and the camera position, then renders the cubes. It
' checks to see if the escape key has been pressed and loops
' if it has not.
Sub MainLoop()
' the mathematical constant pi
Const PI = 3.1415
' the speed of animation
Const SPEED = 0.01
' matrices for animation and cameras
Dim matTranslation As D3DMATRIX, matRotation As D3DMATRIX, matScaling As D3DMATRIX, matView As D3DMATRIX, matProjection As D3DMATRIX, matTransform As D3DMATRIX
' camera position
Dim CameraPos As D3DVECTOR
On Error Resume Next
Do
' let Windows messages be executed
DoEvents
' get keyboard and mouse data
Keyboard.GetDeviceStateKeyboard KeyboardState
Mouse.GetDeviceStateMouse MouseState
' if escape was pressed, exit program
If KeyboardState.Key(DIK_ESCAPE) Then Exit Do
' move camera with mouse
CameraPos.y = CameraPos.y + MouseState.lY / 10
CameraPos.z = -2
' set camera position, using mouse data
D3DXMatrixLookAtLH matView, MakeVector(CameraPos.x, CameraPos.y, CameraPos.z), MakeVector(0, 0, 0), MakeVector(0, 1, 0)
D3Ddevice.SetTransform D3DTS_VIEW, matView
D3DXMatrixPerspectiveFovLH matProjection, PI / 3, 0.75, 0.1, 10000
D3Ddevice.SetTransform D3DTS_PROJECTION, matProjection
' move the rotation angle
Rotation = Rotation + SPEED
If Rotation > 2 * PI Then
Rotation = Rotation - 2 * PI
' once per rotation, play a sound
Sound.Play DSBPLAY_DEFAULT
End If
' clear the rendering device backbuffer and z-buffer
D3Ddevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, vbWhite, 1#, 0
' start rendering
D3Ddevice.BeginScene
' create rotation matrix
D3DXMatrixRotationYawPitchRoll matTransform, Rotation * 2, Rotation, Rotation
' set the world matrix to normal
D3Ddevice.SetTransform D3DTS_WORLD, matTransform
' draw the medium cube
DrawCube
' create movement, rotation and scale matrices
D3DXMatrixTranslation matTranslation, 0, 0, 4
D3DXMatrixRotationYawPitchRoll matRotation, 0, Rotation * 2, Rotation * 4
D3DXMatrixScaling matScaling, 0.5, 0.5, 0.5
' combine them
D3DXMatrixMultiply matTransform, matRotation, matTranslation
D3DXMatrixMultiply matTransform, matTransform, matScaling
' transform the world matrix
D3Ddevice.MultiplyTransform D3DTS_WORLD, matTransform
' draw the small cube
DrawCube
' create movement, rotation and scale matrices
D3DXMatrixTranslation matTranslation, -3, -3, -3
D3DXMatrixRotationYawPitchRoll matRotation, Rotation * 8, 0, Rotation * 6
D3DXMatrixScaling matScaling, 0.5, 0.5, 0.5
' combine them
D3DXMatrixMultiply matTransform, matTranslation, matRotation
D3DXMatrixMultiply matTransform, matTransform, matScaling
' transform the world matrix
D3Ddevice.MultiplyTransform D3DTS_WORLD, matTransform
' draw the small cube
DrawCube
' end rendering
D3Ddevice.EndScene
' present the contents of the backbuffer by flipping it to the screen
D3Ddevice.Present ByVal 0, ByVal 0, 0, ByVal 0
Loop
End Sub
' DRAWCUBE
' Draws the cube
Sub DrawCube()
On Error Resume Next
' draw 12 triangles, in a cube shape, onto the backbuffer of the rendering device
D3Ddevice.DrawIndexedPrimitive D3DPT_TRIANGLELIST, 0, 36, 0, 12
End Sub
' CLEANUP
' This unloads all the DirectX objects - we destroy objects we
' have created, an disassociate our pointers from objects
' create by DirectX, so then DirectX can destroy them. Failing
' to call this sub can cause memory to be lost.
Sub CleanUp()
On Error Resume Next
Set Keyboard = Nothing
Set Mouse = Nothing
Set DI = Nothing
Set Sound = Nothing
Set DS = Nothing
Set Texture = Nothing
Set D3Ddevice = Nothing
Set D3DX = Nothing
Set D3D = Nothing
End Sub
---***---SOURCE CODE ENDS HERE---***---
Hey - does somebody have a HTML VB syntax color highlighter? As you can see,
I got fed up and didn't color in the keywords!
What you have learnt
- Initialization - getting DirectX objects - loading textures, sounds,
geometry, vertex and index buffers, getting input devices
- Rendering - How to draw and present texture mapped triangles
- Sound - erm, how to play one
- Input - how to read the keyboard and mouse
- Animation - how to use matrices to perform complex animation
- Alot of keywords and terms
What you should do next
- I've left a bug in the program for you on purpose! One face of each cube
is not rendered! Find the bug and kill it! I do know the answer, honestly,
but I'm not telling because debugging is a major part of programming for you
to learn!
- Try some different shapes, animation, colors, textures, sounds, camera
movements
- Try adding a background
- Make the program more interactive, maybe even make a puzzle or game
- See some of my other programs on my website for more ideas
Future Tutorials
There's still lots more to learn and more advanced tutorials will come when I
get the time. Some major topics include 3D sound, lighting, and loading model
files. Give me some feedback on what you need to know.
What I'd like you to do now
- Visit my website : www.VBgames.co.uk
- and if you have your own programming site please swap links with me
- Please vote for me - on www.planet-source-code.com
- Give me some feedback - go to www.planet-source-code.com
and tell me what was good and what was bad, suggestions, comments, anything.
Tell me why you voted the rating that you did.
- Hey, my request to write a book got turned down! (erm, private joke with
someone)
Credits
There are lots of sources from where my information came from. Mainly
Microsoft's DirectX SDK (as much as I hate them, DirectX rules!). Many tutorials
on www.planet-source-code.com
and www.gamedev.net , and also thanks to
Richard Hayden for his example program.
Disclaimer
This tutorial might be totally wrong so it's not my fault if something goes
wrong. You've been warned (right at the end, after you messed up your PC)!
| | Other 17 submission(s) by this author
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | 1/7/2001 3:16:12 PM:Simon Price Whoa, that took me a long time, so
please give me some votes, feedback,
suggestions for more tutorials, bugs
(and the bug I did on purspose if you
like), and can someone find or make a
HTML formatter that will color Visual
Basic code automatically? That would be
really useful, I would have clue on
making one myself. Thanks in advance!
| 1/7/2001 3:18:30 PM:Simon Price Oh, and I'll try to answer any
questions you post here too.
| 1/7/2001 3:39:21 PM:Sir Killalot Can u make this tutorial downloadable,
ie a word document format.
| 1/7/2001 4:20:31 PM:Aaron Actually, API stands for Application
Programming Interface.
| 1/7/2001 5:04:27 PM:Niall Mooney Very nice, good work simon. Good
tutorials are few & far between
| 1/7/2001 5:20:51 PM:Chris The time you took shows well.
| 1/7/2001 6:06:18 PM:Simon Price Thanks for votes and feedback. Aaron,
thanks for telling me about API. Sir
Killalot, I'll get it done within 24
hours, although I've no idea why you'd
want it in word format. The tutorial is
in HTML format in the download link and
on my website
www.VBgames.co.uk/tutorials/dx8intro.htm
Niall, I hope we are on good terms
again now.
| 1/7/2001 10:14:31 PM:Chris Oh yeah, I forgot...I changed your
rendering device backbuffer to
vbBlack...after my vision returned.
| 1/8/2001 2:54:00 AM:Simon Price Thanks for feedback, but does anyone
have ideas on how to progress these
tutorials? Chris, the reason for the
brightly colored background was for
debugging. Sometimes if you have a
problem with lighting all the polygons
are black, and at least on a white
background you would be able to see the
problem. It drive me mad once when I
couldn't figure out why nothing was
appearing, because I had a black
background and the light wasn't working!
| 1/8/2001 11:21:30 AM:Chris Just for thought; you might want to
consider widening the foundation a
little. Create a 'static' background
such as Richard's 3DWorld example with
your animation incorporated into it. I
think that would rock.
| 1/8/2001 2:25:34 PM:Simon Price Chris, good idea, it's alot better than
the whiteness :)
What I was really
asking though, is what sort of tutorial
could I make that is DX8 based but
different from this one? I think
loading .x files is a good choice, but
I've just learnt the basics of that
myself :( Well thanks, I'll consider
that for the next one (that is, if
there is another one, I'm not sure).
| 1/9/2001 8:31:16 AM:renyi[ace] d@mn, jsut downloaded the tutorial and
i forgot i dont have directX8,
hehe
anyone can post a shortlink
here, :P
| 1/9/2001 9:17:02 AM:Derek Thanks for the work done.
Unfortunately on my laptop, running
Windows 2000 things the render didn't
work correctly. Even after changing to
D3DDEVTYPE_REF my "cube" looked more
like a mass of green bees. This may
have to do with Win2k, but I'm
suprised. Maybe I've downloaded
something that has broken my
directX?
You asked about other
VB/DirectX tutorials. you could focus
on each individual technology. For
example: DirectInput: how to detect a
joystick or keybad, digital or analog?
How bout an event for the mouse
scrollwheel that I can use in my own
control? Come to think of it, how to
creat call-backs for any directx
event.
now you can move on to sound,
music, graphics, etc.
| 1/10/2001 10:20:45 AM:Richard Hayden More EXCELLENT work from this master of
3D programming! Well down Simon, keep
it up!
P.S. When do you hope to
finish you 3D Modeller, I'm waiting in
anticipation!
| 1/10/2001 5:01:08 PM:Simon Price Derek, I've found in the past that
Win2K users are the ones that have
problems with my DX8 programs, I use
Win98. But not all Win2K users, so it's
a problem that can be fixed, but not
easily without owning 2 OS's to test on
or something.
Richard, firstly thanks,
secondly - I promised to release
something related to it this month, but
maybe not the whole thing, it's just
too bit a project to do in the time.
Yesterday I made some massive changes,
like, I changed the file format to
support .x files, and I split the
program into 2, a model editor and a
world editor. Most things work, the
most likely COA is to make a demo of
the engine it uses and put it on PSC
soon. The idea is that the .si3d files
can be loaded into the game engine in
one function call, and they contain
everything - 3D data, sound and
animation etc. Thirdly - congrats on
the code of the month, are we to expect
another version?
| 1/11/2001 10:39:10 AM:Richard Hayden Yeah I hope to produce version 3 by the
end of this month or maybe middle of
next month (I have my GCSE mock exams
at the moment!). I am also in the
process of making an identical version
in C++ just out of interest to see if
it is any faster, although I doubt
there will be much difference in speed.
| 1/12/2001 6:12:09 AM:Nathan Chere Well done again Simon. This is a top
tutorial, I'm just p!ssed off that you
didn't release it before I learnt it
all through the unfriendly SDK...
:(
Oh well, 5 globes anyway!
| 1/12/2001 2:53:16 PM:Simon Price Thanks Nathan. I had a few delays in
getting DX 8 installed. I have to
actually learn some stuff myself from
the SDK before I can write a tutorial
program :)
News on my 3D engine -
collision detection is sort of working
now!!! Expect a demo soon. I gotta
learn how to draw though.
| 1/16/2001 3:44:38 PM:Kevin Changing Vertex 5,6,7
| 1/16/2001 3:45:35 PM:Kevin Changing Vertex 5,6,7 & 8 to having
positive z values fixes the cube
rendering problem.
ie.
vertex(5)=(1,1,1,0,0,1,0,0)
instead of
(1,1,-1,0,0,1,0,0)
Thanks for the
tutorials
| 1/17/2001 10:23:38 AM:Richard Hayden Thanks for the credit & keep up the
good work!
| 1/18/2001 8:25:09 AM:Philipp Weidmann Excellent tutorial!
I really learned
something.
PS: You can find an
excellent HTML VB syntax highlighter
here on PSC. It is really a project
documentor but it´s almost the same. To
find it, just search for VBDoc.
| 1/18/2001 8:30:06 AM:Philipp Weidmann I´m sorry I cant vot for you, there is
a problem, my browser says 'server not
found'. I will vote for you later.
| 1/18/2001 8:52:27 AM:Philipp Weidmann It´s me again.
I´m afraid I told you
something wrong with the syntax
highlighter. It´s name is not VBDoc,
but 'VB to HTML' by Bryan Cairns. You
can find it at
http://www.planet-source-code.com/vb/scr
ipts/showcode.asp?txtCodeId=6643 or
search for VB to HTML
| 1/22/2001 3:01:22 PM:Simon Price Thanks Phillip, I'll check that out. If
any of you newbiew want to see the
kinda stuff you can do with DX8 once
you get more used to it, see
http://www.planet-source-code.com/xq
/ASP/txtCodeId.14587/lngWId.1/qx/vb/scri
pts/ShowCode.htm
| 1/23/2001 3:35:10 PM:SniperBoB Wow!! Who would have though that this
could be put into english! &?!#
Microsoft and it's complex SDK DOCS!!
I'm finally understanding this, to bad
a bad virus that hit my computer a
while about wont let me run the demo
:-( I'll get to it once my new
computer gets here. KEEP UP THE GREAT
WORK!
| 1/24/2001 3:04:41 PM:Simon Price Kevin, well done for being the first
person to spot the bug and post it! I
am a bit evil for putting that in there
because it's quite hard to spot.
| 1/29/2001 9:37:49 PM:MattKillc Hey, i wanted to start using DirectX in
programming, and i went out and
Downloaded DirectX7 last year, and now
i have downloaded DirectX 8, but i dont
see it in the refrences section. i went
to the Add/Remove Programs, but didnt
see DirectX 7 to delete. can someone
PLEASE tell me what i am doing wrong
(yes, i am a newbie)
| 2/11/2001 12:58:46 PM:Simon Price Sorry for the slow reply MattKillc. I'm
not sure how to install DirectX 8 the
"right" way since I only got it working
after about 10 attempts! It kept
installing DirectSound but not
DirectGraphics. But it sounds like
nothing has installed for you. Do any
other DX8 programs work?
| 2/11/2001 1:01:22 PM:Simon Price Cool! This tutorial just got code of
the month. Thanks for the votes
everyone!
| 2/11/2001 4:19:54 PM:warcrest Actually, if you're doing the partial
d'l from microsoft, you have to hunt
around and find the DX8 home user d/l
or else you won't be able to use DX8.
The dx8vb.dll doesn't come with the vb
sdk.
| 2/13/2001 8:11:20 AM:Bartus Nice tutorial, actualy the first dx8
tutorial I understand:)
However i do
have an improvement for the example in
the tutorial.
When i run the code
the background is blinking (at least in
win2k)... This is simply resolved by
adding in the form load ocde
"windowstate = 2" which will make the
form's state to max. Now the form
covers the new resolution and the
screen doesn't blink.
Your form load
would now look like this:
If Init =
False Then
' display error
message
MsgBox "Error! Could
not initialize DirectX!"
Else
Show
WindowState = 2
MainLoop
End If
Unload Me
| 2/13/2001 1:18:27 PM:Simon Price Thanks Bartus
One thing to note - by
changing the forms size, you don't
change the backbuffer size, and so you
get the same resolution but slower
frame rate because of stretchblt'ing
instead of blt'ing. The best solution
is to maximize the form before loading,
and supply the form size to the load
routine to create a backbuffer of
matching size. This makes for high
resolution and fast blt'ing.
| 2/19/2001 3:42:58 PM:Mike15@blueyonder.co.uk WOW, thats a really good tutorial, well
done and thank you. the only problem
is, my brand-new £1000 pc crashes when
u use directX :(
so i cant really test
it :(
thanks though, im gonna try
make a good game or somin one day :)
| 3/1/2001 8:22:52 PM:Ace315 whenever I run a Direct X example,
either one that I downloaded from the
PSC, or one that came with the Direct X
8 SDK... I get error messages like:
| 3/1/2001 8:25:01 PM:Ace315 hmm my message got cut off, here's the
rest:
"Exiting, Unable to initalize
3D device" (from a Direct3D example)
what does this mean??? do I have a bad
graphics card or what??? (I DO need to
upgrade my computer...) please email me
if you can help me! tc_315@hotmail.com
thanx!
| 3/4/2001 12:59:43 AM:ElectroSuicide I tried to run the sample project by
Mr. Price, and it told me
| 3/4/2001 1:01:56 AM:ElectroSuicide I tried to run the sample project by
Mr. Price, and it told me "Error! Could
not initialize DirectX". Looking
through the code, I found that in the
"Init" function, the line:
Set
DI = DX.DirectInputCreate()
gives an
error - "DX = Object variable or With
block variable not set" or something
like that. By a strange coincidence,
this is the same problem I had in the
project I created while going through
the Microsoft tutorials. Careful
examination proved that my code was
compltely identical to MS's, but even
cutting and pasting the offending code
line didn't help. I tried copying the
entire function from the Microsoft
version, and that seemed to work, but
if my knowledge of programming isn't
too rusty, this would mean that
something later in the function was
affecting this line (yes, I know this
is impossible, that's my point).
Any
help here would be much appreciated.
Thanks.
| 3/10/2001 9:42:40 PM:Matt I look forward to digging through this
tutorial. I'm new to alot of this DX
stuff. Hopefully this will help. But
your other programs, that I've looked
at, are cool, so you obviously know
something about what you do.
| 3/29/2001 2:37:12 PM:Shane for i = 1 to 1000
Thank you for
writing that tut.
next i
You
cleared up so many questions I
had,
with you help, i have been able
to build a small
| 3/29/2001 2:39:04 PM:Shane for i = 1 to 1000
Thank you for
writing that tut.
next i
You
cleared up so many questions I
had,
with you help, i have been able
to build a small "room scene" walls,
floor and ceiling, as well as made the
lookat point move with the mouse, and
the camera move with the arrow keys. My
questions now is how to get the
camera/lookat point to mmove in a
logical direction as opposed to allways
on the z axis? so I can simulate
"walking" around in my scene.
Once
again thank you for writing that tut.
It is by far the best 3d3 example i
have found thus far.
Shane
xyan_vii@yahoo.com
| 3/31/2001 12:03:42 PM:Khaos999 Hey, cool tutorial. I've never really
delved into d3d at all because of it
being quite complex, but it seems that
things have now changed with DX8. I
love the tutorial but it seems that the
source code download is missing (maybe
its me but I can't download it) and the
version that you have here is missing a
number of varibles. Thanks for the
extra chance for debugging. Just so
that you know all of the D3DMATRIXs
that you use in the mainloop have not
been declared. Looking for more of
your tutorials.
| 4/1/2001 11:20:52 PM:404 The demo download file doesn't exsist!
| 4/2/2001 4:13:27 PM:Simon Price Sorry for the download problems, I
should look at what I am deleting when
I rearrange my website! Thanks for
telling me about it, the link is now
fixed. Thanks for all the positive
feedback I've had from this tutorial,
I'll be writing more as soon as I get
the time.
| 4/4/2001 7:58:15 AM:??? I'm a beginner to this...
If you
know...I'd really like to know how to
write three triangles to the screen at
the same time.
And how to print
pictures, and shrink or enlarge the
picture on the screen. Maybe any
suggestion on how to put all this in to
a single file?
A sample-project
would be fine.
Thank you!
| 4/20/2001 5:20:56 PM:Fred I installed DX8 SDK for VB, without
uninstalling DX7 SDK.I have since
uninstalled DX7 SDK but it seems I am
missing this file - DX8VB.DLL
Any
suggestions? Besides jumping off a
cliff?
| 4/20/2001 5:24:47 PM:Fred I installed DX8 SDK without
uninstalling DX7 SDK. I have since
uninstalled DX 7 SDK, but it seems I am
missing DX8VB.DLL. Any suggestions,
besides jumping off a cliff?
| 5/7/2001 2:07:01 PM:Tomas Banovec I would like to know, what means
variables nx, ny, nz, nz, tu, tv in
MakeVertex and how to use them.
Thanx.
PS: This tutorial is great
Simon!!!!
| 5/7/2001 5:38:59 PM:Simon Price nx, ny & nz is a 3D vector which
represents the direction in which the
vertex is facing, often called the
normal. this has several uses, but the
main one is so that dx knows how to
shade the triangles which lighting
effects are applied. tu & tv is a 2D
vector which reprsents the coords of a
texture map. (0,0) is the top left of
the texture, (1,1) is the bottom right.
| 5/20/2001 10:23:38 AM:tony Help, it won't run on my computer. I
have sdk and the runtime developer.
Other 3d programs I built work on this
computer. I don't understand
| 6/13/2001 3:57:18 PM:Codin Genius Well Si, I have been doing VB for 3
years now and I am pretty advanced but
just three days ago thought that I
would try and learn DX. So, I
downloaded a few DX progs from the
planet, opened them, played them,
thought WOW COOL, looked at the source,
then nearly threw myself off a cliff.
Then yestarday I discovered your DX 7.0
tutorial, which was very good, but I
still had difficulty. Just 20 minutes
ago, I downloaded DX 8.0 (I like to
keep wiv the times) and this is the
first place I came. Could you possibly
write a very simplified, very basic DX
8.0 tutorial. Ive went on a bit too
long so Im just gonna say...That I bow
before your great tutorials and I award
you 1 star,(Syke ;)) Only j/k, 5 stars.
Oh and HELP ME SOMEBODY, DX IS HARD ;)
| 6/13/2001 9:07:42 PM:Simon Price Codin genious, thanx v much 4 the
feedback. After my exams I will _try_
to write some more tutorials, but no
promises OK. I haven't done any codin 4
ages due 2 skool stuff.
| 6/19/2001 10:27:34 PM:Robbie Lude I am really new to DirectX and the API
but you make it all really easy! Even a
13 year old like me can understand all
this!
| 7/2/2001 7:02:40 PM:Joset I just cought your tut, but it's great,
one problem tought, i'm getting a ByRef
argument doesn't match in this line:
D3DXMatrixLookAtLH matView,
MakeVector(CameraPos.x, CameraPos.y,
CameraPos.z), MakeVector(0, 0, 0),
MakeVector(0, 1, 0).
does any1 know
why this happens?
| 7/12/2001 6:30:29 PM:Ace315 ok... I got this to work before.. I
came back and I tried it, tweaking
settings in an attempt to get it to
work again (I remember EXACTLY what I
did) but now it doesn't work! in the
Init function, there is not "On error
goto initfailed" and on this one line
(Set D3Ddevice =
D3D.CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
D3Dpp)) it just exits returning false!!
this is the EXTREAMLY fusturating I
want to throw my computer out the
window (lol.. not really) can someone
please email me (tc_315@hotmail.com)
and help me out?
| 7/18/2001 4:20:33 PM:Codin Genius I usually find learning new VB stuff
easy nd master them in about a week,
Ive been at directx for 3 now and all I
can do is create the objects, sorry,
not that advanced yet, I meant object.
So urm, thanx Robbie Lude. That just
makes me feel a whole lot better. I
don't find it easy at all. Oh well.
| 7/20/2001 9:47:21 AM:DeMante Wow! Just was I was looking for, thanks!
| 7/28/2001 8:06:18 AM:Yochai Very good tutorial!!! (But I didn't
found out the bug... [English is not my
main language as well] can someone give
me a clue?)
| 8/12/2001 10:45:49 AM:DJ Evil Fairly ok, but has many poor
programming habits like putting all the
code in the form instead of breaking it
out into a module, functions that work
on variables they were not passed,
etc.
Not bad for someone who is not
a professional programmer, but I fear
the bad code practices will be learned
by a newbie and I'll have to come
behind them someday and fix all of
their poorly done code.
| 8/16/2001 11:24:53 PM:danraf Thank you Simon. I too have been
studying the DX in the C++ SKU on and
off for several months. Even though I
think I am there it's nice to see it
reproduced in a language I am much more
comfortable in. As far as a next
tutorial... how about considering
adding some notes on networking (ie.
multiplayer). I know DX has equally
powerfull networking abilities. Keep
up the good work and don't pay any
attention to BJ Evil.
| 8/21/2001 6:20:54 PM:Monstrosity_ OK, there some Sh!t i didnt
understand(cause im dum), and i was
hoping you could help, the SDK didnt
explain this either, the present
parameters, what does each selection
do? eg: .AutoDepthStencilFormat = ???
what does it all mean? plz help
Monstrosity_@hotmial.com is you wanna
help me by email :P thx in advance
| 9/6/2001 3:21:50 PM:hollifm wondering if you have any exp with
directplay? I am wanting to create a
lobby program to launce directplay
programs but am having trouble. in
particular I am looking at age of
empires II.
THanks,
Michael
| 9/7/2001 1:40:11 AM:Gold Dragon This is in responce to DJ Evil...Who is
to say what is or is-not POOR
programming practice...I have been
using VB since version 3 for
win3.1....
I taught myself...before
the Internet was mainstream...What does
it take to be a PROFESSIONAL
programmer....a large salery.....some
of you so called professionals make me
laugh....basically most of the internet
world out there today are people who
taught themselves everything...no
college...no internet web sites....just
me, my computer, a book or 2, and some
common sense....GOOD coding practice
comes with practice and
time....eventually even without you
mocking comments...this kid will get it
and probably be darn good at
it....Thank you...and BTW...Good job:)
| 9/7/2001 9:06:51 AM:Simon Price Thankyou for all the feedback (both
positive and negative). DJEvil, yes I
agree with your comment that this code
is designed with bad practices.
However, it is designed to demonstrate
DirectX, not good code design such as
minimizing scope. I have laid it out so
it's easy to understand.
| 9/10/2001 1:42:11 PM:computercoder I agree with Simon here...
I think
his point is loud and clear... WHO
CARES if the coding practices are not
in place.. he's not teaching a class on
CODING PRACTICES! DX8 is the issue
here, and he achieved to his abilities
with something to learn DX8 from... I
have seen his work plenty.
SIMON:
GREAT work (as usual)!!!
5 SPINNING
GLOBES! (and flashing if you'd like!)
| 9/15/2001 2:23:33 AM:Adam Didn't help me
much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!
| 9/20/2001 2:39:23 PM:Jonathan Fors Gotta problem there. At the
SET
Sound=DS.loudbufferfromfile..... line I
always get an "Invalid Procedure Call
Or Argument" error. This always happens
with the files my program has copied to
the subdirectory with the FileCopy
Command, but not to the other wav
files. Can someone pls help me?
| 9/23/2001 10:46:52 PM:Mind Beast Simon, could you explain how to
successfully and easily do 2d images
larger then 256x256 in dx 8?
Do i just
put the image as a texture on a
polygon??
Thanks!
Mind Beast
| 11/8/2001 12:43:01 PM:Shep Help!! Whenever I run the program, it
crashes at the line:
Set D3Ddevice
= D3D.CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
D3Dpp)
I've tried changing HAL to
REF but to no avail. Does anyone have
any suggestions as to why it crashes
and how to fix it?
| 11/9/2001 3:54:47 PM:Coding genius Hi, I am trying to create a DirectInput
wrapper class. I am using evsnts to
notify user about Keyboard and mouse
changes. I am using the CreateEvent
method to do so. I have 2 devices, one
called Keyboard, and the other Mouse. I
can create an event for either one of
them, but when I try and create an
event for the sedcond one I get a
permission denied
error:
Mouse.SetCommonDataFormat
DIFORMAT_MOUSE
Mouse.SetCooperativeLeve
l hWnd, DISCL_BACKGROUND Or
DISCL_NONEXCLUSIVE
hMouseEvent =
DX.CreateEvent(Frm)
Keyboard.SetEventNo
tification
hMouseEvent
Mouse.Acquire
Keyboard
.SetCommonDataFormat
DIFORMAT_KEYBOARD
Keyboard.SetCooperati
veLevel hWnd, DISCL_BACKGROUND Or
DISCL_NONEXCLUSIVE
hKeyEvent =
DX.CreateEvent(Frm)
Keyboard.SetEventNo
tification
hKeyEvent
Keyboard.Acquire
Thats
the code. I hope you can please e mail
me.
| 11/9/2001 3:55:54 PM:Coding Genius Sorry, the format got screwed up. I
hope you can still read the code. :)
Thanks in advance.
| 11/21/2001 5:28:36 PM:deVoid ok, this is wierd : all I get when I
run this is a full screen flickering
yellow and purple. When I switch HAL to
REF it stays black. The MS SDK tutes
run fine. Any clues anyone? I've been
trying to fix this for HOURS.
| 11/22/2001 7:25:23 AM:deVoid Found the solution! The tutorial will
NOT run correctly (on my machine
anyway) with the DEBUG version of DX8
selected :-P
on a separate note, why
does the bottom side (opposite the open
side) seem to flicker?
| 11/22/2001 10:00:52 AM:Don B. Great bit of code. I'm using VB5 and
had to tweak it slightly to make it
run. Your code shows how programmers
can grab DirectX by the ears and make
it perform tasks that otherwise would
have to be reproduced each and every
time he/she wanted to spin a cube. Get
it, learn it, know it, and use it!
Great job! 5 rapidly rotating globes!!!
| 12/9/2001 12:47:42 AM:Mark Lu API = Application Program Interface.
Not advanced. But hey! Most programmer
that would read this should already
know what api means. People who can't
do api won't understand this. When I
started out a a newbie, I came here.
Most of the code uses api. I don't
understand it that time, but now, I can
memorize how to do api at the top of my
head.
| 12/18/2001 2:56:22 PM:Mark Withers Hello. Is it posible that you could
write some code for me. Im after some
Direct X 7 code which would play sounds
from a string. I've managed to do it
with sndplaysound api using buffers but
not direct X. I though you could
help...please contact me.
| 12/31/2001 12:27:51 AM:trudoggie Um, the download link for the source
code does NOT work. And I copied the
source code into vb, and the program
doesn't work. It gives me this compile
error: User-defined types and
fixed-length strings not allowed as the
type of a public member of an object
module; private object modules not
allowed as the type of a public member
of a public object module. and it
highlights this line: Function
MakeVector(x As Single, y As Single, z
As Single) As D3DVECTOR. Help
PLEASE!!!!!
| 12/31/2001 1:44:47 AM:trudoggie Could you please make a tutorial that
an absolute beginner could understand?
It would be good for your website. I
haven't found even ONE tutorial
ANYWHERE (that includes yours, no
offense intended) that explains
anything well enough for the absolute
beginner. Plus it would be very useful
if the sample program would work
because then I could SEE what's
happening.
| 1/16/2002 11:50:00 AM:Harris The link to the source is down.. hook a
brother up!
| 1/30/2002 11:10:42 AM:Joe I recommend Copying and pasting the
code from this page into a new project
rather then downloading the premade
source, it will help you learn better.
It helped me anyway. and it WORKED.
Perfectly.
| 2/18/2002 11:04:20 AM:Emil I tried to copy, and paste the source
code, just because I wanted to see how
the program works. I had a lot of
errors when I tried to compile it. The
link to "sound program" you say I shall
download doesn't work, and so does
skeletonX. I jus wanted to tell ya...
| 3/18/2002 7:17:32 PM:Billy I copy and pasted the code also to see
how it worked and i got many errors one
was "Variable not defined"
and during
debugging it highlighted "matView"
somewhere in the code.
| 3/28/2002 6:24:31 PM:Andrew Murphy example link isnt correct
| 3/29/2002 5:40:53 AM:hari_seldon bad link
| 3/30/2002 4:43:50 AM:~ChRiS~ Great tutorial, but program stops
running and says "Variable not
defined"
and highlights the word
MatView on the line
D3DXMatrixLookAtLH matView,
MakeVector(CameraPos.x, CameraPos.y,
CameraPos.z), MakeVector(0, 0, 0),
MakeVector(0, 1, 0).
Pleez help
| 3/30/2002 1:05:51 PM:Spike VERY GOOD!!!!!!!
Your Tutorial is
REALLY GOOD!
But i have one question
to make you?
How can i make a
distortion or chorus or same FX to a
wav file?
Please Help
me!
Spike84@inwind.it
| 4/2/2002 3:47:58 PM:Rob Unable to download sample because of
broken link.
| 4/3/2002 9:54:05 AM:Sergei Where can I get the DirectX Type
Libraries? Please post the straight
link.
| 4/6/2002 12:31:09 AM:Sergei Already got the Type Libraries, but now
there's another problem. The game just
crashes when it gets to the DrawCube
Sub. Even the On Error doesn't help.
Can you tell me what's wrong?
| 4/19/2002 2:00:43 AM:aimtron Honestly I had trouble the whole way,
but thats me. I still don't understand
what I'm doing in this tutorial. I
understand that you need to set your
DirectX as an Object to use throughout
and about the Inputs, but I don't
understand all the setting of buffers
and stuff. I was more or less hoping
for something that would explain it in
absolute plain english. Explain it as
though you were explaining it to a 5
year old. I did however get it to INIT
(couldn't see anything though even with
REF instead of HAL). I'm sure its a
great tutorial minus the fact that the
source isn't downloadable at your site
anymore. If anyone could break this
down even further and tell me why each
thing is needed. I was under the
impression that it would be simplier.
SOmething like X1, Y1, Z1, X2, Y2, Z2
style programming. Plotting points on a
graph sort of. I dunno but if anyone
can help feel free to email me at
vti_cgi_bin@hotmail.com
| 4/28/2002 1:18:43 PM:M. Jahedbozorgan Would you please upload this in a zip
file?
| 5/11/2002 8:38:57 PM:Alaina Very good detail! Thanks
| 5/19/2002 6:42:18 PM:Brian Link nice
| 6/4/2002 6:26:00 AM:Nithin Pradeep Hi, is the HTML tutorial the one in
this page? or is there another one?
Bothe the links(HTML & ZIP) to VBGames
are not working
| 6/11/2002 3:14:21 PM:Hugo Sedryt The problem with matview is that the
line that defines it as a variable is a
comment line when you copy it to the
code.
| 8/2/2002 12:39:10 AM:Daniel Lewis Si
Just wanted to say this is the
best stuff I have ever seen, your
parents must be well proud of u, I
would be! Still at school!
Amazing.
Quick note for noobs having
probs (like I did) u must have a bmp
file and a wav file in the app
directory. See prog listing for the
right names to give them. Also read
wot Hugo Sedry wrote above.
Thanks
for your time Si u really r the
man.
Dan 'not so much' the man
| 8/2/2002 12:42:15 AM:Daniel Lewis Sorry Si. 1 q for u.
Is there a
tool similar to the API text viewer but
for DirectX? Also any other good tuts
u recommend? Is it just practice
practice practice with DirectX? Should
I even read that **** that MS write in
the SDK?
So many questions so few
answers. Well I gotta start somewhere
I gotta college project to get in
within 10 months.
Cheers again Si, I
cant thank you enough
Dan
| 8/15/2002 4:49:23 AM:mondspeer And again ... another excelent work.
Thanks for sharing.
Do You know
anything about direct-transform for vb?
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |