public void AddVertex(ModelVertex v) { ushort index; if(!Vertices.TryGetValue(v, out index)) Vertices.Add(v, index= (ushort)Vertices.Count); Primitives.Last().Indices.Add(index); }
private void DrawTranslateAndRotate(float dispX, float dispY, float dispZ, float rotX, float rotY, float rotZ, ModelPart part) { float cosX = (float)Math.Cos(-rotX), sinX = (float)Math.Sin(-rotX); float cosY = (float)Math.Cos(-rotY), sinY = (float)Math.Sin(-rotY); float cosZ = (float)Math.Cos(-rotZ), sinZ = (float)Math.Sin(-rotZ); VertexP3fT2fC4b vertex = default(VertexP3fT2fC4b); VertexP3fT2fC4b[] finVertices = game.ModelCache.vertices; for (int i = 0; i < part.Count; i++) { ModelVertex v = vertices[part.Offset + i]; // Prepare the vertex coordinates for rotation v.X -= part.RotX; v.Y -= part.RotY; v.Z -= part.RotZ; float t = 0; // Rotate locally. if (Rotate == RotateOrder.ZYX) { t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX } else if (Rotate == RotateOrder.XZY) { t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY } else if (Rotate == RotateOrder.YZX) { t = cosY * v.X - sinY * v.Z; v.Z = sinY * v.X + cosY * v.Z; v.X = t; // Inlined RotY t = cosZ * v.X + sinZ * v.Y; v.Y = -sinZ * v.X + cosZ * v.Y; v.X = t; // Inlined RotZ t = cosX * v.Y + sinX * v.Z; v.Z = -sinX * v.Y + cosX * v.Z; v.Y = t; // Inlined RotX } vertex.X = v.X + part.RotX; vertex.Y = v.Y + part.RotY; vertex.Z = v.Z + part.RotZ; // Translate part vertex.X += dispX; vertex.Y += dispY; vertex.Z += dispZ; vertex.Col = cols[i >> 2]; vertex.U = (v.U & UVMask) * uScale - (v.U >> UVMaxShift) * 0.01f * uScale; vertex.V = (v.V & UVMask) * vScale - (v.V >> UVMaxShift) * 0.01f * vScale; finVertices[index++] = vertex; } }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; head = BuildBox(MakeBoxBounds(-4, 16, -14, 4, 24, -8).TexOrigin(0, 0).RotOrigin(0, 20, -6)); rightHorn = BuildBox(MakeBoxBounds(-5, 22, -13, -4, 25, -12).TexOrigin(22, 0).RotOrigin(0, 20, -6)); leftHorn = BuildBox(MakeBoxBounds(4, 22, -13, 5, 25, -12).TexOrigin(22, 0).RotOrigin(0, 20, -6)); leftLegFront = BuildBox(MakeBoxBounds(-6, 0, -8, -2, 12, -4).TexOrigin(0, 16).RotOrigin(0, 12, -5)); rightLegFront = BuildBox(MakeBoxBounds(2, 0, -8, 6, 12, -4).TexOrigin(0, 16).RotOrigin(0, 12, -5)); leftLegBack = BuildBox(MakeBoxBounds(-6, 0, 5, -2, 12, 9).TexOrigin(0, 16).RotOrigin(0, 12, 7)); rightLegBack = BuildBox(MakeBoxBounds(2, 0, 5, 6, 12, 9).TexOrigin(0, 16).RotOrigin(0, 12, 7)); torso = BuildRotatedBox(MakeBoxBounds(-6, 12, -8, 6, 22, 10).TexOrigin(18, 4)); udder = BuildRotatedBox(MakeBoxBounds(-2, 11, 4, 2, 12, 10).TexOrigin(52, 0)); }
private void DrawTranslate(float dispX, float dispY, float dispZ, ModelPart part) { VertexP3fT2fC4b vertex = default(VertexP3fT2fC4b); VertexP3fT2fC4b[] finVertices = game.ModelCache.vertices; for (int i = 0; i < part.Count; i++) { ModelVertex v = vertices[part.Offset + i]; vertex.X = v.X + dispX; vertex.Y = v.Y + dispY; vertex.Z = v.Z + dispZ; vertex.Col = cols[i >> 2]; vertex.U = (v.U & UVMask) * uScale - (v.U >> UVMaxShift) * 0.01f * uScale; vertex.V = (v.V & UVMask) * vScale - (v.V >> UVMaxShift) * 0.01f * vScale; finVertices[index++] = vertex; } }
internal void Render(IRenderer renderer, GcmfRenderContext context) { // Convert GcmfVertex list to ModelVertex list ModelVertex[] modelVertices = new ModelVertex[Items.Count]; for (int i = 0; i < Items.Count; i++) { GcmfVertex gcmfVtx = Items[i]; // Copy the data from the GCMF vertex to the ModelVertex ModelVertex modelVtx = new ModelVertex() { Position = gcmfVtx.Position, Normal = gcmfVtx.Normal, VertexColor = gcmfVtx.VertexColor, PrimaryTexCoord = gcmfVtx.PrimaryTexCoord }; // Apply transformation matrices to the vertex if (gcmfVtx.TransformMatrixRef != null && gcmfVtx.TransformMatrixRef.Value != 0) { if (gcmfVtx.TransformMatrixRef.Value > 24 || (gcmfVtx.TransformMatrixRef % 3) != 0) { throw new InvalidGmaFileException("Invalid TransformMatrixRef for the transform matrix rendering."); } int rIdx = (gcmfVtx.TransformMatrixRef.Value / 3) - 1; if (context.TransformMatrixIdxs[rIdx] == byte.MaxValue) { throw new InvalidGmaFileException("The transform matrix associated to the matrix reference is not defined."); } // Transform the position and normal vectors according to the transform matrix GcmfTransformMatrix tMtx = context.Gcmf.TransformMatrices[context.TransformMatrixIdxs[rIdx]]; modelVtx.Position = tMtx.TransformPosition(modelVtx.Position); if (modelVtx.Normal != null) { modelVtx.Normal = tMtx.TransformNormal(modelVtx.Normal.Value); } } modelVertices[i] = modelVtx; } // Write triangle strip renderer.WriteTriangleStrip(modelVertices); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * 5]; Head = BuildBox(MakeBoxBounds(-4, 4, -11, 4, 12, -3) .TexOrigin(32, 4) .RotOrigin(0, 8, -3)); Link = BuildBox(MakeBoxBounds(-3, 5, 3, 3, 11, -3) .TexOrigin(0, 0)); End = BuildBox(MakeBoxBounds(-5, 4, 3, 5, 12, 15) .TexOrigin(0, 12)); LeftLeg = BuildBox(MakeBoxBounds(-19, 7, -1, -3, 9, 1) .TexOrigin(18, 0) .RotOrigin(-3, 8, 0)); RightLeg = BuildBox(MakeBoxBounds(3, 7, -1, 19, 9, 1) .TexOrigin(18, 0) .RotOrigin(3, 8, 0)); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; snout = BuildRotatedBox(MakeBoxBounds(-3, 0, -19, 3, 3, -12).TexOrigin(30, 0).RotOrigin(0, 2, -13)); head = BuildRotatedBox(MakeBoxBounds(-4, 0, -12, 4, 4, -7).TexOrigin(0, 23).RotOrigin(0, 2, -7)); frontTail = BuildRotatedBox(MakeBoxBounds(-4, 0, 9, 4, 4, 14).TexOrigin(24, 23).RotOrigin(0, 2, 9)); midTail = BuildRotatedBox(MakeBoxBounds(-3, 0, 13, 3, 3, 18).TexOrigin(30, 10).RotOrigin(0, 2, 13)); backTail = BuildRotatedBox(MakeBoxBounds(-2, 0, 17, 2, 2, 22).TexOrigin(48, 0).RotOrigin(0, 1, 17)); body = BuildRotatedBox(MakeBoxBounds(-5, 0, -8, 5, 5, 10).TexOrigin(0, 0)); leftLegFront = BuildBox(MakeBoxBounds(-8, 0, -7, -5, 3, -4).MirrorX().TexOrigin(48, 7).RotOrigin(-6, 2, -5)); leftLegBack = BuildBox(MakeBoxBounds(-8, 0, 6, -5, 3, 9).MirrorX().TexOrigin(48, 7).RotOrigin(-6, 2, 7)); rightLegFront = BuildBox(MakeBoxBounds(5, 0, -7, 8, 3, -4).TexOrigin(48, 7).RotOrigin(6, 2, -5)); rightLegBack = BuildBox(MakeBoxBounds(5, 0, 6, 8, 3, 9).TexOrigin(48, 7).RotOrigin(6, 2, 7)); }
public PartLight(Color Color, Vector3 Bias = new Vector3()) { _vertices = new ModelVertex[4]; _vertices[0] = new ModelVertex(Bias, Color); _vertices[0].TextureCoordinate = new Vector2(0, 0); _vertices[1] = new ModelVertex(Bias, Color); _vertices[1].TextureCoordinate = new Vector2(0, 1); _vertices[2] = new ModelVertex(Bias, Color); _vertices[2].TextureCoordinate = new Vector2(1, 1); _vertices[3] = new ModelVertex(Bias, Color); _vertices[3].TextureCoordinate = new Vector2(1, 0); _indices = new int[] { 2, 0, 3, 2, 1, 0 }; SetIndices(_indices); SetVertices(_vertices); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; bottom = BuildRotatedBox(MakeRotatedBoxBounds(-8, 0, -8, 8, 1, 8).TexOrigin(0, 0)); left = BuildRotatedBox(MakeRotatedBoxBounds(-7, 1, -7, -6, 6, 7).TexOrigin(34, 14)); right = BuildRotatedBox(MakeRotatedBoxBounds(6, 1, -7, 7, 6, 7).TexOrigin(34, 14)); center = BuildRotatedBox(MakeRotatedBoxBounds(-6, 2, -6, 6, 7, 5).TexOrigin(0, 17)); top = BuildRotatedBox(MakeRotatedBoxBounds(-2, 7, -3, 2, 8, 3).TexOrigin(46, 14)); topLeft = BuildRotatedBox(MakeRotatedBoxBounds(-5, 7, 0, -2, 8, 3).TexOrigin(56, 14)); topRight = BuildRotatedBox(MakeRotatedBoxBounds(2, 7, 0, 5, 8, 3).TexOrigin(56, 14)); lineLeft = BuildRotatedBox(MakeRotatedBoxBounds(-4, 7, -7, -3, 8, 0).TexOrigin(60, 0)); lineRight = BuildRotatedBox(MakeRotatedBoxBounds(3, 7, -7, 4, 8, 0).TexOrigin(60, 0)); back = BuildBox(MakeBoxBounds(-6, 1, 5, 6, 5, 6).TexOrigin(34, 9)); front = BuildBox(MakeBoxBounds(-4, 2, -7, 4, 6, -6).TexOrigin(46, 21)); tray = BuildBox(MakeBoxBounds(-6, 4, 4, 6, 12, 5).TexOrigin(34, 0).RotOrigin(0, 4, 4)); }
public CustomModel(List <RawPart> inputParts) : base() { RawParts = inputParts; vertices = new ModelVertex[RawParts.Count * boxVertices]; parts = new List <ModelPart>(); foreach (RawPart raw in RawParts) { BoxDescription desc = MakeBoxBounds(raw.X1, raw.Y1, raw.Z1, raw.X2, raw.Y2, raw.Z2); desc.SetTexOrigin(raw.TexX, raw.TexY); ModelPart part; if (raw.Rotated) { part = BuildRotatedBox(desc); } else { part = BuildBox(desc); } parts.Add(part); } /* * * Set.Head = BuildBox( MakeBoxBounds( -4, 24, -4, 4, 32, 4 ) * .SetTexOrigin( 0, 0 ) ); * Set.Torso = BuildBox( MakeBoxBounds( -4, 12, -2, 4, 24, 2 ) * .SetTexOrigin( 16, 16 ) ); * Set.LeftLeg = BuildBox( MakeBoxBounds( 0, 0, -2, -4, 12, 2 ) * .SetTexOrigin( 0, 16 ) ); * Set.RightLeg = BuildBox( MakeBoxBounds( 0, 0, -2, 4, 12, 2 ). * SetTexOrigin( 0, 16 ) ); * Set.Hat = BuildBox( MakeBoxBounds( -4, 24, -4, 4, 32, 4 ) * .SetTexOrigin( 32, 0 ) * .SetModelBounds( -4.5f, 23.5f, -4.5f, 4.5f, 32.5f, 4.5f ) ); * Set.LeftArm = BuildBox( MakeBoxBounds( -4, 12, -2, -8, 24, 2 ) * .SetTexOrigin( 40, 16 ) ); * Set.RightArm = BuildBox( MakeBoxBounds( 4, 12, -2, 8, 24, 2 ) * .SetTexOrigin( 40, 16 ) ); */ }
} //UsesHumanSkin = true; } public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; BoxDesc headDesc, torsoDesc, leftUpperArmDesc, rightUpperArmDesc, leftLowerArmDesc, rightLowerArmDesc, leftUpperLegDesc, rightUpperLegDesc, leftLowerLegDesc, rightLowerLegDesc; headDesc = MakeBoxBounds(-4, 24, -4, 4, 32, 4).RotOrigin(0, 24, 0); torsoDesc = MakeBoxBounds(-4, 12, -2, 4, 24, 2).RotOrigin(0, 12, 0); rightUpperArmDesc = MakeBoxBounds(4, 18, -2, 8, 24, 2).RotOrigin(6, 22, 0); rightLowerArmDesc = MakeBoxBounds(4, 12, -2, 8, 18, 2).RotOrigin(4, 18, 0); rightUpperLegDesc = MakeBoxBounds(0, 6, -2, 4, 12, 2).RotOrigin(2, 12, 0); rightLowerLegDesc = MakeBoxBounds(0, 0, -2, 4, 6, 2).RotOrigin(2, 6, 0); leftUpperArmDesc = MakeBoxBounds(-8, 18, -2, -4, 24, 2).RotOrigin(-6, 22, 0); leftLowerArmDesc = MakeBoxBounds(-8, 12, -2, -4, 18, 2).RotOrigin(-4, 18, 0); leftUpperLegDesc = MakeBoxBounds(-4, 6, -2, 0, 12, 2).RotOrigin(-2, 12, 0); leftLowerLegDesc = MakeBoxBounds(-4, 0, -2, 0, 6, 2).RotOrigin(-2, 6, 0); head = BuildBox(headDesc.TexOrigin(0, 0)); torso = BuildBox(torsoDesc.TexOrigin(0, 32)); rightUpperArm = BuildBox(rightUpperArmDesc.TexOrigin(32, 0)); rightLowerArm = BuildBox(rightLowerArmDesc.TexOrigin(32, 10)); rightUpperLeg = BuildBox(rightUpperLegDesc.TexOrigin(32, 20)); rightLowerLeg = BuildBox(rightLowerLegDesc.TexOrigin(32, 30)); leftUpperArm = BuildBox(leftUpperArmDesc.TexOrigin(48, 0)); leftLowerArm = BuildBox(leftLowerArmDesc.TexOrigin(48, 10)); leftUpperLeg = BuildBox(leftUpperLegDesc.TexOrigin(48, 20)); leftLowerLeg = BuildBox(leftLowerLegDesc.TexOrigin(48, 30)); hat = BuildBox(headDesc.TexOrigin(0, 16).Expand(0.5f)); jacket = BuildBox(torsoDesc.TexOrigin(0, 48).Expand(0.5f)); rightUpperSleeve = BuildBox(rightUpperArmDesc.TexOrigin(64, 0).Expand(0.5f)); rightLowerSleeve = BuildBox(rightLowerArmDesc.TexOrigin(64, 10).Expand(0.5f)); rightUpperPant = BuildBox(rightUpperLegDesc.TexOrigin(64, 20).Expand(0.5f)); rightLowerPant = BuildBox(rightLowerLegDesc.TexOrigin(64, 30).Expand(0.5f)); leftUpperSleeve = BuildBox(leftUpperArmDesc.TexOrigin(80, 0).Expand(0.5f)); leftLowerSleeve = BuildBox(leftLowerArmDesc.TexOrigin(80, 10).Expand(0.5f)); leftUpperPant = BuildBox(leftUpperLegDesc.TexOrigin(80, 20).Expand(0.5f)); leftLowerPant = BuildBox(leftLowerLegDesc.TexOrigin(80, 30).Expand(0.5f)); cape = BuildBox(MakeBoxBounds(-6, 1, 2, 6, 24, 3).TexOrigin(24, 40).RotOrigin(0, 23, 3)); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * 6]; Head = BuildBox(MakeBoxBounds(-4, 24, -4, 4, 32, 4) .TexOrigin(0, 0) .RotOrigin(0, 24, 0)); LeftLeg = BuildBox(MakeBoxBounds(-1, 0, -1, -3, 12, 1) .TexOrigin(0, 16) .RotOrigin(0, 12, 0)); RightLeg = BuildBox(MakeBoxBounds(1, 0, -1, 3, 12, 1) .TexOrigin(0, 16) .RotOrigin(0, 12, 0)); LeftArm = BuildBox(MakeBoxBounds(-4, 12, -1, -6, 24, 1) .TexOrigin(40, 16) .RotOrigin(-5, 23, 0)); RightArm = BuildBox(MakeBoxBounds(4, 12, -1, 6, 24, 1) .TexOrigin(40, 16) .RotOrigin(5, 23, 0)); Torso = BuildBox(MakeBoxBounds(-4, 12, -2, 4, 24, 2) .TexOrigin(16, 16)); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * 5]; LeftBreast = BuildBox(MakeBoxBounds(-4, 18, -3, -1, 21, -2) .TexOrigin(24, 21) .RotOrigin(-2, 20, -2) .Expand(0.25f)); RightBreast = BuildBox(MakeBoxBounds(1, 18, -3, 4, 21, -2) .TexOrigin(19, 21) .RotOrigin(2, 20, -2) .Expand(0.25f)); Middle = BuildBox(MakeBoxBounds(-2, 18, -3, 2, 21, -1) .TexOrigin(20, 20) .RotOrigin(0, 20, -1)); //.Expand(-0.25f)); LeftGlute = BuildBox(MakeBoxBounds(-4, 11, 2, 0, 14, 3) .TexOrigin(26, 28)); //.Expand(0.5f)); RightGlute = BuildBox(MakeBoxBounds(0, 11, 2, 4, 14, 3) .TexOrigin(30, 27)); //.Expand(0.5f)); }
/// <summary>Builds a box model assuming the follow texture layout:<br/> /// let SW = sides width, BW = body width, BH = body height<br/> /// ┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ <br/> /// ┃┈┈┈┈┈SW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┈┃┈┈┈┈┈┈┈┈┈┈┈┈┃ <br/> /// ┃S┈┈┈┈┈┈┈┈┈┈┈S┃S┈┈┈┈front┈┈┈S┃S┈┈┈back┈┈┈┈S┃┈┈┈┈┈┈┈┈┈┈┈┈┃ <br/> /// ┃W┈┈┈┈┈┈┈┈┈W┃W┈┈┈┈tex┈┈┈W┃W┈┈┈┈tex┈┈┈┈W┃┈┈┈┈┈┈┈┈┈┈┈┈┃ <br/> /// ┃┈┈┈┈┈SW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┈┃┈┈┈┈┈┈┈┈┈┈┈┈┃ <br/> /// ┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━━━━━┃ <br/> /// ┃┈┈┈┈┈SW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┃┈┈┈┈┈SW┈┈┈┈┈┈┃┈┈┈┈┈SW┈┈┈┈┃ <br/> /// ┃B┈┈┈left┈┈┈┈┈B┃B┈┈bottom┈┈B┃B┈┈┈right┈┈┈┈B┃B┈┈┈┈top┈┈┈B┃ <br/> /// ┃H┈┈┈tex┈┈┈┈┈H┃H┈┈tex┈┈┈┈┈H┃H┈┈┈tex┈┈┈┈┈H┃H┈┈┈┈tex┈┈┈H┃ <br/> /// ┃┈┈┈┈┈SW┈┈┈┈┈┃┈┈┈┈┈BW┈┈┈┈┈┃┈┈┈┈┈SW┈┈┈┈┈┈┃┈┈┈┈┈SW┈┈┈┈┃ <br/> /// ┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛ </summary> protected ModelPart BuildRotatedBox(BoxDescription desc) { int sidesW = desc.SidesW, bodyW = desc.BodyW, bodyH = desc.BodyH; float x1 = desc.X1, y1 = desc.Y1, z1 = desc.Z1; float x2 = desc.X2, y2 = desc.Y2, z2 = desc.Z2; int x = desc.TexX, y = desc.TexY; ZQuad(x + sidesW, y, bodyW, sidesW, x2, x1, y1, y2, z1); // front ZQuad(x + sidesW + bodyW, y, bodyW, sidesW, x1, x2, y2, y1, z2); // back YQuad(x + sidesW + bodyW + sidesW, y + sidesW, bodyW, bodyH, x1, x2, z1, z2, y2); // top YQuad(x + sidesW, y + sidesW, bodyW, bodyH, x2, x1, z1, z2, y1); // bottom XQuad(x, y + sidesW, sidesW, bodyH, y2, y1, z2, z1, x2); // left XQuad(x + sidesW + bodyW, y + sidesW, sidesW, bodyH, y1, y2, z2, z1, x1); // right // rotate left and right 90 degrees for (int i = index - 8; i < index; i++) { ModelVertex vertex = vertices[i]; float z = vertex.Z; vertex.Z = vertex.Y; vertex.Y = z; vertices[i] = vertex; } return(new ModelPart(index - 6 * 4, 6 * 4)); }
public PlayerModel() : base() { vertices = new ModelVertex[boxVertices * ( 7 + 2 )]; Set = new ModelSet(); Set.Head = BuildBox( MakeBoxBounds( -4, 24, -4, 4, 32, 4 ) .SetTexOrigin( 0, 0 ) ); Set.Torso = BuildBox( MakeBoxBounds( -4, 12, -2, 4, 24, 2 ) .SetTexOrigin( 16, 16 ) ); Set.LeftLeg = BuildBox( MakeBoxBounds( 0, 0, -2, -4, 12, 2 ) .SetTexOrigin( 0, 16 ) ); Set.RightLeg = BuildBox( MakeBoxBounds( 0, 0, -2, 4, 12, 2 ). SetTexOrigin( 0, 16 ) ); Set.Hat = BuildBox( MakeBoxBounds( -4, 24, -4, 4, 32, 4 ) .SetTexOrigin( 32, 0 ) .SetModelBounds( -4.5f, 23.5f, -4.5f, 4.5f, 32.5f, 4.5f ) ); Set.LeftArm = BuildBox( MakeBoxBounds( -4, 12, -2, -8, 24, 2 ) .SetTexOrigin( 40, 16 ) ); Set.RightArm = BuildBox( MakeBoxBounds( 4, 12, -2, 8, 24, 2 ) .SetTexOrigin( 40, 16 ) ); texId = graphics.CreateTexture( "char.png" ); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; top = BuildRotatedBox(MakeRotatedBoxBounds(-20, 20, -16, 20, 36, 20).TexOrigin(120, 0)); body = BuildRotatedBox(MakeRotatedBoxBounds(-22, 4, -40, 22, 20, 40).TexOrigin(0, 0)); spoiler = BuildRotatedBox(MakeRotatedBoxBounds(-19, 27, 33, 19, 30, 42).TexOrigin(120, 52)); tireFrontLeft = BuildBox(MakeBoxBounds(-24, 0, -28, -20, 10, -18).MirrorX().TexOrigin(156, 64).RotOrigin(-22, 5, -23)); tireBackLeft = BuildBox(MakeBoxBounds(-24, 0, 18, -20, 10, 28).MirrorX().TexOrigin(156, 64).RotOrigin(-22, 5, 23)); mirrorLeft = BuildBox(MakeBoxBounds(-27, 20, -15, -20, 25, -18).MirrorX().TexOrigin(202, 52).RotOrigin(-20, 22, -15)); spoilerLeft = BuildBox(MakeBoxBounds(-15, 20, 31, -12, 30, 34).MirrorX().TexOrigin(184, 64).RotOrigin(-12, 20, 31)); tireFrontRight = BuildBox(MakeBoxBounds(20, 0, -28, 24, 10, -18).TexOrigin(156, 64).RotOrigin(22, 5, -23)); tireBackRight = BuildBox(MakeBoxBounds(20, 0, 18, 24, 10, 28).TexOrigin(156, 64).RotOrigin(22, 5, 23)); mirrorRight = BuildBox(MakeBoxBounds(20, 20, -15, 27, 25, -18).TexOrigin(202, 52).RotOrigin(20, 22, -15)); spoilerRight = BuildBox(MakeBoxBounds(12, 20, 31, 15, 30, 34).TexOrigin(184, 64).RotOrigin(12, 20, 31)); frontLeft = BuildBox(MakeBoxBounds(-22, 4, -31, -20, 14, -15).MirrorX().TexOrigin(120, 64)); backLeft = BuildBox(MakeBoxBounds(-22, 4, 15, -20, 14, 31).MirrorX().TexOrigin(120, 64)); frontRight = BuildBox(MakeBoxBounds(20, 4, -31, 22, 14, -15).TexOrigin(120, 64)); backRight = BuildBox(MakeBoxBounds(20, 4, 15, 22, 14, 31).TexOrigin(120, 64)); }
public void AddVertex(Vector3 p0, Vector3 n0, Vector3 t) { int index = 0; var p = new Vector3(-p0.X, -p0.Z, p0.Y); var n = new Vector3(-n0.X, -n0.Z, n0.Y); //var v = Tuple.Create(p, n, t); var v = new ModelVertex(p, n, t); #if true if(!VertexMap.TryGetValue(v, out index)) //if(true) { index = Vertices.Count; VertexMap[v] = index; //VertexMap.Add(v, index); //Vertices.Add(new ModelVertex(p, n, t)); Vertices.Add(v); } #else //index = Vertices.IndexOf(v); //if(index < 0) { index = Vertices.Count; Vertices.Add(v); /*} else { var v2 = Vertices[index]; System.Diagnostics.Debug.Assert(v.Equals(v2)); */} #endif Indices.Add((ushort)index); FaceRemap.Add(index); }
public void AddTangent(ModelVertex v1, ModelVertex v2) { Vector3 d0 = v1.Position - Position; Vector3 d1 = v2.Position - Position; Vector2 s = v1.TexCoord - TexCoord; Vector2 t = v2.TexCoord - TexCoord; float r = 1.0F / (s.X * t.Y - t.X * s.Y); Vector3 sdir = new Vector3(t.Y * d0.X - s.Y * d1.X, t.Y * d0.Y - s.Y * d1.Y, t.Y * d0.Z - s.Y * d1.Z) * r; Vector3 tdir = new Vector3(s.X * d1.X - t.X * d0.X, s.X * d1.Y - t.X * d0.Y, s.X * d1.Z - t.X * d0.Z) * r; Tangent += sdir;// new Vector3(s.Y * d1.X - t.Y * d0.X, s.Y * d1.Y - t.Y * d0.Y, s.Y * d1.Z - t.Y * d0.Z) * r; Tangent2 += tdir; Weight++; }
public void GenerateMeshes(BinaryReader reader) { // TODO for (int i = 0; i < Submeshes.Length; ++i) { SubmeshDescriptor submesh = Submeshes[i]; VertexBufferDescriptor vbo = VertexBuffers[submesh.vertexBuffer]; IndexBufferDescriptor ibo = IndexBuffers[submesh.indexBuffer]; byte uvCount = GetMaxIndex(VertexElements[submesh.vertexBuffer], SemanticType.UV); #region IBO ModelIndice[] indices = new ModelIndice[submesh.indicesToDraw / 3]; reader.BaseStream.Position = ibo.dataStreamPointer + submesh.indexStart * 2; List <ushort> indexTracker = new List <ushort>(submesh.verticesToDraw); for (int j = 0; j < submesh.indicesToDraw / 3; ++j) { ModelIndice index = reader.Read <ModelIndice>(); int v1 = indexTracker.IndexOf(index.v1); if (v1 == -1) { v1 = indexTracker.Count; indexTracker.Add(index.v1); } int v2 = indexTracker.IndexOf(index.v2); if (v2 == -1) { v2 = indexTracker.Count; indexTracker.Add(index.v2); } int v3 = indexTracker.IndexOf(index.v3); if (v3 == -1) { v3 = indexTracker.Count; indexTracker.Add(index.v3); } indices[j] = new ModelIndice { v1 = (ushort)v1, v2 = (ushort)v2, v3 = (ushort)v3 }; } Indices[i] = indices; #endregion #region UV Pre. ModelUV[][] uv = new ModelUV[uvCount][]; for (int j = 0; j < uvCount; ++j) { uv[j] = new ModelUV[submesh.verticesToDraw]; } #endregion #region VBO ModelVertex[] vertex = new ModelVertex[submesh.verticesToDraw]; ModelBoneData[] bone = new ModelBoneData[submesh.verticesToDraw]; ModelVertex[] normal = new ModelVertex[submesh.verticesToDraw]; VertexElementDescriptor[][] elements = SplitVBE(VertexElements[submesh.vertexBuffer]); for (int j = 0; j < Stride[submesh.vertexBuffer].Length; ++j) { for (int k = 0; k < submesh.verticesToDraw; ++k) { long offset = submesh.vertexStart + indexTracker[k]; for (int l = 0; l < elements[j].Length; ++l) { VertexElementDescriptor element = elements[j][l]; if (element.format == SemanticFormat.NONE) { break; } object value = Stride[submesh.vertexBuffer][j][offset][l]; switch (element.type) { case SemanticType.POSITION: if (element.index == 0) { float[] t = (float[])value; vertex[k] = new ModelVertex { x = t[0], y = t[1], z = t[2] }; } else { if (unhandledSemanticTypes.Add(element.type) && System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Log(2, "CHUNK_LDOMMNRM", $"Unhandled vertex layer {element.index:X} for type {Util.GetEnumName(typeof(SemanticType), element.type)}!\n"); } } break; case SemanticType.NORMAL: if (element.index == 0) { float[] t = (float[])value; normal[k] = new ModelVertex { x = t[0], y = t[1], z = t[2] }; } else { if (unhandledSemanticTypes.Add(element.type) && System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Log(2, "CHUNK_LDOMMNRM", $"Unhandled vertex layer {element.index:X} for type {Util.GetEnumName(typeof(SemanticType), element.type)}!\n"); } } break; case SemanticType.UV: { ushort[] t = (ushort[])value; uv[element.index][k] = new ModelUV { u = Half.ToHalf(t[0]), v = Half.ToHalf(t[1]) }; } break; case SemanticType.BONE_INDEX: if (element.index == 0) { byte[] t = (byte[])value; bone[k].boneIndex = new ushort[t.Length]; for (int m = 0; m < t.Length; ++m) { bone[k].boneIndex[m] = (ushort)(t[m] + submesh.boneIdOffset); } } else { if (unhandledSemanticTypes.Add(element.type) && System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Log(2, "CHUNK_LDOMMNRM", $"Unhandled vertex layer {element.index:X} for type {Util.GetEnumName(typeof(SemanticType), element.type)}!\n"); } } break; case SemanticType.BONE_WEIGHT: if (element.index == 0) { bone[k].boneWeight = (float[])value; } else { if (unhandledSemanticTypes.Add(element.type) && System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Log(2, "CHUNK_LDOMMNRM", $"Unhandled vertex layer {element.index:X} for type {Util.GetEnumName(typeof(SemanticType), element.type)}!\n"); } } break; default: if (unhandledSemanticTypes.Add(element.type) && System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Log(2, "CHUNK_LDOMMNRM", $"Unhandled vertex type {Util.GetEnumName(typeof(SemanticType), element.type)}!\n"); } break; } } } } indexTracker.Clear(); TextureCoordinates[i] = uv; Vertices[i] = vertex; Bones[i] = bone; Normals[i] = normal; #endregion } }
public void ImportOld(string filename, ModelFile model) { var importer = new DaeIMImpoter(); Root = importer.Import(filename); var skins = Root.Instances.OfType<SkinDeclaraion> ().ToArray(); var morphs = Root.Instances.OfType<MorphingDeclaraion>().ToArray(); var geoms = Root.Instances.OfType<Geometry> ().ToArray(); Vertices = new Dictionary<ModelVertex, int>(); Indices = new List<int>(); //Skins = new Dictionary<int, ModelSkin>(); if(morphs.Length > 0) { var geom = morphs.First().Source as Geometry; var mesh = geom.Get<Mesh>("Data"); var posch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Position); var nrmch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Normal); var texch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.TexCoord); var pos = posch.GetDataAsList<SlimDX.Vector3>(); var nrm = nrmch.GetDataAsList<SlimDX.Vector3>(); var tex = texch.GetDataAsList<SlimDX.Vector2>(); var posface = posch.GetIndicesAsList(mesh); var nrmface = nrmch.GetIndicesAsList(mesh); var texface = texch.GetIndicesAsList(mesh); var numidx = posface.Count; for(int i= 0; i < numidx; ++i) { var p = pos[posface[i]]; var n = nrm[nrmface[i]]; var t = tex[texface[i]]; var v = new ModelVertex() { P = new Vector3(p.X, p.Y, p.Z), N = new Vector3(n.X, n.Y, n.Z), T = new Vector2(t.X, t.Y), }; int index; if(!Vertices.TryGetValue(v, out index)) Vertices.Add(v, index= Vertices.Count); Indices.Add(index); } var prims = new List<ModelPrimitive>(); var start = 0; foreach(var i in mesh.Primitives) { var prim = new ModelPrimitive(); prim.Indices = Indices.Skip(start).Take(i.Count).Select(j => (ushort)j).ToList(); prim.NumIndices = Indices.Count; start +=Indices.Count; prims.Add(prim); } model.Mesh.Skins = Vertices.Keys .Select(i => model.Mesh.Skins[FindClosest(model.Mesh.Vertices, i.P)]) .ToList(); model.Mesh.Vertices = Vertices.Keys.ToList(); model.Mesh.NumVerts = model.Mesh.Vertices.Count; model.Mesh.Primitives = prims; model.Mesh.NumPrims = prims.Count; // モーフィング model.Params = model.Params.Where(i => !(i is ParamMorph)).ToList(); foreach(var i in morphs.First().Channels) { var mmesh = i.Geometry.Get<Mesh>("Data"); var mposch = mmesh.Channels.FirstOrDefault(j => j.Semantic == GeometrySemantic.Position); var mpos = mposch.GetDataAsList<SlimDX.Vector3>(); var mposidx = mposch.GetIndicesAsList(mmesh); var dic = new Dictionary<int, MorphVertex>(); var mnrm = new SlimDX.Vector3[mpos.Count]; // 法線計算 for(int j= 0; j < mposidx.Count; j+=3) { var a = mpos[mposidx[j+0]]; var b = mpos[mposidx[j+1]]; var c = mpos[mposidx[j+2]]; var ab = SlimDX.Vector3.Normalize(SlimDX.Vector3.Subtract(b, a)); var ac = SlimDX.Vector3.Normalize(SlimDX.Vector3.Subtract(c, a)); var n = SlimDX.Vector3.Normalize(SlimDX.Vector3.Cross(ab, ac)); mnrm[mposidx[j+0]] = SlimDX.Vector3.Add(mnrm[mposidx[j+0]], n); mnrm[mposidx[j+1]] = SlimDX.Vector3.Add(mnrm[mposidx[j+1]], n); mnrm[mposidx[j+2]] = SlimDX.Vector3.Add(mnrm[mposidx[j+2]], n); } for(int j= 0; j < mnrm.Length; ++j) mnrm[j] = SlimDX.Vector3.Normalize(mnrm[j]); for(int j= 0; j < mposidx.Count; ++j) { var ii = (ushort)Indices[j]; var v = model.Mesh.Vertices[ii]; var p = mpos[mposidx[j]]; var n = mnrm[mposidx[j]]; p.X -=v.P.X; p.Y -=v.P.Y; p.Z -=v.P.Z; n.X -=v.N.X; n.Y -=v.N.Y; n.Z -=v.N.Z; dic[ii] = new MorphVertex(ii, p.X, p.Y, p.Z, n.X, n.Y, n.Z); } var morph = new ParamMorph() { Name = i.Geometry.Name, Vertices = dic.OrderBy(j => j.Key).Select(j => j.Value).ToList(), NumVertices = dic.Count, }; model.Params.Insert(0, morph); } } else { throw new Exception(); } ModelFile.ToFile(model.FileName, model); System.Diagnostics.Debug.Print("終了"); }
public override void Import(string filename, ModelFile model) { //MorphRange = MorphMax - MorphMin; var importer = new DaeIMImpoter(); Root = importer.Import(filename); var geoms = Root.Instances.OfType<Geometry> ().ToArray(); Vertices = new Dictionary<ModelVertex, int>(); Indices = new List<int>(); var geom = geoms.First(); var mesh = geom.Get<Mesh>("Data"); var posch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Position); var nrmch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Normal); var texch = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.TexCoord); var pos = posch.GetDataAsList<SlimDX.Vector3>(); var nrm = nrmch.GetDataAsList<SlimDX.Vector3>(); var tex = texch.GetDataAsList<SlimDX.Vector2>(); var posface = posch.GetIndicesAsList(mesh); var nrmface = nrmch.GetIndicesAsList(mesh); var texface = texch.GetIndicesAsList(mesh); var numidx = posface.Count; for(int i= 0; i < numidx; ++i) { var p = pos[posface[i]]; var n = nrm[nrmface[i]]; var t = tex[texface[i]]; var v = new ModelVertex() { P = new Vector3(p.X, p.Y, p.Z), N = new Vector3(n.X, n.Y, n.Z), T = new Vector2(t.X, t.Y), }; int index; if(!Vertices.TryGetValue(v, out index)) Vertices.Add(v, index= Vertices.Count); Indices.Add(index); } var prims = new List<ModelPrimitive>(); var start = 0; foreach(var i in mesh.Primitives) { var prim = new ModelPrimitive(); prim.Indices = Indices.Skip(start).Take(i.Count).Select(j => (ushort)j).ToList(); prim.NumIndices = Indices.Count; start +=Indices.Count; prims.Add(prim); } model.Mesh.Skins = Vertices.Keys .Select(i => model.Mesh.Skins[FindClosest(model.Mesh.Vertices, i.P)]) .ToList(); model.Mesh.Vertices = Vertices.Keys.ToList(); model.Mesh.NumVerts = model.Mesh.Vertices.Count; model.Mesh.Primitives = prims; model.Mesh.NumPrims = prims.Count; // モーフィングをボディから反映 var morphs = model.Params.OfType<ParamMorph>().ToList(); var body001 = DataManager.Instance.Body001; foreach(var i in morphs) { var index = (ushort)0; var bodymorph = body001.Params.FirstOrDefault(j => j.Name == i.Name) as ParamMorph; var dic = bodymorph.Vertices.ToDictionary(j => j.Index); var dic2 = new Dictionary<int, MorphVertex>(); var rr = Settings.MorphMax - Settings.MorphMin; MorphVertex mv; foreach(var j in model.Mesh.Vertices) { ++index; var near = FindClosest(body001.Mesh.Vertices, j.P); if(!dic.TryGetValue((ushort)near, out mv)) continue; var v = body001.Mesh.Vertices[near]; var x = v.P.X - j.P.X; var y = v.P.Y - j.P.Y; var z = v.P.Z - j.P.Z; var d = (float)Math.Sqrt(x*x + y*y + z*z); if(d >= Settings.MorphMax) continue; var r = d <= Settings.MorphMin ? 1.0f : 1.0f - (d-Settings.MorphMin) / rr; //r = r * MorphScale; var mv2 = new MorphVertex(index-1, mv.X*r, mv.Y*r, mv.Z*r, mv.NX*r, mv.NY*r, mv.NZ*r); dic2.Add((ushort)(index-1), mv2); } i.Vertices = dic2.OrderBy(j => j.Key).Select(j => j.Value).ToList(); i.NumVertices = i.Vertices.Count; } ModelFile.ToFile(model.FileName, model); System.Diagnostics.Debug.Print("終了"); }
void LoadMS3D(string filename) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { using (BinaryReader br = new BinaryReader(fs, System.Text.Encoding.Default)) { br.ReadChars(10); br.ReadInt32(); vertexCount = br.ReadUInt16(); ModelVertex[] vertices = new ModelVertex[vertexCount]; Vector3 minVert = Vector3.One * float.PositiveInfinity; Vector3 maxVert = Vector3.One * float.NegativeInfinity; for (int i = 0; i < vertexCount; i++) { br.ReadByte(); vertices[i].Position = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); minVert = Vector3.Min(minVert, vertices[i].Position); maxVert = Vector3.Max(maxVert, vertices[i].Position); vertices[i].BoneIndex = (int)br.ReadChar(); if (vertices[i].BoneIndex >= 255) vertices[i].BoneIndex = 0; vertices[i].Weight = 1; br.ReadByte(); } ushort triangleCount = br.ReadUInt16(); Triangle[] triList = new Triangle[triangleCount]; for (int i = 0; i < triangleCount; i++) { br.ReadUInt16(); //flag //Indices ushort v0 = br.ReadUInt16(); ushort v1 = br.ReadUInt16(); ushort v2 = br.ReadUInt16(); triList[i].vertex0 = v0; triList[i].vertex1 = v1; triList[i].vertex2 = v2; //Vertex 0 Normal vertices[v0].Normal += new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); //Vertex 1 Normal vertices[v1].Normal += new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); //Vertex 2 Normal vertices[v2].Normal += new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); //U vertices[v0].TexCoord.X = br.ReadSingle(); vertices[v1].TexCoord.X = br.ReadSingle(); vertices[v2].TexCoord.X = br.ReadSingle(); //V vertices[v0].TexCoord.Y = br.ReadSingle(); vertices[v1].TexCoord.Y = br.ReadSingle(); vertices[v2].TexCoord.Y = br.ReadSingle(); vertices[v0].Weight++; vertices[v1].Weight++; vertices[v2].Weight++; //Smoothing br.ReadByte(); //Group index br.ReadByte(); } for (int i = 0; i < vertexCount; i++) { vertices[i].Normal /= vertices[i].Weight; vertices[i].Weight = 1; } for (int i = 0; i < triangleCount; i++) { vertices[triList[i].vertex0].AddTangent(vertices[triList[i].vertex1], vertices[triList[i].vertex2]); vertices[triList[i].vertex1].AddTangent(vertices[triList[i].vertex0].Tangent, vertices[triList[i].vertex0].Tangent2); vertices[triList[i].vertex2].AddTangent(vertices[triList[i].vertex0].Tangent, vertices[triList[i].vertex0].Tangent2); //vertices[triList[i].vertex1].AddTangent(vertices[triList[i].vertex0], vertices[triList[i].vertex2]); //vertices[triList[i].vertex2].AddTangent(vertices[triList[i].vertex0], vertices[triList[i].vertex1]); } VertexPNTTI[] verts = new VertexPNTTI[vertexCount]; for (int i = 0; i < vertexCount; i++) { Vector3 N = vertices[i].Normal; N.Normalize(); Vector3 tangent = vertices[i].Tangent / vertices[i].Weight; tangent = (tangent - N * Vector3.Dot(N, tangent)); tangent.Normalize(); vertices[i].Tangent = tangent; vertices[i].Tangent2 /= vertices[i].Weight; vertices[i].Weight = 1; float binormSign = (Vector3.Dot(Vector3.Cross(N, tangent), vertices[i].Tangent2) < 0.0f) ? -1.0f : 1.0f; verts[i] = new VertexPNTTI(vertices[i].Position, vertices[i].Normal, vertices[i].TexCoord, vertices[i].Tangent, vertices[i].BoneIndex, binormSign); } vertexBuffer = new VertexBuffer(GFX.Device, vertexCount * VertexPNTTI.SizeInBytes, BufferUsage.None); vertexBuffer.SetData<VertexPNTTI>(verts); ushort groupCount = br.ReadUInt16(); parts = new ModelPart[groupCount]; int[] matIndices = new int[groupCount]; for (int i = 0; i < groupCount; i++) { br.ReadByte(); parts[i] = new ModelPart(); parts[i].name = new string(br.ReadChars(32)); //Group Name parts[i].name = parts[i].name.Replace("\0",string.Empty); ushort numTriangles = br.ReadUInt16(); //numTriangles parts[i].renderElement = new RenderElement(); parts[i].renderElement.PrimitiveCount = numTriangles; parts[i].renderElement.StartVertex = 0; parts[i].renderElement.VertexBuffer = vertexBuffer; parts[i].renderElement.VertexCount = vertexCount; parts[i].renderElement.VertexStride = VertexPNTTI.SizeInBytes; parts[i].renderElement.VertexDec = GFXVertexDeclarations.PNTTIDec; parts[i].bounds.Max = Vector3.Zero; parts[i].bounds.Min = Vector3.Zero; bool useIntIndices = (numTriangles >= ushort.MaxValue); IndexElementSize size = (useIntIndices) ? IndexElementSize.ThirtyTwoBits : IndexElementSize.SixteenBits; int stride = (useIntIndices) ? sizeof(uint) : sizeof(ushort); parts[i].renderElement.IndexBuffer = new IndexBuffer(GFX.Device, stride * numTriangles * 3, BufferUsage.None, size); List<ushort> ushortIndices = new List<ushort>(); List<uint> uintIndices = new List<uint>(); for (int l = 0; l < numTriangles; l++) { ushort t = br.ReadUInt16(); //triangle index if (useIntIndices) { uintIndices.Add((uint)triList[t].vertex2); uintIndices.Add((uint)triList[t].vertex1); uintIndices.Add((uint)triList[t].vertex0); } else { ushortIndices.Add((ushort)triList[t].vertex2); ushortIndices.Add((ushort)triList[t].vertex1); ushortIndices.Add((ushort)triList[t].vertex0); } parts[i].bounds.Max = Vector3.Max(parts[i].bounds.Max, vertices[triList[t].vertex0].Position); parts[i].bounds.Max = Vector3.Max(parts[i].bounds.Max, vertices[triList[t].vertex1].Position); parts[i].bounds.Max = Vector3.Max(parts[i].bounds.Max, vertices[triList[t].vertex2].Position); parts[i].bounds.Min = Vector3.Min(parts[i].bounds.Min, vertices[triList[t].vertex0].Position); parts[i].bounds.Min = Vector3.Min(parts[i].bounds.Min, vertices[triList[t].vertex1].Position); parts[i].bounds.Min = Vector3.Min(parts[i].bounds.Min, vertices[triList[t].vertex2].Position); } if (useIntIndices) parts[i].renderElement.IndexBuffer.SetData<uint>(uintIndices.ToArray()); else parts[i].renderElement.IndexBuffer.SetData<ushort>(ushortIndices.ToArray()); matIndices[i] = (int)br.ReadChar(); //Material index } meshBounds = new BoundingBox(parts[0].bounds.Min, parts[0].bounds.Max); for (int i = 1; i < parts.Length; i++) { meshBounds.Min = Vector3.Min(parts[i].bounds.Min, meshBounds.Min); meshBounds.Max = Vector3.Max(parts[i].bounds.Max, meshBounds.Max); } ushort MaterialCount = br.ReadUInt16(); string[] materialNames = new string[MaterialCount]; for (int i = 0; i < MaterialCount; i++) { materialNames[i] = new string(br.ReadChars(32)); materialNames[i] = materialNames[i].Replace("\0", string.Empty); for (int l = 0; l < 4; l++) br.ReadSingle(); for (int l = 0; l < 4; l++) br.ReadSingle(); for (int l = 0; l < 4; l++) br.ReadSingle(); for (int l = 0; l < 4; l++) br.ReadSingle(); br.ReadSingle(); br.ReadSingle(); br.ReadChar(); br.ReadChars(128); br.ReadChars(128); } CheckMissingMaterials(filename, matIndices, materialNames); for (int i = 0; i < groupCount; i++) { int matIndex = matIndices[i]; if (matIndex < 255) parts[i].material = ResourceManager.Inst.GetMaterial(materialNames[matIndex]); if (parts[i].material == null) parts[i].material = ResourceManager.Inst.GetMaterial("NULL"); } float fps = br.ReadSingle();//FPS br.ReadSingle();//current time int frameCount = br.ReadInt32(); //Total frames ushort boneCount = br.ReadUInt16(); nodes = new AnimationNode[boneCount]; if (boneCount > 0) { List<string> nodeParentNames = new List<string>(); for (int i = 0; i < boneCount; i++) { br.ReadByte(); //flag string name = new string(br.ReadChars(32)).Replace("\0", ""); string parentName = new string(br.ReadChars(32)).Replace("\0", ""); nodeParentNames.Add(parentName); Vector3 rotation = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); Vector3 position = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); nodes[i] = new AnimationNode(name, position, rotation); namesToNodes.Add(name, nodes[i]); ushort keyRotCount = br.ReadUInt16(); //Key frame rot count ushort keyPosCount = br.ReadUInt16(); //Key frame pos count //nodes[i].rotationFrames = new ModelBoneAnimationFrame[keyRotCount]; //nodes[i].translationFrames = new ModelBoneAnimationFrame[keyPosCount]; /* for (int j = 0; j < keyRotCount; j++) { nodes[i].rotationFrames[j].time = br.ReadSingle() * fps; //time nodes[i].rotationFrames[j].Displacement = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); nodes[i].rotationFrames[j].boneName = name; } for (int j = 0; j < keyPosCount; j++) { nodes[i].translationFrames[j].time = br.ReadSingle() * fps; //time nodes[i].translationFrames[j].Displacement = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); nodes[i].translationFrames[j].boneName = name; }*/ int count = (keyRotCount + keyPosCount) * 4; for (int j = 0; j < count; j++) br.ReadSingle(); } List<AnimationNode> rootNodeList = new List<AnimationNode>(); for (int i = 0; i < nodes.Length; i++) { if (namesToNodes.ContainsKey(nodeParentNames[i])) { AnimationNode node = namesToNodes[nodeParentNames[i]]; node.children.Add(nodes[i]); } else rootNodeList.Add(nodes[i]); } rootNodes = rootNodeList.ToArray(); for (int i = 0; i < rootNodes.Length; i++) { Matrix identityMat = Matrix.Identity; rootNodes[i].ApplyTransform(ref identityMat); } Matrix[] inverseMats = new Matrix[nodes.Length]; Matrix[] invRotMats = new Matrix[nodes.Length]; for (int i = 0; i < inverseMats.Length; i++) { inverseMats[i] = Matrix.Invert(nodes[i].Transform); invRotMats[i] = MathUtils.Invert3x3(nodes[i].Transform); } if (nodesAreAnimated) { for (int i = 0; i < verts.Length; i++) { verts[i].Position = Vector3.Transform(verts[i].Position, inverseMats[(int)verts[i].Index]); verts[i].Normal = Vector3.TransformNormal(verts[i].Normal, inverseMats[(int)verts[i].Index]); verts[i].Tangent = Vector3.TransformNormal(verts[i].Tangent, inverseMats[(int)verts[i].Index]); } } vertexBuffer.SetData<VertexPNTTI>(verts); for (int i = 0; i < nodes.Length; i++) { Vector3 Rotation = nodes[i].Rotation; Matrix transform = Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y) * Matrix.CreateRotationZ(Rotation.Z); transform.Translation = nodes[i].Translation; nodes[i].Transform = transform; } } } } }
public void DrawVertex(ModelVertex vertex) { SetTextureCoordinates(vertex.Texture.U, vertex.Texture.V); SetNormalCoordinates(vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z); DrawVertex(vertex.Position.X, vertex.Position.Y, vertex.Position.Z); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * boxesBuilt]; head = BuildBox(MakeBoxBounds(4, 8, -4, -4, 0, 4).TexOrigin(0, 0).RotOrigin(0, 0, 0)); }
private static void WriteVertex(BinaryWriter w, ModelVertex data) { w.Write(data.P.X); w.Write(data.P.Y); w.Write(data.P.Z); w.Write(data.N.X); w.Write(data.N.Y); w.Write(data.N.Z); w.Write(data.T.X); w.Write(data.T.Y); }
protected void YQuad( int texX, int texY, int texWidth, int texHeight, float x1, float x2, float z1, float z2, float y) { vertices[index++] = new ModelVertex( x1, y, z2, texX, texY + texHeight ); vertices[index++] = new ModelVertex( x1, y, z1, texX, texY ); vertices[index++] = new ModelVertex( x2, y, z1, texX + texWidth, texY ); vertices[index++] = new ModelVertex( x2, y, z2, texX + texWidth, texY + texHeight ); }
public override void CreateParts() { SetHoofOffset(); vertices = new ModelVertex[boxVertices * 22]; //20 so we have enough here for Pony2. Was 17 RightWing2 = BuildWing(MakeBoxBounds(0, 0, 0, 13, 1, 8) .SetModelBounds(-17, 12.5f, -8, -4, 13.5f, 0) .TexOrigin(23, 39) .RotOrigin(-4, 13, -4)); LeftWing2 = BuildWing(MakeBoxBounds(0, 0, 0, 13, 1, 8) .SetModelBounds(17, 12.5f, -8, 4, 13.5f, 0) .TexOrigin(23, 39) .RotOrigin(4, 13, -4)); LeftWing = BuildBox(MakeBoxBounds(0, 0, 0, 1, 4, 7) .SetModelBounds(-5, 11, -5, -4, 15, 2) .TexOrigin(41, 17) .RotOrigin(-4, 18, -5)); RightWing = BuildBox(MakeBoxBounds(0, 0, 0, 1, 4, 7) .SetModelBounds(5, 11, -5, 4, 15, 2) .TexOrigin(41, 17) .RotOrigin(-4, 18, -5)); Head = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 8) .SetModelBounds(-4, 18, -10, 4, 26, -2) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); Hat = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 8) .SetModelBounds(-4, 18, -10, 4, 26, -2) .TexOrigin(32, 0) .RotOrigin(0, 18, -5).Expand(0.5F)); Snout = BuildBox(MakeBoxBounds(0, 0, 0, 3, 2, 1) .SetModelBounds(-1.5F, 18, -11, 1.5F, 20, -10) .TexOrigin(0, 37) .RotOrigin(0, 18, -5)); Horn = BuildBox(MakeBoxBounds(0, 0, 0, 1, 5, 1) .SetModelBounds(-0.5F, 26, -7, 0.5F, 31, -6) .TexOrigin(24, 0) .RotOrigin(0, 18, -5)); LeftEar = BuildBox(MakeBoxBounds(0, 0, 0, 3, 4, 1) .SetModelBounds(-4.5F, 24, -5, -1.5F, 28, -4) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); RightEar = BuildBox(MakeBoxBounds(0, 0, 0, 3, 4, 1) .SetModelBounds(4.5F, 24, -5, 1.5F, 28, -4) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); Torso = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 12) .SetModelBounds(-4, 8, -6, 4, 16, 6) .TexOrigin(0, 16) .RotOrigin(0, 12, -2)); Tail = RightQuad(MakeBoxBounds(0, 0, 0, 2, 16, 16) .SetModelBounds(-2, 3, 6, 0, 19, 22) .TexOrigin(41, 32) .RotOrigin(-1, 14, 6)); Neck = BuildBox(MakeBoxBounds(0, 0, 0, 4, 7, 5) .SetModelBounds(-2, 12.5F, -7, 2, 19.5F, -2) .TexOrigin(5, 36) .RotOrigin(-4, 18, -5)); LeftLegFront = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(0.5F, 0 + hoofOffset, -5.5F, 3.5F, 8, -2.5F) .TexOrigin(28, 16) .RotOrigin(-4, 8, -4)); RightLegFront = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(-0.5F, 0 + hoofOffset, -5.5F, -3.5F, 8, -2.5F) .TexOrigin(28, 16) .RotOrigin(-4, 8, -4)); LeftLegBack = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(1, 0 + hoofOffset, 4, 4, 8, 7) .TexOrigin(0, 16) .RotOrigin(-3, 8, 5)); RightLegBack = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(-1, 0 + hoofOffset, 4, -4, 8, 7) .TexOrigin(0, 16) .RotOrigin(3, 8, 5)); float offsetX = -2; float offsetY = 17; float offsetZ = -15; Hair1 = RightQuad(MakeBoxBounds(0, 0, 0, 2, 16, 16) //middle .SetModelBounds(offsetX, offsetY, offsetZ, 2 + offsetX, 16 + offsetY, 16 + offsetZ) .TexOrigin(41, 49) .RotOrigin(0, 18, -5)); offsetX = 2; offsetY = 12; offsetZ = -14; Hair2 = RightQuad(MakeBoxBounds(0, 0, 0, 2, 16, 16) //right side .SetModelBounds(offsetX, offsetY, offsetZ, 2 + offsetX, 16 + offsetY, 16 + offsetZ) .TexOrigin(5, 49) .RotOrigin(0, 18, -5).Expand(0.5f)); offsetX = -4; offsetY = 12; offsetZ = -14; Hair3 = LeftQuad(MakeBoxBounds(0, 0, 0, 2, 16, 16) //left side .SetModelBounds(offsetX, offsetY, offsetZ, 2 + offsetX, 16 + offsetY, 16 + offsetZ) .TexOrigin(5, 66) .RotOrigin(0, 18, -5).Expand(0.5f)); }
public SceneImportResult LoadStaticMesh(string filename) { var file = new FileInfo(filename); VertexDescriptor vd = VertexDescriptor.Get <ModelVertex>(); List <MeshLayer> layers = new List <MeshLayer>(); int[] adjacency; ExtendedMaterial[] meshMaterials; Mesh mesh = new Mesh(vd); List <MeshMaterial> material = new List <MeshMaterial>(); using (SlimDX.Direct3D9.Mesh d3dMesh = SlimDX.Direct3D9.Mesh.FromFile(Engine.Graphics, filename, MeshFlags.Managed)) { adjacency = d3dMesh.GetAdjacency(); meshMaterials = d3dMesh.GetMaterials(); for (int i = 0; i < meshMaterials.Length; i++) { var matd3d = meshMaterials[i].MaterialD3D; string textureFilename = meshMaterials[i].TextureFileName; if (textureFilename != null && !Path.IsPathRooted(textureFilename)) { textureFilename = Path.Combine(Path.GetDirectoryName(filename), textureFilename); } material.Add(new MeshMaterial() { Name = file.Name + "_material" + i, Alpha = matd3d.Diffuse.Alpha, Diffuse = matd3d.Diffuse.ToVector3(), Specular = matd3d.Specular.ToVector3(), SpecularPower = Math.Max(1, matd3d.Power), Reflectivity = 0, Refractitity = 0, EmissiveColor = matd3d.Emissive.ToVector3(), DiffuseMap = textureFilename != null && File.Exists(textureFilename) ? textureFilename : null }); } ModelVertex[] vertexes = new ModelVertex[d3dMesh.VertexCount]; Array indices; if (d3dMesh.IndexBuffer.Description.Format == Format.Index16) { indices = new ushort[d3dMesh.FaceCount * 3]; } else { indices = new uint[d3dMesh.FaceCount * 3]; } DataStream vertexStream = d3dMesh.LockVertexBuffer(0); VertexDescriptor meshVD = new VertexDescriptor(d3dMesh.GetDeclaration()); int positionOffset = meshVD.GetOffset(DeclarationUsage.Position, 0); int normalOffset = meshVD.GetOffset(DeclarationUsage.Normal, 0); int texCoordOffset = meshVD.GetOffset(DeclarationUsage.TextureCoordinate, 0); byte[] buffer; unsafe { buffer = new byte[vertexStream.Length]; vertexStream.Read(buffer, 0, (int)vertexStream.Length); fixed(byte *_pter = buffer) { byte *pter = _pter; for (int i = 0; i < d3dMesh.VertexCount; i++) { vertexes[i].Position = *((Vector3 *)(pter + positionOffset)); if (normalOffset > 0) { vertexes[i].Normal = *((Vector3 *)(pter + normalOffset)); } if (texCoordOffset > 0) { vertexes[i].TexCoord = *((Vector2 *)(pter + texCoordOffset)); } pter += d3dMesh.BytesPerVertex; } } d3dMesh.UnlockVertexBuffer(); DataStream indexStream = d3dMesh.LockIndexBuffer(0); GCHandle handler = GCHandle.Alloc(indices, GCHandleType.Pinned); byte * indexPter = (byte *)Marshal.UnsafeAddrOfPinnedArrayElement(indices, 0); buffer = new byte[indexStream.Length]; indexStream.Read(buffer, 0, (int)indexStream.Length); for (int i = 0; i < indexStream.Length; i++) { indexPter[i] = buffer[i]; } handler.Free(); d3dMesh.UnlockIndexBuffer(); } mesh.CreateVertexBuffer(vertexes); if (d3dMesh.IndexBuffer.Description.Format == Format.Index16) { mesh.CreateIndexBuffer((ushort[])indices); } else { mesh.CreateIndexBuffer((uint[])indices); } var d3dComponents = d3dMesh.GetAttributeTable(); if (d3dComponents == null) { MeshLayer component = new MeshLayer(); layers.Add(component); material.Add(MeshMaterial.CreateDefaultMaterial(file.Name + "_default")); component.materialIndex = 0; component.primitiveCount = mesh.FaceCount; component.startIndex = 0; component.startVertex = 0; component.vertexCount = mesh.VertexCount; } else { for (int i = 0; i < d3dComponents.Length; i++) { AttributeRange ar = d3dComponents[i]; MeshLayer component = new MeshLayer(); layers.Add(component); component.materialIndex = ar.AttribId; component.primitiveCount = ar.FaceCount; component.startIndex = ar.FaceStart * 3; component.startVertex = ar.VertexStart; component.vertexCount = ar.VertexCount; } } mesh.Materials = material.ToArray(); mesh.SetLayers(layers.ToArray()); if (normalOffset < 0) { mesh.ComputeNormals(); } if (texCoordOffset < 0) { mesh.ComputeTextureCoords(CoordMappingType.Spherical); } mesh.ComputeTangents(); meshVD.Dispose(); } StaticMeshSceneNode meshNode = new StaticMeshSceneNode(file.Name, mesh); return(new SceneImportResult { VisualSceneRoot = meshNode, VisualMaterials = material }); }
protected void XQuad( int texX, int texY, int texWidth, int texHeight, float z1, float z2, float y1, float y2, float x) { vertices[index++] = new ModelVertex( x, y1, z1, texX, texY + texHeight ); vertices[index++] = new ModelVertex( x, y2, z1, texX, texY ); vertices[index++] = new ModelVertex( x, y2, z2, texX + texWidth, texY ); vertices[index++] = new ModelVertex( x, y1, z2, texX + texWidth, texY + texHeight ); }
public override void CreateParts() { vertices = new ModelVertex[boxVertices * 17]; RightWing2 = BuildBox(MakeBoxBounds(0, 0, 0, 13, 1, 8) .SetModelBounds(-17, 12.5f, -8, -4, 13.5f, 0) .TexOrigin(23, 39) .RotOrigin(-4, 13, -4)); LeftWing2 = BuildBox(MakeBoxBounds(0, 0, 0, 13, 1, 8) .SetModelBounds(17, 12.5f, -8, 4, 13.5f, 0) .TexOrigin(23, 39) .RotOrigin(4, 13, -4)); LeftWing = BuildBox(MakeBoxBounds(0, 0, 0, 1, 4, 7) .SetModelBounds(-5, 11, -5, -4, 15, 2) .TexOrigin(41, 17) .RotOrigin(-4, 18, -5)); RightWing = BuildBox(MakeBoxBounds(0, 0, 0, 1, 4, 7) .SetModelBounds(5, 11, -5, 4, 15, 2) .TexOrigin(41, 17) .RotOrigin(-4, 18, -5)); Head = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 8) .SetModelBounds(-4, 18, -10, 4, 26, -2) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); Hat = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 8) .SetModelBounds(-4, 18, -10, 4, 26, -2) .TexOrigin(32, 0) .RotOrigin(0, 18, -5).Expand(0.5F)); Snout = BuildBox(MakeBoxBounds(0, 0, 0, 3, 2, 1) .SetModelBounds(-1.5F, 18, -11, 1.5F, 20, -10) .TexOrigin(0, 37) .RotOrigin(0, 18, -5)); Horn = BuildBox(MakeBoxBounds(0, 0, 0, 1, 5, 1) .SetModelBounds(-0.5F, 26, -7, 0.5F, 31, -6) .TexOrigin(24, 0) .RotOrigin(0, 18, -5)); LeftEar = BuildBox(MakeBoxBounds(0, 0, 0, 3, 4, 1) .SetModelBounds(-4.5F, 24, -5, -1.5F, 28, -4) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); RightEar = BuildBox(MakeBoxBounds(0, 0, 0, 3, 4, 1) .SetModelBounds(4.5F, 24, -5, 1.5F, 28, -4) .TexOrigin(0, 0) .RotOrigin(0, 18, -5)); Torso = BuildBox(MakeBoxBounds(0, 0, 0, 8, 8, 12) .SetModelBounds(-4, 8, -6, 4, 16, 6) .TexOrigin(0, 16) .RotOrigin(-4, 18, -5)); Tail = BuildBox(MakeBoxBounds(0, 0, 0, 2, 16, 16) .SetModelBounds(-2, 3, 6, 0, 19, 22) .TexOrigin(41, 32) .RotOrigin(-1, 14, 6)); Neck = BuildBox(MakeBoxBounds(0, 0, 0, 4, 7, 5) .SetModelBounds(-2, 12.5F, -7, 2, 19.5F, -2) .TexOrigin(5, 36) .RotOrigin(-4, 18, -5)); LeftLegFront = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(0.5F, 0, -5.5F, 3.5F, 8, -2.5F) .TexOrigin(28, 16) .RotOrigin(-4, 8, -4)); RightLegFront = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(-0.5F, 0, -5.5F, -3.5F, 8, -2.5F) .TexOrigin(28, 16) .RotOrigin(-4, 8, -4)); LeftLegBack = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(1, 0, 4, 4, 8, 7) .TexOrigin(0, 16) .RotOrigin(-4, 8, 5)); RightLegBack = BuildBox(MakeBoxBounds(0, 0, 0, 3, 8, 3) .SetModelBounds(-1, 0, 4, -4, 8, 7) .TexOrigin(0, 16) .RotOrigin(-4, 8, 5)); }
protected void ZQuad( int texX, int texY, int texWidth, int texHeight, float x1, float x2, float y1, float y2, float z) { vertices[index++] = new ModelVertex( x1, y1, z, texX, texY + texHeight ); vertices[index++] = new ModelVertex( x1, y2, z, texX, texY ); vertices[index++] = new ModelVertex( x2, y2, z, texX + texWidth, texY ); vertices[index++] = new ModelVertex( x2, y1, z, texX + texWidth, texY + texHeight ); }
public EngineContent ToSceneNode() { string name = Path.GetFileNameWithoutExtension(this.name); VertexDescriptor vd = EngineResources.CreateVertexDescriptor <ModelVertex>(); List <Tuple <string, Mesh> > meshes = new List <Tuple <string, Mesh> >(groups.Count); List <SceneNode> nodes = new List <SceneNode>(groups.Count); MeshMaterial[] sceneMaterials = new MeshMaterial[materials.Count]; Dictionary <string, MeshMaterial> materialLookup = new Dictionary <string, MeshMaterial>(); for (int i = 0; i < materials.Values.Count; i++) { var sourceMat = materials.Values[i]; sceneMaterials[i] = new MeshMaterial { Name = name + "_" + sourceMat.Name, Surface = sourceMat.ToSurfaceInfo(), NormalMap = sourceMat.NormalMap, DiffuseMap = sourceMat.Textures != null && sourceMat.Textures.Length > 0 ? sourceMat.Textures[0] : null }; materialLookup.Add(sceneMaterials[i].Name, sceneMaterials[i]); } List <MeshLayer> layers = new List <MeshLayer>(); //register for each positionIndex in the source container the List of the destination vertices that contain that position Dictionary <int, List <VertexInfo> > lookup = new Dictionary <int, List <VertexInfo> >(); VertexInfo vi; ModelVertex v; List <ModelVertex> vertexes = new List <ModelVertex>(); uint[] indexes; #region Groups foreach (var g in groups) { List <MeshMaterial> meshMaterials = new List <MeshMaterial>(); Mesh mesh = new Mesh(vd: vd); mesh.Name = g.Name; indexes = new uint[g.FaceCount * 3]; int k = 0; #region Layers layers.Clear(); foreach (var layer in g.Layers) { int startVertex = int.MaxValue; int vertexCount = 0; MeshLayer meshLayer = new MeshLayer(); meshLayer.startIndex = k; meshLayer.primitiveCount = layer.Faces.Count; var mat = materialLookup[name + "_" + layer.MaterialName]; meshLayer.materialIndex = meshMaterials.IndexOf(mat); if (meshLayer.materialIndex < 0) { meshLayer.materialIndex = meshMaterials.Count; meshMaterials.Add(mat); } #region Faces foreach (var face in layer.Faces) { //for each vertex of the face create a new mesh vertex if the vertex if not yet in the mesh add it to the VertexBuffer //and create a new face in the IndexBuffer for (int i = 0; i < 3; i++) { //vi describe a new vertex vi = new VertexInfo() { PositionIndex = face.Vertexes[i].Position, NormalIndex = -1, TcIndex = -1 }; //if the vertex position is not in the VertexBuffer add it if (!lookup.ContainsKey(vi.PositionIndex)) { v = new ModelVertex(position: positions[vi.PositionIndex]); if ((vertexFormat & VertexFormat.Normal) == VertexFormat.Normal && face.Vertexes[i].Normal >= 0) { vi.NormalIndex = face.Vertexes[i].Normal; v.Normal = normals[vi.NormalIndex]; } if ((vertexFormat & VertexFormat.TexCoord) == VertexFormat.TexCoord && face.Vertexes[i].TexCoord >= 0) { vi.TcIndex = face.Vertexes[i].TexCoord; v.TexCoord = texCoords[vi.TcIndex]; } vi.VertexIndex = vertexes.Count; lookup.Add(vi.PositionIndex, new List <VertexInfo>() { vi }); vertexes.Add(v); vertexCount++; indexes[k] = (uint)vi.VertexIndex; } else { //else get the list of vertices that contains that position and // if new vertex is not in the list create the new destination vertex and add it to the VertexBuffer var vlist = lookup[vi.PositionIndex]; if ((vertexFormat & VertexFormat.Normal) == VertexFormat.Normal) { vi.NormalIndex = face.Vertexes[i].Normal; } if ((vertexFormat & VertexFormat.TexCoord) == VertexFormat.TexCoord) { vi.TcIndex = face.Vertexes[i].TexCoord; } int index = vlist.FindIndex(x => x.Equals(vi)); if (index < 0) { v = new ModelVertex(positions[vi.PositionIndex]); if (vi.NormalIndex >= 0) { v.Normal = normals[vi.NormalIndex]; } if (vi.TcIndex >= 0) { v.TexCoord = texCoords[vi.TcIndex]; } vi.VertexIndex = vertexes.Count; indexes[k] = (uint)vi.VertexIndex; vertexCount++; vertexes.Add(v); vlist.Add(vi); } else { //else the vertex is already in the VertexBuffer so create add the vertex index //to the indexbuffer vi = vlist[index]; indexes[k] = (uint)vi.VertexIndex; } } k++; startVertex = Math.Min(startVertex, vi.VertexIndex); } } #endregion Faces meshLayer.startVertex = startVertex; meshLayer.vertexCount = vertexCount; layers.Add(meshLayer); } #endregion Layers mesh.SetLayers(layers.ToArray()); var data = vertexes.ToArray(); mesh.CreateVertexBuffer(data); if (mesh.VertexCount < ushort.MaxValue) { mesh.CreateIndexBuffer(indexes.Select(x => (ushort)x).ToArray()); } else { mesh.CreateIndexBuffer(indexes); } mesh.DefragmentLayers(); mesh.BlendLayers(); if ((vertexFormat & VertexFormat.Normal) != VertexFormat.Normal) { mesh.ComputeNormals(); } if ((vertexFormat & VertexFormat.TexCoord) != VertexFormat.TexCoord) { mesh.ComputeTextureCoords(CoordMappingType.Spherical); } mesh.ComputeTangents(); nodes.Add(new SceneNode <MeshInstance>(g.Name, new MeshInstance(meshMaterials.ToArray(), mesh))); vertexes.Clear(); //test lookup.Clear(); } #endregion QuadTreeSceneNode node = new QuadTreeSceneNode(name, 10); node.Context = new TechniqueRenderContext(node); foreach (var item in nodes) { node.Add(item); } node.UpdateLayout(); var package = new EngineContent(name); package.Providers.AddRange(sceneMaterials); package.Providers.Add(node); return(package); }
private static ModelVertex ReadVertex(BinaryReader r) { var data = new ModelVertex(); data.P = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle()); data.N = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle()); data.T = new Vector2(r.ReadSingle(), r.ReadSingle()); return data; }
public void DrawVertex(ModelVertex vertex) { Internal.OpenGL.Methods.glTexCoord(vertex.Texture.U, vertex.Texture.V); Internal.OpenGL.Methods.glNormal3dv(new double[] { vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z }); Internal.OpenGL.Methods.glVertex3d(vertex.Position.X, vertex.Position.Y, vertex.Position.Z); }