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 & HelpPrograms › background refresh
Page Index Toggle Pages: 1
background refresh (Read 737 times)
background refresh
Dec 21st, 2009, 5:14am
 
hi, I have this problem, where I want mouse to draw in 3D so the 3D CUBE GRID needs to be freely rotated on the screen. That is all fine. But now how do I make the objects "stick" to their places since background() is refreshing the position every frame? Some one suggested arrays, but i can't figure out how to do it.

import processing.opengl.*;

Ball b1 = new Ball();
float xpos,ypos,zpos, sz;
PGraphics bf;
float expo = 1;
float rotx, roty;
float rate = 0.01;
color c2 = color(0);

void mouseDragged()
{
rotx = rotx + (mouseY - pmouseY) * rate;
roty = roty + (mouseX - pmouseX) * rate;
}

void setup()
{
size(700,400,OPENGL);
bf = createGraphics(width,height,P3D);
stroke(150);
}

void draw()
{
background(255);

if (keyPressed && keyCode==UP)
{
  expo = expo + 0.01;
}
if (keyPressed && keyCode==DOWN)
{
  expo = expo - 0.01;
}
bf.beginDraw();
bf.background(0);
translate(350,200,0);
bf.translate(350,200,0);
rotateX(rotx);
bf.rotateX(rotx);
rotateY(roty);
bf.rotateY(roty);
scale(expo);
bf.scale(expo);
translate(-350,-350,-350);
bf.translate(-350,-350,-350);
for (int x = 150; x<=550; x+=100)
{
  for (int y = 150; y<=550; y+=100)
  {
    for (int z = 150; z<=550; z+=100)
    {      
      line(150,y,z,x,y,z);
      line(x,150,z,x,y,z);
      line(x,y,150,x,y,z);
      bf.pushMatrix();
      bf.translate(x,y,z);
      bf.noStroke();
      bf.fill(x/10,y/10,z/10);
      bf.rectMode(CENTER);
      bf.rect(0,0,10,10);
      bf.rotateX(PI/2);
      bf.rect(0,0,10,10);
      bf.rotateX(PI/2);
      bf.rotateY(PI/2);
      bf.rect(0,0,10,10);
      bf.popMatrix();
    }
  }
}
color c1 = bf.get(mouseX,mouseY);
if (c1 > c2)
  {
    b1.drawball();
    xpos = red(c1)*10;
    ypos = green(c1)*10;
    zpos = blue(c1)*10;
    sz = 10;
  }
}

class Ball
{
 void drawball()
 {
   translate(xpos,ypos,zpos);
   sphere(sz);
 }
}
Page Index Toggle Pages: 1