Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 17 Aug 2010
Offline Last Active Sep 13 2013 05:29 AM

Posts I've Made

In Topic: Questions on the "dynamic content loading" example on AppHub.

16 March 2012 - 06:44 AM

I guess, I am the champion in asking stupid questions Posted Image

In Topic: Why does RayIntersect return wrong values?

10 February 2012 - 01:56 PM

Figured out, the offset is caused by the viewport being drawn into a PictureBox in a WinForm. Still don't know how to fix it.

In Topic: Drawing BoundingBoxes with problems.

31 January 2012 - 05:17 PM

Thanks a lot Tsus thats what I wanted to know. The problem is, the basic hlsl-tutorials always deal with the basic effect or a single effect at once.
Now the BB and the model are drawn with correct shaders on them.

The Lightsource still screws up it seems to somehow change position when I strafe around my object. As seen here. But I guess I somehow messed up the shader code ;)

Thanks for taking the time and writing almost a tutorial :)

In Topic: Drawing BoundingBoxes with problems.

31 January 2012 - 09:07 AM

Hey thanks for the response.

I'm still learning, so could you tell me how I would declare and bind the vertices?

In Topic: Drawing BoundingBoxes with problems.

31 January 2012 - 06:23 AM

I'm not really sure why nobody can help me with that problem. Is it a stupid or a hard question?

Well after one week of messing around with that problem managed to finaly draw the bounding-boxes. The shader issue persists though. I will try to describe it more precise this time.

Here is my BasicModel.cs. It is a little messy but the functionality should be clear. It is responsible for drawing the Model and the corresponding bounding-box.
It contains a method called DrawModel. Here I have two possibilities implemented, drawing the model with basic effect and drawing it with a diffuse light (taken from here).
I also have a method DebugDraw which uses a basic effect in order to draw the bounding box.
(really sorry for posting the entire class, it is a mess I admit)

