Smooth 2D Noise Viewer
Perlin and Value Noise
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CMain Class Reference

The main class. More...

#include <CMain.h>

Public Member Functions

 CMain (const HWND hwnd)
 Constructor. More...
 
 ~CMain ()
 Destructor. More...
 
void CreateBitmap (int w, int h)
 Create bitmap. More...
 
void ClearBitmap (Gdiplus::Color)
 Clear bitmap to color. More...
 
void Randomize ()
 Randomize PRNG. More...
 
void GenerateNoiseBitmap (eNoise)
 Generate Perlin or Value noise bitmap. More...
 
void GenerateNoiseBitmap ()
 Generate bitmap again with saved parameters. More...
 
bool SetDistribution (eDistribution)
 Set probability distribution. More...
 
void SetSpline (eSpline)
 Set spline function. More...
 
void SetHash (eHash)
 Set hash function. More...
 
void ToggleViewCoords ()
 Toggle View Coordinates flag. More...
 
void ToggleViewGrid ()
 Toggle View Grid flag. More...
 
void Jump ()
 Change origin coordinates. More...
 
void Jump (float x, float y)
 Change origin coordinates. More...
 
const bool Origin (float x, float y) const
 Check origin coordinates. More...
 
void IncreaseOctaves ()
 Increase number of octaves. More...
 
void DecreaseOctaves ()
 Decrease number of octaves. More...
 
void IncreaseScale ()
 Increase scale. More...
 
void DecreaseScale ()
 Decrease scale. More...
 
void IncreaseTableSize ()
 Increase table size. More...
 
void DecreaseTableSize ()
 Decrease table size. More...
 
void Reset ()
 Reset number of octaves, scale, table size. More...
 
void OnPaint ()
 Paint the client area of the window. More...
 
Gdiplus::Bitmap * GetBitmap () const
 Get pointer to bitmap. More...
 
const std::wstring GetFileName () const
 Get noise file name. More...
 
const std::wstring GetNoiseDescription () const
 Get noise description. More...
 

Private Member Functions

void CreateMenus ()
 Create menus. More...
 
void UpdateMenus ()
 Update menus. More...
 
void SetPixel (UINT, UINT, float)
 Set pixel grayscale from float. More...
 
void SetPixel (UINT, UINT, BYTE)
 Set pixel grayscale from byte. More...
 
void SetPixel (UINT, UINT, Gdiplus::Color)
 Set pixel from GDI+ color. More...
 
void DrawCoords ()
 Draw coordinates to bitmap. More...
 
void DrawGrid ()
 Draw grid to bitmap. More...
 
void GenerateNoiseBitmap (Gdiplus::PointF, Gdiplus::RectF)
 Generate bitmap rectangle. More...
 

Private Attributes

HWND m_hWnd = nullptr
 Window handle. More...
 
HMENU m_hFileMenu = nullptr
 Handle to the File menu. More...
 
HMENU m_hGenMenu = nullptr
 Handle to the Generate menu. More...
 
HMENU m_hViewMenu = nullptr
 Handle to the View menu. More...
 
HMENU m_hSetMenu = nullptr
 Handle to the Settings menu. More...
 
HMENU m_hDistMenu = nullptr
 Handle to the Distribution menu. More...
 
HMENU m_hHashMenu = nullptr
 Handle to the Hash menu. More...
 
HMENU m_hSplineMenu = nullptr
 Handle to the Spline menu. More...
 
HMENU m_hOctaveMenu = nullptr
 Handle to the Octave menu. More...
 
eNoise m_eNoise = eNoise::None
 Noise type. More...
 
float m_fOriginX = 0.0f
 X-coordinate of top. More...
 
float m_fOriginY = 0.0f
 Y-coordinate of left. More...
 
const size_t m_nDefOctaves = 4
 Default number of octaves of noise. More...
 
size_t m_nOctaves = m_nDefOctaves
 Number of octaves of noise. More...
 
const size_t m_nMinOctaves = 1
 Minimum number of octaves of noise. More...
 
const size_t m_nMaxOctaves = 8
 Maximum number of octaves of noise. More...
 
const float m_fDefScale = 64.0f
 Default scale. More...
 
float m_fScale = m_fDefScale
 Scale. More...
 
const float m_fMinScale = 8.0f
 Minimum scale. More...
 
const float m_fMaxScale = 512.0f
 Minimum scale. More...
 
float m_fMin = 0.0f
 Smallest noise value in generated noise. More...
 
float m_fMax = 0.0f
 Largest noise value in generated noise. More...
 
float m_fAve = 0.0f
 Average noise value in generated noise. More...
 
