Perlin Noise Help
Aug 11, 2014 at 9:40pm UTC
Hi
I'm trying to use
https://github.com/nothings/stb/blob/master/stb_perlin.h for some perlin noise to create a image for a procedural map.
I'm not sure how to use it.
I created the following code that uses it but a little confused how to map it since it uses float. Do I loop through each x,y,z and give the z a new value turning the float to a char(up to 256bit)?
Vivienne
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
void Image::FreeImageData(unsigned char * pixelData)
{
if (!pixelData)
return ;
stbi_image_free(pixelData);
}
bool Image::GeneratePerlin(int width, int height, unsigned components)
{
// create a location for pixelData
unsigned char * pixelData;
int depth;
// not sure of what the following is
components=4; // rgba 4 components
depth=1; // meaning 8-bit
unsigned dataSize = height*width*depth*components;
// code
SharedArrayPtr<unsigned char > buffer(new unsigned char [dataSize]);
// null the memory area
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
for (int componentsidx=0; componentsidx<components; componentsidx++)
{
buffer[x*y*componentsidx]=0;
}
}
}
// Manipulation here
// point pixelData to buffer
pixelData=buffer;
// set image information
SetSize(width, height, depth, components);
SetData(pixelData);
FreeImageData(pixelData);
return false ;
}
Aug 12, 2014 at 3:31am UTC
Why are you taking in components and depth if you're just going to end up hardcoding a set value?
You multiply the unsigned char by a common value.
Aug 12, 2014 at 3:47am UTC
I am planning to change it in the future. The code iis modified but I'm getting a cross on the following. Which I'm not sure whats wrong.
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
void Image::CopyData(const unsigned char * pixelData)
{
if (!data_)
return ;
memcpy(data_, pixelData, width_ * height_ * depth_ * components_);
}
Class Info
rivate:
/// Decode an image using stb_image.
static unsigned char * GetImageData(Deserializer& source, int & width, int & height, unsigned & components);
/// Free an image file's pixel data.
static void FreeImageData(unsigned char * pixelData);
/// Width.
int width_;
/// Height.
int height_;
/// Depth.
int depth_;
/// Number of color components.
unsigned components_;
/// Number of compressed mip levels.
unsigned numCompressedLevels_;
/// Compressed format.
CompressedFormat compressedFormat_;
/// Pixel data.
SharedArrayPtr<unsigned char > data_;
/// Precalculated mip level image.
SharedPtr<Image> nextLevel_;
};
Aug 12, 2014 at 3:50am UTC
"Class" should be lowercase (eg. class
) and you're missing the p on private
.
Last edited on Aug 12, 2014 at 3:51am UTC
Topic archived. No new replies allowed.