Jump to content
  • Advertisement
Sign in to follow this  
GKalman

ZBuffer Question

This topic is 3861 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

For learning purposes I have a modified version of Jack Hoxley's Tutorial #2 from www.DirectX4VB.com. The only change I made was instead of showing a cube, I have two triangles, each in a different z-plane. The (simplified) sample shows the two triangles in their correct perspective (the closer one is bigger than the other one). However, they cover-up each other in the wrong order!!!!! Could someone please explain what did I do wrong? Here is the sample code (VB5): '==================================================================== Dim Dx As DirectX8 'The master Object, everything comes from here Dim D3D As Direct3D8 'This controls all things 3D Dim D3DDevice As Direct3DDevice8 'This actually represents the hardware doing the rendering Dim bRunning As Boolean 'Controls whether the program is running or not... Const FVF_LVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR Or D3DFVF_TEX1) Private Type LITVERTEX X As Single Y As Single Z As Single Color As Long Specular As Long tU As Single tV As Single End Type Dim TRiVerts(0 To 5) As LITVERTEX '//for our 3D cube Dim vbCube As Direct3DVertexBuffer8 '//The vertex buffer for our cube. Dim matProj As D3DMATRIX Dim matView As D3DMATRIX Dim matWorld As D3DMATRIX Const PI = 3.14159 Const RAD = PI / 180 Const DEG = 180 / PI '----------------------------------------------- Public Function Initialise() As Boolean Dim DispMode As D3DDISPLAYMODE '//Describes our Display Mode Dim tmpDispMode As D3DDISPLAYMODE '//used during the enumeration of avail. modes Dim D3DWindow As D3DPRESENT_PARAMETERS '//Describes our Viewport Dim I As Long '//so we can loop through the avail. display modes Set Dx = New DirectX8 '//Create our Master Object Set D3D = Dx.Direct3DCreate() '//Let our Master Object create the Direct3D Interface '################## '## WINDOWED MODE ## '################# D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DispMode '//Retrieve the current display Mode D3DWindow.Windowed = 1 '//Tell it we're using Windowed Mode D3DWindow.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC '//We'll refresh when the monitor does D3DWindow.BackBufferFormat = DispMode.Format '//We'll use the format we just retrieved... D3DWindow.BackBufferCount = 1 D3DWindow.BackBufferFormat = DispMode.Format D3DWindow.BackBufferWidth = DispMode.Width D3DWindow.BackBufferHeight = DispMode.Height D3DWindow.hDeviceWindow = frmMain.hWnd D3DWindow.AutoDepthStencilFormat = D3DFMT_D16 D3DWindow.EnableAutoDepthStencil = 1 D3DWindow.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC '//This line creates a device that uses a hardware device if possible; software vertex processing and uses the form as it's target '//See the lesson text for more information on this line... Set D3DDevice = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, _ frmMain.hWnd, _ D3DCREATE_SOFTWARE_VERTEXPROCESSING, _ D3DWindow) '//Configure the rendering device D3DDevice.SetVertexShader FVF_LVERTEX '//Tell it what type of vertex we are using D3DDevice.SetRenderState D3DRS_LIGHTING, 0 '//Disable lighting. D3DDevice.SetRenderState D3DRS_ZENABLE, 1 D3DDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE '//configure the world matrices '//1. The World Matrix D3DXMatrixIdentity matWorld D3DDevice.SetTransform D3DTS_WORLD, matWorld 'commit this matrix to the device '//2. The View Matrix ''D3DXMatrixLookAtLH matView, MakeVector(0, 5, 2), MakeVector(0, 0, 0), MakeVector(0, 1, 0) D3DXMatrixLookAtRH matView, MakeVector(0, 0.25, 3), MakeVector(0, 0, 0), MakeVector(0, 1, 0) D3DDevice.SetTransform D3DTS_VIEW, matView '//3. The projection Matrix D3DXMatrixPerspectiveFovRH matProj, PI / 3, 1, 0, 100 ''''0.1, 75 '''&&& '''' Dim matAxesProj As D3DMATRIX '''' D3DXMatrixPerspectiveFovLH matAxesProj, g_pi / 4, 1, 1, 1000 '''' '' '''' End If '''' 'Else '''' If bCommonProjections = False Then '''' 'D3DMATRIX result of the operation (LH-ORTHO projection) '''' 'view -Width '''' 'view -Height '''' 'view z - Min '''' 'view Z_max '''' '''D3DXMatrixOrthoLH matAxesProj, Canvas, Canvas, 0, 100 '' D3DXMatrixOrthoLH matProj, 2, 2, 0, 100 '''' '''' '''' End If '''' g_D3DDevice.SetTransform D3DTS_PROJECTION, matAxesProj ' '' '' '' '''&&& D3DDevice.SetTransform D3DTS_PROJECTION, matProj InitialiseGeometry '//Setup the vertices Initialise = True '//We succeeded Exit Function ErrHandler: Debug.Print "Error Number Returned: " & Err.Number, Err.Description Initialise = False End Function '----------------------------------------------- Public Sub Render() '//1. We need to clear the render device before we can draw anything ' This must always happen before you start rendering stuff... D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &H8080CC, 1#, 0 'the hexidecimal value in the middle is the same as when you're using colours in HTML - if you're familiar 'with that. '//2. Next we would render everything. D3DDevice.BeginScene '##RENDERING METHOD 2## D3DDevice.SetStreamSource 0, vbCube, Len(TRiVerts(0)) D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 2 D3DDevice.EndScene '//3. Update the frame to the screen... ' This is the same as the Primary.Flip method as used in DirectX 7 ' These values below should work for almost all cases... D3DDevice.Present ByVal 0, ByVal 0, 0, ByVal 0 End Sub '---------------------------------------------------- Private Sub InitialiseGeometry() '//0. Any Variables '//2. Define the faces TRiVerts(0) = CreateLitVertex(0.5, 0.5, 1, RGB(255, 0, 0), 0, 0, 0) TRiVerts(1) = CreateLitVertex(-0.5, 0, 1, RGB(255, 0, 0), 0, 0, 0) TRiVerts(2) = CreateLitVertex(-0.5, 0.5, 1, RGB(255, 0, 0), 0, 0, 0) TRiVerts(3) = CreateLitVertex(0.75, 0.25, 0, RGB(0, 0, 255), 0, 0, 0) TRiVerts(4) = CreateLitVertex(-0.75, 0.25, 0, RGB(0, 0, 255), 0, 0, 0) TRiVerts(5) = CreateLitVertex(-0.75, 0, 0, RGB(0, 0, 255), 0, 0, 0) '//Setup the Vertex buffer Set vbCube = D3DDevice.CreateVertexBuffer(Len(TRiVerts(0)) * 6, 0, FVF_LVERTEX, D3DPOOL_MANAGED) D3DVertexBuffer8SetData vbCube, 0, Len(TRiVerts(0)) * 6, 0, TRiVerts(0) End Sub '------------------------------------------------ Private Function CreateLitVertex(X As Single, Y As Single, Z As Single, Colour As Long, Specular As Long, tU As Single, tV As Single) As LITVERTEX CreateLitVertex.X = X CreateLitVertex.Y = Y CreateLitVertex.Z = Z CreateLitVertex.Color = Colour CreateLitVertex.Specular = Specular CreateLitVertex.tU = tU CreateLitVertex.tV = tV End Function '------------------------------------------------------ Private Function MakeVector(X As Single, Y As Single, Z As Single) As D3DVECTOR MakeVector.X = X: MakeVector.Y = Y: MakeVector.Z = Z End Function '------------------------------------------ Private Sub Form_Click() bRunning = False End Sub '--------------------------------------------- Private Sub Form_Load() Dim matTemp As D3DMATRIX Dim Angle As Single Me.Show '//Make sure our window is visible bRunning = Initialise() Debug.Print "Device Creation Return Code : ", bRunning 'So you can see what happens... Do While bRunning D3DXMatrixIdentity matWorld '' D3DXMatrixIdentity matTemp '' D3DXMatrixRotationX matTemp, Angle * RAD '' D3DXMatrixMultiply matWorld, matWorld, matTemp '' '' D3DXMatrixIdentity matTemp '' D3DXMatrixRotationY matTemp, Angle * RAD '' D3DXMatrixMultiply matWorld, matWorld, matTemp '' '' D3DXMatrixIdentity matTemp '' D3DXMatrixRotationZ matTemp, Angle * RAD '' D3DXMatrixMultiply matWorld, matWorld, matTemp '' '' Angle = 0 '''' Angle + 1 D3DDevice.SetTransform D3DTS_WORLD, matWorld Render '//Update the frame... DoEvents '//Allow windows time to think; otherwise you'll get into a really tight (and bad) loop... Loop '//Begin the next frame... '//If we've gotten to this point the loop must have been terminated ' So we need to clean up after ourselves. This isn't essential, but it' ' good coding practise. On Error Resume Next 'If the objects were never created; ' (the initialisation failed) we might get an ' error when freeing them... which we need to ' handle, but as we're closing anyway... Set D3DDevice = Nothing Set D3D = Nothing Set Dx = Nothing Debug.Print "All Objects Destroyed" '//Final termination: Unload Me End End Sub

Share this post


Link to post
Share on other sites
Advertisement
It may have to do with your hardware not supporting the requested display surface format. Also try turning on back-face culling.

Share this post


Link to post
Share on other sites
i think you mistake is that you must also clear the ZBUFFER
..
in your render()

D3DCLEAR_TARGET...etc... // copy it and paste on the next line..
change it to D3DCLEAR_ZBUFFER..

D3DDevice.Clear 0, ByVal 0, D3DCLEAR_TARGET, &H8080CC, 1#, 0
D3DDevice.Clear 0, ByVal 0, D3DCLEAR_ZBUFFER, &H8080CC, 1#, 0

please tell me if it worked.. im using C++ not VB so forgive me if i gave a wrong information..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!