Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hidden Face Removal Problem

Started by spiffycrony Mar 14, 2007 at 9:08 PM 5 replies 650+ views
Original Post
spiffycrony
spiffycrony
I am working with directx8 and vb6. This is a continuation of the Hidden face removal problem. I am trying to draw 2 triangles one inside the other in wireframe mode. The inner triangle also has a z value so that it is at the back of the bigger triangle. I am trying to enable the z buffer and I want only the outer bigger triangle to be drawn. The main functions are as follows. 1. The Direct3d start up function Public Function Startup(ByVal hWnd As Long, ByVal lWidth As Long, _ ByVal lHeight As Long) As Boolean On Local Error GoTo startup_error Startup = False 'Create the d3d object Set m_d3d = m_dx.Direct3DCreate() If m_d3d Is Nothing Then Exit Function End If 'Get the current display mode m_d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, m_mode 'Fill in the structre used to create the device m_d3dpp.Windowed = 1 m_d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC m_d3dpp.BackBufferFormat = m_mode.Format m_d3dpp.BackBufferCount = 1 m_d3dpp.EnableAutoDepthStencil = 1 m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16 'In order of preference: 32, 24, 16 If m_d3d.CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mode.Format, _ D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D32) = D3D_OK Then '//Enable a pure 32bit Depth buffer m_d3dpp.AutoDepthStencilFormat = D3DFMT_D32 m_d3dpp.EnableAutoDepthStencil = 1 Debug.Print "32 bit Depth buffer selected" Else '//search for a 24 bit depth buffer ' If m_d3d.CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mode.Format, _ ' D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8) = D3D_OK Then '//Enable a 24 bit depth buffer ' m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8 ' m_d3dpp.EnableAutoDepthStencil = 1 ' Debug.Print "24 bit Depth buffer selected" ' Else If m_d3d.CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mode.Format, _ D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) = D3D_OK Then '//Enable a 16 bit depth buffer m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16 m_d3dpp.EnableAutoDepthStencil = 1 Debug.Print "16 bit Depth buffer selected" Else '//hmm. No depth buffers available... Dont use one then :) m_d3dpp.EnableAutoDepthStencil = 0 Debug.Print "no Depth buffer selected" End If ' End If End If 'Create the d3ddevice with hardware 3d support Set m_d3dDevice = m_d3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, _ hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, m_d3dpp) If m_d3dDevice Is Nothing Then Exit Function 'Set the vertex shader m_d3dDevice.SetVertexShader D3DFVF_VERTEX m_d3dDevice.SetRenderState D3DRS_ZENABLE, 1 m_d3dDevice.SetRenderState D3DRS_ZWRITEENABLE, 1 m_d3dDevice.SetRenderState D3DRS_ZFUNC, D3DCMP_LESSEQUAL 'Turn on ambient lighting m_d3dDevice.SetRenderState D3DRS_AMBIENT, True 'Turn off hardware lighting m_d3dDevice.SetRenderState D3DRS_LIGHTING, False 'Turn on counter-clockwise backface removal m_d3dDevice.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE m_d3dDevice.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME 'Retrieve device capabilities Device.GetDeviceCaps m_caps Startup = True Exit Function startup_error: Startup = False End Function 2. The rendering function Public Sub RenderScene() 'Clear the buffer m_d3d.Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, lColor, 1, 0 'Start the rendering m_d3d.BeginScene 'Draw the triangle m_triangle(0).Draw m_triangle(1).Draw 'Stop the rendering m_d3d.EndScene m_d3d.Present End Sub I am still getting both the triangles drawn one within the other. Am i doing something wrong? Any suggestions? PS: Nik02 - I am trying to do what you have suggested in the other thread.
Cowboy Coder
Cowboy Coder
What happens if you draw triangle 1 first?
spiffycrony
spiffycrony
My understanding is that when i draw both the triangles, the smaller triangle should be hidden and i should see only the bigger triangle because of the z buffer enabled. I am seeing both the triangles.
spiffycrony
spiffycrony
Hi,

The code works only for the solid triangles. I am trying the z buffer for the wireframe mesh.

Cowboy Coder
Cowboy Coder
Wireframe does not set the Z values for the pixels inside the triangles, just the pixels along the lines. So you'll be able to see through it to things behind it.
spiffycrony
spiffycrony
Hi Cowboy coder,

I just understood that. I believe the following steps suggested by Nik02 is for using the z buffer for wire frame

1. Render your model as solid faces into the z-buffer only, after disabling color writes by using D3DRS_COLORWRITEENABLE render state.

2. Set your depth comparison function to "less or equal" (D3DRS_ZFUNC renderstate).

3. Enable color writing and draw your wireframe. Invisible faces will be filtered out by the z-buffer filled in step one.

But i couldnt understand how to implement these steps. How and when to draw the solid faces in z-buffer only? and when to do the drawing wireframe? Any suggestions.

spiffycrony
spiffycrony
I finally got it working trying to implement what Nik02 has said

Public Sub RenderScene()

'Clear the buffer
m_d3d.Clear C_WHITE

'Start the rendering
m_d3d.BeginScene

m_d3d.Device.SetRenderState D3DRS_COLORWRITEENABLE, 0
m_d3d.Device.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID

'Draw the triangle
m_triangle(0).Draw
m_triangle(1).Draw

m_d3d.Device.SetRenderState D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA Or _
D3DCOLORWRITEENABLE_BLUE Or D3DCOLORWRITEENABLE_GREEN Or D3DCOLORWRITEENABLE_RED

m_d3d.Device.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME

'Draw the triangle
m_triangle(0).Draw
m_triangle(1).Draw

'Stop the rendering
m_d3d.EndScene

m_d3d.Present

End Sub


But when i use the same code for a mesh of large no of triangles then some triangles are not drawn properly. Any ideas?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.