Marbellous
Marbellous, a WebApp to generate paper marbling patterns.
Classes | Macros | Functions | Variables
main.cpp File Reference
#include <emscripten.h>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <random>
#include "WGLContext.hpp"
#include "WGLSceneRenderer.hpp"
#include "WGLRakeRenderer.hpp"
#include "Scene.hpp"
#include "Options.hpp"
Include dependency graph for main.cpp:

Classes

struct  DropData
 All relevant data for a single drop grouped as a struct. More...
 

Macros

#define checkSetup(RET)
 Check wether the backend is initialized or not. More...
 
#define checkState(STATE, RET)
 Check wether the backend is currently in the state given by STATE. More...
 

Functions

void _initWGLContext (const char *canvasID, size_t x)
 Used to initialize the WGL context. More...
 
int EMSCRIPTEN_KEEPALIVE addDrop (float, float, float, unsigned)
 
int EMSCRIPTEN_KEEPALIVE finishDrop (int)
 
template<typename C , typename R >
void sprinkle (int amt, C &coord, R &radius)
 Generate drops of random size and position. More...
 
void EMSCRIPTEN_KEEPALIVE initBackend (const char canvasID[], size_t dropRes, size_t rakeRes)
 Init function. Should be called first. More...
 
int EMSCRIPTEN_KEEPALIVE addPalette (size_t num_colors)
 Add a new palette. More...
 
int EMSCRIPTEN_KEEPALIVE setActivePalette (int const id)
 Set an active palette. More...
 
int EMSCRIPTEN_KEEPALIVE setColorAt (size_t const colorNumber, unsigned int const color)
 Set the number at position colorNumber of the active palette. More...
 
int EMSCRIPTEN_KEEPALIVE setColorRatioAt (size_t const colorNumber, unsigned const ratio)
 Set the the ratio of a color in the active palette. More...
 
void EMSCRIPTEN_KEEPALIVE setBGColor (unsigned int const color)
 Set the background color. More...
 
void EMSCRIPTEN_KEEPALIVE setFilter (bool filter)
 Set the filter option. More...
 
int EMSCRIPTEN_KEEPALIVE addDrop (float const x, float const y, float r, unsigned int const color)
 Add a new color drop. More...
 
void EMSCRIPTEN_KEEPALIVE addDrops (int count, DropData drops[])
 Add multiple drops at once. More...
 
void EMSCRIPTEN_KEEPALIVE redraw ()
 Redraw the current data model based on the backend state. More...
 
void EMSCRIPTEN_KEEPALIVE resize (size_t dropSize, size_t rakeSize)
 Resize the canvas. More...
 
int EMSCRIPTEN_KEEPALIVE resizeDrops (float const newRadius)
 
int EMSCRIPTEN_KEEPALIVE finishDrops ()
 Finish all drops applying their displacement on all old drops. More...
 
char *EMSCRIPTEN_KEEPALIVE getImage ()
 Return the current canvas content as image. More...
 
void EMSCRIPTEN_KEEPALIVE sprinkleLocal (int amt, float r_min, float r_max, float x, float y, float sig)
 Create drops around the cursor postion. More...
 
void EMSCRIPTEN_KEEPALIVE sprinkleGlobal (int amt, float r_min, float r_max)
 Create drops at random positions on the canvas. More...
 
void EMSCRIPTEN_KEEPALIVE clearCanvas ()
 Clear canvas content and data model. More...
 
void EMSCRIPTEN_KEEPALIVE rake (float x, float y, float period, float amplitude, float phase, bool nails[1000])
 
void EMSCRIPTEN_KEEPALIVE startRaking ()
 Set rake state. More...
 
void EMSCRIPTEN_KEEPALIVE startDropping ()
 Set drop state. More...
 
void EMSCRIPTEN_KEEPALIVE undoLastRake ()
 Undo the last rake. More...
 
int EMSCRIPTEN_KEEPALIVE main ()
 < Currently does nothing. Initialization is the frontend's job. More...
 

Variables

bool setupDone = false
 Stores wether backend is initialized or not. More...
 
WGLSceneRenderersceneRenderer
 global WGLSceneRenderer More...
 
Scenescene
 global scene More...
 
WGLRakeRendererrakeRenderer
 global WGLRakeRenderer More...
 

Macro Definition Documentation

◆ checkSetup

