Hi Norbo, happy new year! lol, im still driving my self nuts....
ive taken a diffrent approach to not use the model drawer in BEPU and to code from scratch (to a point...)
I was wondering if you could take a look at these couple of Methods, because i am getting the same result, where the static mesh will load and its physics work, however it will not render in the custom effect's texture... tell me to bug off at any point
if i cant get this working at this point im going to start a new project and create a model dll drawer and pump it into that, because if i can just draw it with a texture, the physics are there and my job will b complete :\
Its really messy i know, im sorry but any thing commented out has been tried or tried in combination,
i was trying to draw using primitives, then tried another approach by loading the model into a second Model variable and then drawing the model as normal, while BEPU took care for the StaticMesh's physics, thus "overlapping" them
My Vertex buffer and everything seems to work and populate propelry, however ushlly all my texturecorrdinates are 0,0,0 :\
once again to many hours programming, and my eyes are far to buggy :\
Norbo, god knows this aint ur job, but ur always there for a quick response and ur great at it :\
Hope u have a drink on me tonight!, im going out now, cuz i need some R & R :\
Thanks again !
Code: Select all
internal void InitEffect()
{
staticmesheffect = Content.Load<Effect>("InstancedEffect3");
//staticmesheffect.Parameters["LightDirection1"].SetValue(Vector3.Normalize(new Vector3(.8f, -1.5f, -1.2f)));
//staticmesheffect.Parameters["DiffuseColor1"].SetValue(new Vector3(.66f, .66f, .66f));
//staticmesheffect.Parameters["LightDirection2"].SetValue(Vector3.Normalize(new Vector3(-.8f, 1.5f, 1.2f)));
//staticmesheffect.Parameters["DiffuseColor2"].SetValue(new Vector3(.3f, .3f, .5f));
//staticmesheffect.Parameters["AmbientAmount"].SetValue(.5f);
staticmesheffect.Parameters["xTexture"].SetValue(Content.Load<Texture2D>("Textures\\tex1"));
staticmesheffect.CurrentTechnique = staticmesheffect.Techniques[0];
/// hmmmm m
worldTransformsParameter = staticmesheffect.Parameters["xWorldViewProjection"];
//textureIndicesParameter = staticmesheffect.Parameters["TextureIndices"];
//viewParameter = staticmesheffect.Parameters["View"];
//projectionParameter = staticmesheffect.Parameters["Projection"];
textureIndicesarray[0] = 1;
foreach (ModelMesh mesh in this.playgroundModelModel.Meshes)
foreach (ModelMeshPart meshPart in mesh.MeshParts)
meshPart.Effect = this.staticmesheffect.Clone();
CreateVertexBuffer();
}
private void CreateVertexBuffer()
{
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
}
);
vertexBuffer = new VertexBuffer(this.GraphicsDevice, vertexDeclaration, this.verticesarray.Length, BufferUsage.None);
VertexPositionTexture[] objectVertices = new VertexPositionTexture[this.staticmeshPositionNormalTextures.Count];
for (int x = 0; x < objectVertices.Length; x++)
{
//objectVertices[x] = new VertexPositionTexture(this.staticmeshPositionNormalTextures[x].Position, verticesarray[x].TextureCoordinate);
//objectVertices[x] = new VertexPositionTexture(new Vector3(this.staticMeshVertices[x].X, this.staticMeshVertices[x].Y, this.staticMeshVertices[x].Z), verticesarray[x].TextureCoordinate);
objectVertices[x] = new VertexPositionTexture(this.staticmeshPositionNormalTextures[x].Position, this.staticmeshPositionNormalTextures[x].TextureCoordinate);
}
vertexBuffer.SetData<VertexPositionTexture>(objectVertices);
this.GraphicsDevice.SetVertexBuffer(vertexBuffer);
}
Effect staticmesheffect;
private readonly Matrix[] worldTransforms = new Matrix[1];
internal void DrawStaticMesh()
{
worldTransforms[0] = Matrix.Identity;
this.GraphicsDevice.SetVertexBuffers(bindings);
this.GraphicsDevice.Indices = indexBuffer;
//worldTransformsParameter.SetValue(worldTransforms);
//textureIndicesParameter.SetValue(textureIndices);
RasterizerState rasterizerState1 = new RasterizerState();
rasterizerState1.CullMode = CullMode.None;
this.GraphicsDevice.RasterizerState = rasterizerState1;
staticmesheffect.Parameters["xWorldViewProjection"].SetValue(WorldMatrix);
staticmesheffect.Parameters["xTexture"].SetValue(Content.Load<Texture2D>("Textures\\tex1"));
//staticmesheffect.Parameters["View"].SetValue(this.viewMatrix);
//staticmesheffect.Parameters["Projection"].SetValue(this.ImaginePlayer.Projection);
//staticmesheffect.Parameters["TextureIndices"].SetValue(textureIndicesarray);
//staticmesheffect.Parameters["WorldTransforms"].SetValue(worldTransforms);
Matrix[] playgroundmodeltransforms = new Matrix[this.playgroundModelModel.Bones.Count];
this.playgroundModelModel.CopyAbsoluteBoneTransformsTo(playgroundmodeltransforms);
foreach(EffectPass effectpass in staticmesheffect.CurrentTechnique.Passes)
{
effectpass.Apply();
}
foreach (ModelMesh mesh in this.playgroundModelModel.Meshes)
{
foreach (Effect currentEffect in mesh.Effects)
{
Matrix worldMatrix = playgroundmodeltransforms[mesh.ParentBone.Index] * Matrix.Identity;
currentEffect.CurrentTechnique = currentEffect.Techniques[0];
currentEffect.Parameters["xWorldViewProjection"].SetValue(worldMatrix);
currentEffect.Parameters["xTexture"].SetValue(Content.Load<Texture2D>("Textures\\tex1"));
}
mesh.Draw();
}
//this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, staticMeshVertices.Length, 0, staticMeshIndices.Length / 3);
}
private void InitializeTransform()
{
//float tilt = (float)System.Math.PI / 8.0f;
// Use the world matrix to tilt the cube along x and y axes.
//World = Matrix.CreateRotationX(tilt) *
// Matrix.CreateRotationY(tilt);
//View = Matrix.CreateLookAt(new Vector3(0, 0, 10),
// Vector3.Zero, Vector3.Up);
//Projection = Matrix.CreatePerspectiveFieldOfView(
// (float)System.Math.PI / 4.0f, // 2 PI Radians is 360 degrees,
// so this is 45 degrees.
//(float)this.MAINGAME.GraphicsDevice.Viewport.Width /
//(float)this.MAINGAME.GraphicsDevice.Viewport.Height,
//1.0f, 100.0f);
World = Matrix.Identity * Matrix.CreateTranslation(Vector3.Zero);
WorldMatrix = World * this.viewMatrix * this.ImaginePlayer.Projection;
}
Matrix World;
internal void LoadStaticMesh()
{
//Model temp = Content.Load<Model>("CharacterControllerTestTerrain");
var t = Content.Load<Model>("CharacterControllerTestTerrain");
this.playgroundModelModel = t;
//playgroundModel = (BEPUphysics.Collidables.StaticMesh)temp;
//This load method wraps the TriangleMesh.GetVerticesAndIndicesFromModel method
//to output vertices of type StaticTriangleGroupVertex instead of TriangleMeshVertex or simply Vector3.
BEPUphysics.DataStructures.TriangleMesh.GetVerticesAndIndicesFromModel(t, out staticMeshVertices, out staticMeshIndices);
playgroundModel = new BEPUphysics.Collidables.StaticMesh(this.staticMeshVertices, this.staticMeshIndices, new AffineTransform(new Vector3(.01f, .01f, .01f), Quaternion.Identity, new Vector3(0, 0, 0)));
var staticMesh = new BEPUphysics.Collidables.StaticMesh(staticMeshVertices, staticMeshIndices, new AffineTransform(new Vector3(0.1f,0.1f, 0.1f), Quaternion.Identity, new Vector3(0, 0, 0)));
staticMesh.Sidedness = BEPUphysics.CollisionShapes.ConvexShapes.TriangleSidedness.Counterclockwise;
GetVertexData(staticmeshPositionNormalTextures, staticMeshUShortIndices, (ushort)staticMeshUShortIndices.Count);
this.indicesarray = new ushort[staticMeshVertices.Length];
this.verticesarray = new VertexPositionNormalTexture[staticMeshIndices.Length];
var newVertices = new VertexPositionNormalTexture[verticesarray.Length + vertexList.Count];
this.verticesarray.CopyTo(newVertices, 0);
vertexList.CopyTo(newVertices, this.verticesarray.Length);
this.verticesarray = newVertices;
var newIndices = new ushort[this.indicesarray.Length + staticMeshUShortIndices.Count];
this.staticMeshUShortIndices.CopyTo(newIndices, 0);
indexList.CopyTo(newIndices, indicesarray.Length);
indicesarray = newIndices;
instancingIndices = new float[this.staticMeshVertices.Length];
var newInstancingIndices = new float[instancingIndices.Length + vertexList.Count];
instancingIndices.CopyTo(newInstancingIndices, 0);
for (int i = instancingIndices.Length; i < newInstancingIndices.Length; i++)
newInstancingIndices[i] = 1; // length of how many objects we have should only b one for testing because of just terrain
instancingIndices = newInstancingIndices;
this.vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
}
);
vertexBuffer = new VertexBuffer(this.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, verticesarray.Length, BufferUsage.WriteOnly);
vertexBuffer.SetData(verticesarray);
instancingBuffer = new VertexBuffer(this.GraphicsDevice, this.vertexDeclaration, instancingIndices.Length, BufferUsage.WriteOnly);
instancingBuffer.SetData(instancingIndices);
bindings = new VertexBufferBinding[] { vertexBuffer, instancingBuffer };
indexBuffer = new IndexBuffer(this.GraphicsDevice, IndexElementSize.SixteenBits, indicesarray.Length, BufferUsage.WriteOnly);
indexBuffer.SetData(indicesarray);
vertexList.Clear();
indexList.Clear();
this.PhysicsSpace.Add(staticMesh);
//this.BuildingDrawer.Add(staticMesh);
InitEffect();
}