Skip to content

Commit

Permalink
Merge pull request #79 from ker2x/main
Browse files Browse the repository at this point in the history
GUI Refactoring + Experimental features
  • Loading branch information
hunar4321 authored Nov 15, 2022
2 parents 6ea3416 + 94a0a76 commit 700335e
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 87 deletions.
1 change: 1 addition & 0 deletions particle_life/bin/interesting_models/waves
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.63334 47.1279 -56.928 -36.195 61.8049 18.3926 87.2929 32.2429 76.5575 -73.7449 -44.4862 -27.5472 112.955 64.3864 63.9446 13.5636 -65.4341 68.3151 -55.8538 60.4047 110.391 52.3731 31.2723 17.1192 -44.4179 1.14505 -63.5249 -7.51244 99.9638 83.9564 107.079 17.7316 3243 3175 3141 3108 0.5
10 changes: 6 additions & 4 deletions particle_life/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

//========================================================================
int main( ){
ofSetupOpenGL(1600,900,OF_WINDOW); // <-------- setup the GL context
//ofSetupOpenGL(1600,900,OF_WINDOW);
ofGLWindowSettings s;
//s.setGLVersion(4, 3);
s.setSize(1600, 900);
//s.setPosition(glm::vec2(0, 0));
ofCreateWindow(s);

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());

}
250 changes: 185 additions & 65 deletions particle_life/src/ofApp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ofApp.h"
#include "ofUtils.h"

#include <iostream>
#include <vector>
Expand All @@ -21,25 +22,30 @@ constexpr float rr = 8;

//int countThresh = 0;
std::string fps_text;
std::string physic_text;

//Simulation parameters
int cntFps = 0;
clock_t now, lastTime, delta;
clock_t physic_begin, physic_delta;

//Particle groups by color
std::vector<point> green;
std::vector<point> red;
std::vector<point> white;
std::vector<point> blue;

//Subdivison grid
grid subdiv;

