## Recommended Posts

Comboy    100
I want to apply effect to scene (or some meshes individually) I’m doing it in DX10 XNA2 First of all the blinn.fx from fx composer, I load it
Effect blinn;
Texture2D diffuseMap;


and then in draw:
private void DrawModel(Model m)
{
Matrix[] transforms = new Matrix[m.Bones.Count];
float aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height;
m.CopyAbsoluteBoneTransformsTo(transforms);
aspectRatio, 1.0f, 10000.0f);
Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom), Vector3.Zero, Vector3.Up);

foreach (ModelMesh mesh in m.Meshes)
{
blinn.CurrentTechnique = blinn.Techniques[0];

foreach (ModelMeshPart part in mesh.MeshParts)
blinn.Begin();
foreach (EffectPass pass in this.blinn.CurrentTechnique.Passes)
{
pass.Begin();
blinn.Parameters["ColorTexture"].SetValue(diffuseMap);

part.Effect = blinn;
pass.End();
}
}
blinn.End();
mesh.Draw();
}
}


so I first set the technique, then for each mesh part I start the effect then for each pass I set the parameters then end the pass then set the effect for the mesh part, but nothing is on the monitor :( But with basiceffect it works, so mesh position and camera position are suitable.

##### Share on other sites
sirob    1181
Two things:

1) You need to call Effect::CommitChanges if you modify parameter values after calling Begin.

2) You havn't pasted your effect file. There's a good chance an error in the effect file might cause nothing to show on screen. I would recommend you start off with a very simple shader and attempt to get something on-screen, then gradually add complexity to the shader, while making sure things remain visible.

Hope this helps.

##### Share on other sites
Comboy    100
I also tried to commit changes but it still remains blank, the shader is default fx composer hlsl blinn shader and i compiled it there,

in basiceffect there is something which blinn doesn't have:

foreach (BasicEffect effect in mesh.Effects)                {                    effect.EnableDefaultLighting();                             effect.View = view;                    effect.Projection = projection;                    effect.World = gameWorldRotation * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Position);                }

that's view and projection and world

##### Share on other sites
Comboy    100
attempt using other effects simpler failed, Am i doing it in right order?

first tech set
foreach meshpart in mesh {effect.begin}
setvalues and commit them
foreach pass in effect {pass.begin}
end pass
end effect
draw mesh

##### Share on other sites
PKLoki    1492
I think you need to put drawing the mesh within the Pass begin/end calls, otherwise you're drawing nothing during each pass.

L
-

##### Share on other sites
Comboy    100
if i do so it says

Begin cannot be called again until End has been successfully called.

##### Share on other sites
Buckeye    10747
Quote:
 Original post by Comboyattempt using other effects simpler failed, Am i doing it in right order?first tech setforeach meshpart in mesh {effect.begin}setvalues and commit them foreach pass in effect {pass.begin}end passend effectdraw mesh

Looks like you need "effect.end" somewhere!

Post your actual code for a bit more help.

##### Share on other sites
Comboy    100
Quote:
 Original post by BuckeyeLooks like you need "effect.end" somewhere!Post your actual code for a bit more help.

I do it after ending the pass, you see
end pass
*end effect*
draw mesh

but ok i'll post the code

##### Share on other sites
Comboy    100
 private void DrawModel(Model m)        {            Matrix[] transforms = new Matrix[m.Bones.Count];            float aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height;            m.CopyAbsoluteBoneTransformsTo(transforms);            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),                aspectRatio, 1.0f, 10000.0f);            Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom), Vector3.Zero, Vector3.Up);            foreach (ModelMesh mesh in m.Meshes)            {                blinn.CurrentTechnique = blinn.Techniques[0];                foreach (ModelMeshPart part in mesh.MeshParts)                {                    blinn.Begin();                    foreach (EffectPass pass in this.blinn.CurrentTechnique.Passes)                    {                        pass.Begin();                        blinn.Parameters["ColorTexture"].SetValue(diffuseMap);                        blinn.CommitChanges();                        part.Effect = blinn;                        pass.End();                    }                    blinn.End();                }                mesh.Draw();            }        }

##### Share on other sites
KOzymandias    132
As I see it, you only set the effect, you do not actually draw the parts...

##### Share on other sites
Comboy    100
Quote:
 Original post by KOzymandiasAs I see it, you only set the effect, you do not actually draw the parts...

