Sign in to follow this  
Feadur

Using FX files in XNA

Recommended Posts

Hi all, I've been using .FX files with HLSL to render my models in XNA. But always the models and the shaders came togheter. But now I'm trying to use the sample shaders from "NVIDIA FX Composer", doesn't work at all. I'm not shure if I'm using them incorrectly or why aren't working. That's the code for the drawing:

foreach (ModelMesh modmesh in this.model.Meshes)
            {
                foreach (Effect currenteffect in modmesh.Effects)
                {
                    
                    Matrix worldMatrix = Matrix.CreateRotationX((float)Math.PI)
                        * Matrix.CreateScale(new Vector3(1, 1, 1));

                    currenteffect.CurrentTechnique = effect.Techniques["glass"];
                    currenteffect.Parameters["worldIT"].SetValue(Matrix.Transpose(Matrix.Invert(worldMatrix)));
                    currenteffect.Parameters["world"].SetValue(worldMatrix);
                    currenteffect.Parameters["wvp"].SetValue(worldMatrix * matrizVista * matrizProyeccion);
                    currenteffect.Parameters["viewI"].SetValue(Matrix.Invert(matrizVista));
                }

                modmesh.Draw();
            }


And this are the parameter I think I have to pass to the vertex shader

/************* TWEAKABLES **************/

float4x4 worldIT : WorldInverseTranspose;
float4x4 wvp : WorldViewProjection;
float4x4 world : World;
float4x4 viewI : ViewInverse;



It's not drawing anything at all :( Thank's!

Share this post


Link to post
Share on other sites
IIRC Mesh.Draw will still use the shader that's set on the model by default (BasicEffect if no shader is specified by the model). Since you aren't setting the correct parameters on that, pretty much nothing should be getting rendered. You could replace the effect instance on the model with your own, but since that's a bit of a pain I've gotten around this by drawing the mesh 'manually', like this:

[source lang=C#]
...

// set up your effect parameters/constants here

deferredShadingEffect.Begin();

foreach (EffectPass pass in deferredShadingEffect.CurrentTechnique.Passes)
{
pass.Begin();

// There's only one mesh in the model, so we'll just draw that. To
// apply a custom effect, we'll need to draw the mesh manually with
// our DrawMesh method, props to Kris Nye
//
// To draw the whole mesh, you'd need to loop over the model.Meshes
// collection and call DrawMesh for each mesh in the model.

DrawMesh(graphics.GraphicsDevice, model.Meshes[0]);

pass.End();
}

deferredShadingEffect.End();

}

private void DrawMesh(GraphicsDevice device, ModelMesh mesh)
{
// This method basically does what mesh.Draw() does, only without
// using the default BasicEffect set by the framework. Since we
// are using a custom effect with different parameters, we'll need
// this approach. Otherwise we might have just set the effect on the
// appropriate mesh parts.

device.Indices = mesh.IndexBuffer;

for (int i = 0; i < mesh.MeshParts.Count; i++)
{
ModelMeshPart part = mesh.MeshParts[i];

if (part.NumVertices > 0)
{
device.VertexDeclaration = part.VertexDeclaration;
device.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount);
}
}
}




Hope this helps :)

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