namespace XNA3Editor
    public class BasicModel : DrawableGameComponent
	    public Model model { get; protected set; }
	    public String name { get; set; }
	    public String id { get; set; }
	    public float yawAngle { get; set; }
	    public float pitchAngle { get; set; }
	    public float rollAngle { get; set; }
	    Matrix rotation = Matrix.Identity;
	    Vector3 vRotation = Vector3.Zero;
	    Matrix translation = Matrix.Identity;
	    Vector3 position = Vector3.Zero;
	    Vector3 direction = new Vector3(0, 0, 0);
	    private BasicEffect renderer;
	    private Effect effect;
	    private BoundingBox aabb { get; set; } //axis-aligned boundin box
	    private Camera camera;
	    private short[] indexData; //The index array used to render the AABB
	    private VertexPositionColor[] aabbVertices; //The AABB vertex array used for rendering
	    protected Matrix world = Matrix.Identity;
	    public BasicModel(Game game,Model m, String name) : base(game)
		    model = m;
		    this.name = name;
		    // Vertex declaration for rendering our 3D model.
		    Game.GraphicsDevice.VertexDeclaration = new VertexDeclaration(Game.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
		    camera = ((Editor)game).camera;
	    private void CreateAABB(Model model)
		    aabb = new BoundingBox();
		    foreach(ModelMesh mesh in model.Meshes)
			    //Create an array to store the vertex data.
			    VertexPositionNormalTexture[] modelVertices =
				    new VertexPositionNormalTexture[mesh.VertexBuffer.SizeInBytes /
			    //Get the models vertices
			    //Create a new array to store the position of each vertex
			    Vector3[] vertices = new Vector3[modelVertices.Length];
			    //Loop throught the vertices
			    for (int i = 0; i < vertices.Length;i++)
				    //Get the position of the vertex.
				    vertices[i] = modelVertices[i].Position;
			    //Create a AABB from the model's vertices.
			    aabb = BoundingBox.CreateMerged(aabb,
	    protected override void LoadContent()
		    effect = Game.Content.Load<Effect>("Effects/Diffuse");
		    //Create the bounding box from the model's vertices
	    public virtual void Update()
		    // Rotate model
		    rotation *= Matrix.CreateFromYawPitchRoll(yawAngle,
		    pitchAngle, rollAngle);		  
	    public override void Draw(GameTime gameTime)
		    //Create a new vertex declaration
		    Game.GraphicsDevice.VertexDeclaration =
			    new VertexDeclaration(Game.GraphicsDevice,
		    //Draw bounding box.
		    //Draw model.

	    private void DrawModel(Model model)
		    #region draw model with diffuse effect
		    Matrix[] transforms = new Matrix[model.Bones.Count];
		   // worldMatrix = Matrix.CreateWorld(position, rotationMatrix.Forward, rotationMatrix.Up);
		    // Use the DiffuseLight technique from Shader.fx. You can have multiple techniques in a effect file. If you don't specify
		    // what technique you want to use, it will choose the first one by default.
		    effect.CurrentTechnique = effect.Techniques["DiffuseLight"];
		    // Begin our effect

		    // A shader can have multiple passes, be sure to loop trough each of them.
		    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
			    // Begin current pass
			    foreach (ModelMesh mesh in model.Meshes)
				    foreach (ModelMeshPart part in mesh.MeshParts)
					    // calculate our worldMatrix..
					    //world = GetWorld() * mesh.ParentBone.Transform;
					    world =  Matrix.CreateWorld(position, rotation.Forward, rotation.Up);
					    // .. and pass it into our shader.
					    // To access a parameter defined in our shader file ( Shader.fx ), use effectObject.Parameters["variableName"]
					    Matrix worldInverse = Matrix.Invert(world);
					    Vector4 vLightDirection = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
					    effect.Parameters["matWorldViewProj"].SetValue(world * camera.view * camera.projection);
					    // Render our meshpart
					    Game.GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride);
					    Game.GraphicsDevice.Indices = mesh.IndexBuffer;
																	  part.BaseVertex, 0, part.NumVertices,
																	  part.StartIndex, part.PrimitiveCount);

			    // Stop current pass
		    // Stop using this effect
		    #region draw model using basic effect
		    //Create a rotation matrix from GameModel's rotation.
		    Matrix rotatioMatrix = Matrix.CreateFromYawPitchRoll(
		    //Create the world matrix from the GameModel's position and rotation.
		    world = Matrix.CreateWorld(position,rotation.Forward,
		    foreach(ModelMesh mesh in model.Meshes)
			    foreach(BasicEffect Beffect in mesh.Effects)
				    //Set effect lightning.
				    Beffect.PreferPerPixelLighting = true;
				    //Set effect matrices.
				    Beffect.World = world;
				    Beffect.View = camera.view;
				    Beffect.Projection = camera.projection;
	    public Matrix GetWorld()
		    return world;//* Matrix.CreateTranslation(position)
	    private void SetupRenderer()
		    //Create a new Basiceffect instance.
		    renderer = new BasicEffect(Game.GraphicsDevice,null);
		    //This lets you color the the AABB
		    renderer.VertexColorEnabled = true;
		    //Set renderer matrix.
		    renderer.World = world;
		    renderer.View = camera.view;
		    renderer.Projection = camera.projection;
	    private void DebugDraw(BoundingBox aabb, Color color)
		    //Setup the debug renderer.
		    //Create an array to store the AABB's vertices.
		    aabbVertices = new VertexPositionColor[8];
		    //Get an array of points that make up the corners of the AABB.
		    Vector3[] corners = aabb.GetCorners();
		    //Fill the AABB vertex array.
		    for (int i = 0; i < 8; i++ )
			    aabbVertices[i].Position = corners[i];
			    aabbVertices[i].Color = color;
		    //Create the index array.
		    indexData = new short[]
		    //Start drawing the AABB.
		    //Loop through  each effect pass.
		    foreach(EffectPass pass in renderer.CurrentTechnique.Passes)
			    //Start pass.
			    //Draw AABB.
			    //End pass.
		    //End rendering.
	    public Vector3 _position
		    get { return this.position; }
			    world = Matrix.Identity;
			    world *= Matrix.CreateTranslation(this.position = value); }
	    public Model _model
		    get { return model; }

Now my problem is, if I draw the model using basic effect, everything looks fine. The bounding box and the Model are drwan as expected. But if I try to apply the diffuse effect to the model, it is drawn wrong. Like there is no shader on it it just looks flat.

Now the thing is in the Draw Method I have a vertex declaration. If I comment that out and remove the bounding box he actually draws the model with diffuse light on it. But it still looks wrong.

I have no idea what is going wrong there! Maybe the vertex declaration somehow screws up between the basic effect for the BB and the diffuse effect on the model?

Help would be great,