Sign in to follow this  
Lars Junered

How get directional light on a mesh (.x file)

Recommended Posts

Lars Junered    122
Have a simple engine with skybox and some meshes loaded. My meshes don't look so nice because they have no shade. Have directional light code active but it not seems to work so good... Have tried to change values on ambient and diffuse and direction but nothing happens with my textured meshes. Ambient light on mesh parts who NOT are textured seems to work. But nothing else. All meshes are visible even if I rem out all light code??? And skybox is the same maybe because I load it as Down is mesh creating code and set light code. Is it something with normals on faces for meshes who are missing? Where could I find good reading about lighting on meshes? Microsoft DirectX don't give me any answer when reading light and mesh tutorial. ----------------------------------------------------------- Mesh Create: bool SMesh::Create(char* szPathName, char* szFileName, float XR,float YR,float ZR,float XT,float YT,float ZT,float XS,float YS,float ZS) { LPD3DXBUFFER materialBuffer; //*** MESH *** HRESULT hr=D3DXLoadMeshFromX(szFileName, D3DXMESH_SYSTEMMEM, g_pDirect3DDevice, NULL, &materialBuffer,NULL, &m_numMaterials, &m_mesh ); D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer(); m_meshMaterials = new D3DMATERIAL9[m_numMaterials]; m_meshTextures = new LPDIRECT3DTEXTURE9[m_numMaterials]; // char buffer2[30]; // testing(itoa(m_numMaterials,buffer2,10)); for (DWORD i=0; i<m_numMaterials; i++) { // Copy the material m_meshMaterials[i] = d3dxMaterials[i].MatD3D; // Set the ambient color for the material (D3DX does not do this) // m_meshMaterials[i].Ambient = m_meshMaterials[i].Diffuse; //--- Sätt material egenskaper för Directional light --- m_meshMaterials[i].Ambient.r = 0.7f; m_meshMaterials[i].Ambient.g = 0.3f; m_meshMaterials[i].Ambient.b = 0.3f; m_meshMaterials[i].Ambient.a = 0.0f; m_meshMaterials[i].Diffuse.r = 0.7f; m_meshMaterials[i].Diffuse.g = 0.3f; m_meshMaterials[i].Diffuse.b = 0.3f; m_meshMaterials[i].Diffuse.a = 0.0f; g_pDirect3DDevice->SetMaterial(&m_meshMaterials[i]); // Create the texture if it exists - it may not m_meshTextures[i] = NULL; if (d3dxMaterials[i].pTextureFilename) { // testing(d3dxMaterials[i].pTextureFilename); char fFileName[80]; strcpy(fFileName,szPathName); strcat(fFileName,d3dxMaterials[i].pTextureFilename); testing(fFileName); if( FAILED( D3DXCreateTextureFromFile(g_pDirect3DDevice, fFileName, &m_meshTextures[i]))) { testing("fel hittar inte"); testing(fFileName); m_meshTextures[i] = NULL; } } } materialBuffer->Release(); SMesh::m_rotation.x = XR; //spara som medlemsvariabler SMesh::m_rotation.y = YR; SMesh::m_rotation.z = ZR; SMesh::m_translation.x = XT; SMesh::m_translation.y = YT; SMesh::m_translation.z = ZT; SMesh::m_scale.x = XS; SMesh::m_scale.y = YS; SMesh::m_scale.z = ZS; SetWorldTransformation(XR, YR, ZR, XT, YT, ZT, XS, YS, ZS); //sätt rotering, placering, skalning return true; } ////////////////////////////////////////////////////// void SetVariableRenderStates(int level) { D3DLIGHT9 light; D3DVECTOR pos; // Fill in a light structure defining our light ZeroMemory( &light, sizeof(D3DLIGHT9) ); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; light.Ambient.r = 1.0f; light.Ambient.g = 1.0f; light.Ambient.b = 1.0f; light.Specular.r = 1.0f; light.Specular.g = 1.0f; light.Specular.b = 1.0f; light.Range = 1000.0f; // Create a direction for our light - it must be normalized D3DXVECTOR3 vecDir; vecDir = D3DXVECTOR3(1.0f,0.0f,0.0); D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir ); // Tell the device about the light and turn it on g_pDirect3DDevice->SetLight( 0, &light ); g_pDirect3DDevice->LightEnable( 0, TRUE ); // Turn light On if (g_bLightingOn) g_pDirect3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE);

Share this post

Link to post
Share on other sites
Lars Junered    122
Thanks for your answer.

I think you have right but it's very at the end of this reply

I use gamespace 1.6.
Save to a .x file and flip the UV map (bitmap).
This are the options for saving to .x format...

Export geometri: ON
Triangulate all faces: OFF (my object already is triangulated)
Store the object as single mesh: ON
Center and normalize object: OFF (ON don't help me)

Export textures: ON
Change texture orientation: ON (must be on else UV not work)
Export with full texture path: OFF
Inline material: ON
Export vertex color: OFF

Convert texture to:
1.Original format --> (use this)
2.Info about normals
Completly faceted
Completely smooth
From material editor -->(used)
From material editorOptimized
Convert resolution to:
1. Original size --> (used)
2. Closest 2^n size
3. 512x512
.. ..
.. 32x32

Animation: not relevant when single mesh I suppose.

Coordinate system:
Left handed --> (used)
Right handed

Float size:
32 bit
64 bit --> (used)

Great tip because I have found a small clou now. But I'm not the men to know whats wrong.

Have found that if I save as DDS format then lights on that mesh is ok (shade on right side). Have changed values for ambient to 0.1 for RGB.

The problem now is:
It don't show the UV map on my mesh!!! Only the pink/red color se my values for RGB.

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