1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace HekaEngine
{
public struct triangle
{
public int a;
public int b;
public int c;
};
public class Quad
{
#region "Champs"
public BoundingBox zone;
public Quad HautGauche = null;
public Quad HautDroit = null;
public Quad BasGauche = null;
public Quad BasDroit = null;
public int[] indices;
private BasicEffect shader = new BasicEffect(Moteur.GraphicsDevice, null);
private VertexPositionColor[] points = new VertexPositionColor[8];
private short[] inds = {
0, 1, 0, 2, 1, 3, 2, 3,
4, 5, 4, 6, 5, 7, 6, 7,
0, 4, 1, 5, 2, 6, 3, 7
};
private int taille;
#endregion
public Quad(int tailleTerrain)
{
// On vérifie que l'affinage est encore bon
if (tailleTerrain >= Terrain.precisionQuad)
{
taille = tailleTerrain;
tailleTerrain /= 2;
HautGauche = new Quad(tailleTerrain);
HautDroit = new Quad(tailleTerrain);
BasGauche = new Quad(tailleTerrain);
BasDroit = new Quad(tailleTerrain);
}
}
public void CreerQuad(BoundingBox boite, int[] i, int tailleTotale)
{
zone = boite;
// Création des indices
indices = creerIndices(i, tailleTotale);
Vector3 debut = Vector3.Zero;
Vector3 fin = Vector3.Zero;
// Q1
debut = zone.Min;
fin = zone.Max;
fin.X = fin.X - taille / 2.0f;
fin.Z = fin.Z - taille / 2.0f;
HautGauche.zone = new BoundingBox(debut, fin);
// Q2
debut = zone.Min;
fin = zone.Max;
debut.X = fin.X - taille / 2.0f;
fin.Z = fin.Z - taille / 2.0f;
HautDroit.zone = new BoundingBox(debut, fin);
// Q3
debut = zone.Min;
fin = zone.Max;
debut.Z = fin.Z - taille / 2.0f;
fin.X = fin.X - taille / 2.0f;
BasGauche.zone = new BoundingBox(debut, fin);
// Q4
debut = zone.Min;
fin = zone.Max;
debut.X = fin.X - taille / 2.0f;
debut.Z = fin.Z - taille / 2.0f;
BasDroit.zone = new BoundingBox(debut, fin);
if (HautGauche.HautGauche != null)
{
HautGauche.CreerQuad(HautGauche.zone, i, tailleTotale);
HautDroit.CreerQuad(HautDroit.zone, i, tailleTotale);
BasGauche.CreerQuad(BasGauche.zone, i, tailleTotale);
BasDroit.CreerQuad(BasDroit.zone, i, tailleTotale);
}
}
#region "Affichage des boites"
public void Afficher()
{
Affichage();
if (HautGauche != null)
{
HautGauche.Afficher();
HautDroit.Afficher();
BasGauche.Afficher();
BasDroit.Afficher();
}
}
private void Affichage()
{
Vector3[] corners = zone.GetCorners();
points[0] = new VertexPositionColor(corners[1], Color.Green);
points[1] = new VertexPositionColor(corners[0], Color.Green);
points[2] = new VertexPositionColor(corners[2], Color.Green);
points[3] = new VertexPositionColor(corners[3], Color.Green);
points[4] = new VertexPositionColor(corners[5], Color.Green);
points[5] = new VertexPositionColor(corners[4], Color.Green);
points[6] = new VertexPositionColor(corners[6], Color.Green);
points[7] = new VertexPositionColor(corners[7], Color.Green);
Moteur.GraphicsDevice.VertexDeclaration = new VertexDeclaration(Moteur.GraphicsDevice, VertexPositionColor.VertexElements);
shader.World = Matrix.Identity;
shader.View = Moteur.Service.ObtenirService<Camera>().View;
shader.Projection = Moteur.Service.ObtenirService<Camera>().Projection;
shader.DiffuseColor = zone.Min;
shader.DiffuseColor = Color.Gold.ToVector3();
shader.Begin(SaveStateMode.SaveState);
for (int pass = 0; pass < shader.CurrentTechnique.Passes.Count; pass++)
{
shader.CurrentTechnique.Passes[pass].Begin();
Moteur.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineList, points, 0, 8, inds, 0, 12);
shader.CurrentTechnique.Passes[pass].End();
}
shader.End();
}
#endregion
public int[] creerIndices(int[] indices, int tailleTotale)
{
List<int> nouveauxindices = new List<int>();
int position = 0;
foreach (int indice in indices)
{
if (
((indice % tailleTotale) >= zone.Min.X && (indice % tailleTotale) <= zone.Max.X) &&
((indice / tailleTotale) >= zone.Min.Z)
)
nouveauxindices.Add(indice);
}
return nouveauxindices.ToArray();
}
}
} |
Partager