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.
Page Index Toggle Pages: 1
Draw on demand (Read 1757 times)
Draw on demand
Mar 16th, 2009, 4:17pm
 
Let's say I have 100,000 objects...
Of course, this drags the performance down.  However, I don't need to see or track all the 100,000 objects at once.  I thought that perhaps a solution might be to only draw what is notably visible in a particular x,y,z range.  As the camera moves, the range changes so the program is only having to draw and track 1,000 points or less at any particular moment.  Any thoughts on how one might be able to accomplish this or any other ideas on drawing on demand.  Thanks
Re: Draw on demand
Reply #1 - Mar 16th, 2009, 7:08pm
 
In particular, I want to be able to do this with text, which kills the system.  I've tried several different methods on the forum, but none of them were able to handle 15,000 or so labels being displayed, never mind anything larger.  So I need to figure out something that tells the text to only draw if the text is in a viewable area.
Re: Draw on demand
Reply #2 - Mar 17th, 2009, 8:04am
 
Offer your code if possible,and I'll try it on my quatro-core machine:)
Re: Draw on demand
Reply #3 - Mar 17th, 2009, 7:20pm
 
I'll have to create an example, my code is way too long and ugly.

The camera perspective() zFar and zNear can control the entire picture, but I'd like to be more selective in what I crop out.  I've read where people consider the distance from the camera and change the size or quality... this would be what I'm looking for.  I'd like to selectively crop the text at a certain zFar.
Re: Draw on demand
Reply #4 - Mar 23rd, 2009, 1:49pm
 
I wanted to report (for anyone searching in the future) that I was able to achieve this using screenZ().  An obvious solution that caused me problems due to the way OpenGL handles the Z depth.  It appears the far clip (camera perspective) is set to 1 and decreases as it moves toward the camera reaching 0.  Most drawing can be clipped between 1 and 0, although the number can go negative (which decreases at some normal scale factor it seems).  I set my object to clip at less than ".885", with a key control to increase and decrease by ".01".

Code:

float  texttol = .885;
...

if ( screenZ(x,y,z) < texttol) {
   pushMatrix();
   translate(x, y, z);
   text("label", 0,20,0);
   popMatrix();
}
...

void keyPressed() {
 if (key == ',' || key == '<') {
     texttol = texttol - .01;
   }
   else if (key == '.' || key == '>') {
     texttol = texttol + .01;
   }
}


The question that I'm now testing is does the camera clipping improve performance.  If I selectively (as above) clip the far text, does it also help to clip the near text that falls behind the camera.  Is the performance the same as the camera automatically clipping the view, than if I manually reduce the objects outside of the camera view.
Re: Draw on demand
Reply #5 - Mar 27th, 2009, 1:09pm
 
Ok, this comes as a surprise to me but I tested it out.  The clipping of the camera does not reduce the work for the items drawn and thus it will decrease performance on a large number of objects (particularly text) whether you can see them or not.   Here is what I did to decrease the draw, which significantly improved the performance and made it usable.  I increased my textol to ".90" (I increment by .01 for testing what works best).
Code:

float  texttol = .90;
int gh, gw;
gh = getHeight();
gw = getWidth();
...
if ( screenZ(x,y,z) < texttol && screenZ(x,y,z) > 0 &&
    screenX(x,y,z) > -gw && screenX(x,y,z) < gw &&
    screenY(x,y,z) > -gh && screenY(x,y,z) < gh)
  {
     pushMatrix();
     translate(x, y, z);
     rotateX(rotations[0]);
     rotateY(rotations[1]);
     rotateZ(rotations[2]);
     text(label, 0,w/2,0);
     popMatrix();
   }
Re: Draw on demand
Reply #6 - May 2nd, 2009, 8:55am
 
jeffg,

Thanks so much for this bit of code. This is extremely helpful, and at least doubles the framerate on a current project. Thanks for posting!

best,
Zachary
Page Index Toggle Pages: 1