Página 66
Transcript of Página 66
-
8/6/2019 Pgina 66
1/3
Pgina 66:
Finally, after the world, view, and projection matrices have been set to theeffect in the Draw() function, we will call SetEffectParameters() on our material:
Material.SetEffectParameters(effect);
A funo draw em CMODELS.cs deve ficar como:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;
namespace WindowsGame3{ publicclassCModel
{
publicVector3 Position { get; set; } publicVector3 Rotation { get; set; } publicVector3 Scale { get; set; } publicModel Model { get; privateset; } privateMatrix[] modelTransforms; privateGraphicsDevice graphicsDevice; privateBoundingSphere boundingSphere; publicMaterial Material { get; set; }
publicBoundingSphere BoundingSphere{
get{
// No need for rotation, as this is a sphere
Matrix worldTransform = Matrix.CreateScale(Scale)* Matrix.CreateTranslation(Position);
BoundingSphere transformed = boundingSphere;transformed = transformed.Transform(worldTransform);
return transformed;}
}
public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale,GraphicsDevice graphicsDevice)
{ this.Model = Model; this.Material = newMaterial();
modelTransforms = newMatrix[Model.Bones.Count];Model.CopyAbsoluteBoneTransformsTo(modelTransforms); this.Position = Position; this.Rotation = Rotation; this.Scale = Scale; this.graphicsDevice = graphicsDevice;
buildBoundingSphere();generateTags();
}
publicvoid Draw(Matrix View, Matrix Projection, Vector3 CameraPosition){
// Calculate the base transformation by combining
// translation, rotation, and scaling Matrix baseWorld = Matrix.CreateScale(Scale)
-
8/6/2019 Pgina 66
2/3
* Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X,Rotation.Z)* Matrix.CreateTranslation(Position);
foreach (ModelMesh mesh in Model.Meshes){
Matrix localWorld = modelTransforms[mesh.ParentBone.Index] *baseWorld;
foreach (ModelMeshPart meshPart in mesh.MeshParts){
Effect effect = meshPart.Effect; if(effect isBasicEffect)
{((BasicEffect)effect).World = localWorld;((BasicEffect)effect).View = View;((BasicEffect)effect).Projection = Projection;((BasicEffect)effect).EnableDefaultLighting();Material.SetEffectParameters(effect);
} else
{setEffectParameter(effect, "World", localWorld);setEffectParameter(effect, "View", View);setEffectParameter(effect, "Projection", Projection);setEffectParameter(effect, "CameraPosition", CameraPosition);
}}
mesh.Draw();
}}
privatevoid buildBoundingSphere()
{ BoundingSphere sphere = newBoundingSphere(Vector3.Zero, 0); // Merge all the model's built in bounding spheres foreach (ModelMesh mesh in Model.Meshes)
{ BoundingSphere transformed =mesh.BoundingSphere.Transform(modelTransforms[mesh.ParentBone.Index]);
sphere = BoundingSphere.CreateMerged(sphere, transformed);}
this.boundingSphere = sphere;}
privatevoid generateTags(){
foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) if(part.Effect isBasicEffect)
{ BasicEffect effect = (BasicEffect)part.Effect; MeshTag tag = newMeshTag(effect.DiffuseColor, effect.Texture,
effect.SpecularPower);part.Tag = tag;
}}
// Store references to all of the model's current effects publicvoid CacheEffects()
{ foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts)
-
8/6/2019 Pgina 66
3/3
((MeshTag)part.Tag).CachedEffect = part.Effect;}
// Restore the effects referenced by the model's cache publicvoid RestoreEffects()
{ foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) if(((MeshTag)part.Tag).CachedEffect != null)
part.Effect = ((MeshTag)part.Tag).CachedEffect;}
publicvoid SetModelEffect(Effect effect, bool CopyEffect){
foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts)
{ Effect toSet = effect; // Copy the effect if necessary if(CopyEffect)
toSet = effect.Clone(); MeshTag tag = ((MeshTag)part.Tag); // If this ModelMeshPart has a texture, set it to the effect if(tag.Texture != null)
{setEffectParameter(toSet, "BasicTexture", tag.Texture);setEffectParameter(toSet, "TextureEnabled", true);
} else
setEffectParameter(toSet, "TextureEnabled", false); // Set our remaining parameters to the effect
setEffectParameter(toSet, "DiffuseColor", tag.Color);setEffectParameter(toSet, "SpecularPower", tag.SpecularPower);
part.Effect = toSet;}}
// Sets the specified effect parameter to the given effect, if it // has that parameter void setEffectParameter(Effect effect, string paramName, object val)
{ if(effect.Parameters[paramName] == null) return; if(val isVector3)
effect.Parameters[paramName].SetValue((Vector3)val); elseif(val isbool)
effect.Parameters[paramName].SetValue((bool)val);
elseif(val isMatrix)effect.Parameters[paramName].SetValue((Matrix)val);
elseif(val isTexture2D)effect.Parameters[paramName].SetValue((Texture2D)val);
}}
}