PDA

Click to See Complete Forum and Search --> : Simple DX8 - Why won't it work?


rEaL iGoR
May 10th, 2002, 05:49 AM
I am...pretty new to DX in VB. But I'm trying. So I would like anyone to help me figuring out why this won't work.. I'm trying to render a triangle in Full-Screen mode..:P

Dim DX As New DirectX8
Dim D3D As Direct3D8
Dim D3DDevice As Direct3DDevice8

Private Const MyFVF = D3DFVF_XYZ Or D3DFVF_DIFFUSE
Private Type VERTEX
X As Single
Y As Single
Z As Single
Color As Long
End Type

Dim Trekant(0 To 2) As VERTEX
Dim bStop As Boolean

Private Sub Form_Click()
bStop = True
End Sub

Private Sub Form_Load()
Set DX = New DirectX8
Set D3D = DX.Direct3DCreate

Call Initialize
Call CreateGeometry

Do Until bStop = True
Render
DoEvents
Loop

Unload Me
End
End Sub

Sub Initialize()

Dim DisplayMode As D3DDISPLAYMODE
Dim D3DPP As D3DPRESENT_PARAMETERS

DisplayMode.Format = D3DFMT_R5G6B5
DisplayMode.Width = 640
DisplayMode.Height = 480

D3DPP.SwapEffect = D3DSWAPEFFECT_FLIP
D3DPP.BackBufferCount = 1
D3DPP.BackBufferWidth = 640
D3DPP.BackBufferHeight = 480
D3DPP.BackBufferFormat = DisplayMode.Format
D3DPP.hDeviceWindow = Form1.hWnd

Set D3DDevice = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, _
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DPP)

D3DDevice.SetRenderState D3DRS_LIGHTING Or D3DRS_CULLMODE, False Or D3DCULL_NONE
D3DDevice.SetVertexShader MyFVF
End Sub

Sub Render()
D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, 0, 1#, 0

D3DDevice.BeginScene

D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLELIST, 1, Trekant(0), Len(Trekant(0))

D3DDevice.EndScene
D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub
Sub CreateGeometry()
Trekant(0) = CrtVertex(10, 10, 0, RGB(255, 0, 0))
Trekant(1) = CrtVertex(10, 200, 0, RGB(255, 0, 0))
Trekant(2) = CrtVertex(200, 10, 0, RGB(255, 0, 0))
End Sub

Private Function CrtVertex(X As Single, Y As Single, Z As Single, Color As Long) As VERTEX
CrtVertex.X = X
CrtVertex.Y = Y
CrtVertex.Z = Z
CrtVertex.Color = Color
End Function


In advance, thank you
-Lars Espen Rosness

Sastraxi
May 10th, 2002, 07:37 AM
Try changing R5G6B5 to R8G8B8, and make sure your 3D card supports display acceleration.

rEaL iGoR
May 10th, 2002, 10:08 AM
Actually, It did not work anyways
32bit resolution : Check
640x480: Check

Option Explicit

Dim DX As New DirectX8
Dim D3D As Direct3D8
Dim D3DDev As Direct3DDevice8

Const MyFVF = D3DFVF_XYZ Or D3DFVF_DIFFUSE

Private Type VERTEX
X As Single
Y As Single
Z As Single
Color As Long
End Type

Dim Triangle(0 To 2) As VERTEX

Private Sub Form_Load()
Me.Show
InitProject
CreateGeom

Do
RenderProject
DoEvents
Loop

Set DX = Nothing
Set D3D = Nothing
Set D3DDev = Nothing
End Sub


Sub InitProject()

Set DX = New DirectX8
Set D3D = DX.Direct3DCreate

Dim DisplayMode As D3DDISPLAYMODE
DisplayMode.Format = D3DFMT_X8R8G8B8
DisplayMode.Width = 640
DisplayMode.Height = 480

D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DisplayMode

Dim D3PP As D3DPRESENT_PARAMETERS
D3PP.Windowed = 1
D3PP.BackBufferCount = 1
D3PP.BackBufferFormat = DisplayMode.Format
D3PP.BackBufferWidth = 640
D3PP.BackBufferHeight = 480
D3PP.SwapEffect = D3DSWAPEFFECT_FLIP
D3PP.hDeviceWindow = Form1.hWnd
Set D3DDev = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, _
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3PP)

D3DDev.SetVertexShader MyFVF
D3DDev.SetRenderState D3DRS_LIGHTING, False
End Sub