i set part.effect to blinn and i draw the mesh, did i miss something, there's no such a thing part.draw

##### Share on other sites
Buckeye    10747
foreach (ModelMeshPart part in mesh.MeshParts){  blinn.Begin();  foreach (EffectPass pass in this.blinn.CurrentTechnique.Passes)  {     pass.Begin();     blinn.Parameters["ColorTexture"].SetValue(diffuseMap);     blinn.CommitChanges();     part.Effect = blinn;     pass.End();  }  blinn.End();}

Drawing needs to take place within the effect loop, not after.

In the above loop, it doesn't appear that you draw anything. Look at the previous posts and see if the comments make sense now.

##### Share on other sites
Comboy    100
ok but if i put mesh.draw before effect.end it shows the error message

Begin cannot be called again until End has been successfully called.

##### Share on other sites
Buckeye    10747
I'm not familar with XNA but the XNA reference at Microsoft says, about ModelMesh.Draw:

"Draws all the ModelMeshPart objects in this mesh, using their current Effect settings."

I'm guessing but something like the following appears to be more appropriate.

blinn.Parameters["ColorTexture"].SetValue(diffuseMap);foreach (ModelMeshPart part in mesh.MeshParts){     part.Effect = blinn;}mesh.Draw();

##### Share on other sites
Comboy    100
this is from apply an effect sample from microsoft
protected override void Draw(GameTime gameTime)        {            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);            graphics.GraphicsDevice.RenderState.CullMode =                CullMode.CullClockwiseFace;            graphics.GraphicsDevice.VertexDeclaration =                    cubeVertexDeclaration;            graphics.GraphicsDevice.Indices = indexBuffer;            graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);            // The effect is a compiled effect created and compiled elsewhere            // in the application.            effect.Begin();            foreach (EffectPass pass in effect.CurrentTechnique.Passes)            {                pass.Begin();                graphics.GraphicsDevice.DrawIndexedPrimitives(                    PrimitiveType.TriangleList,                    0,                    0,                    cubeVertices.Length,                    0,                    12);                pass.End();            }            effect.End();            base.Draw(gameTime);        }    }

it use base.draw after ending the effect

##### Share on other sites
Buckeye    10747
That's an overloaded function for the Draw() method.

