Maverick
YaBB Newbies
Offline
Posts: 5
obj meshes and 3d color picking doesnt work
Feb 6th , 2010, 4:18pm
we have tried to make an 3d adventure in processing. with the 3d color picking method from the processing site, we were able to find out if the mouse is over a box and program some interactions, but if you use meshes instead of boxes it isnt working anymore. we have no clue, where the problem is and we would be very grateful, if somebody could help us. import processing.opengl.*; import saito.objtools.*; import saito.objloader.*; OBJModel room; OBJModel bed; OBJModel stool; OBJModel desk; OBJModel lightswitch; OBJModel casette; OBJModel bible; OBJModel sink; OBJModel blanket; OBJModel door; PickableItems[]myPickableItemsArray; int wSize; int hSize; float rotX; float rotY; color c; color staticMesh; int xItemsPos; int yItemsPos; int zItemsPos; int itemId; PGraphics buffer; int boxPressed; void setup() { frameRate(30); startParameter(); size(wSize,hSize, OPENGL); meshLoad(); buffer = createGraphics(width, height, P3D); pickableItemsDraw(); } void draw() { lightning(); background(0); camera(0, -20, 0, 0, -20, -50, 0, 1, 0); // itemArrayDraw(); pickableItemsArrayDraw(); // itemDraw(); } //void itemDraw() //{ // for (int i=0; i<myItemArray.length; i++) // { // myItemArray[i].itemsDisplay(); // } //} void pickableItemsDraw() { myPickableItemsArray = new PickableItems[6]; myPickableItemsArray[0] = new PickableItems(120, -27, -188, 5); myPickableItemsArray[1] = new PickableItems(259, 4, -59, 6); myPickableItemsArray[2] = new PickableItems(250, 9, 0, 7); myPickableItemsArray[3] = new PickableItems(-18, 13, 235, 8); myPickableItemsArray[4] = new PickableItems(-231, 88, 49, 9); myPickableItemsArray[5] = new PickableItems(0, 0, 400, 10); } //void itemArrayDraw() //{ // myItemArray = new Items[4]; // myItemArray[0] = new Items(0, 0, 0, 1); // myItemArray[1] = new Items(-250, 90, -26, 2); // myItemArray[2] = new Items(140, 65, 20, 3); // myItemArray[3] = new Items(247, 85, 0, 4); //} void pickableItemsArrayDraw() { rotateY(rotX); for (int i = 0; i < myPickableItemsArray.length; i++) { myPickableItemsArray[i].display(this.g); } } void meshLoad() { room = new OBJModel(this, "meshes/room.obj"); bed = new OBJModel(this, "meshes/bed.obj"); stool = new OBJModel(this, "meshes/stool.obj"); desk = new OBJModel(this, "meshes/desk.obj"); lightswitch = new OBJModel(this, "meshes/lightswitch.obj"); casette = new OBJModel(this, "meshes/casette.obj"); bible = new OBJModel(this, "meshes/bible.obj"); sink = new OBJModel(this, "meshes/sink.obj"); blanket = new OBJModel(this, "meshes/blanket.obj"); door = new OBJModel(this, "meshes/door.obj"); } void startParameter() { wSize = 800; hSize = 800; } void mouseDragged() { rotX += (mouseX - pmouseX) * 0.005; rotY += (mouseY - pmouseY) * 0.0005; } void lightning() { rotateY(rotX); lights(); // pointLight(234, 234, 234, 0, -150, 0); // ambientLight(100, 100, 100); // emissive(12, 12, 12); } void mouseClicked() { buffer.beginDraw(); buffer.background(0); buffer.noStroke(); buffer.camera(0, -20, 0, 0, -20, -50, 0, 1, 0); buffer.rotateY(rotX); buffer.rotateX(rotY); for (int i = 0; i < myPickableItemsArray.length; i++) { myPickableItemsArray[i].drawBuffer(buffer); } buffer.endDraw(); // get the pixel color under the mouse color pick = buffer.get(mouseX, mouseY); // get object id int itemId = getItemId(pick); if (itemId >= 0) { println("0"); // change the cube color myPickableItemsArray[itemId].removeModell(); } } // id 0 gives color -2, etc. color getColor(int itemId) { return itemId; } int getItemId(color staticMesh) { { println("hoh"); return staticMesh+1; } } class PickableItems { color staticMesh; int xItemsPos; int yItemsPos; int zItemsPos; int itemId; public PickableItems(int xItemsPos, int yItemsPos,int zItemsPos, int itemId) { this.xItemsPos=xItemsPos; this.yItemsPos=yItemsPos; this.zItemsPos=zItemsPos; this.itemId=itemId; staticMesh = color(random(255), 250, 50); } public void removeModell() { int r = (int)red(c); int g = (int)green(c); int b = (int)blue(c); staticMesh = color(r, 255 - g, b); if (itemId == 10) { println("hello"); door.clear(); } } public void display(PGraphics ecran) { ecran.fill(staticMesh); drawPicking(ecran); } public void drawBuffer(PGraphics buffer) { color itemIdColor = getColor(itemId); buffer.fill(itemIdColor); drawPicking(buffer); } private void drawPicking(PGraphics g) { g.lightswitch = new OBJModel(this, "meshes/lightswitch.obj"); g.pushMatrix(); if (itemId == 5) { g.rotateY(rotX); g.rotateX(rotY); g.translate(xItemsPos, yItemsPos, zItemsPos); g.lightswitch.disableMaterial(); g.lightswitch.draw(); } g.popMatrix(); pushMatrix(); if (itemId == 6) { rotateY(rotX); rotateX(rotY); translate(xItemsPos, yItemsPos, zItemsPos); casette.disableMaterial(); casette.draw(); } popMatrix(); pushMatrix(); if (itemId == 7) { rotateY(rotX); rotateX(rotY); translate(xItemsPos, yItemsPos, zItemsPos); bible.disableMaterial(); bible.draw(); } popMatrix(); pushMatrix(); if (itemId == 8) { rotateY(rotX); rotateX(rotY); translate(xItemsPos, yItemsPos, zItemsPos); sink.disableMaterial(); sink.draw(); } popMatrix(); pushMatrix(); if (itemId == 9) { rotateY(rotX); rotateX(rotY); translate(xItemsPos, yItemsPos, zItemsPos); blanket.disableMaterial(); blanket.draw(); } popMatrix(); pushMatrix(); if (itemId == 10) { rotateY(rotX); rotateX(rotY); translate(xItemsPos, yItemsPos, zItemsPos); door.disableMaterial(); door.draw(); } popMatrix(); } }