Files
libsst/ZTestSuite/Test-ZSimplexNoise.cpp
2026-04-03 00:22:39 -05:00

125 lines
2.9 KiB
C++

/*
Test-ZSimplexNoise.cpp
Author: Chris Ertel
Purpose : Unit tests for ZSimplexNoise
Changelog :
05/11/2012 - Removed dependency on FreeImage
12/08/2011 - Removed dependency on ZString (crertel)
15/02/2011 - Created (crertel)
*/
#include "ZUnitTest.hpp"
#include <ZUtil/ZSimplexNoise.hpp>
#include <iostream>
static const char* test2DSimplexNoise();
//List of unit tests
ZUnitTest ZSimplexNoiseUnitTests[] =
{
{
"test2DSimplexNoise",
test2DSimplexNoise
},
};
DECLARE_ZTESTBLOCK(ZSimplexNoise);
static void SaveTGA(const char* filename, const char* bitmap)
{
FILE* fp = fopen(filename, "wb");
if(fp == NULL)
return;
fputc(0, fp); //Image ID length
fputc(0, fp); //Color map type
fputc(2, fp); //Image type. 2 = uncompressed true color
char zero[5] = { 0, 0, 0, 0, 0 };
fwrite(zero, 5, sizeof(char), fp); //5 bytes of zero for colormap
fputc(0, fp); fputc(0, fp); //X image origin: 0x0000
fputc(0, fp); fputc(0, fp); //Y image origin: 0x0000
fputc(0x00, fp); fputc(0x01, fp); //X image width: 0x0100
fputc(0x00, fp); fputc(0x01, fp); //Y image width: 0x0100
fputc(0x20, fp); //Image bits per pixel (32)
fputc((8<<0) | (2<<4), fp); //bits 0-3: alpha channel depth, bits 4-5: image origin, 2 = top-left
fwrite(bitmap, 256*256, 4, fp);
fclose(fp);
}
/*************************************************************************/
static const char* test2DSimplexNoise()
{
char* bitmap = NULL;
ZSimplexNoise noisegen;
int i;
int j;
bitmap = new char[256*256*4];
noisegen.reseed(0);
for (i = 0; i <256; i++)
{
for (j = 0; j <256; j++)
{
double noiseval = noisegen.noise2( ((float) 2*i)/256.0f,((float) 2*j)/256.0f);
bitmap[ (i*4*256) + (4*j)+ 0] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 1] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 2] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 3] = -1;
}
}
SaveTGA("testnoise1.tga", bitmap);
noisegen.reseed(1);
for (i = 0; i <256; i++)
{
for (j = 0; j <256; j++)
{
double noiseval = noisegen.noise2( ((float) 2*i)/256.0f,((float) 2*j)/256.0f);
bitmap[ (i*4*256) + (4*j)+ 0] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 1] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 2] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 3] = -1;
}
}
SaveTGA("testnoise2.tga", bitmap);
noisegen.reseed(2);
for (i = 0; i <256; i++)
{
for (j = 0; j <256; j++)
{
double noiseval = noisegen.noise2( ((float) 2*i)/256.0f,((float) 2*j)/256.0f);
bitmap[ (i*4*256) + (4*j)+ 0] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 1] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 2] = (char)( 128.0f + (128.0f * (noiseval)));
bitmap[ (i*4*256) + (4*j)+ 3] = -1;
}
}
SaveTGA("testnoise3.tga", bitmap);
delete[] bitmap;
return ZTEST_SUCCESS;
}