Sign in to follow this  
Lio

XNA BasicEffect effect loops, basic questions

Recommended Posts

Hello I have some questions about how you do some drawing stuff in XNA. Ive made a simple Box class with contains a draw method like the one below. Should such a "object" have its own effect object? or should i do like in the Render() method? Ive just realized you can actualy change an effect like the world matrix etc with the effect.CommitChanges() method within the pass.Begin(). I reckon you would try and do as much rendering within one effect given that all objects use the same effect?
class Box
{
   public void Draw(){
      effect.Begin();
          
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
    
                device.DrawUserIndexedPrimitives(whatever);
          
                pass.End();
            };
          
       effect.End();
   }
}

public void Render(){
     effect.Begin();
          
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
   
                   box1.Draw();
                   box2.Draw();             
                          
                pass.End();
            };
          
      effect.End();
}

Also is there anyone who can shed some light on what the passes do? I understand you can set a current Technique and i did that from a effect file wich had a few different ones. But looking into the effect file i understood very little of it. It also seems a bit hard to dig up info about this whole concept of Effect's.
     
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                pass.End();
            };

Thank you for any reply.

Share this post


Link to post
Share on other sites
Quote:
Ive made a simple Box class with contains a draw method like the one below. Should such a "object" have its own effect object? or should i do like in the Render() method? Ive just realized you can actualy change an effect like the world matrix etc with the effect.CommitChanges() method within the pass.Begin(). I reckon you would try and do as much rendering within one effect given that all objects use the same effect?


Ok, so this is actually more of a design question than an actual technology question.
There are a couple of ways you could do this. I prefer the following methods.
1) A render able object should know how to render itself.. This means that you can have your renderer call Draw on all your objects.
So...


class Box
{
private Texture2D texture;
private Material material;
private Vector4 position; //This should be in some sort of base class.
public void Draw()
{
//Render the object
}
}




2) The other approach would be that an object contains just enough information for a renderer to render it. This means that an object should not contain any information that is not a part of it's model. The Renderer should be responsible for rendering objects, not objects being responsible for rendering themselves.


class Box : RenderableEntity
{
private string texture;
private Material material;
private Vector4 position; //This should be in some sort of base class.
}



I hope this helps.
Take care.

Share this post


Link to post
Share on other sites

Aside from the design consideration, the 2nd approach where you render the boxes all within the same effect would probably be more efficient. Switching effects is an expensive operation, so by rendering everything within one effect's Begin() and End() you can save quite a bit of performance (for more info on operations costs, go here).

Note that once you've called pass.Begin(), you'll have to call effect.CommitChanges() to actually apply changes to the effect (for example when setting other world matrices or textures for different models).

The effect passes primarily serve to divide your effect code into multiple chunks. This is useful for legacy reasons back when shaders could only support few instructions/registers per pass, but it also comes in handy for organizing your shader code. It's good form to support multiple passes in your C# rendering code, so you don't have to rework things if you switch to an effect with multiple passes, but typically they only see some specialist use.

Share this post


Link to post
Share on other sites
Thanks for the help from both of you, that cleared up some things.

I will read through the article you linked Remigius, tonight when i have some more time. It looks like a good read and some background stuff of what is actualy going on is always good :)

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