What an overloaded (perhaps it's "overridden" in XNA) method does is "intercept" the Draw method for your object.

In other words, when mesh.Draw() is called, it executes the overridden method.

Note in the example that base.Draw() is called after all the parts are drawn.

However, the info at Microsoft for ModelMeshPart states:

"It is not necessary to use this class directly. In advanced rendering scenarios, it is possible to draw using ModelMeshPart properties in combination with the vertex and index buffers on ModelMesh. However, in most cases, ModelMesh.Draw will be sufficient."

You may be making the process much more complicated than it needs to be.

##### Share on other sites
Comboy    100
i use (modelmesh mesh) and then mesh.draw
so i'm using the modelmesh.draw

and for (modelmeshpart part) there's nothing like part.draw

so i can't draw part and i can't draw mesh before ending the effect, that confuses me

on the other hand modelmesh doesn't have effect property, it's only with the modelmeshpart

##### Share on other sites
Buckeye    10747
Microsoft, on ModelMesh Draw: "Draws all the ModelMeshPart objects in this mesh, using their current Effect settings."

Why not give it a try?

2. set the effect parameters (like the texture)
4. set the effect for each part

Since you don't change the effect settings, you should only need to do the above once.

Then call mesh.Draw().

##### Share on other sites
Comboy    100
believe me or not I can't draw the mesh or meshparts before ending the effect.

someone please tell me if i'm doing it in right order, many days have passed and i still can't do anything,

##### Share on other sites
XVincentX    129
blinn = Content.Load<Effect>("blinn");

Mmm...shouldn't it be
blinn = Content.Load<Effect>("blinn.fx");

Or your file has got no extension?

##### Share on other sites
Ging    181
When loading via the Content Manager there's generally no need to specify file extensions (it's aware you're wanting an effect, so it'll look for the asset called "blinn" that's tagged as an Effect content type)

Here's some example code that uses a custom lighting shader in xna that works:

            foreach (ModelMesh modmesh in LAShip.Meshes)            {                foreach (Effect currenteffect in modmesh.Effects)                {                    currenteffect.CurrentTechnique = currenteffect.Techniques["Textured"];                    currenteffect.Parameters["xEnableLighting"].SetValue(true);                    currenteffect.Parameters["xLightDirection"].SetValue(new Vector3(0.5f, 1f, 1));                    currenteffect.Parameters["xAmbient"].SetValue(0.6f);                    currenteffect.Parameters["shininess"].SetValue(24.0f);                                        Matrix worldMatrix = Matrix.CreateScale(0.01f, 0.01f, 0.01f) * Matrix.CreateRotationX((float)Math.PI / 2) * Matrix.CreateRotationZ(3.0f * (float)Math.PI / 2) * Matrix.CreateTranslation(new Vector3(60 + shipx + shipxoffset, (heightMap.Height / 2) + 30 + shipy, 32.5f + shipz + shipzOffset));                    currenteffect.Parameters["xWorld"].SetValue(worldMatrix);                    currenteffect.Parameters["xView"].SetValue(viewMatrix);                    currenteffect.Parameters["xProjection"].SetValue(projectionMatrix);                    currenteffect.Parameters["xTexture"].SetValue(LAShipTexture[i]);                    i++;                }                modmesh.Draw();            }

##### Share on other sites
Buckeye    10747
Quote:
 Original post by Comboybelieve me or not I can't draw the mesh or meshparts before ending the effect.someone please tell me if i'm doing it in right order, many days have passed and i still can't do anything,

According to the info at Microsoft, you're not doing things in the right order. That info implies you needn't use effect.begin()/end() at all.

Have you tried it that way?

##### Share on other sites
Comboy    100
no effect end or begin, it returns error for pass.begin
so i just removed both the effect and pass begin and end
the code looks like this now

foreach (ModelMesh mesh in m.Meshes)            {             foreach (ModelMeshPart part in mesh.MeshParts)                {                 simpleEffect.Parameters["WorldViewProj"].SetValue(projection);                 simpleEffect.CommitChanges();                 part.Effect =simpleEffect;                }            mesh.Draw();            }

but it still shows nothing, note i have used another effect which has worldviewprojec

ging>
it shows declaring currenteffect and then set it values, is the effect from a file and shouldn't it be loaded? i mean how can the parameters come from,

##### Share on other sites
Buckeye    10747
I reviewed a couple of the CS examples at the Microsoft site. By the way, you could do that, too. You really should be reviewing the information at msdn yourself, Comboy.

Particularly because you're new at using effects, review the information at:

http://msdn2.microsoft.com/en-us/library/bb203926.aspx

and

http://msdn2.microsoft.com/en-us/library/bb203933.aspx

In one of the examples, the part effect is set like this, after the effect is loaded and the effect parameters set. There's no drawing involved in this!

public static void RemapModel(Model model, Effect effect){    foreach (ModelMesh mesh in model.Meshes)    {        foreach (ModelMeshPart part in mesh.MeshParts)        {            part.Effect = effect;        }    }}

The information at Microsoft also implies that you could just use:
mesh.Effect = effect;
You don't need to set the effect for each part if you're using the same effect throughout the mesh.

After the effect has been set and part.Effect set:

foreach (ModelMesh mesh in m.Meshes){    foreach (Effect effect in mesh.Effects)    {        mesh.Draw();    }}

##### Share on other sites
Comboy    100
i also read the pages from msdn, but for some reason they load very slowly and sometimes there's no vertical scroll bars on the pages,

there no effect property for mesh , it's only for meshpart, may be it's with older versions of DX and XNA.

and also i set effect for each part, the following two codes are equal:

foreach (ModelMesh mesh in model.Meshes)    {        foreach (ModelMeshPart part in mesh.MeshParts)        {            part.Effect = effect;        }    }

foreach (ModelMesh mesh in m.Meshes)            {             foreach (ModelMeshPart part in mesh.MeshParts)                {                 simpleEffect.Parameters["WorldViewProj"].SetValue(projection);                 simpleEffect.CommitChanges();                 part.Effect =simpleEffect;                }                     }

by the way, all this time i was looking for help from msdn or other sources, i try the msdn with ie and firefox but it's so hard to get all the page.
i think i'm doing it in right order and if there's gonna need of effect begin and end, it must end before mesh.draw. but if doesn't, mesh.draw is still necessary. everywhere i look there's a different way of applying the effect. i use several effects with different parameters but not success