Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Texture Mapping DirectX8

This topic is 5807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if someone can tell me what is wrong with my DirectX8 code. It can draw a diffuse triangle when I comment out the texture triangle coding, but when I enter the code for texture mapping code from the Microsoft Visual Basic Game Programming with DirectX, and remark out the diffuse triangle drawing, it doesn''t draw anything to the window. ''For texture maps present Const D3DFVF_MYVERTEX = (D3DFVF_XYZ Or D3DFVF_TEX1 Or D3DFVF_NORMAL) ''For no texture maps present ''Const D3DFVF_MYVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE) ''Windows API functions and structures Private Declare Function GetTickCount Lib "kernel32" () As Long Const colWhite As Long = &HFFFFFF Const colGray As Long = &H111111 Const colRed As Long = &HFF0000 Const colGreen As Long = &H1FF00 Const colBlue As Long = &H1FF Const colBlack As Long = &H0 Const PI = 3.141592654 Private Type VERTEXMAP vecPos As D3DVECTOR vecNorm As D3DVECTOR u As Single v As Single End Type Private Type VERTEX_TYPE x As Single y As Single z As Single Color As Long End Type Private Type TRIANGLE_TYPE v1 As VERTEX_TYPE v2 As VERTEX_TYPE v3 As VERTEX_TYPE End Type ''For not textured program ''Dim Tri1 As TRIANGLE_TYPE ''For textured program Dim Tri1(0 To 5) As VERTEXMAP ''For both texture and no texture program Dim vbTri1 As Direct3DVertexBuffer8 ''Texture variables Dim d3dtText1 As Direct3DTexture8 Dim DX As DirectX8 Dim d3d As Direct3D8 Dim d3dx As D3DX8 Dim d3dDevice As Direct3DDevice8 Dim d3dpp As D3DPRESENT_PARAMETERS Dim DisplayMode As D3DDISPLAYMODE Dim matrixView As D3DMATRIX Dim vectorCameraSource As D3DVECTOR Dim vectorCameraTarget As D3DVECTOR Dim vectorCameraTargetHeight As D3DVECTOR Dim matrixProjection As D3DMATRIX Dim matrixWorld As D3DMATRIX Dim fTheta As Single Dim matrixRotateX As D3DMATRIX Dim matrixRotateY As D3DMATRIX Dim matrixTranslate As D3DMATRIX Dim i As Long Dim EndGame As String Private Sub Form_Load() EndGame = "False" Form1.Caption = "Cube3D" Form1.AutoRedraw = False Form1.BorderStyle = 1 Form1.ClipControls = False Form1.KeyPreview = True Form1.ScaleMode = 3 Form1.Width = Screen.TwipsPerPixelX * 652 Form1.Height = Screen.TwipsPerPixelY * 510 Form1.Show ''create the DirectX8 and Direct3D8 objects Set DX = New DirectX8 Set d3d = DX.Direct3DCreate() Set d3dx = New D3DX8 ''get the display mode d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DisplayMode ''set the d3dDevice properties d3dpp.hDeviceWindow = Form1.hWnd d3dpp.BackBufferWidth = 640 d3dpp.BackBufferHeight = 480 d3dpp.BackBufferFormat = DisplayMode.Format d3dpp.BackBufferCount = 1 d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE d3dpp.AutoDepthStencilFormat = D3DFMT_D32 d3dpp.Windowed = 1 ''create the d3dDevice using the above properties Set d3dDevice = d3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, _ Form1.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp) ''set the vertex shader type d3dDevice.SetVertexShader D3DFVF_MYVERTEX ''turnon the ambient lighting d3dDevice.SetRenderState D3DRS_AMBIENT, True ''turn off z buffer d3dDevice.SetRenderState D3DRS_ZENABLE, D3DZB_FALSE ''turn off lights d3dDevice.SetRenderState D3DRS_LIGHTING, False ''turn on culling For textured d3dDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_CCW ''for no texture ''d3dDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE ''turn on the alpha blending d3dDevice.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA d3dDevice.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA d3dDevice.SetRenderState D3DRS_ALPHABLENDENABLE, 1 ''create the Viewport vectorCameraSource = CreateVector(0, 0, 46) vectorCameraTarget = CreateVector(0, 0, 0) vectorCameraTargetHeight = CreateVector(0, 1, 0) D3DXMatrixLookAtLH matrixView, vectorCameraSource, vectorCameraTarget, _ vectorCameraTargetHeight d3dDevice.SetTransform D3DTS_VIEW, matrixView ''create the Projection matrix D3DXMatrixPerspectiveFovLH matrixProjection, (PI / 4), 0.7, 1, 1000 d3dDevice.SetTransform D3DTS_PROJECTION, matrixProjection ''load a texture ''Set d3dtText1 = d3dx.CreateTextureFromFileEx(d3dDevice, (App.Path & "\background.bmp"), _ '' D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, _ '' D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_POINT, _ '' D3DX_FILTER_POINT, colBlack, ByVal 0, ByVal 0) ''Set d3dtText1 = d3dx.CreateTextureFromFile(d3dDevice, (App.Path & "\background.bmp")) Set d3dtText1 = d3dx.CreateTextureFromFileEx(d3dDevice, _ (App.Path & "\cube.bmp"), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, _ 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_POINT, _ D3DX_FILTER_POINT, &HFF000000, ByVal 0, ByVal 0) ''create the triangle for textured triangle Tri1(0) = CreateVertexUV(0, 1, -1, 0, -1, 1) Tri1(1) = CreateVertexUV(1, 1, 1, 0, -1, 0) Tri1(2) = CreateVertexUV(2, -1, 1, 0, 0, 0) Tri1(3) = CreateVertexUV(3, 1, -1, 0, -1, 1) Tri1(4) = CreateVertexUV(4, -1, -1, 0, 0, 1) Tri1(5) = CreateVertexUV(5, -1, 1, 0, 0, 0) ''create the vertex buffer for texture Set vbTri1 = d3dDevice.CreateVertexBuffer(Len(Tri1(0)) * 6, 0, D3DFVF_MYVERTEX, _ D3DPOOL_DEFAULT) ''For no textured triangle ''Tri1.v1 = CreateVertex(-1, -1, 0, colBlue) ''Tri1.v2 = CreateVertex(1, -1, 0, colGreen) ''Tri1.v3 = CreateVertex(-1, 1, 0, colWhite) ''create the vertex buffer for no texture ''Set vbTri1 = d3dDevice.CreateVertexBuffer(Len(Tri1), 0, D3DFVF_MYVERTEX, _ '' D3DPOOL_DEFAULT) ''fill the buffer with the data for no texture ''D3DVertexBuffer8SetData vbTri1, 0, Len(Tri1), 0, Tri1 ''fill the buffer with the data for texture D3DVertexBuffer8SetData vbTri1, 0, Len(Tri1(0)) * 6, 0, Tri1(0) ''game loop i = 0 Do While EndGame = "False" ''clear the back buffer d3dDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, colBlack, 1#, 0 d3dDevice.BeginScene D3DXMatrixIdentity matrixWorld d3dDevice.SetTransform D3DTS_WORLD, matrixWorld ''fTheta = fTheta + fRotate ''D3DXMatrixRotationX matrixRotateX, fTheta / 2 ''D3DXMatrixRotationY matrixRotateY, fTheta ''D3DXMatrixTranslation matrixTranslate, 0, 0, 40 ''D3DXMatrixMultiply matrixWorld, matrixRotateX, matrixRotateY ''D3DXMatrixMultiply matrixWorld, matrixWorld, matrixTranslate ''d3dDevice.SetTransform D3DTS_WORLD, matrixWorld ''set the texture d3dDevice.SetTexture 0, d3dtText1 ''For no texture ''d3dDevice.SetStreamSource 0, vbTri1, Len(Tri1.v1) ''For texture d3dDevice.SetStreamSource 0, vbTri1, Len(Tri1(0)) ''for both no texture and with texture d3dDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 2 ''Form1.Caption = i i = i + 1 ''delete the texture ''d3dDevice.SetTexture 0, Nothing d3dDevice.EndScene ''blit the back to the front d3dDevice.Present ByVal 0, ByVal 0, 0, ByVal 0 DoEvents Loop Unload Form1 End Sub Private Function CreateVertexUV(ByVal pos As Long, ByVal x As Single, ByVal y As Single, ByVal z As _ Single, ByVal u As Single, ByVal v As Single) As VERTEXMAP ''this creates a vertex with mapping coordinates Tri1(pos).vecPos.x = x Tri1(pos).vecPos.y = y Tri1(pos).vecPos.z = z Tri1(pos).vecNorm.x = 0 Tri1(pos).vecNorm.y = 0 Tri1(pos).vecNorm.z = 1 Tri1(pos).u = u Tri1(pos).v = v End Function Private Function CreateVertex(ByVal x As Single, ByVal y As Single, ByVal z As Single, _ Color As Single) As VERTEX_TYPE ''this creates a simple vertex with color CreateVertex.x = x CreateVertex.y = y CreateVertex.z = z CreateVertex.Color = Color End Function Private Function CreateVector(ByVal x As Single, ByVal y As Single, _ ByVal z As Single) As D3DVECTOR ''creates vectors for calls like the view matrix CreateVector.x = x CreateVector.y = y CreateVector.z = z End Function Private Sub Picture1_Click() EndGame = "True" End Sub Randy Chow - MCSE< CCNA, A+

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!