Applying 3Ds Max 9 FX file to a DirectX 9 mesh

Started by
1 comment, last by Gerhard Mostert 17 years, 1 month ago
How does one apply a .fx file that was created in 3Ds Max 9 to a mesh that was created in DirectX 9c. I know the basics of HLSL but can not seem to get this right. All I get is a mesh that is black. I worked through the Chapters of HLSL in Tom Miller's Managed DirectX 9 and I am now trying to use the .fx file that was created in 3Ds max9 instead of simple.fx that miller used, but now Ii only get a black Tiny instead.
Advertisement
I exported a FX file from 3DSMAX 9 that is a normal diffuse color effect(just to test if the .fx file works, I did not try to do anything fancy), I got 2 files .fx, .hlsl.

I tried to render the box, but it's seem to be black. How could I use these effect files in Managed DirectX for C#?

This is my .fx file
float4x4 mWorld_0_0 : WORLDT;float4x4 mIWorld_0_0 : WORLDI;float4x4 mWorldViewProj_0_0 : WORLDVIEWPROJECTION;float4 mCamPos_0_0 : WORLD_CAMERA_POSITION = {0.000000f, 0.000000f, 0.000000f, 0.000000f};float Opacity_1_0 : OPACITY<string UIName = "Opacity";float UIMin = 0.0;float UIMax = 1.0;float UIStep = 0.01;> = 1.000000f;float SpecularLevel_1_0 : SPECULARLEVEL<string UIName = "SpecularLevel";float UIMin = 0.0;float UIMax = 1.0;float UIStep = 0.01;> = 0.000000f;float GlossLevel_1_0 : GLOSSLEVEL<string UIName = "GlossLevel";float UIMin = 0.0;float UIMax = 100.0;float UIStep = 1.0;> = 10.000000f;float4 LightDir0_1_0 : DIRECTION<string Object = "TargetLight";string UIName = "LightDir0";int refID = 0;> = {0.000000f, 0.000000f, 0.000000f, 0.000000f};float4 LightCol0_1_0 : LIGHTCOLOR<int LightRef = 0;> = {0.000000f, 0.000000f, 0.000000f, 0.000000f};float4 Emissive_1_0 : EMISSIVE<string UIName = "Emissive";> = {0.000000f, 0.000000f, 0.000000f, 1.000000f};float4 Specular_1_0 : SPECULAR<string UIName = "Specular";> = {0.900000f, 0.900000f, 0.900000f, 1.000000f};float4 Diffuse_1_0 : DIFFUSE<string UIName = "Diffuse";> = {1.000000f, 0.000000f, 0.000000f, 1.000000f};VertexShader VS_pass0 = asm{vs_2_0// Generated by ATI's Ashli Compiler on 2007-03-15 13:55:56.// Ashli Version: 1.7.9// Instructions (Alu):            18// Registers    (Temp):           3// Registers    (Constant):       14// Registers    (Output):         1// Registers    (OutputTexCoord): 2dcl_position0 v0dcl_normal0 v2def c5, 0, 0, 0, 0mul r0, c13, v0.rmad r0, c12, v0.g, r0mad r0, c11, v0.b, r0mad r0, c10, v0.a, r0mov r1, c5.rdp4 r1.r, c9, v2dp4 r1.g, c8, v2dp4 r1.b, c7, v2dp4 r1.a, c6, v2mov r2, c5.rdp4 r2.r, c4, v0dp4 r2.g, c3, v0dp4 r2.b, c2, v0dp4 r2.a, c1, v0add r2, c0, -r2mov oPos, r0mov oT0, r1mov oT2, r2};PixelShader PS_pass0 = asm{ps_2_0// Generated by ATI's Ashli Compiler on 2007-03-15 13:55:56.// Ashli Version: 1.7.9// Instructions (Alu):            38// Registers    (Temp):           6// Registers    (Constant):       10// Registers    (TexCoord):       2// Registers    (OutputColor):    1dcl t0.rgbadcl t2.rgbadef c0, 0, 1, 0, 0def c9, 0, 0, 0, 1mov r0, c0.rmov r0.rgb, t2mov r0.a, t2.bdp3 r1.r, r0, r0rsq r1.r, r1.rmov r2, c0.rmul r2.rgb, r1.r, r0dp3 r0.r, r2, r2rsq r0.r, r0.rmul r0, r0.r, r2mov r1, c0.rmov r1.rgb, t0mov r1.a, t0.bdp3 r2.r, r1, r1rsq r2.r, r2.rmul r1, r2.r, r1dp3 r0.r, r1, r0max r0.r, r0.r, c0.rmin r0.r, r0.r, c0.gpow r0.r, r0.r, c3.rmul r0.r, r0.r, c2.rmul r0, c7, r0.rdp3 r1.r, r1, c4max r1.r, r1.r, c0.rmin r1.r, r1.r, c0.gmul r1, c8, r1.rmov r2, c0.rmov r2.rgb, c5mov r2.a, c0.gmov r4, c6mov r5, c8mad r3, r4, r5, c9mad r1, r2, r1, r3mad r0, r2, r0, r1mov r0.a, c1.rmov oC0, r0};technique technique0{pass{VertexShaderConstant[0] = <mCamPos_0_0>;VertexShaderConstant[1] = <mWorld_0_0[3]>;VertexShaderConstant[2] = <mWorld_0_0[2]>;VertexShaderConstant[3] = <mWorld_0_0[1]>;VertexShaderConstant[4] = <mWorld_0_0[0]>;VertexShaderConstant[6] = <mIWorld_0_0[3]>;VertexShaderConstant[7] = <mIWorld_0_0[2]>;VertexShaderConstant[8] = <mIWorld_0_0[1]>;VertexShaderConstant[9] = <mIWorld_0_0[0]>;VertexShaderConstant[10] = <mWorldViewProj_0_0[3]>;VertexShaderConstant[11] = <mWorldViewProj_0_0[2]>;VertexShaderConstant[12] = <mWorldViewProj_0_0[1]>;VertexShaderConstant[13] = <mWorldViewProj_0_0[0]>;PixelShaderConstant[1] = <Opacity_1_0>;PixelShaderConstant[2] = <SpecularLevel_1_0>;PixelShaderConstant[3] = <GlossLevel_1_0>;PixelShaderConstant[4] = <LightDir0_1_0>;PixelShaderConstant[5] = <LightCol0_1_0>;PixelShaderConstant[6] = <Emissive_1_0>;PixelShaderConstant[7] = <Specular_1_0>;PixelShaderConstant[8] = <Diffuse_1_0>;VertexShader  = <VS_pass0>;PixelShader  = <PS_pass0>;}}bool UseLPRT = false;


This is the HLSL file that was created, I do not know how to use this file either, since Tom Miller's book only uses .fx files (ironically, this file looks more like the .fx files that I learned about than the .fx that was created).

struct PixelInput {	float4 norm		: TEXCOORD0;	float4 worldpos	: TEXCOORD1;	float4 eyevector	: TEXCOORD2;	float4 tangent		: TEXCOORD3;	float4 binormal	: TEXCOORD4;	float4 objpos		: TEXCOORD5;	float4 tex0		: TEXCOORD6;	float4 tex1		: TEXCOORD7;};struct PixelOutput {	float4 c 		: COLOR;};uniform float4 Diffuse = float4(1.000000,0.000000,0.000000,1.000000);uniform float4 Specular = float4(0.900000,0.900000,0.900000,1.000000);uniform float4 Emissive = float4(0.000000,0.000000,0.000000,1.0f);uniform float SpecularLevel = 0.000000;uniform float GlossLevel = 9.999999;uniform float3 LightCol0;uniform float3 LightDir0;uniform float LightFallOff0;uniform float LightHotSpot0;uniform float Opacity = 1.000000;PixelOutput fragmentEntry(PixelInput pi){	PixelOutput PO;	float3 N;	N = normalize(pi.norm.xyz);	float3 Eye;	Eye.xyz = normalize(pi.eyevector.xyz);	float4 nColor = float4(0.0,0.0,0.0,1.0);	float4 mDiffuse = Diffuse;	float4 mSpecular = Specular;	float4 SelfIllum = Emissive;	nColor = nColor + SelfIllum * mDiffuse;	float3  H;	float3 Light;	float f;	float fAtt;	float fDist;/*  default light */	fAtt = 1.0;	nColor = nColor + float4(LightCol0,1.0) * mDiffuse * clamp(dot(N,LightDir0),0,1) * fAtt;	H = normalize(Eye+Light);	f = clamp(dot(N,H),0,1);	f = pow(f, GlossLevel);	f = f * SpecularLevel;	nColor = nColor + float4(LightCol0,1.0) * mSpecular * f * fAtt;/* end of default light */	nColor.a = Opacity;	PO.c = nColor;	return PO;}


This is my Framework class for setting up the HLSL and effects

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace HLSL_Framework{    public partial class HLSLFramework : Form   {        private Device device = null;                private Effect effect = null;                // Matrices        private Matrix worldMatrix;        private Matrix viewMatrix;        private Matrix projMatrix;                private MeshObject mesh;               /// <summary>        /// 1) Called once and sets presentation parameters and create device        /// 2) Pass a call to OnDeviceReset         /// </summary>        public bool InitializeGraphics()        {            PresentParameters presentParams = new PresentParameters();            presentParams.Windowed = true;            presentParams.SwapEffect = SwapEffect.Discard;            presentParams.AutoDepthStencilFormat = DepthFormat.D16;            presentParams.EnableAutoDepthStencil = true;            bool canDoShaders = true;            // Does a hardware device support shaders?            Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);            if (hardware.VertexShaderVersion >= new Version(2, 0))            {                // Default to software processing                CreateFlags flags = CreateFlags.SoftwareVertexProcessing;                // Use hardware if it's available                if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)                    flags = CreateFlags.HardwareVertexProcessing;                // Use pure if it's available                if (hardware.DeviceCaps.SupportsPureDevice)                    flags |= CreateFlags.PureDevice;                // Yes, Create our device                device = new Device(0, DeviceType.Hardware, this, flags, presentParams);            }            else            {                // No shader support                canDoShaders = false;                // Create a reference device                device = new Device(0, DeviceType.Reference, this,                    CreateFlags.SoftwareVertexProcessing, presentParams);            }            //Load .fx file here ex:            effect = Effect.FromFile(device, "..\\..\\boilerDiffuseRed.fx", null, ShaderFlags.None, null);            //Set effect.Technique here ex:             effect.Technique = "technique0";            device.DeviceReset += new EventHandler(OnDeviceReset);            OnDeviceReset(device, null);            return canDoShaders;        }        /// <summary>        /// Called on each device reset event, this includes instantiation of the device        /// 1) Sets up Camera position        /// 2) Sets Cull mode to Counter clockwise        /// 3) Load Meshes        /// 4) Sets up lights        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void OnDeviceReset(object sender, EventArgs e)        {            Device dev = (Device)sender;            SetupCamera();                       //Load Mesh here ex:            mesh = new MeshObject(@"..\..\boilerpipe.x", device);        }        /// <summary>        /// Called on every frame        /// 1) Clear device        /// 2) Start rendering        /// 3) Draw Meshes        /// </summary>        /// <param name="e"></param>        protected override void OnPaint(PaintEventArgs e)        {            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);            device.BeginScene();            //Do World transformation here "TransformWorld(,,,,,,);"            TransformWorld(0, 0, 0, 0, 0, 0);            //& Draw the mesh            mesh.DrawMesh(device, ref effect);                                             device.EndScene();            device.Present();            this.Invalidate();        }        // Sets up Projection and Viwe Transformation.        // Called from OnDeviceReset()        private void SetupCamera()        {            //Setup Projection Frustum            projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height,                                                                   1.0f, 10000.0f);            //Setup View Point            viewMatrix = Matrix.LookAtLH(new Vector3(0,0,580.0f),                                                     new Vector3(),                                                     new Vector3(0,1,0));                   }        //Transform the object's position, rotation and scaling        private void TransformWorld(float x, float y, float z, float yaw, float pitch, float roll,                                    float sx, float sy, float sz)        {            Matrix worldViewProj;            //Rotation & Translation            worldMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll)                                   * Matrix.Translation(x, y, z)                                   * Matrix.Scaling(sx, sy, sz);            worldViewProj = worldMatrix * viewMatrix * projMatrix;            effect.SetValue("mWorldViewProj_0_0", worldViewProj);        }        //Transform the object's position and rotation        private void TransformWorld(float x, float y, float z, float yaw, float pitch, float roll)        {            Matrix worldViewProj;            //Rotation & Translation            worldMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll)                                   * Matrix.Translation(x, y, z);            worldViewProj = worldMatrix * viewMatrix * projMatrix;            effect.SetValue("mWorldViewProj_0_0", worldViewProj);        }        public HLSLFramework()        {            InitializeComponent();            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);        }    }}


This is just a class that I use as a mesh container tipe of class, setting up the mesh, loading and drawing it.

using System;using System.Collections.Generic;using System.Text;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace HLSL_Framework{    class MeshObject    {        private Mesh ObjectMesh = null;        private Material[] ObjectMeshMaterials = null;        private Texture[] ObjectMeshTextures = null;        /// <summary>        /// Loads mesh from file and laods mesh materials and textures        /// </summary>        /// <param name="file"></param>        /// <returns></returns>        public MeshObject(String file, Device device)        {            ExtendedMaterial[] mtrl;            ObjectMesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);            if ((mtrl != null) && (mtrl.Length > 0))            {                ObjectMeshMaterials = new Material[mtrl.Length];                ObjectMeshTextures = new Texture[mtrl.Length];                for (int i = 0; i < mtrl.Length; i++)                {                    ObjectMeshMaterials = mtrl.Material3D;                    if ((mtrl.TextureFilename != null) &&                       (mtrl.TextureFilename != string.Empty))                    {                        ObjectMeshTextures = TextureLoader.FromFile(device, @"..\..\" + mtrl.TextureFilename);                    }                }            }        }        /// <summary>        /// Receives a Mesh object retrieved from LoadMesh and draw this object        /// </summary>        /// <param name="objectMesh"></param>        public void DrawMesh(Device device, ref Effect fx )        {            int numPasses = fx.Begin(0);            for (int iPass = 0; iPass < numPasses; iPass++)            {                fx.BeginPass(iPass);                //Draw                for (int i = 0; i < ObjectMeshMaterials.Length; i++)                {                                        device.SetTexture(0, ObjectMeshTextures);                    ObjectMesh.DrawSubset(i);                }                fx.EndPass();            }            fx.End();        }    }}
So by the way, I am using C# and MDX for this specific example.

This topic is closed to new replies.

Advertisement