Sub CreateGeom()
Triangle(0).X = 0
Triangle(0).X = 0
Triangle(0).X = 0
Triangle(0).Color = vbRed

Triangle(1).X = 0
Triangle(1).Y = 200
Triangle(1).Z = 0
Triangle(1).Color = vbRed

Triangle(2).X = 200
Triangle(2).X = 200
Triangle(2).X = 0
Triangle(2).Color = vbRed
End Sub


Sub RenderProject()
D3DDev.Clear 0, ByVal 0, D3DCLEAR_TARGET, 0, 1#, 0

D3DDev.BeginScene

D3DDev.DrawPrimitiveUP D3DPT_TRIANGLELIST, 1, Triangle(0), Len(Triangle(0))
D3DDev.EndScene

D3DDev.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub


My problem is: Though my window turns black and clearly refreshes(I can see my mouse flickers), The triangle I supposably should be viewing is NOT there.

Zaei
May 10th, 2002, 07:25 PM
Try turning backface culling off:

D3DDevice.SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)

If something doesnt show up the way you expect, turn culling off. If it shows up, you know that you defined your vertices in the wrong order. Simply flop the last 2 vertex definition's orders.

As to the error you were getting; Create a new Module, and define your VECTOR type there, as Public. It should work then.

Z.

rEaL iGoR
May 11th, 2002, 11:42 AM
VECTOR type? You mean VERTEX type?

rEaL iGoR
May 11th, 2002, 12:01 PM
GOD DAMN no, it does'nt show. You don't find anything possible mistakes I've made the should prevent it from showing up?

Zaei
May 11th, 2002, 03:47 PM
Wait. I just ran your code, and I know what is wrong =).

You dont have either a View or Projection matrix created or set. You need to do a few more things. Go and take a look in the SDK docs for Tutorial 3. Take a look though the code. You mostly want to look at the sections that deal with the View, Projection, and World matrices. In a nutshell, the Projection Matrix is what transforms a 3D point into a 2D one. The View Matrix defines your camera, and the World matrix defines world transformations (for instance, you want to move and rotate a mesh. You use the world matrix for those tasks).

Z.

rEaL iGoR
May 11th, 2002, 03:52 PM
Yeah...but I don't believe Jack Hoxley uses it at 3rd Tutorial at DX4VB..I downloaded his code, and it works :P Cannot understand why cause our code is practicly identical...

Zaei
May 11th, 2002, 09:59 PM
To make it show in 2D, Add a Single to your vertex type, AFTER the x, y, and z values, but before everything else, called RHW. Always set it to 1.0 =). Change the first constant in your FVF, to D3DFVF_XYZRHW, and change our vertex definitions so you set the RHW to 1.0. Try that.

Z.

rEaL iGoR
May 12th, 2002, 09:18 AM
aha. thank you for that. very much. I've come to realize that learning DX in VB is pretty...tiresome. Why? Well, evertime you do something, even COPY that god damn code, it just won't show! I'm getting pretty frustrated here. Because. Now, I attempt a simple textureloading. And I cannot see *** is wrong this time. It's basicly the same code, I just added a few things..

Dim DX As DirectX8
'Dimensioning a new object, how exciting!
Dim D3DX As D3DX8
Dim D3D As Direct3D8
Dim D3DDevice As Direct3DDevice8

Const MyFVF = D3DFVF_XYZRHW Or D3DFVF_DIFFUSE Or D3DFVF_TEX1 Or D3DFVF_SPECULAR
Private Type VERTEX
X As Single
Y As Single
Z As Single
RHW As Single
Color As Long
Specular As Long
TU As Single
TV As Single
End Type

'Dimentioning variables...INCLUDING my new texturevariable!
Dim MTexture As Direct3DTexture8
Dim Square(0 To 3) As VERTEX
Dim Square2(0 To 3) As VERTEX
Dim bStop As Boolean

Private Sub Form_Click()
bStop = True
End Sub

Private Sub Form_Load()
Set DX = New DirectX8
'Here is a whole new object. Guess I need it...
Set D3DX = New D3DX8
Set D3D = DX.Direct3DCreate

Call Initialize
Call CreateGeometry

Do Until bStop = True
Render
DoEvents
Loop

Unload Me
End
End Sub

Sub Initialize()

Dim DisplayMode As D3DDISPLAYMODE
Dim D3DPP As D3DPRESENT_PARAMETERS

DisplayMode.Format = D3DFMT_R5G6B5
DisplayMode.Width = 640
DisplayMode.Height = 480

