Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scaling a meshes normals

Status
Not open for further replies.

cherrymount

Programmer
Dec 30, 2004
2
AU
Hello,

I've got a problem when scaling a mesh: I don't know how to scale the normals. So when I resize my mesh - the lighting doesnt work!

Here's the code:


LPD3DXMESH ScaleMesh(LPD3DXMESH pMesh, double xscale, double yscale, double zscale){

struct Vertex {
D3DXVECTOR3 pos;
float tu;
float tv;

enum {
VertexFVF = D3DFVF_XYZ | D3DFVF_TEX1
};
};

LPD3DXMESH meshCopy;
Vertex *pVertex;
DWORD NUM_VERTICES = pMesh->GetNumVertices();
DWORD i;

pMesh->CloneMeshFVF(0, Vertex::VertexFVF, gD3DDevice, shCopy);

meshCopy->LockVertexBuffer(0,(VOID**)&pVertex);

for ( i=0;i<NUM_VERTICES;i++) {
// Scale
pVertex.pos.x *= xscale;
pVertex.pos.y *= yscale;
pVertex.pos.z *= zscale;

}

meshCopy->UnlockVertexBuffer();
pMesh->Release();

return meshCopy;

}

What do I need to do to scale the normals? Any ideas?
 
It automatically scales the normals - that's the problem. I think this should fix it:

m_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

It will automatically normalize them so it should work properly.
 
Thanks timmay3141,

But that's not it!

I already have the D3DRS_NORMALIZENORMALS flag set to true.
All I know, is that when I dont call the above scale function - the light works fine! And when I do - the light has no affect whatsoever.

Here are the render flags I set..
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
Device->SetRenderStateD3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
Device-SetRenderStateD3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);

Device->SetRenderState(D3DRS_ALPHATESTENABLE,true);
Device->SetRenderState(D3DRS_ALPHAREF,0);
Device->SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATER);
Device->SetRenderState( D3DRS_ZENABLE, TRUE );
Device->SetRenderState( D3DRS_DITHERENABLE, TRUE );
Device->SetRenderState( D3DRS_SPECULARENABLE, TRUE);
Device->SetRenderState( D3DRS_LIGHTING, TRUE );
Device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);



Any other ideas???????
How does someone normally scale a mesh in directX. Is the above method the correct way of doing it????
 
Oh crap...

Sorry, I should have looked at your code. That is NOT how you scale a mesh.

You need to add scaling to your (world) transformation matrix. Have you never used matrix transformations before?
 
The Vertex structure and FVF need a little change. Try this:


LPD3DXMESH ScaleMesh(LPD3DXMESH pMesh, double xscale, double yscale, double zscale)
{

/*struct Vertex {
D3DXVECTOR3 pos;
float tu;
float tv;

enum {
VertexFVF = D3DFVF_XYZ | D3DFVF_TEX1
};
};*/
struct Vertex
{
D3DXVECTOR3 Position;
D3DXVECTOR3 Normal;
D3DXVECTOR2 Tex;

enum { VertexFVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 };
};
LPD3DXMESH meshCopy;
Vertex *pVertex;
DWORD NUM_VERTICES = pMesh->GetNumVertices();
DWORD i;

pMesh->CloneMeshFVF(0, Vertex::VertexFVF, pDirect3DDevice, &meshCopy);

meshCopy->LockVertexBuffer(0,(VOID**)&pVertex);

for ( i=0;i<NUM_VERTICES;i++) {
// Scale
pVertex.Position.x *= xscale;
pVertex.Position.y *= yscale;
pVertex.Position.z *= zscale;

}

meshCopy->UnlockVertexBuffer();
pMesh->Release();

return meshCopy;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top