#define checkSetup (   RET)
Value:
if (!setupDone) \
{ \
fprintf(stderr, "%s: %s", __func__, ": Backend is not initialized yet!\n"); \
return RET; \
} \
bool setupDone
Stores wether backend is initialized or not.
Definition: main.cpp:37

Check wether the backend is initialized or not.

Parameters
RETreturn value of the function in case that the backend is not initialized.

◆ checkState

#define checkState (   STATE,
  RET 
)
Value:
if(Options::getInstance()->getState() != STATE) \
{ \
fprintf(stderr, "Drop function called in rake state or rake function called in drop state!\n"); \
return RET; \
} \
static Options *const getInstance()
Definition: Options.cpp:7

Check wether the backend is currently in the state given by STATE.

Parameters
STATEState to compare with. True for drop state, false for rake state
RETReturn value of the function in case of unmatched state.

Function Documentation

◆ _initWGLContext()

void _initWGLContext ( const char *  canvasID,
size_t  x 
)

Used to initialize the WGL context.

< Friend function to use for initialization.

Friend function of WGLContext

Parameters
canvasIDCSS identifier of the HTML5 canvas to use.
xResolution of the canvas will be set to x times x.
Here is the caller graph for this function:

◆ addDrop() [1/2]

int EMSCRIPTEN_KEEPALIVE addDrop ( float const  x,
float const  y,
float  r,
unsigned int const  color 
)

Add a new color drop.

Intended workflow for animation is to add a drop, keep its ID and resize it step wise. After the animation is done and the drop will not further grow call finishDrops().

Parameters
xx coordinate for the drop.
yy coordinate for the drop.
rRadius of the drop.
colorColor of th drop. This is a position inside a palette. Changing the active palette will result in a different color later on.
Returns
int Negative value in case of failure.
Parameters
colordraw a circle at point (x,y) (should be normed to [-1,1]^2) with radius r in the given color
Here is the call graph for this function:

◆ addDrop() [2/2]

int EMSCRIPTEN_KEEPALIVE addDrop ( float  ,
float  ,
float  ,
unsigned   
)

◆ addDrops()

void EMSCRIPTEN_KEEPALIVE addDrops ( int  count,
DropData  drops[] 
)

Add multiple drops at once.

Parameters
countNumber of drops.
dropsDrop data.
Attention
This is an unstable and experimental feature as it's hard to do with our model.
Here is the call graph for this function:

◆ addPalette()

int EMSCRIPTEN_KEEPALIVE addPalette ( size_t  num_colors)

Add a new palette.

Parameters
num_colorsNumber of colors to pre allocate.
Returns
int ID of this palette. May be used to set it active later.
Here is the call graph for this function:

◆ clearCanvas()

void EMSCRIPTEN_KEEPALIVE clearCanvas ( )

Clear canvas content and data model.

This can not be undone!

Here is the call graph for this function:

◆ finishDrop()

int EMSCRIPTEN_KEEPALIVE finishDrop ( int  )

◆ finishDrops()

int EMSCRIPTEN_KEEPALIVE finishDrops ( )

Finish all drops applying their displacement on all old drops.

Returns
int Negative value in case of failure.
Here is the call graph for this function:

◆ getImage()

char* EMSCRIPTEN_KEEPALIVE getImage ( )

Return the current canvas content as image.

Returns
char* Image as raw pixel buffer.
Here is the call graph for this function:

◆ initBackend()

void EMSCRIPTEN_KEEPALIVE initBackend ( const char  canvasID[],
size_t  dropRes,
size_t  rakeRes 
)

Init function. Should be called first.

Parameters
canvasIDCSS identifier of HTML5 canvas in the frontend.
dropResResolution of the canvas for drop mode.
rakeResResolution of the canvas for rake mode.
Attention
Currently dropRes and rakeRes need to be the same.
Here is the call graph for this function:

◆ main()

int EMSCRIPTEN_KEEPALIVE main ( )

< Currently does nothing. Initialization is the frontend's job.

◆ rake()

void EMSCRIPTEN_KEEPALIVE rake ( float  x,
float  y,
float  period,
float  amplitude,
float  phase,
bool  nails[1000] 
)

Rake paint on the canvas. Wraps around all edges.