D3DPP.SwapEffect = D3DSWAPEFFECT_FLIP
D3DPP.BackBufferCount = 1
D3DPP.BackBufferFormat = DisplayMode.Format
D3DPP.BackBufferWidth = 640
D3DPP.BackBufferHeight = 480
D3DPP.hDeviceWindow = Form1.hWnd

Set D3DDevice = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, _
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DPP)

D3DDevice.SetRenderState D3DRS_LIGHTING, False
D3DDevice.SetVertexShader MyFVF

'Here is some added code to load the texture
Set MTexture = D3DX.CreateTextureFromFile(D3DDevice, App.Path & "\ExampleTexture.bmp")
End Sub

Sub Render()
D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, 0, 1#, 0

D3DDevice.BeginScene

'Here is some added code to use the texture
D3DDevice.SetTexture 0, MTexture
D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Square(0), Len(Square(0))
D3DDevice.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Square2(0), Len(Square2(0))

D3DDevice.EndScene
D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0
End Sub

Sub CreateGeometry()
'My nice square that shows up, but without textures
Square(0) = CrtVertex(10, 10, 0, 1, RGB(255, 255, 255), 0, 0, 0)
Square(1) = CrtVertex(210, 10, 0, 1, RGB(255, 255, 255), 0, 1, 0)
Square(2) = CrtVertex(10, 210, 0, 1, RGB(255, 255, 255), 0, 0, 1)
Square(3) = CrtVertex(210, 210, 0, 1, RGB(255, 255, 255), 0, 1, 1)
'Here is another square. It's there, but it's not textured..
Square2(0) = CrtVertex(210, 10, 0, 1, RGB(255, 255, 255), 0, 0, 0)
Square2(1) = CrtVertex(420, 10, 0, 1, RGB(255, 0, 0), 0, 1, 0)
Square2(2) = CrtVertex(210, 210, 0, 1, RGB(255, 0, 255), 0, 0, 1)
Square2(3) = CrtVertex(420, 210, 0, 1, RGB(0, 255, 255), 0, 1, 1)
End Sub

Private Function CrtVertex(X As Single, Y As Single, Z As Single, RHW As Single, Color As Long, _
Specular As Long, TU As Single, TV As Single) As VERTEX
CrtVertex.X = X
CrtVertex.Y = Y
CrtVertex.Z = Z
CrtVertex.Color = Color
CrtVertex.Specular = Specular
CrtVertex.TU = TU
CrtVertex.TV = TV
End Function

Zaei
May 12th, 2002, 11:06 AM
Remove the Specular component of your variable type, the reference to it in your FVF declaration, and any references to it in your code. it should work then.

Z.

rEaL iGoR
May 12th, 2002, 01:00 PM
Nope. It does'nt..The texture still won't show...anything else?

Zaei
May 12th, 2002, 04:06 PM
I have had this problem before. If you go and change the clear color to something like &H000000ff, you will see that your squares are still drawing, but the texture is being applied incorrectly. I dont remember how I fixed it, but I will see if I can find it.

Z.

Zaei
May 12th, 2002, 04:11 PM
Ok, it seems to be that the D3DX functions are the problem. It probably doesnt like your texture format. Try this. Open the DirectX Texture tool (it comes with the SDK, its in the Utilities folder of your start menu), and convert your textuer to .dds format. It should work.

Z.

rEaL iGoR
May 13th, 2002, 09:08 AM
Are you sure about that? Because, Jack Hoxley uses the exact same format...I've copied his code. Well, I've written so it kind of matches his. Then, I've cut down on his code so that it's exactly the same as mine. And HIS code works :P

Zaei
May 14th, 2002, 08:11 AM
Try using the same image that he uses.

This is why I wrote my own texture format for TOW =).

Z.

rEaL iGoR
May 18th, 2002, 06:27 PM
Well...I am using a copy of his picture

/\/\isanThr0p
May 18th, 2002, 08:26 PM
I had this problem before and what I did was change the textureload command to the Extended function... that worked for me... give it a try if you haven't yet

rEaL iGoR
May 19th, 2002, 03:07 PM
Hell! I'm soon-to-giveup this! It still won't work

Zaei
May 19th, 2002, 08:26 PM
Eh, I know exactly how it feels. Try changing the Pool value to something else (from D3DPOOL_DEFAULT to _MANAGED or _SYSTEMMEM) (I think there is a pool value needed in the D3DX load texture call) (ack, my brain is degrading!!!). You might want to try copying the entire source from Jack's example, then strip it down until it doesnt work, and see whats going wrong.

Z.