ULONG_PTR m_gdiplusToken = 0
 GDI+ token. More...
 
Gdiplus::Bitmap * m_pBitmap = nullptr
 Pointer to a bitmap image. More...
 
CPerlinNoise2Dm_pPerlin = nullptr
 Pointer to Perlin noise generator. More...
 
bool m_bShowCoords = false
 Show coordinates flag. More...
 
bool m_bShowGrid = false
 Show grid flag. More...
 

Detailed Description

The interface between I/O from Windows (input from the drop-down menus, output to the client area of the window), the noise generator, and the GDI+ graphics interface. This class maintains a single GDI+ bitmap to which all noise and related elements (coordinates, grids) are drawn.

Definition at line 41 of file CMain.h.

Constructor & Destructor Documentation

◆ CMain()

CMain::CMain ( const HWND  hwnd)

Initialize GDI+, create the menus, and create the Perlin noise generator.

Parameters
hwndWindow handle.

Definition at line 43 of file CMain.cpp.

◆ ~CMain()

CMain::~CMain ( )

Delete the Perlin noise generator, delete the GDI+ objects, shut down GDI+.

Definition at line 51 of file CMain.cpp.

Member Function Documentation

◆ ClearBitmap()

void CMain::ClearBitmap ( Gdiplus::Color  clr)

Clear the bitmap.

Parameters
clrColor to set the pixels of bitmap to.

Definition at line 184 of file CMain.cpp.

◆ CreateBitmap()

void CMain::CreateBitmap ( int  w,
int  h 
)

Create bitmap and set all pixels to white.

Parameters
wBitmap width in pixels.
hBitmap height in pixels.

Definition at line 175 of file CMain.cpp.

◆ CreateMenus()

void CMain::CreateMenus ( )
private

Create and add menus to the menu bar.

Definition at line 112 of file CMain.cpp.

◆ DecreaseOctaves()

void CMain::DecreaseOctaves ( )

Decrease the number of octaves in m_nOctaves by one down to a minimum of m_nMinOctaves and regenerate the noise bitmap.

Definition at line 504 of file CMain.cpp.

◆ DecreaseScale()

void CMain::DecreaseScale ( )

Decrease the scale in m_fScale by a factor of 2 down to a minimum of m_fMinScale and regenerate the noise bitmap.

Definition at line 520 of file CMain.cpp.

◆ DecreaseTableSize()

void CMain::DecreaseTableSize ( )

Decrease the table size by a factor of 2 and regenerate the noise bitmap.

Definition at line 534 of file CMain.cpp.

◆ DrawCoords()

void CMain::DrawCoords ( )
private

If m_bShowCoords is true, then draw the coordinates of the top left and bottom right of the noise to the corresponding corners of the bitmap. The font family, font, style, and color of the text are hard-coded. If m_bShowCoords is false, then overwrite the pixels within the bounding rectangle of the text coordinates with the corresponding noise. This enables us to remove the coordinate text from the bitmap without having to regenerate the whole bitmap.

Definition at line 297 of file CMain.cpp.

◆ DrawGrid()

void CMain::DrawGrid ( )
private

If m_bShowGrid is true, draw a grid for first noise octave to the bitmap. The line color for the grid is fixed. If m_bShowGrid is false, then overwrite the pixels in the lines with the corresponding noise. This enables us to remove the grid lines from the bitmap without having to regenerate the whole bitmap.

Definition at line 344 of file CMain.cpp.

◆ GenerateNoiseBitmap() [1/3]

void CMain::GenerateNoiseBitmap ( )

Generate the noise bitmap using the last noise type. Call this function when some other noise parameter has changed.

Definition at line 391 of file CMain.cpp.

◆ GenerateNoiseBitmap() [2/3]

void CMain::GenerateNoiseBitmap ( eNoise  t)

Generate Perlin or Value noise into the bitmap. Pixel coordinates (which are whole numbers) are offset by m_fOriginX and m_fOriginY and scaled by m_fScale to get noise coordinates (which are floating point numbers).

Parameters
tType of noise.

Definition at line 233 of file CMain.cpp.

◆ GenerateNoiseBitmap() [3/3]

void CMain::GenerateNoiseBitmap ( Gdiplus::PointF  point,
Gdiplus::RectF  rect 
)
private

Generate Perlin or Value noise into a rectangle in the bitmap. This is the cool thing about Perlin noise - the noise value at each point is computed independently of all the other points. We will use this to quickly remove the grid and/or the coordinate text from the bitmap without having to recompute noise values for the whole bitmap.

Parameters
pointThe position of the rectangle to be written in the bitmap.
rectThe bounds of the rectangle to be written in the bitmap.

