class ImageBuffer
{
friend class FillColour;
private:
Ogre::uchar* mPixels;
unsigned int mWidth;
unsigned int mHeight;
private:
void setPixel(size_t x, size_t y, Ogre::ColourValue colour);
void setPixel(size_t x, size_t y, Ogre::Real red, Ogre::Real green, Ogre::Real blue, Ogre::Real alpha = 1.0f);
public:
ImageBuffer(unsigned int width_height);
~ImageBuffer();
void saveImage(Ogre::String filename);
};
class FillColour
{
private:
ImageBuffer* mBuffer;
Ogre::ColourValue mColour;
public:
FillColour(ImageBuffer* pBuffer)
: mBuffer(pBuffer), mColour(Ogre::ColourValue::Black)
{
}
/** Sets the colour of texture */
inline FillColour & setColour(Ogre::ColourValue colour)
{
mColour = colour;
return *this;
}
void process();
void setPixl(size_t x, size_t y, Ogre::ColourValue color);
};
ImageBuffer::ImageBuffer(unsigned int width_height)
: mWidth(width_height), mHeight(width_height)
{
mPixels = new Ogre::uchar[mWidth * mHeight * 4];
// set R=0, G=0, B=0, A=255
for(size_t p = 0; p < (mWidth * mHeight * 4); p ++)
{
mPixels[p] = (p % 4 == 0 && p > 0) ? 255 : 0;
}
}
ImageBuffer::~ImageBuffer()
{
delete mPixels;
}
void ImageBuffer::setPixel(size_t x, size_t y, Ogre::ColourValue colour)
{
setPixel(x, y, colour.r, colour.g, colour.b, colour.a);
}
void ImageBuffer::setPixel(size_t x, size_t y, Ogre::Real red, Ogre::Real green, Ogre::Real blue, Ogre::Real alpha)
{
mPixels[y * mWidth * 4 + x * 4 + 3] = (Ogre::uchar)(red * 255.0f);
mPixels[y * mWidth * 4 + x * 4 + 2] = (Ogre::uchar)(green * 255.0f);
mPixels[y * mWidth * 4 + x * 4 + 1] = (Ogre::uchar)(blue * 255.0f);
mPixels[y * mWidth * 4 + x * 4 + 0] = (Ogre::uchar)(alpha * 255.0f);
}
void ImageBuffer::saveImage(Ogre::String filename)
{
Ogre::Image* image = new Ogre::Image();
image->loadDynamicImage(mPixels, mWidth, mHeight, 1, Ogre::PF_R8G8B8A8);
image->save(filename);
delete image;
}
void FillColour::process()
{
for(size_t y = 0; y < mBuffer->mHeight; y++)
{
for(size_t x = 0; x < mBuffer->mWidth; x++)
{
mBuffer->setPixel(x, y, mColour);
}
}
}
void FillColour::setPixl(size_t x, size_t y, Ogre::ColourValue color){
mBuffer->setPixel(x,y, color);
}
#include "PerlinGenerator.cpp"
#include "Imagestuff.cpp"
#include
class PerlinTest{
public:
PerlinTest(float size, float scale);
PerlinTest(float size, float scale, float zdepth);
};
PerlinTest::PerlinTest(float size, float scale){
Ogre::Log* tlog = Ogre::LogManager::getSingleton().createLog("Perlin.log");
std::ostringstream ss5;
ss5<< "Test" << "\n";
tlog->logMessage(ss5.str());
PerlinGenerator* pgen = new PerlinGenerator(size, scale);
//ss5<<"Test2 " <<"\n";
//tlog->logMessage(ss5.str());
vector
ImageBuffer buffer(size);
FillColour* fill = new FillColour (&buffer);
//buffer.saveImage("test1.png");
for (int i = 0; i
fill->setPixl((size_t)i, (size_t)j, col);
}
}
//tlog->logMessage(ss5.str());
buffer.saveImage("test2.png");
}
PerlinTest::PerlinTest(float size, float scale, float zdepth){
Ogre::Log* tlog = Ogre::LogManager::getSingleton().createLog("Perlin.log");
std::ostringstream ss5;
ss5<< "Test" << "\n";
tlog->logMessage(ss5.str());
PerlinGenerator* pgen = new PerlinGenerator(size, scale, zdepth);
//ss5<<"Test2 " <<"\n";
//tlog->logMessage(ss5.str());
vector
ImageBuffer buffer(size);
FillColour* fill = new FillColour (&buffer);
//buffer.saveImage("test1.png");
//no test on z depth. This function is designed a single 2 dimensional slice
//implementation of the test provides a number 2
for (int i = 0; i
//ss5<<"Color value: "<
fill->setPixl((size_t)i, (size_t)j, col);
}
}
}
//tlog->logMessage(ss5.str());
buffer.saveImage("test2.png");
}
This test class actually is adapted from some code that includes image writing packaging inside Ogre.
I'd possibly write a binary 16 bit raw heightmap encode process but it appears Ogre provides a lot of codec encoding stuff that appears to handle much of this.
So this class passes off to the middle class PerlinGenerator which in turn gets and processes the data needed...
Here's a three dimension implementation say in your Ogre screen method:
PerlinTest* test = new PerlinTest(1200.0f,20.0f, 2.0f);
This is only written for obtaining one 2d slice at the moment so any parameter other than 2.0f will crash for three dimensional computations.
No comments:
Post a Comment