need help for boolean array
in
Contributed Library Questions
•
1 year ago
Hi everyone.
I'm quite a newbie in programming, and just learned processing for about 2 months. I'm currently working on a marker based augmented reality program using Nyartoolkit for my school project. The idea is to display a 3d models, texts (a name for each model), and play a sound for each model that can be re-triggered with a key press. So with my very limited knowledge on programming (not to mention bad English) I wrote a code based on many tutorials and processing references. The code itself works fine as I wanted, but as you can see, it's a mess and inefficient, especially the audio sample trigger part. I need the trigger so the sound do not overlap with each other when a key is pressed, but all i can do is to write all the boolean trigger for each marker tracking. I already tried to simplify it with boolean array, but I can't get it to work. I plan to put more than 20 models so I think the code will be too much. Please help me to use boolean array, or is there a better way?
By the way I use processing 1.2. and here's the code:
Thanks very much in advance! (and sorry for my bad english)
I'm quite a newbie in programming, and just learned processing for about 2 months. I'm currently working on a marker based augmented reality program using Nyartoolkit for my school project. The idea is to display a 3d models, texts (a name for each model), and play a sound for each model that can be re-triggered with a key press. So with my very limited knowledge on programming (not to mention bad English) I wrote a code based on many tutorials and processing references. The code itself works fine as I wanted, but as you can see, it's a mess and inefficient, especially the audio sample trigger part. I need the trigger so the sound do not overlap with each other when a key is pressed, but all i can do is to write all the boolean trigger for each marker tracking. I already tried to simplify it with boolean array, but I can't get it to work. I plan to put more than 20 models so I think the code will be too much. Please help me to use boolean array, or is there a better way?
By the way I use processing 1.2. and here's the code:
- import codeanticode.gsvideo.*;
import jp.nyatla.nyar4psg.*;
import processing.opengl.*;
import javax.media.opengl.*;
import saito.objloader.*;
import ddf.minim.*;
Minim minim;
//sound variable
AudioSample sndGajah;
AudioSample sndBadak;
AudioSample sndPenguin;
AudioSample sndFlamingo;
AudioSample sndKura;
AudioSample sndKelinci;
AudioSample sndDeer;
AudioPlayer plgajah;
AudioPlayer plbadak;
AudioPlayer plpenguin;
AudioPlayer plflamingo;
AudioPlayer plkura;
AudioPlayer plkelinci;
AudioPlayer pldeer;
GSCapture cam;
NyARMultiBoard nya;
OBJModel modelgajah ;
OBJModel modelbadak ;
OBJModel modelpenguin;
OBJModel modelflamingo;
OBJModel modelkura;
OBJModel modelkelinci;
OBJModel modeldeer;
PFont fontA;
label lbl = new label();
teks nama = new teks();
//sound TRIGGER
boolean trgrGajah;
boolean trgrBadak;
boolean trgrPenguin;
boolean trgrFlamingo;
boolean trgrKura;
boolean trgrKelinci;
boolean trgrDeer;
void setup() {
size(640,480,P3D);
frame.setResizable(true);
colorMode(RGB, 100);
cam=new GSCapture(this,width,height);
String[] patts = {"patt.1", "patt.2", "patt.3", "patt.4", "patt.5", "patt.6", "patt.7"};
double[] widths = {80,80,80,80,80,80,80};
nya=new NyARMultiBoard(this,width,height,"camera_para.dat",patts,widths);
print(nya.VERSION);
nya.gsThreshold=120;//(0<n<255) default=110
nya.cfThreshold=0.5;//(0.0<n<1.0) default=0.4
// load audio file
minim = new Minim(this);
sndGajah = minim.loadSample("elephant.mp3", 2048);
sndBadak = minim.loadSample("rhinoceros2.mp3", 2048);
sndPenguin = minim.loadSample("penguin.mp3", 2048);
sndFlamingo = minim.loadSample("flamingo.mp3", 2048);
sndKura = minim.loadSample("turtle.mp3", 2048);
sndKelinci = minim.loadSample("rabbit.mp3", 2048);
sndDeer = minim.loadSample("deer.mp3", 2048);
plgajah = minim.loadFile("elephant.mp3", 2048);
plbadak = minim.loadFile("rhinoceros2.mp3", 2048);
plpenguin = minim.loadFile("penguin.mp3", 2048);
plflamingo = minim.loadFile("flamingo.mp3", 2048);
plkura = minim.loadFile("turtle.mp3", 2048);
plkelinci = minim.loadFile("rabbit.mp3", 2048);
pldeer = minim.loadFile("deer.mp3", 2048);
fontA = loadFont("OneTrickPonyOT-56.vlw");
textFont(fontA,56);
//load model
modelgajah = new OBJModel(this, "gajah.obj", "absolute", TRIANGLES);
modelgajah.enableDebug();
modelgajah.scale(1);
modelgajah.translateToCenter();
modelgajah.enableTexture();
modelbadak = new OBJModel(this, "badak2.obj", "absolute", TRIANGLES);
modelbadak.enableDebug();
modelbadak.scale(1);
modelbadak.translateToCenter();
modelbadak.enableTexture();
modelbadak.enableMaterial() ;
modelpenguin = new OBJModel(this, "penguin.obj", "absolute", TRIANGLES);
modelpenguin.enableDebug();
modelpenguin.scale(1);
modelpenguin.translateToCenter();
modelpenguin.enableTexture();
modelpenguin.enableMaterial() ;
modelflamingo = new OBJModel(this, "flamingo.obj", "absolute", TRIANGLES);
modelflamingo.enableDebug();
modelflamingo.scale(1);
modelflamingo.translateToCenter();
modelflamingo.enableTexture();
modelflamingo.enableMaterial() ;
modelkura = new OBJModel(this, "kura.obj", "absolute", TRIANGLES);
modelkura.enableDebug();
modelkura.scale(3);
modelkura.translateToCenter();
modelkura.enableTexture();
modelkura.enableMaterial() ;
modelkelinci = new OBJModel(this, "kelinci.obj", "absolute", TRIANGLES);
modelkelinci.enableDebug();
modelkelinci.scale(1);
modelkelinci.translateToCenter();
modelkelinci.enableTexture();
modelkelinci.enableMaterial() ;
modeldeer = new OBJModel(this, "deer.obj", "absolute", TRIANGLES);
modeldeer.enableDebug();
modeldeer.scale(1);
modeldeer.translateToCenter();
modeldeer.enableTexture();
modeldeer.enableMaterial() ;
}
void draw() {
if (cam.available() !=true) {
return;
}
cam.read();
hint(DISABLE_DEPTH_TEST);
image(cam,0,0,width,height);
//display label
lbl.display();
nama.tksGajah();
nama.tksBadak();
nama.tksPenguin();
nama.tksFlamingo();
nama.tksKura();
nama.tksKelinci();
nama.tksDeer();
hint(ENABLE_DEPTH_TEST);
// if *any* markers have been detected this will be true
if (nya.detect(cam))
{
// depth test back on, we're going to draw 3D YEAH!!
hint(ENABLE_DEPTH_TEST);
// for all detected markers:
for (int i=0; i < nya.markers.length; i++)
{
if (nya.markers[i].detected)
{
// set the model-view transform to that of the marker
// this will adapt automatically to P3D or OPENGL renderers
nya.markers[i].beginTransform();
translate(0,0,20);
// draw gajah
if (i == 0)
{
trgrBadak = false;
trgrPenguin = false;
trgrFlamingo = false;
trgrKura = false;
trgrKelinci = false;
trgrDeer = false;
trgrGajah = true;
plgajah.play();
noStroke();
modelgajah.draw();
lbl.visible();
nama.gajahvisible();
}
// draw badak
else if (i == 1)
{
trgrGajah = false;
trgrPenguin = false;
trgrFlamingo = false;
trgrKura = false;
trgrKelinci = false;
trgrDeer = false;
trgrBadak = true;
plbadak.play();
noStroke();
modelbadak.draw();
lbl.visible();
nama.badakvisible();
}
// draw penguin
else if (i == 2)
{
trgrGajah = false;
trgrBadak = false;
trgrFlamingo = false;
trgrKura = false;
trgrKelinci = false;
trgrDeer = false;
trgrPenguin = true;
plpenguin.play();
noStroke();
modelpenguin.draw();
lbl.visible();
nama.penguinvisible();
}
// draw flamingo
else if (i == 3)
{
trgrGajah = false;
trgrBadak = false;
trgrPenguin = false;
trgrKura = false;
trgrKelinci = false;
trgrDeer = false;
trgrFlamingo = true;
plflamingo.play();
noStroke();
modelflamingo.draw();
lbl.visible();
nama.flamingovisible();
}
// draw kura
else if (i == 4)
{
trgrGajah = false;
trgrBadak = false;
trgrPenguin = false;
trgrFlamingo = false;
trgrKelinci = false;
trgrDeer = false;
trgrKura = true;
plkura.play();
noStroke();
modelkura.draw();
lbl.visible();
nama.kuravisible();
}
// draw kelinci
else if (i == 5)
{
trgrGajah = false;
trgrBadak = false;
trgrPenguin = false;
trgrFlamingo = false;
trgrKura = false;
trgrDeer = false;
trgrKelinci = true;
plkelinci.play();
noStroke();
modelkelinci.draw();
lbl.visible();
nama.kelincivisible();
}
// draw rusa
else if (i == 6)
{
trgrGajah = false;
trgrBadak = false;
trgrPenguin = false;
trgrFlamingo = false;
trgrKura = false;
trgrKelinci = false;
trgrDeer = true;
pldeer.play();
noStroke();
modeldeer.draw();
lbl.visible();
nama.deervisible();
}
else {
}
// after drawing marker-relative 3D geometry, we
// HAVE to end the transform (so now we're back in
// world space)
nya.markers[i].endTransform();
}
}
}
}
void keyPressed()
{
if ((keyPressed == true) && (trgrGajah == true)) sndGajah.trigger();
if ((keyPressed == true) && (trgrBadak == true)) sndBadak.trigger();
if ((keyPressed == true) && (trgrPenguin == true)) sndPenguin.trigger();
if ((keyPressed == true) && (trgrFlamingo == true)) sndFlamingo.trigger();
if ((keyPressed == true) && (trgrKura == true)) sndKura.trigger();
if ((keyPressed == true) && (trgrKelinci == true)) sndKelinci.trigger();
if ((keyPressed == true) && (trgrDeer == true)) sndDeer.trigger();
}
void stop()
{
// always close Minim audio classes when you are done with them
plgajah.close();
plbadak.close();
plpenguin.close();
plflamingo.close();
plkura.close();
plkelinci.close();
pldeer.close();
sndGajah.stop();
sndBadak.stop();
sndPenguin.stop();
sndFlamingo.stop();
sndKura.stop();
sndKelinci.stop();
sndDeer.stop();
minim.stop();
super.stop();
}
Thanks very much in advance! (and sorry for my bad english)
1