Sign in to follow this  
LinkOfTime

Hi again, I have got another newbie problem (easier this time, I hope)

Recommended Posts

LinkOfTime    128
Hi all This time I have managed to draw a square in DX8 but I am having some hard time coloring it in different colors. It always stays red... Well, here is the code, I hope someone can tell me what my mistake is: Thanks.
Dim DX As DirectX8
Dim D3D As Direct3D8
Dim D3DD As Direct3DDevice8
Dim D3DDP As D3DPRESENT_PARAMETERS
Dim D3DX As D3DX8
Dim Texture As Direct3DTexture8
Dim Vertices(·po··po·3·pc··pc·) As VertexType
Private Type VertexType
   X As Single
   Y As Single
   Z As Single
   Color As Long
   rhw As Single
   specular As Long
   tu As Single
   tv As Single
End Type
Const FVF = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR
Dim b As Boolean

Private Sub Load_DX()
   Set DX = New DirectX8
   Set D3D = DX.Direct3DCreate
   Set D3DX = New D3DX8
   D3DDP.SwapEffect = D3DSWAPEFFECT_FLIP
   D3DDP.BackBufferCount = ·po··po·1·pc··pc·
   D3DDP.BackBufferFormat = D3DFMT_R5G6B5
   D3DDP.BackBufferWidth = ·po··po·800·pc··pc·
   D3DDP.BackBufferHeight = ·po··po·600·pc··pc·
   Set D3DD = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DDP)
   'Set Texture = D3DX.CreateTextureFromFile(D3DD, App.Path & "\pic.bmp")
   D3DD.SetRenderState D3DRS_LIGHTING, False
   D3DD.SetVertexShader FVF
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
End
End Sub

Private Sub Reset_DX()
   Vertices(·po··po·0·pc··pc·) = Load_Vertex(·po··po·100·pc··pc·, ·po··po·100·pc··pc·, RGB(·po··po·255·pc··pc·, ·po··po·0·pc··pc·, ·po··po·255·pc··pc·))
   Vertices(·po··po·1·pc··pc·) = Load_Vertex(·po··po·200·pc··pc·, ·po··po·100·pc··pc·, RGB(·po··po·255·pc··pc·, ·po··po·0·pc··pc·, ·po··po·0·pc··pc·))
   Vertices(·po··po·2·pc··pc·) = Load_Vertex(·po··po·200·pc··pc·, ·po··po·200·pc··pc·, RGB(·po··po·0·pc··pc·, ·po··po·0·pc··pc·, ·po··po·255·pc··pc·))
   Vertices(·po··po·3·pc··pc·) = Load_Vertex(·po··po·100·pc··pc·, ·po··po·200·pc··pc·, RGB(·po··po·255·pc··pc·, ·po··po·0·pc··pc·, ·po··po·0·pc··pc·))
End Sub

Private Function Load_Vertex(X As Single, Y As Single, Color As Long) As VertexType
   Load_Vertex.X = X
   Load_Vertex.Y = Y
   Load_Vertex.Z = ·po··po·0·pc··pc·
   Load_Vertex.rhw = ·po··po·1·pc··pc·
   Load_Vertex.Color = Color
   Load_Vertex.specular = ·po··po·0·pc··pc·
   Load_Vertex.tu = ·po··po·0·pc··pc·
   Load_Vertex.tv = ·po··po·0·pc··pc·
End Function

Private Sub Play_DX()
   D3DD.Clear ·po··po·0·pc··pc·, ByVal ·po··po·0·pc··pc·, D3DCLEAR_TARGET, RGB(·po··po·255·pc··pc·, ·po··po·0·pc··pc·, ·po··po·0·pc··pc·), ·po··po·1·pc··pc·, ·po··po·0·pc··pc·
   D3DD.BeginScene
      D3DD.DrawPrimitiveUP D3DPT_TRIANGLEFAN, ·po··po·2·pc··pc·, Vertices(·po··po·0·pc··pc·), Len(Vertices(·po··po·0·pc··pc·))
   D3DD.EndScene
   D3DD.Present ByVal ·po··po·0·pc··pc·, ByVal ·po··po·0·pc··pc·, ·po··po·0·pc··pc·, ByVal ·po··po·0·pc··pc·
End Sub

Private Sub Form_Load()
   b = False
   Load_DX
   Reset_DX
   While b = False
      Play_DX
      DoEvents
   Wend
End Sub

Private Sub Timer1_Timer()
   b = True
End Sub

Share this post


Link to post
Share on other sites
Agony    3452
Do you actually see the rectangle or square itself, or is the whole screen or window actually red? Because you clear the render target to red, so the whole window should be red. Then the rectangle should draw on top of the red background, if all is working well. Try changing your D3DD.Clear color to something other than red; if it changes the results, then you know that you're not actually seeing your rectangle, just the background.