/**
* @brief Return a random float in range [a,b[
*
* @param a lower bound
* @param b higher bound
* @return random float
*/
float RandomFloat(const float a, const float b)
inline float RandomFloat(const float a, const float b)
{
const float diff = b - a;
const float r = ofRandomuf() * diff;
Expand All @@ -65,7 +71,7 @@ void Draw(const std::vector<point>* points)
* @param b blue
* @return a group of random point
*/
std::vector<point> CreatePoints(const int num, int r, int g, int b)
std::vector<point> CreatePoints(const int num, const int r, const int g, const int b)
{
std::vector<point> points;
points.reserve(num);
Expand All @@ -87,15 +93,15 @@ std::vector<point> CreatePoints(const int num, int r, int g, int b)
*/
void ofApp::interaction(std::vector<point>* Group1, const std::vector<point>* Group2, const float G, const float radius)
{
//Gravity coefficient
const float g = G / -100;
const float g = G / -100; //Gravity coefficient
const auto group1size = Group1->size();
const auto group2size = Group2->size();
const bool radius_toggle = radiusToogle;

boundHeight = ofGetHeight();
boundWidth = ofGetWidth();
const auto group1size = Group1->size();
const auto group2size = Group2->size();

#pragma omp parallel default(none)
#pragma omp parallel
{
std::random_device rd;
#pragma omp for
Expand All @@ -117,7 +123,7 @@ void ofApp::interaction(std::vector<point>* Group1, const std::vector<point>* Gr
const auto r = dx * dx + dy * dy;

//Calculate the force in given bounds.
if (r != 0 && r < (radius * radius))
if ((r < radius * radius || radius_toggle) && r != 0.0F)
{
fx += (dx / std::sqrt(dx * dx + dy * dy));
fy += (dy / std::sqrt(dx * dx + dy * dy));
Expand Down Expand Up @@ -165,7 +171,6 @@ void ofApp::interaction(std::vector<point>* Group1, const std::vector<point>* Gr
p1.y = boundHeight;
}
}
//(*Group1)[i] = p1; // seems to be useless
}
}
}
Expand Down Expand Up @@ -375,70 +380,108 @@ void ofApp::setup()
{
lastTime = clock();
ofSetWindowTitle("Particle Life - www.brainxyz.com");
ofSetVerticalSync(false);

// Interface
gui.setup("Settings");
gui.loadFont("Arial", 12);
gui.setWidthElements(300.0f);

gui.add(resetButton.setup("START/RESTART"));
gui.add(randomChoice.setup("Randomize"));
gui.add(fps.setup("FPS", "0"));

gui.add(resetButton.setup("Restart"));
gui.add(randomChoice.setup("Randomize (space bar)"));
gui.add(save.setup("Save Model"));
gui.add(load.setup("Load Model"));
gui.add(probabilitySlider.setup("Probability", probability, 1, 100));
gui.add(viscoSlider.setup("Viscosity/Friction", viscosity, 0, 1));
gui.add(gravitySlider.setup("Gravity", worldGravity, -1, 1));
gui.add(wallRepelSlider.setup("Wall Repel", wallRepel, 0, 100));
//gui.add(labelG.setup("GREEN:", "-"));
gui.add(numberSliderG.setup("GREEN:", pnumberSliderG, 0, 10000));
gui.add(powerSliderGG.setup("green x green:", ppowerSliderGG, -100, 100));
gui.add(powerSliderGR.setup("green x red:", ppowerSliderGR, -100, 100));
gui.add(powerSliderGW.setup("green x white:", ppowerSliderGW, -100, 100));
gui.add(powerSliderGB.setup("green x blue:", ppowerSliderGB, -100, 100));

gui.add(vSliderGG.setup("radius g x g:", pvSliderGG, 10, 500));
gui.add(vSliderGR.setup("radius g x r:", pvSliderGR, 10, 500));
gui.add(vSliderGW.setup("radius g x w:", pvSliderGW, 10, 500));
gui.add(vSliderGB.setup("radius g x b:", pvSliderGB, 10, 500));

//gui.add(labelR.setup("RED:", "-"));
gui.add(numberSliderR.setup("RED:", pnumberSliderR, 0, 10000));
gui.add(powerSliderRR.setup("red x red:", ppowerSliderRR, -100, 100));
gui.add(powerSliderRG.setup("red x green:", ppowerSliderRG, -100, 100));
gui.add(powerSliderRW.setup("red x white:", ppowerSliderRW, -100, 100));
gui.add(powerSliderRB.setup("red x blue:", ppowerSliderRB, -100, 100));

gui.add(vSliderRG.setup("radius r x g:", pvSliderRG, 10, 500));
gui.add(vSliderRR.setup("radius r x r:", pvSliderRR, 10, 500));
gui.add(vSliderRW.setup("radius r x w:", pvSliderRW, 10, 500));
gui.add(vSliderRB.setup("radius r x b:", pvSliderRB, 10, 500));

//gui.add(labelW.setup("WHITE:", "-"));
gui.add(numberSliderW.setup("WHITE:", pnumberSliderW, 0, 10000));
gui.add(powerSliderWW.setup("white x white:", ppowerSliderWW, -100, 100));
gui.add(powerSliderWR.setup("white x red:", ppowerSliderWR, -100, 100));
gui.add(powerSliderWG.setup("white x green:", ppowerSliderWG, -100, 100));
gui.add(powerSliderWB.setup("white x blue:", ppowerSliderWB, -100, 100));

gui.add(vSliderWG.setup("radius w x g:", pvSliderWG, 10, 500));
gui.add(vSliderWR.setup("radius w x r:", pvSliderWR, 10, 500));
gui.add(vSliderWW.setup("radius w x w:", pvSliderWW, 10, 500));
gui.add(vSliderWB.setup("radius w x b:", pvSliderWB, 10, 500));

//gui.add(labelB.setup("BLUE:", "-"));
gui.add(numberSliderB.setup("BLUE:", pnumberSliderB, 0, 10000));
gui.add(powerSliderBB.setup("blue x blue:", ppowerSliderBB, -100, 100));
gui.add(powerSliderBW.setup("blue x white:", ppowerSliderBW, -100, 100));
gui.add(powerSliderBR.setup("blue x red:", ppowerSliderBR, -100, 100));
gui.add(powerSliderBG.setup("blue x green:", ppowerSliderBG, -100, 100));

gui.add(vSliderBG.setup("radius b x g:", pvSliderBG, 10, 500));
gui.add(vSliderBR.setup("radius b x r:", pvSliderBR, 10, 500));
gui.add(vSliderBW.setup("radius b x w:", pvSliderBW, 10, 500));
gui.add(vSliderBB.setup("radius b x b:", pvSliderBB, 10, 500));
gui.add(boundsToggle.setup("Bounded", true));
gui.add(modelToggle.setup("Show Model", false));
gui.add(motionBlurToggle.setup("Motion Blur", false));
gui.add(fps.setup("FPS", "0"));

globalGroup.setup("Global");
globalGroup.add(viscoSlider.setup("Viscosity/Friction", viscosity, 0, 1));
globalGroup.add(gravitySlider.setup("Gravity", worldGravity, -1, 1));
globalGroup.add(wallRepelSlider.setup("Wall Repel", wallRepel, 0, 100));
globalGroup.add(boundsToggle.setup("Bounded", false));

gui.add(&globalGroup);

// Quantity
qtyGroup.setup("Quantity (require restart/randomize)");
qtyGroup.add(numberSliderG.setup("Green", pnumberSliderG, 0, 10000));
qtyGroup.add(numberSliderR.setup("Red", pnumberSliderR, 0, 10000));
qtyGroup.add(numberSliderW.setup("White", pnumberSliderW, 0, 10000));
qtyGroup.add(numberSliderB.setup("Blue", pnumberSliderB, 0, 10000));
gui.add(&qtyGroup);

// GREEN
greenGroup.setup("Green");
greenGroup.add(powerSliderGG.setup("green x green:", ppowerSliderGG, -100, 100));
greenGroup.add(powerSliderGR.setup("green x red:", ppowerSliderGR, -100, 100));
greenGroup.add(powerSliderGW.setup("green x white:", ppowerSliderGW, -100, 100));
greenGroup.add(powerSliderGB.setup("green x blue:", ppowerSliderGB, -100, 100));

greenGroup.add(vSliderGG.setup("radius g x g:", pvSliderGG, 10, 500));
greenGroup.add(vSliderGR.setup("radius g x r:", pvSliderGR, 10, 500));
greenGroup.add(vSliderGW.setup("radius g x w:", pvSliderGW, 10, 500));
greenGroup.add(vSliderGB.setup("radius g x b:", pvSliderGB, 10, 500));

greenGroup.minimize();
gui.add(&greenGroup);

// RED
redGroup.setup("Red");
redGroup.add(powerSliderRR.setup("red x red:", ppowerSliderRR, -100, 100));
redGroup.add(powerSliderRG.setup("red x green:", ppowerSliderRG, -100, 100));
redGroup.add(powerSliderRW.setup("red x white:", ppowerSliderRW, -100, 100));
redGroup.add(powerSliderRB.setup("red x blue:", ppowerSliderRB, -100, 100));

redGroup.add(vSliderRG.setup("radius r x g:", pvSliderRG, 10, 500));
redGroup.add(vSliderRR.setup("radius r x r:", pvSliderRR, 10, 500));
redGroup.add(vSliderRW.setup("radius r x w:", pvSliderRW, 10, 500));
redGroup.add(vSliderRB.setup("radius r x b:", pvSliderRB, 10, 500));

redGroup.minimize();
gui.add(&redGroup);

// WHITE
whiteGroup.setup("White");
whiteGroup.add(powerSliderWW.setup("white x white:", ppowerSliderWW, -100, 100));
whiteGroup.add(powerSliderWR.setup("white x red:", ppowerSliderWR, -100, 100));
whiteGroup.add(powerSliderWG.setup("white x green:", ppowerSliderWG, -100, 100));
whiteGroup.add(powerSliderWB.setup("white x blue:", ppowerSliderWB, -100, 100));

whiteGroup.add(vSliderWG.setup("radius w x g:", pvSliderWG, 10, 500));
whiteGroup.add(vSliderWR.setup("radius w x r:", pvSliderWR, 10, 500));
whiteGroup.add(vSliderWW.setup("radius w x w:", pvSliderWW, 10, 500));
whiteGroup.add(vSliderWB.setup("radius w x b:", pvSliderWB, 10, 500));

whiteGroup.minimize();
gui.add(&whiteGroup);

// BLUE
blueGroup.setup("Blue");
blueGroup.add(powerSliderBB.setup("blue x blue:", ppowerSliderBB, -100, 100));
blueGroup.add(powerSliderBW.setup("blue x white:", ppowerSliderBW, -100, 100));
blueGroup.add(powerSliderBR.setup("blue x red:", ppowerSliderBR, -100, 100));
blueGroup.add(powerSliderBG.setup("blue x green:", ppowerSliderBG, -100, 100));

blueGroup.add(vSliderBG.setup("radius b x g:", pvSliderBG, 10, 500));
blueGroup.add(vSliderBR.setup("radius b x r:", pvSliderBR, 10, 500));
blueGroup.add(vSliderBW.setup("radius b x w:", pvSliderBW, 10, 500));
blueGroup.add(vSliderBB.setup("radius b x b:", pvSliderBB, 10, 500));

blueGroup.minimize();
gui.add(&blueGroup);

expGroup.setup("Experimental");
expGroup.add(evoToggle.setup("Evolve parameters", false));
expGroup.add(evoProbSlider.setup("evo chance%", evoChance, 0, 100));
expGroup.add(evoAmountSlider.setup("evo amount%%", evoAmount, 0, 100));
expGroup.add(radiusToogle.setup("infinite radius", false));
expGroup.add(probabilitySlider.setup("interaction prob%", probability, 1, 100));
expGroup.add(motionBlurToggle.setup("Motion Blur", false));
expGroup.add(physicLabel.setup("physic (ms)", "0"));

expGroup.minimize();
gui.add(&expGroup);

ofSetBackgroundAuto(false);
ofEnableAlphaBlending();
Expand All @@ -449,10 +492,84 @@ void ofApp::setup()
//------------------------------Update simulation with sliders values------------------------------
void ofApp::update()
{
physic_begin = clock();
probability = probabilitySlider;
viscosity = viscoSlider;
worldGravity = gravitySlider;
wallRepel = wallRepelSlider;
evoChance = evoProbSlider;
evoAmount = evoAmountSlider;

if(evoToggle && ofRandom(1.0F) < (evoChance / 100.0F))
{
powerSliderRR = powerSliderRR + ((ofRandom(2.0F) - 1.0F) * (powerSliderRR.getMax() - powerSliderRR.getMin()) * (evoAmount / 100.0F));
powerSliderRG = powerSliderRG + ((ofRandom(2.0F) - 1.0F) * (powerSliderRG.getMax() - powerSliderRG.getMin()) * (evoAmount / 100.0F));
powerSliderRB = powerSliderRB + ((ofRandom(2.0F) - 1.0F) * (powerSliderRB.getMax() - powerSliderRB.getMin()) * (evoAmount / 100.0F));
powerSliderRW = powerSliderRW + ((ofRandom(2.0F) - 1.0F) * (powerSliderRW.getMax() - powerSliderRW.getMin()) * (evoAmount / 100.0F));
powerSliderGR = powerSliderGR + ((ofRandom(2.0F) - 1.0F) * (powerSliderGR.getMax() - powerSliderGR.getMin()) * (evoAmount / 100.0F));
powerSliderGG = powerSliderGG + ((ofRandom(2.0F) - 1.0F) * (powerSliderGG.getMax() - powerSliderGG.getMin()) * (evoAmount / 100.0F));
powerSliderGB = powerSliderGB + ((ofRandom(2.0F) - 1.0F) * (powerSliderGB.getMax() - powerSliderGB.getMin()) * (evoAmount / 100.0F));
powerSliderGW = powerSliderGW + ((ofRandom(2.0F) - 1.0F) * (powerSliderGW.getMax() - powerSliderGW.getMin()) * (evoAmount / 100.0F));
powerSliderBR = powerSliderBR + ((ofRandom(2.0F) - 1.0F) * (powerSliderBR.getMax() - powerSliderBR.getMin()) * (evoAmount / 100.0F));
powerSliderBG = powerSliderBG + ((ofRandom(2.0F) - 1.0F) * (powerSliderBG.getMax() - powerSliderBG.getMin()) * (evoAmount / 100.0F));
powerSliderBB = powerSliderBB + ((ofRandom(2.0F) - 1.0F) * (powerSliderBB.getMax() - powerSliderBB.getMin()) * (evoAmount / 100.0F));
powerSliderBW = powerSliderBW + ((ofRandom(2.0F) - 1.0F) * (powerSliderBW.getMax() - powerSliderBW.getMin()) * (evoAmount / 100.0F));
powerSliderWR = powerSliderWR + ((ofRandom(2.0F) - 1.0F) * (powerSliderWR.getMax() - powerSliderWR.getMin()) * (evoAmount / 100.0F));
powerSliderWG = powerSliderWG + ((ofRandom(2.0F) - 1.0F) * (powerSliderWG.getMax() - powerSliderWG.getMin()) * (evoAmount / 100.0F));
powerSliderWB = powerSliderWB + ((ofRandom(2.0F) - 1.0F) * (powerSliderWB.getMax() - powerSliderWB.getMin()) * (evoAmount / 100.0F));
powerSliderWW = powerSliderWW + ((ofRandom(2.0F) - 1.0F) * (powerSliderWW.getMax() - powerSliderWW.getMin()) * (evoAmount / 100.0F));

vSliderRR = vSliderRR + ((ofRandom(2.0F) - 1.0F) * (vSliderRR.getMax() - vSliderRR.getMin()) * (evoAmount / 100.0F));
vSliderRG = vSliderRG + ((ofRandom(2.0F) - 1.0F) * (vSliderRG.getMax() - vSliderRG.getMin()) * (evoAmount / 100.0F));
vSliderRB = vSliderRB + ((ofRandom(2.0F) - 1.0F) * (vSliderRB.getMax() - vSliderRB.getMin()) * (evoAmount / 100.0F));
vSliderRW = vSliderRW + ((ofRandom(2.0F) - 1.0F) * (vSliderRW.getMax() - vSliderRW.getMin()) * (evoAmount / 100.0F));
vSliderGR = vSliderGR + ((ofRandom(2.0F) - 1.0F) * (vSliderGR.getMax() - vSliderGR.getMin()) * (evoAmount / 100.0F));
vSliderGG = vSliderGG + ((ofRandom(2.0F) - 1.0F) * (vSliderGG.getMax() - vSliderGG.getMin()) * (evoAmount / 100.0F));
vSliderGB = vSliderGB + ((ofRandom(2.0F) - 1.0F) * (vSliderGB.getMax() - vSliderGB.getMin()) * (evoAmount / 100.0F));
vSliderGW = vSliderGW + ((ofRandom(2.0F) - 1.0F) * (vSliderGW.getMax() - vSliderGW.getMin()) * (evoAmount / 100.0F));
vSliderBR = vSliderBR + ((ofRandom(2.0F) - 1.0F) * (vSliderBR.getMax() - vSliderBR.getMin()) * (evoAmount / 100.0F));
vSliderBG = vSliderBG + ((ofRandom(2.0F) - 1.0F) * (vSliderBG.getMax() - vSliderBG.getMin()) * (evoAmount / 100.0F));
vSliderBB = vSliderBB + ((ofRandom(2.0F) - 1.0F) * (vSliderBB.getMax() - vSliderBB.getMin()) * (evoAmount / 100.0F));
vSliderBW = vSliderBW + ((ofRandom(2.0F) - 1.0F) * (vSliderBW.getMax() - vSliderBW.getMin()) * (evoAmount / 100.0F));
vSliderWR = vSliderWR + ((ofRandom(2.0F) - 1.0F) * (vSliderWR.getMax() - vSliderWR.getMin()) * (evoAmount / 100.0F));
vSliderWG = vSliderWG + ((ofRandom(2.0F) - 1.0F) * (vSliderWG.getMax() - vSliderWG.getMin()) * (evoAmount / 100.0F));
vSliderWB = vSliderWB + ((ofRandom(2.0F) - 1.0F) * (vSliderWB.getMax() - vSliderWB.getMin()) * (evoAmount / 100.0F));
vSliderWW = vSliderWW + ((ofRandom(2.0F) - 1.0F) * (vSliderWW.getMax() - vSliderWW.getMin()) * (evoAmount / 100.0F));

if(vSliderRR < 0.0F) vSliderRR = 0;
if(vSliderRG < 0.0F) vSliderRG = 0;
if(vSliderRB < 0.0F) vSliderRB = 0;
if(vSliderRW < 0.0F) vSliderRW = 0;
if(vSliderGR < 0.0F) vSliderGR = 0;
if(vSliderGG < 0.0F) vSliderGG = 0;
if(vSliderGB < 0.0F) vSliderGB = 0;
if(vSliderGW < 0.0F) vSliderGW = 0;
if(vSliderBR < 0.0F) vSliderBR = 0;
if(vSliderBG < 0.0F) vSliderBG = 0;
if(vSliderBB < 0.0F) vSliderBB = 0;
if(vSliderBW < 0.0F) vSliderBW = 0;
if(vSliderWR < 0.0F) vSliderWR = 0;
if(vSliderWG < 0.0F) vSliderWG = 0;
if(vSliderWB < 0.0F) vSliderWB = 0;
if(vSliderWW < 0.0F) vSliderWW = 0;

if (vSliderRR > 200.0F) vSliderRR = 200.0F;
if (vSliderRG > 200.0F) vSliderRG = 200.0F;
if (vSliderRB > 200.0F) vSliderRB = 200.0F;
if (vSliderRW > 200.0F) vSliderRW = 200.0F;
if (vSliderGR > 200.0F) vSliderGR = 200.0F;
if (vSliderGG > 200.0F) vSliderGG = 200.0F;
if (vSliderGB > 200.0F) vSliderGB = 200.0F;
if (vSliderGW > 200.0F) vSliderGW = 200.0F;
if (vSliderBR > 200.0F) vSliderBR = 200.0F;
if (vSliderBG > 200.0F) vSliderBG = 200.0F;
if (vSliderBB > 200.0F) vSliderBB = 200.0F;
if (vSliderBW > 200.0F) vSliderBW = 200.0F;
if (vSliderWR > 200.0F) vSliderWR = 200.0F;
if (vSliderWG > 200.0F) vSliderWG = 200.0F;
if (vSliderWB > 200.0F) vSliderWB = 200.0F;
if (vSliderWW > 200.0F) vSliderWW = 200.0F;
}

if (numberSliderR > 0)
{
Expand Down Expand Up @@ -488,6 +605,7 @@ void ofApp::update()

if (save) { saveSettings(); }
if (load) { loadSettings(); }
physic_delta = clock() - physic_begin;
}

//--------------------------------------------------------------
Expand All @@ -513,6 +631,8 @@ void ofApp::draw()
{
lastTime = now;
fps.setup("FPS", to_string(static_cast<int>((1000 / static_cast<float>(delta)) * cntFps)));
physicLabel.setup("physics (ms)", to_string(physic_delta) );

cntFps = 0;
}

Expand Down
Loading

0 comments on commit 700335e

Please sign in to comment.