We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am having trouble figuring out how to translate this. I am trying to make something that tracks pixels of a certain color based on their location and draws a model on that location. This is easy to do with one model and one pixel color, following Shiffman's example (http://www.learningprocessing.com/examples/chapter-16/example-16-11/) but I am trying to be able to do this three times, so the user can click up to three different colors and it will display a different object per color. The place I am held up with is converting the PVector trackColor[] (which is also an Array) into the color and location that the mousePressed && KeyPressed is. I've tried reformatting it but don't know what I am doing wrong with regard to assigning the array index those 3 variable. I get various error messages from missing a right parenthesis, to cannot convert Pvector to int.....
Here's the code,
you can substitute your own .obj models or simply erase the pushMatrix() stuff and draw an ellipse using the same variables??
Any advice is much appreciated!!!!
import saito.objloader.*;
import processing.video.*;
OBJModel model0, model1, model2;
Capture video;
int[] models = new int[3]; //average of pixel locations tracked, where models should be placed, this should be a PVector???
PVector[] trackColor= new PVector[5]; //color as a location in the video where it is tracked, I want to use a standard z value
int Assigned = 0; //# of mousepressed event/assigned pixel colors to toggle on/off
int mouseColor = 0; //sample of the color where mouse is pressed
void setup() {
size(screen.width, screen.height, P3D);
model0 = new OBJModel(this, "planque1.obj");
model0.enableDebug();
model0.translateToCenter();
model1 = new OBJModel(this, "planque2.obj");
model1.enableDebug();
model1.translateToCenter();
model2 = new OBJModel(this, "planque4.obj");
model2.enableDebug();
model2.translateToCenter();
video = new Capture(this,width,height,30);
}
void draw() {
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
background(video);
for (int x = 0; x < video.width; x++) {
for (int y = 0; y < video.height; y++ ) {
//calculate the 1D location from a 2D grid
int loc = x + y*video.width;
//get the R,G,B values from image
color currentColor = video.pixels[loc];
float r1 = red (currentColor);
float g1 = green (currentColor);
float b1 = blue (currentColor);
float r2 = red (mouseColor);
float g2 = green(mouseColor);
float b2 = blue(mouseColor);
float d= dist(r1, g1, b1, r2, g2, b2);
if ((Assigned>0) && (currentColor== mouseColor)) {
pushMatrix();
translate(width/2, height/2, 1);
model0.draw();
popMatrix();
}
if ((Assigned>0) && (currentColor== mouseColor)) {
pushMatrix();
translate(trackColor[1].x, trackColor[1].y, trackColor[1].z);
model1.draw();
popMatrix();
}
if ((Assigned>0) && (currentColor== mouseColor)) {
pushMatrix();
translate(trackColor[2].x, trackColor[2].y, trackColor[2].z);
model2.draw();
popMatrix();
}
}
}
}
void mousePressed() {
//get location of mouse press :
int loc = mouseX + mouseY*video.width;
mouseColor = video.pixels[loc];
//depending on keypressed:
if(mousePressed && keyPressed && keyCode == '1');
trackColor[0] = mouseColor.get(); //get mousecolor location, and set it as the #index in the PVector array, this is the problem!!!
Assigned= 1;
if(mousePressed && keyPressed && keyCode == '2');
trackColor[1]= mouseColor;
Assigned=2;
if(mousePressed && keyPressed && keyCode == '3');
trackColor[2]= mouseColor;
Assigned=3;
if(mousePressed && keyPressed && keyCode == SHIFT);
Assigned = 0;
}
Answers
Sorry, first time i reply here, beside the code is a bit messy, but it works :p
Alright, i don't know what's wrong with my computer but i couldn't post the code correctly...I worked on a similar project, here's the code i made based on the previous tutorial, i track four colors chosen by a right click of the mouse, you can predefine them...
//Tracking four colors, each color is selected by a right mouse click
//By HARIK el Houssein Chouaib //2014
Merci!!!! its working just figuring out some OBJloader stuff now, although still curious what was wrong with array/PVector situation....
Thank you!!!!!!
@Chouaib! In order to post code correctly formatted for the forum, highlight it and hit CTRL+K. Thx! =:)
@julia789 You're welcome :)
@GoToLoop Thank you very much, i edited the post :)