Another thing, order of items in VertexType matters, and you appear to have a few things out of order. I believe it should be more like this:
Private Type VertexType
X As Single
Y As Single
Z As Single
rhw As Single
tu As Single
tv As Single
Color As Long
specular As Long
End Type
Otherwise, DirectX will be trying to read your Color value as if it were rhw, etc.

And you might need to set the texture stage states. I don't know what they default to, but it could be useful to do so. I don't use VB for DX, but I'm guessing it'd look like this:
D3DD.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
D3DD.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
D3DD.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
D3DD.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
D3DD.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
D3DD.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
D3DD.SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
D3DD.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE );
D3DD.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

Share this post


Link to post
Share on other sites
LinkOfTime    128
Hi again
Thanks for the help. I am going to be a bit more specific with my explanation. Although I format the background to the color of red, it appears all blue. The square (which I can see in the correct coordinates) appears in a red color. Now, I tried to change the order of the VertexType just like you said and the square now appears in black.
Well, I am beginning to understand that the problem comes from the order of the VertexType declaration, but if any one finds how to fix the problem, please send me a response.

P.S: Here is the code as it looks now, after the changes:
Dim DX As DirectX8
Dim D3D As Direct3D8
Dim D3DD As Direct3DDevice8
Dim D3DDP As D3DPRESENT_PARAMETERS
Dim D3DX As D3DX8
Dim Texture As Direct3DTexture8
Dim Vertices(·po·3·pc·) As VertexType
Private Type VertexType
X As Single
Y As Single
Z As Single
rhw As Single
tu As Single
tv As Single
Color As Long
specular As Long
End Type
Const FVF = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR
Dim b As Boolean

Private Sub Load_DX()
Set DX = New DirectX8
Set D3D = DX.Direct3DCreate
Set D3DX = New D3DX8
D3DDP.SwapEffect = D3DSWAPEFFECT_FLIP
D3DDP.BackBufferCount = ·po·1·pc·
D3DDP.BackBufferFormat = D3DFMT_R5G6B5
D3DDP.BackBufferWidth = ·po·800·pc·
D3DDP.BackBufferHeight = ·po·600·pc·
Set D3DD = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DDP)
'Set Texture = D3DX.CreateTextureFromFile(D3DD, App.Path & "\pic.bmp")
D3DD.SetRenderState D3DRS_LIGHTING, False
D3DD.SetVertexShader FVF
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
End
End Sub

Private Sub Reset_DX()
Vertices(·po·0·pc·) = Load_Vertex(·po·100·pc·, ·po·100·pc·, RGB(·po·255·pc·, ·po·0·pc·, ·po·255·pc·))
Vertices(·po·1·pc·) = Load_Vertex(·po·200·pc·, ·po·100·pc·, RGB(·po·255·pc·, ·po·0·pc·, ·po·0·pc·))
Vertices(·po·2·pc·) = Load_Vertex(·po·200·pc·, ·po·200·pc·, RGB(·po·0·pc·, ·po·0·pc·, ·po·255·pc·))
Vertices(·po·3·pc·) = Load_Vertex(·po·100·pc·, ·po·200·pc·, RGB(·po·255·pc·, ·po·0·pc·, ·po·0·pc·))
End Sub

Private Function Load_Vertex(X As Single, Y As Single, Color As Long) As VertexType
Load_Vertex.X = X
Load_Vertex.Y = Y
Load_Vertex.Z = ·po·0·pc·
Load_Vertex.rhw = ·po·1·pc·
Load_Vertex.Color = Color
Load_Vertex.specular = ·po·0·pc·
Load_Vertex.tu = ·po·0·pc·
Load_Vertex.tv = ·po·0·pc·
End Function

Private Sub Play_DX()
D3DD.Clear ·po·0·pc·, ByVal ·po·0·pc·, D3DCLEAR_TARGET, RGB(·po·255·pc·, ·po·0·pc·, ·po·0·pc·), ·po·1·pc·, ·po·0·pc·
D3DD.BeginScene
D3DD.DrawPrimitiveUP D3DPT_TRIANGLEFAN, ·po·2·pc·, Vertices(·po·0·pc·), Len(Vertices(·po·0·pc·))
D3DD.EndScene
D3DD.Present ByVal ·po·0·pc·, ByVal ·po·0·pc·, ·po·0·pc·, ByVal ·po·0·pc·
End Sub

Private Sub Form_Load()
b = False
Load_DX
Reset_DX
While b = False
Play_DX
DoEvents
Wend
End Sub

Private Sub Timer1_Timer()
b = True
End Sub

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this