private void drawingAlgorithm(baseObject currentObj, GraphicsDevice device) { if (currentObj is meshObject)//Implying that there is rendering code { meshObject Obj = (meshObject)currentObj; Matrix worldMatrix = Obj.getWorldTransform(); Matrix cameraMatrix = this.mainCamera.getWorldTransform(); Obj.shader.Parameters["World"].SetValue(Matrix.Invert(worldMatrix)); Obj.shader.Parameters["View"].SetValue(cameraMatrix); Obj.shader.Parameters["Projection"].SetValue(this.mainCamera.projectionMatrix); Obj.shader.Parameters["WorldInverseTranspose"].SetValue(Matrix.Transpose(Matrix.Invert(worldMatrix))); //Give the video card the vertex buffer. device.SetVertexBuffer(Obj.objBuffer); foreach (EffectPass pass in Obj.shader.CurrentTechnique.Passes)//Run the shader. { pass.Apply(); device.DrawPrimitives(PrimitiveType.TriangleList, 0, Obj.triangles.Length); } } if (currentObj.getFirstChild() != null) { drawingAlgorithm(currentObj.getFirstChild(), device); } if (currentObj.getSibling() != null) { drawingAlgorithm(currentObj.getSibling(), device); } }
public void addChild(baseObject newChild) // the object this is called upon is assumed to be the parent. Also operation time is O(1). :) { newChild._parent = this; if (this.firstChild == null) { this.firstChild = newChild; } else { newChild.nextSibling = this.firstChild; this.firstChild.previousSibling = newChild; this.firstChild = newChild; } }
//This method tries to find a child object by it's name. Throws an exception if it's not there. public baseObject getChildByName(string name)//I have not yet tested this, so I'm not entirely sure if it works. { baseObject currentChild = this.firstChild; while (currentChild != null) { if (currentChild.Name == name) { return(currentChild); } else { currentChild = currentChild.nextSibling; } } throw new Exception("Could not find an object named '" + name + "' "); }
public Matrix getWorldTransform() { baseObject currentObj = this; Matrix currentTransform = Matrix.Identity; while (currentObj.GetType() != typeof(Scene)) { if (currentObj is gameObject) { gameObject obj = (gameObject)currentObj; currentTransform = obj.transform * currentTransform; } if (currentObj.GetType() == typeof(Scene)) { break; } currentObj = currentObj.Parent; } return(currentTransform); }
public void Remove()//Removes an object from the hierarchy { if (this.previousSibling != null) { //Is not the first child this.nextSibling.previousSibling = this.previousSibling; this.previousSibling.nextSibling = this.nextSibling; } else if (this.nextSibling != null) { //Is the first child, but not an only child this._parent.firstChild = this.nextSibling; this._parent = null; } else { this._parent.firstChild = null; } this.nextSibling = null; this.previousSibling = null; }