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 › 2D Overlays
Page Index Toggle Pages: 1
2D Overlays (Read 1515 times)
2D Overlays
Jan 15th, 2007, 5:31am
 
I find myself wanting to create a 2D HUD for a 3D view. Is there an easy way to do this? I thought maybe beginCamera() and endCamera() would do the trick, but that doesn't seem to be the case, unless I am using them wrong.

Anybody done this before?
Re: 2D Overlays
Reply #1 - Jan 15th, 2007, 12:02pm
 
You probably need to reset the depth-buffer before you do the 2D overlay, or it could well be "behind" the 3D elements.

What I've done in the past is:

Code:
void draw()
{
// do all 3D stuff
camera(); // back to default camera settings
for(int i=0;i<((PGraphics3D)g).zbuffer.length;i++)
{
((PGraphics3D)g).zbuffer[i]=Float.MAX_VALUE;
}
//draw 2D stuff
}
Re: 2D Overlays
Reply #2 - Jan 15th, 2007, 3:51pm
 
hint(DISABLE_DEPTH_TEST) should be all you need, just before you draw in 2D. if that doesn't work, someone please file a bug (and specify whether it's broken in P3D or OPENGL or both).
Re: 2D Overlays
Reply #3 - Jan 15th, 2007, 5:55pm
 
I did a HUD overlay a few weeks ago. What I did was to create a separate PGraphics object that I then was able to draw into. One of the benefits is that this PGraphics instance can use the JAVA2D renderer which supports antialiasing, while the sketch iselfs runs some 3D renderer. At least antialiased HUD should be possible, I just used the the color "color(129)" as a transparency mask when a copied the pixels from the HUD layer to the main graphics layer.

PGraphics gui;
PFont font;
//-----------------
void setup(){
 size(400,400,P3D);
 gui = createGraphics(width, height, JAVA2D );
 font = loadFont("silkscreen.vlw");
 gui.beginDraw();
 gui.textFont( font );
 gui.endDraw();
}
//-----------------
void draw(){

 translate( width/2, height/2 );
 noFill();
 sphere(100);
 // put arbitrary 3D-code here

 gui.beginDraw();
 gui.background( 129 );
 gui.text("this text resides in a separate graphics object",10,15);
 gui.text("with independent drawing routines",10,25);
 gui.rect( 30,300,40,40);
 // put arbitrary 2D-code here
 gui.endDraw();

 gui.loadPixels();
 loadPixels();
 for(int i=0;i<pixels.length;i++)
   if( gui.pixels[i]!=color(129) ) pixels[i] = gui.pixels[i];
 updatePixels();
 gui.updatePixels();

}
Re: 2D Overlays
Reply #4 - Jan 15th, 2007, 6:33pm
 
Before I file a bug about this I just want to make sure I'm using it right.

Here's a simple sketch:

Code:

import processing.opengl.*;

void setup()
{
size(400, 300, OPENGL);
}

void draw()
{
background(0);
pointLight(255, 255, 255, 0, 0, 0);
perspective(PI/1.5, width/height, 1, 1000);
camera(0, 0, 0, 0, 0, 1, 0, -1, 0);
stroke(0);
fill(255, 0, 0);
box(10);
hint(DISABLE_DEPTH_TEST);
fill(255);
rect(0, 0, 20, 20);
}


I run it and it looks like the inside of a room, that's fine. The place where I expect the rectangle to appear is the upper left hand corner, as if I was drawing in 2D or with no camera transforms. When I run this I don't see any rectangle. When I change the drawing mode to P3D I get a black screen. Is this a bug or am I just doing things out of order?

skanaar: That method seems like a good one, except that I want to use OPENGL and loading pixels is a slow operation in that mode.
Re: 2D Overlays
Reply #5 - Jan 15th, 2007, 7:16pm
 
I should have played with this a bit more before posting, here's a bit more info. I moved the camera outside of the cube just to make things a bit clearer.

Turns out, you don't need to disable depth testing, you just need to reset the camera by calling camera(). However, in the example I posted, I'm also setting a custom perspective. If you don't reset the perspective by calling perspective() then the point (0, 0) doesn't map to the top left corner because it is being transformed by the perspective. However, if you *do* reset the perspective before drawing 2D it removes the perspective transform from your 3D drawing. This was problematic when I had the camera inside the box because it meant that I could no longer even see the box, presumably because it was being clipped by the near clip plane.

To see the inside of the box *and* the rectangle, I had to reset *only* the camera and call hint(DISABLE_DEPTH_TEST). But this means that the rectangle is being transformed by the perspective and is therefore not drawing how I want it. Adding a perspective reset to that list make the rectangle appear in the upper left untransformed by the box is not visible.

All of that was with OPENGL. Switching to P3D and the outside the box behaves the same, inside exhibits the same behaviour for the rectangle but the box doesn't render properly. However, as has been pointed out, a 2D overlay can be easily accomplished in P3D with a separate drawing surface.

I think I'm going to make a webpage with pictures and code snippets to illustrate what I'm talking about because probably this is all very poorly described.
Re: 2D Overlays
Reply #6 - Jan 15th, 2007, 7:17pm
 
you'd need to call camera() (with no parameters) before you did your 2D drawing. so you'd need to add that before or after the hint() line.
Re: 2D Overlays
Reply #7 - Jan 15th, 2007, 8:10pm
 
roundup page:

http://code.compartmental.net/overlay_test/

will also post a bug because there seems to be some inconsistencies.
Re: 2D Overlays
Reply #8 - Jan 15th, 2007, 10:51pm
 
I think the call to perspective may be having an effect too. And processing isn't always happy with multiple calls to it within draw.
Page Index Toggle Pages: 1