Ciao a tutti. Ho impostato un progetto Game di Xna in cui, fra le altre cose, mi servo di un array di VertexPositionColor, un array di interi ed una struttura che ha per campi un array di VertexPositionColorTexture, un array di int ed una texture.
codice:
public class Game1 : Microsoft.Xna.Framework.Game
{
...
VertexPositionColor[] vertices = new VertexPositionColor[8];
#if CPU16
System.Int16[] indices = new System.Int16[36];
#elif CPU32
int[] indices = new int[36];
#endif
float Lx = 10f, Ly = 7f, Lz = 5f;
SolidFace face0, face1, face2, face3, face4, face5;
Texture2D facetex0, facetex1, facetex2, facetex3, facetex4, facetex5;
...
public struct SolidFace
{
//Vertici della parete (4).
public VertexPositionColorTexture[] fvertices;
//Indici da usare per disegnare i triangoli (6).
#if CPU16
public System.Int16[] findices;
#elif CPU32
public int[] findices;
#endif
//Immagine da applicare a questa parete
public Texture2D ftexture;
}
Ho scritto una sub che imposta l'array vertices
codice:
public void Set_Vertices()
{
vertices[0].Position = new Vector3(-Lx / 2, -Ly / 2, Lz / 2);
vertices[0].Color = Color.White;
vertices[1].Position = new Vector3(-Lx / 2, -Ly / 2, -Lz / 2);
vertices[1].Color = Color.White;
vertices[2].Position = new Vector3(Lx / 2, -Ly / 2, -Lz / 2);
vertices[2].Color = Color.White;
vertices[3].Position = new Vector3(Lx / 2, -Ly / 2, Lz / 2);
vertices[3].Color = Color.White;
vertices[4].Position = new Vector3(-Lx / 2, Ly / 2, Lz / 2);
vertices[4].Color = Color.White;
vertices[5].Position = new Vector3(-Lx / 2, Ly / 2, -Lz / 2);
vertices[5].Color = Color.White;
vertices[6].Position = new Vector3(Lx / 2, Ly / 2, -Lz / 2);
vertices[6].Color = Color.White;
vertices[7].Position = new Vector3(Lx / 2, Ly / 2, Lz / 2);
vertices[7].Color = Color.White;
}
ed un'altra che imposta 6 oggetti SolidFace
codice:
public void Set_Faces()
{
face0 = new SolidFace();
face1 = new SolidFace();
face2 = new SolidFace();
face3 = new SolidFace();
face4 = new SolidFace();
face5 = new SolidFace();
//FACCIA 0:
//posizioni e colori
face0.fvertices[0].Position = vertices[4].Position; <======
!Riferimento a un oggetto non impostato su un'istanza di oggetto!.
face0.fvertices[1].Position = vertices[5].Position;
face0.fvertices[2].Position = vertices[6].Position;
face0.fvertices[3].Position = vertices[7].Position;
face0.fvertices[0].Color = vertices[4].Color;
face0.fvertices[1].Color = vertices[5].Color;
face0.fvertices[2].Color = vertices[6].Color;
face0.fvertices[3].Color = vertices[7].Color;
//Coordinate texture
face0.fvertices[0].TextureCoordinate = new Vector2(0f, 0f);
face0.fvertices[1].TextureCoordinate = new Vector2(1f, 0f);
face0.fvertices[2].TextureCoordinate = new Vector2(1f, 1f);
face0.fvertices[3].TextureCoordinate = new Vector2(0f, 1f);
//Indici delle 2 primitive: usiamo 4-5-6 e 4-6-7
face0.findices[0] = 4;
face0.findices[1] = 5;
face0.findices[2] = 6;
face0.findices[3] = 4;
face0.findices[4] = 6;
face0.findices[5] = 7;
//texture
face0.ftexture = facetex0;
... (le altre 5 facce)
}
...
protected override void Initialize()
{
Set_Vertices();
Set_Faces(); <----- si blocca qui
...
}
Ecco il problema nasce quando nella sub Initialize() viene chiamata Set_Faces. Viene sollevata una NullReference Exception ma non capisco dove. Mi potete aiutare? Grazie