Salut à tous.
Je suis entrain de développer une game-engine spécifique qui pourrait être utilisé dans des cours pour faire des démos et développer des compétences.
Voila je crée un tube (courbé ou non) qui est définit par des points et par un radius (demi diamètre). Ce tube est créé à partir de triangles qui sont définit dans le champ _indices. Les coordonnées des points se trouvent dans _ropeVertices.
Lorsque je dessine le tube avec le vertexBuffer et Indexbuffer et drawIndexedPrimitive... ca marche sans problème.
Lorsque je veux, comme ici, le transformer dans un mesh, je recoit une message d'erreur: Stack Overflow à un moment de l'exécution (qui change naturellement à chaque fois :-( ). Si j'enlève le mesh ca marche, mais ca ne rentre pas dans la structure globale
du projet. J'ai loupé qqch.?
private CustomVertex.PositionNormalTextured[] _ropeVertices;
private VertexBuffer _vb;
private IndexBuffer _ib;
private int[] _indices;
private Mesh mesh;
public Mesh createRope(Device dev, Vector3[] points, float radiusOfRope)
{
Vector3 n;
int circleSubdivision = 20;
for (int i = 0; i < points.Length-1; i++)
{
n = Vector3.Subtract(points[i], points[i+1]);
createCircle(points[i], n, radiusOfRope, circleSubdivision, (float)i % 2);
}
n = Vector3.Subtract(points[points.Length - 2], points[points.Length - 1]);
createCircle(points[points.Length - 1], n, radiusOfRope, circleSubdivision, (float)points.Length - 1 % 2);
_indices = newint[(points.Length - 1)*circleSubdivision* 6];
int k=0;
int faces = 0;
for (int j = 0; j < points.Length - 1; j++)
{
for (int i = 0; i < circleSubdivision - 1; i++)
{
_indices[k] = i + j * circleSubdivision;
_indices[k + 1] = _indices[k] + 1;
_indices[k + 2] = _indices[k] + circleSubdivision;
_indices[k + 3] = _indices[k + 1];
_indices[k + 4] = _indices[k + 2] + 1;
_indices[k + 5] = _indices[k + 2];
k += 6;
faces += 2;
}
_indices[k] = (j + 1) * circleSubdivision - 1;
_indices[k + 1] = j * circleSubdivision;
_indices[k + 2] = _indices[k] + circleSubdivision;
_indices[k + 3] = _indices[k + 1];
_indices[k + 4] = _indices[k + 1] + circleSubdivision;
_indices[k + 5] = _indices[k + 2];
k += 6;
faces += 2;
}
mesh = newMesh(faces, _ropeVertices.Length, MeshFlags.Dynamic,
CustomVertex.PositionNormalTextured.Format, dev);
AttributeRange[] attributeRange = newAttributeRange[1];
attributeRange[0].AttributeId = 0;
attributeRange[0].FaceStart = 0;
attributeRange[0].FaceCount = faces;
attributeRange[0].VertexStart = 0;
attributeRange[0].VertexCount = _ropeVertices.Length;
mesh.SetAttributeTable(attributeRange);
mesh.VertexBuffer.SetData(_ropeVertices, 0, LockFlags.None);
mesh.IndexBuffer.SetData(_indices, 0, LockFlags.None); <--- souvent ca plante ici
return mesh;
}
//créer un cercle dans la plaine définit par le point et la normale et le subdivier en un nombre de //points
private void createCircle(Vector3 center, Vector3 normal, float radius, int subdivisions, float texturePosition)
{
float arcus_increment = (float)(2.0 * Math.PI /subdivisions); //cylinder angular increment
float tu_increment = (float)(1.0 / (subdivisions)); //texture horiz. increment
Vector3 v = newVector3();
_ropeVertices = incArray(_ropeVertices,subdivisions);
int insertPosition = _ropeVertices.Length - subdivisions;
Vector3 x = newVector3(normal.Y, -normal.X, 0);
if (x.X == 0 && x.Y == 0 && x.Z == 0)
{
x = newVector3(0, normal.Z, -normal.Y);
}
Vector3 y = newVector3(normal.Z, 0, -normal.X);
if (y.X == 0 && y.Y == 0 && y.Z == 0)
{
y = newVector3(0, normal.Z, -normal.Y);
}
x.Normalize();
y.Normalize();
if (y.Z > 0)
{
y.X *= -1;
y.Z *= -1;
}
for (int i = 0; i < subdivisions; i++)
{
float arcus = i * arcus_increment;
v = Vector3.Multiply(x, radius * (float)Math.Cos(arcus)) + Vector3.Multiply(y, radius * (float)Math.Sin(arcus));
v += center;
//Point
_ropeVertices[insertPosition + i].Position = v;
//Normale
v.Y = 0;
_ropeVertices[insertPosition + i].Normal = v;
//Texture
_ropeVertices[insertPosition + i].Tv = i * tu_increment;
_ropeVertices[insertPosition + i].Tu = texturePosition;
}
}
private CustomVertex.PositionNormalTextured[] incArray(CustomVertex.PositionNormalTextured[] srcArray, int numItems)
{
if (srcArray == null)
{
return new CustomVertex.PositionNormalTextured[numItems];
}
else
{
CustomVertex .PositionNormalTextured[] newArray = newCustomVertex.PositionNormalTextured[srcArray.Length + numItems];
Array .Copy(srcArray, newArray, srcArray.Length);
return newArray;
}
}