Definition at line 273 of file CMain.cpp.

◆ GetBitmap()

Gdiplus::Bitmap * CMain::GetBitmap ( ) const

Reader function for the bitmap pointer m_pBitmap.

Returns
The bitmap pointer m_pBitmap.

Definition at line 695 of file CMain.cpp.

◆ GetFileName()

const std::wstring CMain::GetFileName ( ) const

Make up a file name from the noise parameters.

Returns
A file name with no spaces and without extension.

Definition at line 559 of file CMain.cpp.

◆ GetNoiseDescription()

const std::wstring CMain::GetNoiseDescription ( ) const

Get noise description including type of noise and its parameters.

Returns
Wide string noise description.

Definition at line 598 of file CMain.cpp.

◆ IncreaseOctaves()

void CMain::IncreaseOctaves ( )

Increase the number of octaves in m_nOctaves by one up to a maximum of m_nMaxOctaves and regenerate the noise bitmap.

Definition at line 496 of file CMain.cpp.

◆ IncreaseScale()

void CMain::IncreaseScale ( )

Increase the scale in m_fScale by a factor of 2 up to a maximum of m_fMaxScale and regenerate the noise bitmap.

Definition at line 512 of file CMain.cpp.

◆ IncreaseTableSize()

void CMain::IncreaseTableSize ( )

Increase the table size by a factor of 2 and regenerate the noise bitmap.

Definition at line 527 of file CMain.cpp.

◆ Jump() [1/2]

void CMain::Jump ( )

Increment both coordinates of the origin by table size and regenerate the noise bitmap.

Definition at line 467 of file CMain.cpp.

◆ Jump() [2/2]

void CMain::Jump ( float  x,
float  y 
)

Set the coordinates of the origin and regenerate the noise bitmap.

Parameters
xNew X-coordinate of origin.
yNew Y-coordinate of origin.

Definition at line 478 of file CMain.cpp.

◆ OnPaint()

void CMain::OnPaint ( )

Draw the bitmap to the window client area, scaled down if necessary. This function should only be called in response to a WM_PAINT message.

Definition at line 67 of file CMain.cpp.

◆ Origin()

const bool CMain::Origin ( float  x,
float  y 
) const

Check origin coordinates.

Parameters
xDesired X-coordinate of origin.
yDesired Y-coordinate of origin.
Returns
true if the origin coordinates are the desired coordinates.

Definition at line 489 of file CMain.cpp.

◆ Randomize()

void CMain::Randomize ( )

Change the seed for the noise generator's pseudo-random number generator, update the gradient/value table using the current distribution, and regenerate the noise bitmap.

Definition at line 406 of file CMain.cpp.

◆ Reset()

void CMain::Reset ( )

Reset number of octaves, scale, and table size to defaults and regenerate the noise bitmap.

Definition at line 542 of file CMain.cpp.

◆ SetDistribution()

bool CMain::SetDistribution ( eDistribution  d)

Change Perlin noise probability distribution and regenerate noise. This will have no effect if the new distribution is the same as the old one.

Parameters
dProbability distribution enumerated type.
Returns
true if the distribution changed.

Definition at line 417 of file CMain.cpp.

◆ SetHash()

void CMain::SetHash ( eHash  d)

Set Perlin noise hash function and regenerate noise.

Parameters
dHash function enumerated type.

Definition at line 440 of file CMain.cpp.

◆ SetPixel() [1/3]

void CMain::SetPixel ( UINT  i,
UINT  j,
BYTE  b 
)
private

Set a grayscale pixel in the bitmap from a byte value in the range \([0, 255]\), where \(0\) is black and \(255\) is white. The indices of the pixel are assumed to be in range.

Parameters
iRow number.
jColumn number.
bGrayscale value in the range \([0, 255]\).

Definition at line 207 of file CMain.cpp.

◆ SetPixel() [2/3]

void CMain::SetPixel ( UINT  i,
UINT  j,
float  g 
)
private

Set a grayscale pixel in the bitmap from a floating point value in \([-1, 1]\), where \(-1\) is black and \(+1\) is white. The indices of the pixel are assumed to be in range.

Parameters
iRow number.
jColumn number.
gGrayscale value in the range \([-1, 1]\).

Definition at line 196 of file CMain.cpp.

◆ SetPixel() [3/3]

void CMain::SetPixel ( UINT  i,
UINT  j,
Gdiplus::Color  clr 
)
private

Set a pixel in the bitmap to a GDI+ color. The indices of the pixel are assumed to be in range.

Parameters
iRow number.
jColumn number.
clrA GDI+ color.

