hi. i would like to translate this code http://www.openprocessing.org/visuals/?visualID=10424 from Processing to OF. this is the P5 code:
/* SinusPlasma
* An oldschool demo effect, reborn
* Left click to change gradient colors, right click to change plasma curve
*
* Joerg Reuter, jore[at]stachelig[dot]de
* Creative Commons Attribution 3.0 license
* [http://stachelig.de](http://stachelig.de)
*/
// global parameters ... feel free to tweak
// effect is largely tied to screen size, because of the simplistic plasma function
// drop me an e-mail for an improved version that renders to a OpenGL texture and scales it up
int SCREENWIDTH = 640;
int SCREENHEIGHT = 360;
int GRADIENTLEN = 1500;
// use this factor to make things faster, esp. for high resolutions
int SPEEDUP = 3;
// swing/wave function parameters
int SWINGLEN = GRADIENTLEN*3;
int SWINGMAX = GRADIENTLEN / 2 - 1;
// gradient & swing curve arrays
private int[] colorGrad;
private int[] swingCurve;
// create standard gradient & swing curve
void setup() {
size(SCREENWIDTH, SCREENHEIGHT, P2D);
makeGradient(GRADIENTLEN);
makeSwingCurve(SWINGLEN, SWINGMAX);
}
// draw one frame of the sinus plasma
void draw() {
loadPixels();
int i = 0;
int t = frameCount*SPEEDUP;
int swingT = swing(t); // swingT/-Y/-YT variables are used for a little tuning ...
for (int y = 0; y < SCREENHEIGHT; y++) {
int swingY = swing(y);
int swingYT = swing(y + t);
for (int x = 0; x < SCREENWIDTH; x++) {
// this is where the magic happens: map x, y, t around
// the swing curves and lookup a color from the gradient
// the "formula" was found by a lot of experimentation
pixels[i++] = gradient(
swing(swing(x + swingT) + swingYT) +
swing(swing(x + t ) + swingY ));
}
}
updatePixels();
}
// create a new random gradient when mouse is pressed
void mousePressed() {
if (mouseButton == LEFT)
makeGradient(GRADIENTLEN);
else if (mouseButton == RIGHT)
makeSwingCurve(SWINGLEN, SWINGMAX);
}
// fill the given array with a nice swingin' curve
// three cos waves are layered together for that
// the wave "wraps" smoothly around, uh, if you know what i mean ;-)
void makeSwingCurve(int arrlen, int maxval) {
// default values will be used upon first call
int factor1=2;
int factor2=3;
int factor3=6;
if (swingCurve == null) {
swingCurve = new int[SWINGLEN];
} else {
factor1=(int) random(1, 7);
factor2=(int) random(1, 7);
factor3=(int) random(1, 7);
}
int halfmax = maxval/factor1;
for( int i=0; i<arrlen; i++ ) {
float ni = i*TWO_PI/arrlen; // ni goes [0..TWO_PI] -> one complete cos wave
swingCurve[i]=(int)(
cos( ni*factor1 ) *
cos( ni*factor2 ) *
cos( ni*factor3 ) *
halfmax + halfmax );
}
}
// create a smooth, colorful gradient by cosinus curves in the RGB channels
private void makeGradient(int arrlen) {
// default values will be used upon first call
int rf = 4;
int gf = 2;
int bf = 1;
int rd = 0;
int gd = arrlen / gf;
int bd = arrlen / bf / 2;
if (colorGrad == null) {
// first call
colorGrad = new int[GRADIENTLEN];
} else {
// we are called again: random gradient
rf = (int) random(1, 5);
gf = (int) random(1, 5);
bf = (int) random(1, 5);
rd = (int) random(0, arrlen);
gd = (int) random(0, arrlen);
bd = (int) random(0, arrlen);
System.out.println("Gradient factors("+rf+","+gf+","+bf+"), displacement("+rd+","+gd+","+bd+")");
}
// fill gradient array
for (int i = 0; i < arrlen; i++) {
int r = cos256(arrlen / rf, i + rd);
int g = cos256(arrlen / gf, i + gd);
int b = cos256(arrlen / bf, i + bd);
colorGrad[i] = color(r, g, b);
}
}
// helper: get cosinus sample normalized to 0..255
private int cos256(final int amplitude, final int x) {
return (int) (cos(x * TWO_PI / amplitude) * 127 + 127);
}
// helper: get a swing curve sample
private int swing(final int i) {
return swingCurve[i % SWINGLEN];
}
// helper: get a gradient sample
private int gradient(final int i) {
return colorGrad[i % GRADIENTLEN];
}
this is my TestApp.h:
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void makeSwingCurve(int arrlen, int maxval);
public: int changecol( int amplitude, int x);
int swing( int i);
int gradient( int i);
void makeGradient(int arrlen);
int SCREENWIDTH;// = 640;
int SCREENHEIGHT;// = 360;
int GRADIENTLEN;
// use this factor to make things faster, esp. for high resolutions
int SPEEDUP;// = 3;
// swing/wave function parameters
int SWINGLEN;
int SWINGMAX;
int t;
int i;
int swingT;
int colorGrad;
int amplitude;
int x;
int arrlen;
unsigned char * colorPixels;
ofTexture texColor;
};
#endif
and my TestApp.cpp:
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
SCREENWIDTH = 640;
SCREENHEIGHT = 360;
GRADIENTLEN = 1500;
SWINGLEN = GRADIENTLEN*3;
SWINGMAX = GRADIENTLEN / 2 - 1;
SPEEDUP = 3;
makeGradient(GRADIENTLEN);
makeSwingCurve(SWINGLEN, SWINGMAX);
texColor.allocate(SCREENWIDTH,SCREENHEIGHT,GL_RGB);
colorPixels = new unsigned char [SCREENWIDTH*SCREENHEIGHT*3];
//loadPixels();
i = 0;
t = (float)ofGetFrameRate()*SPEEDUP;
swingT = swing(t); // swingT/-Y/-YT variables are used for a little tuning ...
for (int y = 0; y < SCREENHEIGHT; y++) {
int swingY = swing(y);
int swingYT = swing(y + t);
for (int x = 0; x < SCREENWIDTH; x++) {
// this is where the magic happens: map x, y, t around
// the swing curves and lookup a color from the gradient
// the "formula" was found by a lot of experimentation
colorPixels[i++] = gradient(
swing(swing(x + swingT) + swingYT) +
swing(swing(x + t ) + swingY ));
}
}
//updatePixels();
texColor.loadData(colorPixels, SCREENWIDTH,SCREENHEIGHT, GL_RGB);
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
texColor.draw(350,300,SCREENWIDTH,SCREENHEIGHT);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
void makeGradient(int arrlen) {
// default values will be used upon first call
int rf = 4;
int gf = 2;
int bf = 1;
int rd = 0;
int gd = arrlen / gf;
int bd = arrlen / bf / 2;
int const GRADIENTLEN = 1500;
int colorGrad [GRADIENTLEN];
arrlen =GRADIENTLEN;
// if (colorGrad == NULL) {
// first call
// colorGrad = new int[GRADIENTLEN];
//} else {
// we are called again: random gradient
rf = (int) ofRandom(1, 5);
gf = (int) ofRandom(1, 5);
bf = (int) ofRandom(1, 5);
rd = (int) ofRandom(0, arrlen);
gd = (int) ofRandom(0, arrlen);
bd = (int) ofRandom(0, arrlen);
//printf("Gradient factors("+rf+","+gf+","+bf+"), displacement("+rd+","+gd+","+bd+")");
//}
// fill gradient array
for (int i = 0; i < arrlen; i++) {
int red = changecol(arrlen / rf, i + rd);
int green = changecol(arrlen / gf, i + gd);
int blu = changecol(arrlen / bf, i + bd);
colorGrad[i] = ofSetColor(red, green, blu);
}
}
void makeSwingCurve(int arrlen, int maxval) {
// default values will be used upon first call
int factor1=2;
int factor2=3;
int factor3=6;
int const GRADIENTLEN = 1500;
int const SWINGLEN = GRADIENTLEN*3;
int swingCurve [SWINGLEN];
//if (swingCurve == NULL) {
// swingCurve = new int[SWINGLEN];
//} else {
factor1=(int) ofRandom(1, 7);
factor2=(int) ofRandom(1, 7);
factor3=(int) ofRandom(1, 7);
//}
int halfmax = maxval/factor1;
for( int i=0; i<arrlen; i++ ) {
float ni = i*TWO_PI/arrlen; // ni goes [0..TWO_PI] -> one complete cos wave
swingCurve[i]=(int)(
cos( ni*factor1 ) *
cos( ni*factor2 ) *
cos( ni*factor3 ) *
halfmax + halfmax );
}
}
// helper: get cosinus sample normalized to 0..255
int changecol( int amplitude,int x) {
int p;
p = cos(x * TWO_PI / amplitude) * 127 + 127;
return p;
}
// helper: get a swing curve sample
int swing( int i) {
int r;
int const GRADIENTLEN = 1500;
int const SWINGLEN = GRADIENTLEN*3;
int swingCurve [SWINGLEN];
r = swingCurve[i % SWINGLEN];
return r;
}
// helper: get a gradient sample
int gradient( int i) {
int const GRADIENTLEN = 1500;
int colorGrad [GRADIENTLEN];
return colorGrad[i % GRADIENTLEN];
}
Ireceive this error:
1>------ Inizio compilazione: Progetto: Sinusplasma, Configurazione: Debug Win32 ------
1>Compilazione in corso…
1>testApp.cpp
1>c:\openframework\of_prerelease_v0061_vs2008_fat\apps\examples\sinusplasma\src\testapp.cpp(116) : error C3861: ‘changecol’: identificatore non trovato
1>c:\openframework\of_prerelease_v0061_vs2008_fat\apps\examples\sinusplasma\src\testapp.cpp(117) : error C3861: ‘changecol’: identificatore non trovato
1>c:\openframework\of_prerelease_v0061_vs2008_fat\apps\examples\sinusplasma\src\testapp.cpp(118) : error C3861: ‘changecol’: identificatore non trovato
1>c:\openframework\of_prerelease_v0061_vs2008_fat\apps\examples\sinusplasma\src\testapp.cpp(119) : error C2440: ‘=’: impossibile convertire da ‘void’ a ‘int’
1> Impossibile convertire un’espressione di tipo void in altri tipi
1>c:\openframework\of_prerelease_v0061_vs2008_fat\apps\examples\sinusplasma\src\testapp.cpp(157) : error C2065: ‘x’: identificatore non dichiarato
1>Il log di compilazione è stato salvato in ‘file://c:\openFramework\of_preRelease_v0061_vs2008_FAT\apps\examples\Sinusplasma\obj\Debug\BuildLog.htm’
1>Sinusplasma - 5 errore/i, 0 avviso/i
========== Compilazione: 0 completate, 1 non riuscite, 1 aggiornate, 0 ignorate ==========
of course i’ve tried to translate P5 color() in to ofSetColor() that return void and this is wrong but does exist something (color()) similar in OF? what do you suggest to use?
and why dont find the identifier when is in the TestApp.h?
i’m more confortable with C# so let me know if is also something else wrong…
Many thanks