Problem with lights

I've created a model with 3ds max 5 and I've exported to .x fileformat.

I used Manged DirectX to load the file and render it, but all the materials are rendered in white color.

Here is the C# code I use to load the meses, the method I use to load is called LoadMesh and the one which renders them is called DrawObject. Can somebody help me thanks:


/* Author: Eduardo Fonseca B.
 * Create on: 23.October 2005
 * <comments><comments>
 * <changes>
 * Author   Date    Description
 * </changes>
 * */
using System;
using System.Drawing;
#region DirectX-References
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
#endregion
namespace FrameWork.Meshes
{
 /// <summary>
 /// Summary description for CMesh.
 /// </summary>
 public class CMesh : FrameWork.Objects.CObject
 {
  protected Mesh objMesh;
  protected Material[] objMaterials;
  protected Texture[] objTextures;
  public CMesh(Utilities.CShapeProperties[] pobjCoordinates):
   base(pobjCoordinates)
  {
  }
  public override void AutoMove(float pfXMovement, float pfYMovement, float pfZMovement, int piMovementInterval)
  {
   
  }
  public override void DrawObject(float pfFormHeight, float pfFormWidth, ref Device pobjDevice)
  {
   this.SetupCamera(pfFormHeight, pfFormWidth,
    ref pobjDevice);
   if (this.objMesh != null)
   {
    pobjDevice.RenderState.Lighting = false;
    if (this.objMaterials != null)
    {
     for (int cont = 0; cont < this.objMaterials.Length; cont++)
     {
      pobjDevice.Material = this.objMaterials[cont];
      pobjDevice.SetTexture(cont, this.objTextures[cont]);
      //pobjDevice.Lights[cont].Enabled = true;
      this.objMesh.DrawSubset(cont);
     }
    }
    else
    {
     this.objMesh.DrawSubset(0);
    }
   }
  }
  public override void Rotate(float pfXAxis, float pfYAxis, float pfZAxis)
  {
   
  }
  protected override void Rotate(ref Device pobjDevice)
  {
   
  }
  public override void SetupCamera(float pfFormHeight, float pfFormWidth, ref Device pobjDevice)
  {
   float fFieldOfView = 45.0f;
   float fAspectRadio = pfFormWidth / pfFormHeight;
   float fZNearPlane = 0.0f;
   float fZFarPlane = 1000.0f;
   //pobjDevice.RenderState.Lighting = false;
   pobjDevice.Transform.Projection =
    Matrix.PerspectiveFovLH(
    fFieldOfView, fAspectRadio, fZNearPlane, fZFarPlane);
   Vector3 objCameraPosition =
    new Vector3(11.637f,11.637f,11.637f);
   Vector3 objCameraTarget =
    new Vector3();
   Vector3 objCameraUpVector =
    new Vector3(
    0,4.0f,0);
   pobjDevice.Transform.View = Matrix.LookAtLH(objCameraPosition,
    objCameraTarget, objCameraUpVector);
   pobjDevice.Transform.World = Matrix.Scaling(0.2f,0.2f,0.2f);
   /*pobjDevice.Lights[0].Enabled = true;
   pobjDevice.Lights[0].Commit();*/
   //pobjDevice.RenderState.Ambient = Color.Yellow;
   /*pobjDevice.Lights[0].Type = LightType.Directional;
   pobjDevice.Lights[0].Diffuse = Color.Transparent;
   pobjDevice.Lights[0].Direction = new Vector3(0,-1,-1);
   pobjDevice.Lights[0].Commit();
   pobjDevice.Lights[0].Enabled = true;
   Material objBoxMaterial =
    new Material();
   objBoxMaterial.Ambient = Color.White;
   objBoxMaterial.Diffuse = Color.White;
   pobjDevice.Material = objBoxMaterial;*/
  }
  public void LoadMesh(Device pobjDevice,
   string pstrFile)
  {
   ExtendedMaterial[] objMaterial1;
   EffectInstance objEffectInstance = new EffectInstance();
   Mesh objTempMesh = Mesh.FromFile(pstrFile,MeshFlags.Managed,
    pobjDevice, out objMaterial1,ref objEffectInstance);
   if ( (objMaterial1 != null) && (objMaterial1.Length > 0 ) )
   {
    this.objMaterials = new Material[objMaterial1.Length];
    this.objTextures = new Texture[objMaterial1.Length];
    pobjDevice.RenderState.Lighting = false;
    for (int i = 0; i < objMaterial1.Length; i++)
    {
     this.objMaterialsIdea = objMaterial1Idea.Material3D;
     if ( (objMaterial1Idea.TextureFilename != null) &&
      objMaterial1Idea.TextureFilename != string.Empty)
     {
      this.objTexturesIdea = TextureLoader.FromFile(
       pobjDevice,@"..\..\" + objMaterial1Idea.TextureFilename);
     }
    }
   }
   this.objMesh = objTempMesh;
   //return objTempMesh;
  }

 
 }
}

 




Answer this question

Problem with lights

  • Stechmann

     efonsecab wrote:
    I've loaded it with Meshviewer and it appears in white color, I guess the exporter I use is not exporting the lights.
    You can look at your exporter's settings to see if you can customize it; but off the top of my head I'm pretty sure that .X files are geometry/material only - not lights.

     efonsecab wrote:
    Is there any way I can work around it, besides coding the lights logic on my application
    No, not really. Even if you find a way to export the lights from 3DSMax you'll still have to "hook them up" in your application.

    For simple lighting you might get away with the fixed-function pipeline (which could be as simple as just copying your values to some light structures) but this is pretty limited.

    The simple answer is that, for anything remotely complex created in 'Max, it'll take a fair bit of work on your part to replicate it under Direct3D.

    hth
    Jack



  • SubratGaur

    I've loaded it with Meshviewer and it appears in white color, I guess the exporter I use is not exporting the lights.

    Is there any way I can work around it, besides coding the lights logic on my application



  • SoQL

    Firstly, it's worth noting that Direct3D can't always reproduce the exact materials as seen in 3DSMax - the basic .X file format and the fixed function pipeline are no where near as expressive as Max's material system.

    The next thing to try is to view the mesh using "Mesh Viewer" (if you have an older SDK) or "dxviewer". If they can correctly render your mesh then you'll know that your source code is wrong and if the proper tools can't replicate things how you want them you'll know that you've just hit a limitation and you'll have to work around it.

    hth
    Jack



  • Problem with lights