Definition at line 217 of file CMain.cpp.

◆ SetSpline()

void CMain::SetSpline ( eSpline  d)

Set Perlin noise spline function and regenerate noise.

Parameters
dSpline function enumerated type.

Definition at line 431 of file CMain.cpp.

◆ ToggleViewCoords()

void CMain::ToggleViewCoords ( )

Toggle the View Coordinates flag, put a checkmark next to the menu item, and draw or undraw the coordinates on the bitmap.

Definition at line 449 of file CMain.cpp.

◆ ToggleViewGrid()

void CMain::ToggleViewGrid ( )

Toggle the View Grid flag, put a checkmark next to the menu item, and draw or undraw the grid on the bitmap..

Definition at line 458 of file CMain.cpp.

◆ UpdateMenus()

void CMain::UpdateMenus ( )
private

Update all menus, that is, gray out inactive menu items and place a checkmark next to chosen menu items.

Definition at line 131 of file CMain.cpp.

Member Data Documentation

◆ m_bShowCoords

bool CMain::m_bShowCoords = false
private

Definition at line 78 of file CMain.h.

◆ m_bShowGrid

bool CMain::m_bShowGrid = false
private

Definition at line 79 of file CMain.h.

◆ m_eNoise

eNoise CMain::m_eNoise = eNoise::None
private

Definition at line 54 of file CMain.h.

◆ m_fAve

float CMain::m_fAve = 0.0f
private

Definition at line 71 of file CMain.h.

◆ m_fDefScale

const float CMain::m_fDefScale = 64.0f
private

Definition at line 64 of file CMain.h.

◆ m_fMax

float CMain::m_fMax = 0.0f
private

Definition at line 70 of file CMain.h.

◆ m_fMaxScale

const float CMain::m_fMaxScale = 512.0f
private

Definition at line 67 of file CMain.h.

◆ m_fMin

float CMain::m_fMin = 0.0f
private

Definition at line 69 of file CMain.h.

◆ m_fMinScale

const float CMain::m_fMinScale = 8.0f
private

Definition at line 66 of file CMain.h.

◆ m_fOriginX

float CMain::m_fOriginX = 0.0f
private

Definition at line 56 of file CMain.h.

◆ m_fOriginY

float CMain::m_fOriginY = 0.0f
private

Definition at line 57 of file CMain.h.

◆ m_fScale

float CMain::m_fScale = m_fDefScale
private

Definition at line 65 of file CMain.h.

◆ m_gdiplusToken

ULONG_PTR CMain::m_gdiplusToken = 0
private

Definition at line 73 of file CMain.h.

◆ m_hDistMenu

HMENU CMain::m_hDistMenu = nullptr
private

Definition at line 49 of file CMain.h.

◆ m_hFileMenu

HMENU CMain::m_hFileMenu = nullptr
private

Definition at line 45 of file CMain.h.

◆ m_hGenMenu

HMENU CMain::m_hGenMenu = nullptr
private

Definition at line 46 of file CMain.h.

◆ m_hHashMenu

HMENU CMain::m_hHashMenu = nullptr
private

Definition at line 50 of file CMain.h.

◆ m_hOctaveMenu

HMENU CMain::m_hOctaveMenu = nullptr
private

Definition at line 52 of file CMain.h.

◆ m_hSetMenu

HMENU CMain::m_hSetMenu = nullptr
private

Definition at line 48 of file CMain.h.

◆ m_hSplineMenu

HMENU CMain::m_hSplineMenu = nullptr
private

Definition at line 51 of file CMain.h.

◆ m_hViewMenu

HMENU CMain::m_hViewMenu = nullptr
private

Definition at line 47 of file CMain.h.

◆ m_hWnd

HWND CMain::m_hWnd = nullptr
private

Definition at line 43 of file CMain.h.

◆ m_nDefOctaves

const size_t CMain::m_nDefOctaves = 4
private

Definition at line 59 of file CMain.h.

◆ m_nMaxOctaves

const size_t CMain::m_nMaxOctaves = 8
private

Definition at line 62 of file CMain.h.

◆ m_nMinOctaves

const size_t CMain::m_nMinOctaves = 1
private

Definition at line 61 of file CMain.h.

◆ m_nOctaves

size_t CMain::m_nOctaves = m_nDefOctaves
private

Definition at line 60 of file CMain.h.

◆ m_pBitmap

Gdiplus::Bitmap* CMain::m_pBitmap = nullptr
private

Definition at line 75 of file CMain.h.

◆ m_pPerlin

CPerlinNoise2D* CMain::m_pPerlin = nullptr
private

Definition at line 76 of file CMain.h.