Sign in to follow this  
akhin

Seeing throught objects

Recommended Posts

Hi ,

In my scene , I need a mesh which has many objects itself.
Unfortunately I can see thru objects , so I can see even objects behind other objects
which is something I dont want :

-DX SDK s mesh viewer is able to render it properly so I dont see thru objects in the mesh

- There is no textures applied , so I am sure it has nothing to do with texture stages

-For my rendering parameters :
backface culling has turned off ( for debugging )
z buffering has been turned on

So what do you suggest me to check ?

Share this post


Link to post
Share on other sites
Sounds like you have alpha blending enabled.

Have you checked your Pixel Shader Code if the Alpha-Value gets calculated correctly?

Things you could try:

- just before the return command in your pixel shader, set your Out-Color Alpha value to 1.0f
- Turn the Alpha Blend state off for that Pixel shader

Share this post


Link to post
Share on other sites
Sorry , i forgot to mention it ,
no i turned alphablending off before rendering and it is not being turned on anywhere:

myDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

Just in case all `alpha` values in my lights and materials are zero even i turned alpha blending off...

And also I dont have any shaders at the moment

Share this post


Link to post
Share on other sites
Wait a minute... By "Seeing Through an Object", what exactly do you mean?

Is the object infront blended with the object in the Back? Like looking through a colored Glas window?

Or is it drawing the object in the back on top of the object infront?

Share this post


Link to post
Share on other sites
The first one :

"the object infront blended with the object in the Back"

For example there is a house in my scene , its door is open , DX SDK mesh viewer
just shows the interior of the house , whereas my code shows interior blendded with things in the back of that house.

Alphablending is turned off , Zbuffering is used properly, there is no textures so nothing todo with texture stages or texture blending

Share this post


Link to post
Share on other sites
Post your code for setting the device state ( SetRenderState(...), etc. ) and the code for rendering the mesh. Please use [code][/code] or [source][/source] tags.

Share this post


Link to post
Share on other sites
the repro code is as below.
The problem is as I said , i can backs of objects...

As I play with lighting it does not change it.( if i just set ambient light something like 255-255-255-1 this time I dont see back of objects but I cant see details in the objects since intensity of light is too much )

But please note that if I fill vertex buffers customly with the vertex type
with appropriate surface normals, then i have no problem...

I have found some 3dsmax models , converted them to X by using Panda and the problem occurs for them...

Also would there be any workaround by texturing and setting texture stages properly ?

1.

HRESULT SetupD3D(HWND hWnd)
{
// Create the D3D object.
if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

// Create the D3DDevice
if (FAILED(g_pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice)))
{
return E_FAIL;
}

// Enable the Z buffer, since we're dealing with 3D geometry.
g_pd3dDevice -> SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

return S_OK;
}



2. Load a mesh , setup view and projection matrix

3. Lighting :


VOID SetupLights()
{
// Define a light - possesses only a diffuse colour.
D3DLIGHT9 Light1;
ZeroMemory(&Light1, sizeof(D3DLIGHT9));
Light1.Type = D3DLIGHT_POINT;

Light1.Diffuse.r = 1.0f;
Light1.Diffuse.g = 1.0f;
Light1.Diffuse.b = 1.0f;

Light1.Position.x = 25.0f;
Light1.Position.y = 30.0f;
Light1.Position.z = -50.0f;

Light1.Attenuation0 = 1.0f;
Light1.Attenuation1 = 0.0f;
Light1.Attenuation2 = 0.0f;

Light1.Range = 500.0f;

// Select and enable the light.
g_pd3dDevice -> SetLight(0, &Light1);
g_pd3dDevice -> LightEnable(0, TRUE);
g_pd3dDevice ->SetRenderState(D3DRS_LIGHTING,TRUE);

}



4. Render


void Render()
{
g_pd3dDevice -> Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

// Begin the scene
if (SUCCEEDED(g_pd3dDevice -> BeginScene()))
{

g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);

for (DWORD i = 0; i < dwNumMaterials; i++)
{

g_pd3dDevice -> SetMaterial(&(MeshMaterials[i]));
g_pd3dDevice -> SetTexture(i, MeshTextures[i]);
Mesh -> DrawSubset(i);
}
g_pd3dDevice -> EndScene();
}

g_pd3dDevice -> Present(NULL, NULL, NULL, NULL);
}

Share this post


Link to post
Share on other sites
It looks like many objects are being rendered with CCW faces and not CW. How do you setup your projection and view matrices? Are they left-hand? Do you set the cullmode (you shouldn't need to)? It should, by default, be for CCW culling. You don't have culling turned off, do you?

Share this post


Link to post
Share on other sites
Also calling the one below doesnt help :


g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);



And regarding my coordinate system , yes I use left-handed coordinate system


void SetupViewMatrices()
{
// Set up the view matrix.
// This defines which way the 'camera' will look at, and which way is up.
D3DXVECTOR3 vCamera(15.0f, 2.0f, -2.0f);
D3DXVECTOR3 vLookat(2.0f, 2.0f, 0.0f);
D3DXVECTOR3 vUpVector(0.0f, 1.0f, 0.0f);
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vCamera, &vLookat, &vUpVector);
g_pd3dDevice -> SetTransform(D3DTS_VIEW, &matView);

// Set up the projection matrix.
// This transforms 2D geometry into a 3D space.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 0.0f, 200.0f);
g_pd3dDevice -> SetTransform(D3DTS_PROJECTION, &matProj);
}

Share this post


Link to post
Share on other sites
It looks like you forgot one render state (like zwrite)

A smart tips for that quind of problem, launch your application with Pix, take a single frame capture and browse to the draw that show the problem, inspect the device and you will see in less that thirty seconds what is wrong!

Share this post


Link to post
Share on other sites
g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,TRUE); unfortunately did not help as well

I will look into pix

Share this post


Link to post
Share on other sites
Ran meshview and my code with pix
after that i made my device state exactly same with meshview

and again it did not work...

That strongly suggests me I dont have appropriate lighting ?

When I just turn on a white ambient light there is no problem ,
but ofcourse because of full white light , i cant see some details on the objects...

With point light , I can see details of the objects , but this time seeing backfaces problem occur...

Share this post


Link to post
Share on other sites
Found it! the near plane of the projection can not be at 0.f.

If you do that, some part of the projection matrix are incorrect, every vertex ends with z == w after the projection and then are projected to the far plane

juste replace with

float aspectRatio = backbufferWidth / backbufferHeight;
float near = 1.f;
float far = 200.f
float fovy = D3DX_PI/4;
D3DXMatrixPerspectiveFovLH(&matProj, fovy, aspectRatio , near, far);

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