We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › obj meshes and 3d color picking doesnt work
Page Index Toggle Pages: 1
obj meshes and 3d color picking doesnt work (Read 1433 times)
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();

 }

}
Re: obj meshes and 3d color picking doesnt work
Reply #1 - Feb 6th, 2010, 4:20pm
 
if you want to start it, please download an obj or use your own, because i cant upload ours to this site. thank you very much for your trouble.
Re: obj meshes and 3d color picking doesnt work
Reply #2 - Feb 6th, 2010, 5:26pm
 
maybe upload it to yousendit.com or something similar.
you will get a lot more help if people get the chance to run your code.
probably nobody will download some obj files and rename them to make it work
Re: obj meshes and 3d color picking doesnt work
Reply #3 - Feb 7th, 2010, 2:21am
 
ive uploaded it to rapidshare.

h t t p://rapidshare.com/files/347139223/game_1_.rar.html
Re: obj meshes and 3d color picking doesnt work
Reply #4 - Feb 7th, 2010, 3:27am
 
trying to download it since an hour. rs servers dont let me download it unless i am premium member. guess i have to wait until tonigh
Re: obj meshes and 3d color picking doesnt work
Reply #5 - Feb 7th, 2010, 7:35am
 
here is another downloadlink: h t t p://ww w.MegaShare.com/1790356

now we are trying to use boxes at the same position of the meshes to use the picking method, but still when we try to use the itemId to determine, which box we picked and what shall happen, it doesnt work.

it seems that the id is changing while the is sktech is running.
box one keeps id 1, box changes between id 1 and 2 and the third box doesnt show any reaction (we used println to find out which id is given to the boxes).
Re: obj meshes and 3d color picking doesnt work
Reply #6 - Feb 7th, 2010, 7:58am
 
The colour picking method requires that any translations, rotations and scaling that are used in the draw() method are duplicated when drawing to the buffer.

If we look at your code we can see that the same camera() call is made in both which is good but then in the mousePressed you have included
Code:
 buffer.rotateY(rotX);
buffer.rotateX(rotY);

after the camera() call so the objects drawn in the buffer will be in a different position to what you see on the screen.



Re: obj meshes and 3d color picking doesnt work
Reply #7 - Feb 7th, 2010, 1:55pm
 
this is our latest version, which still doesnt work properly:

h ttp://ww w.megaupload.com/?d=TS9V4UFV

if you cklick on the box for the lightswitch or on the box for the bible, the cassette mesh will be coloured, which is quite strange and we cant find the solution.

in our opinion, it has something to do with the id of the items and the getcolor function.

Page Index Toggle Pages: 1