Parameters
xStroke strength in x direction.
yStroke strength in y direction.
periodPeriod length of waves given as factor for canvas size.
amplitudeAmplitude of waves given as factor for canvas size.
phasePhase shift of waves given as factor for canvas size.
nailsBoolean array which describes the rake.
Here is the call graph for this function:

◆ redraw()

void EMSCRIPTEN_KEEPALIVE redraw ( )

Redraw the current data model based on the backend state.

Useful for color updates for example.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resize()

void EMSCRIPTEN_KEEPALIVE resize ( size_t  dropSize,
size_t  rakeSize 
)

Resize the canvas.

Parameters
dropSizeNew resolution for drop state.
rakeSizeNew resolution for rake state.
Attention
Currently both need to be the same.
Todo:
for now both need to be the same
Here is the call graph for this function:

◆ resizeDrops()

int EMSCRIPTEN_KEEPALIVE resizeDrops ( float const  newRadius)
Parameters
newRadiusresize all drops
Here is the call graph for this function:

◆ setActivePalette()

int EMSCRIPTEN_KEEPALIVE setActivePalette ( int const  id)

Set an active palette.

Parameters
idID of the palette to set active. Return value of addPalette()
Returns
int Negative in case of failure.
Here is the call graph for this function:

◆ setBGColor()

void EMSCRIPTEN_KEEPALIVE setBGColor ( unsigned int const  color)

Set the background color.

Parameters
colorColor in HEX Code
Here is the call graph for this function:

◆ setColorAt()

int EMSCRIPTEN_KEEPALIVE setColorAt ( size_t const  colorNumber,
unsigned int const  color 
)

Set the number at position colorNumber of the active palette.

Parameters
colorNumberNumber of color to set.
colorColor als HEX code.
Returns
int Negative in case of failure.
Here is the call graph for this function:

◆ setColorRatioAt()

int EMSCRIPTEN_KEEPALIVE setColorRatioAt ( size_t const  colorNumber,
unsigned const  ratio 
)

Set the the ratio of a color in the active palette.

Parameters
colorNumberNumber of the color to modify.
ratioNew ratio for sprinkler drops.
Returns
int Negative in case of failure.
Here is the call graph for this function:

◆ setFilter()

void EMSCRIPTEN_KEEPALIVE setFilter ( bool  filter)

Set the filter option.

Parameters
filterTrue for post processing filter, false otherwise.
Here is the call graph for this function:

◆ sprinkle()

template<typename C , typename R >
void sprinkle ( int  amt,
C &  coord,
R &  radius 
)

Generate drops of random size and position.

Template Parameters
CA function type for transforming random numbers to drop coordinates.
RA function type for transforming random numbers to drop sizes.
Parameters
amtNumber of drops to generate
coordA function of type C.
radiusA function of type R.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sprinkleGlobal()

void EMSCRIPTEN_KEEPALIVE sprinkleGlobal ( int  amt,
float  r_min,
float  r_max 
)

Create drops at random positions on the canvas.

Parameters
amtNumber of drops to create.
r_minMinimum size.
r_maxMaximum size.
Here is the call graph for this function:

◆ sprinkleLocal()

void EMSCRIPTEN_KEEPALIVE sprinkleLocal ( int  amt,
float  r_min,
float  r_max,
float  x,
float  y,
float  sig 
)

Create drops around the cursor postion.

\param amt Number of drops to create.
\param r_min,r_max drop size range
\param x,y cursor position
\param sig Sigma of normal distribution for drop position
Here is the call graph for this function:

◆ startDropping()

void EMSCRIPTEN_KEEPALIVE startDropping ( )

Set drop state.

All rakes will be lost.

Here is the call graph for this function:

◆ startRaking()

void EMSCRIPTEN_KEEPALIVE startRaking ( )

Set rake state.

This results in the scene being rendered to a hidden buffer which is given to the WGLRakeRenderer

Here is the call graph for this function:

◆ undoLastRake()

void EMSCRIPTEN_KEEPALIVE undoLastRake ( )

Undo the last rake.

Reverting only the last step is very cheap by swapping buffer pointers. Calling it again will swap back (e.g. undo the undo).

Here is the call graph for this function:

Variable Documentation

◆ rakeRenderer

WGLRakeRenderer* rakeRenderer

◆ scene

Scene* scene

global scene

◆ sceneRenderer

WGLSceneRenderer* sceneRenderer

◆ setupDone

bool setupDone = false

Stores wether backend is initialized or not.