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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
//
//----------------------------------------------------------------------------
//Calque, fonctions de calque.c
ref class calque {
public:
array<int, 2>^ v;
int taille;
float persistance;
System::Random^ rnd;
public:
static calque^ init(int t, float p);
static void free(calque^ s);
void generer(int frequence, int octaves, float persistance, int liss);
private:
static int interpolate(int y1, int y2, int n, int delta);
System::Byte aleatoire(float a);
//void enregistrer_bmp(String^ filename);
int valeur_interpolee(int i, int j, int frequence);
};
//
//----------------------------------------------------------------------------
//Fonctions de main.c
ref class Perlin abstract sealed {
public:
static void enregistrer_bmp(calque^ c, System::String^ filename);
static int Main(array<System::String^>^ args);
};
//
//----------------------------------------------------------------------------
//Fonctions de calque.c
//#include "Perlin_calque.h"
//#include "Perlin_main.h"
using namespace System;
calque^ calque::init(int t, float p) {
calque^ s = gcnew calque;
//Pour bien faire, je devrais mettre ça dans le constructeur de calque
s->v = gcnew array<int, 2>(t, t);
s->taille = t;
s->persistance = p;
s->rnd = gcnew Random();
return s;
}
void calque::free(calque^ s) {
//Rien à faire ici, le garbage collector s'en occupe
}
Byte calque::aleatoire(float a)
{
//Il y aurait probablement plus efficace avec un argument entier et Random::Next(),
//mais vu que je ne sais pas encore comment cette fonction est appelée...
return (Byte)(rnd->NextDouble() * a);
}
//La fonction principale
void calque::generer(int frequence, int octaves, float persistance, int liss) {
calque^ c = this;
int taille = c ->taille;
float pas = (float)taille / frequence;
//Calque aléatoire
calque^ random;
random = init(taille, 1);
for(int i=0; i<taille ; i++) {
for(int j=0; j<taille ; j++) {
random->v[i, j] = random->aleatoire(256); //Qu'est-ce que je disais...
}
}
Perlin::enregistrer_bmp(random, L"alea.bmp");
//Calques de travail
float persistance_courante = persistance;
array<calque^>^ mes_calques = gcnew array<calque^>(octaves);
for(int i=0 ; i<octaves ; i++) {
mes_calques[i] = calque::init(taille, persistance_courante);
persistance_courante *= persistance;
}
//Remplissage des calques
int f_courante = frequence;
for (int n=0; n<octaves; n++) {
for(int i=0; i<taille; i++) {
for(int j=0; j<taille; j++) {
int a = random->valeur_interpolee(i, j, f_courante);
mes_calques[n]->v[i, j] = a;
}
}
f_courante*=frequence;
}
//Ajout de tous les calques (en gros, c'est une moyenne pondérée)
/* calcul de la somme de toutes les persistances,
pour ramener les valeurs dans un intervalle acceptable */
float sum_persistances = 0;
for (int i=0; i<octaves; i++)
sum_persistances += mes_calques[i]->persistance;
/* ajout des calques successifs */
for (int i=0; i<taille; i++) {
for (int j=0; j<taille; j++) {
float sum = 0;
for (int n=0; n<octaves; n++) {
sum += mes_calques[n]->v[i, j] * mes_calques[n]->persistance;
}
/* normalisation */
c->v[i, j] = (int)(sum / sum_persistances);
}
}
/* lissage */
calque ^lissage;
lissage = calque::init(taille, 0);
for (int x=0; x<taille; x++) {
for (int y=0; y<taille; y++) {
int a=0;
int n=0;
for (int k=x-liss; k<=x+liss; k++) {
for (int l=y-liss; l<=y+liss; l++) {
if ((k >= 0) && (k<taille) && (l>=0) && (l<taille)) {
n++;
a += c->v[k, l];
}
}
}
lissage->v[x, y] = (int)( (float)a/n );
}
}
Perlin::enregistrer_bmp(lissage, L"lisse.bmp");
}
int calque::valeur_interpolee(int i, int j, int frequence)
{
//déterminations des bornes
int borne1x, borne1y, borne2x, borne2y, q;
float pas;
pas = (float)this->taille/frequence;
q = (int)( (float)i/pas );
borne1x = (int)( q*pas );
borne2x = (int)( (q+1)*pas );
if(borne2x >= this->taille)
borne2x = this->taille-1;
q = (int)( (float)j/pas );
borne1y = (int)( q*pas );
borne2y = (int)( (q+1)*pas );
if(borne2y >= this->taille)
borne2y = this->taille-1;
/* récupérations des valeurs aléatoires aux bornes */
int b00,b01,b10,b11;
b00 = this->v[borne1x, borne1y];
b01 = this->v[borne1x, borne2y];
b10 = this->v[borne2x, borne1y];
b11 = this->v[borne2x, borne2y];
int v1 = interpolate(b00, b01, borne2y-borne1y, j-borne1y);
int v2 = interpolate(b10, b11, borne2y-borne1y, j-borne1y);
int fin = interpolate(v1, v2, borne2x-borne1x , i-borne1x);
return fin;
}
int calque::interpolate(int y1, int y2, int n, int delta) {
//Interpolation linéaire
/*
if (n!=0)
return y1 delta*(y2-y1)/n;
else
return y1;
*/
//Interpolation non linéaire
if (n==0)
return y1;
if (n==1)
return y2;
float a = (float)delta/n;
double v1 = 3*Math::Pow(1-a, 2) - 2*Math::Pow(1-a,3);
double v2 = 3*Math::Pow(a, 2) - 2*Math::Pow(a, 3);
return (int)( y1*v1 + y2*v2 );
}
//
//----------------------------------------------------------------------------
//Fonctions de main.c
#using <System.Drawing.dll>
//#include "Perlin_main.h"
//#include "Perlin_calque.h"
using namespace System;
void Perlin::enregistrer_bmp(calque^ c, String^ filename) {
System::Drawing::Bitmap^ s = gcnew System::Drawing::Bitmap(c->taille, c->taille, System::Drawing::Imaging::PixelFormat::Format24bppRgb);
//On boucle sur chaque pixel
for(int i=0; i<c->taille ; i++) {
for(int j=0; j<c->taille ; j++) {
System::Drawing::Color u = System::Drawing::Color::FromArgb(255, c->v[i, j], c->v[i, j], c->v[i, j]);
s->SetPixel(i, j, u);
}
}
s->Save(filename, System::Drawing::Imaging::ImageFormat::Bmp);
delete s; //This maps into s->Dispose();
}
int Perlin::Main(cli::array<System::String ^,1> ^args)
{
// valeurs d'entree
int octaves = 3;
int frequence = 4;
float persistence = 0.5;
int taille_de_sortie = 200;
int lissage = 3;
calque^ s;
s = calque::init(taille_de_sortie, 1);
s->generer(frequence, octaves, persistence, lissage);
enregistrer_bmp(s, L"resultat.bmp");
return 0;
} |
Partager