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
|
inline unsigned int MakeTexFromImg(GTexture::Image *i) {
unsigned int id = 0;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, i->format, i->width, i->height, 0, i->format, GL_UNSIGNED_BYTE, i->data);
delete [] i->data;
delete i;
return id;
}
unsigned int MakePerlinNoiseTexture( VGL_PerlinNoiseImg* perlin ,int width,int height) {
GTexture::Image *img = new GTexture::Image;
img->data=NULL;
img->width = width;
img->height = height;
img->format = GL_RGBA;
img->data = new unsigned char [img->width * img->height * 4];
float p[ 2 ];
int pos = 0;
Vector3 col = Vector3(1,1,1);
float fHeight = (float)height;
float delta = perlin->scale.x / (float)width;
for ( int y=0; y<height; y++ ) {
p[0] = (float)y / fHeight * perlin->scale.y;
p[1] = 0.0f;
for ( int x=0; x<width; x++ ) {
p[1] += delta;
col = perlin->GetColor( p );
for ( int d=0; d<3; d++ ) img->data[ pos++ ] = FloatToByte( col.v[ d ] * 255.0f );
img->data[ pos++ ] = 255; // alpha channel
}
}
return MakeTexFromImg(img);
}
/// la structure GTexture::Image en question
struct Image
{
int width, height;
unsigned int format;
unsigned char *data;
}; |
Partager