X Files (ID3DXMesh), Lights, and Transformations

Started by
1 comment, last by MrDoomMaster 18 years, 8 months ago
I recently made a class to load and render X files. I encapsulated the functionality of ID3DXMesh in DirectX 9.0c. The thing is, I don't know how to add lights based on the loaded mesh, nor do I know how to transform the mesh. I want to rotate the mesh around the world origin, for example. Or perhaps I want to move the object. I've had a real tough time finding tutorials that cover a lot on Direct3D topics such as loading meshes and adding lights to them, etc. Can anyone help out? Thank you!
Advertisement
Generally, you don't add lights to your mesh but to your world. For instance, you can define a directional white light at 0,0,0 emitting at 10, 0, -10.

In Managed DirectX to define a light I do this:
device.Lights[0].Type = LightType.Directional;device.Lights[0].Diffuse = Color.White;device.Lights[0].Ambient = Color.White;etc.device.Lights[0].Update();device.Lights[0].Enabled = true;


Since you're probably using C++, try looking at these link:
Tutorial 4: Creating and Using Lights

You're probably best of wrapping this in a light class yourself:
class Lights{public:    Lights(IDirect3DDevice9 *pDev, int index):pDevice(pDevice), m_LightIndex(index)    {        ZeroMemory( &Light, sizeof(Light) );        // Perhaps call SetLight?    }    // ... Fill in other functions    void Enable(bool enable)    {        pDevice->LightEnable(m_LightIndex, enable);    }private:    int m_LightIndex;    D3DLIGHT9 m_Light;    D3DXVECTOR3 vecDir;    IDirect3DDevice9 *pDevice;}


Toolmaker

For some reason this still isn't working for me. I fill in my class exactly following the tutorial used by MSDN. Here is my CLight class:

//====================================================================================// Defines/Macros//====================================================================================//====================================================================================//====================================================================================// Includes//====================================================================================#include    <windows.h>#include    <d3d9.h>#include    <d3dx9.h>#include    <dxerr9.h>#include    <atlstr.h>#include    <math.h>#include    <vector>#include    "Shared.h"#include    "Display.h"#include    "Light.h"//====================================================================================//====================================================================================// Function Definitions//====================================================================================/******/DXLIB::CLight::CLight(CDisplay* DisplayObject) :    m_Display(DisplayObject){    memset(&m_Light, 0, sizeof(D3DLIGHT9));    m_Light.Type = D3DLIGHT_DIRECTIONAL;}//====================================================================================DXLIB::CLight::~CLight(void){    Cleanup();}//====================================================================================/******/int DXLIB::CLight::SetLightColor(UCHAR a, UCHAR r, UCHAR g, UCHAR b){    m_Light.Diffuse.a = (float)a / 255;    m_Light.Diffuse.r = (float)r / 255;    m_Light.Diffuse.g = (float)g / 255;    m_Light.Diffuse.b = (float)b / 255;    if(!UpdateLight())        return 0;    return 1;}//====================================================================================/******/int DXLIB::CLight::SetLightPosition(float x, float y, float z){    m_Light.Position.x = x;    m_Light.Position.y = y;    m_Light.Position.z = z;    if(!UpdateLight())        return 0;    return 1;}//====================================================================================/******/int DXLIB::CLight::SetLightDirection(float x, float y, float z){    D3DXVECTOR3 Direction(x, y, z);    D3DXVec3Normalize((D3DXVECTOR3*)&m_Light.Direction, &(Direction - (D3DXVECTOR3)m_Light.Position));    if(!UpdateLight())        return 0;    return 1;}//====================================================================================/******/int DXLIB::CLight::EnableLight(bool Enable){    if(!UpdateLight())        return 0;    if(FAILED(hr = m_Display->GetDevice()->LightEnable(0, Enable ? TRUE : FALSE)))        return 0;    return 1;}//====================================================================================/******/int DXLIB::CLight::UpdateLight(void){    if(FAILED(hr = m_Display->GetDevice()->SetLight(0, &m_Light)))        return 0;    return 1;}//====================================================================================/******/void DXLIB::CLight::Cleanup(void){    // Perform Cleanup Here}//====================================================================================


Keep in mind that CDisplay contains my Direct3D Device.

Also, after I create my device, I set the render states to enable lighting and also an ambience color (the same color used in the MSDN tutorial).

What else could I possibly be missing? Here are the values I set:

        CLight WorldLight(&Display);        WorldLight.SetLightColor(255, 255, 0, 0);        WorldLight.SetLightPosition(5, 5, 5);        WorldLight.SetLightDirection(0, 0, 0);        WorldLight.EnableLight(true);


I also realize that all objects of the class will share the index 0, but for now I am not worried about making more than 1 until I get at least 1 working. I will fix this index issue later when I get my first light working.

My 3D cube mesh is completely white when it renders, no lighting at all. every unit in the cube is pure 255,255,255 white

Thank you all!

[Edited by - MrDoomMaster on August 16, 2005 5:28:40 PM]

This topic is closed to new replies.

Advertisement