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 & HelpSyntax Questions › ZNear, does it work very near to the camera
Page Index Toggle Pages: 1
ZNear, does it work very near to the camera? (Read 530 times)
ZNear, does it work very near to the camera?
Sep 25th, 2008, 11:19pm
 
Hello.
I started with Processing yesterday, I was trying to modify ZNear because when an object comes to close to the camera it disappears.
I have tried with different values but nothing seems to work.
Do you have a clue? I have read in the ALPHA forum that this is normal, but I wonder if it has been solved for the BETA.

In my code, if you move the cube very close to the camera it disappears. You can move it with WASD

Code:


float myZ=0, myX=0;
boolean pressW, pressA, pressS, pressD;
float step=0.5, nextX, nextZ, eyeX=0, eyeZ=20;
float camdegree=90;

MobileThing theBox = new MobileThing(0, 0, 0);

void setup() {
size(700, 500, P3D);
fill(204);
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(PI * fov / 360.0);
perspective(fov, float(width)/float(height),
cameraZ/10.0, cameraZ*10.0);




}


void draw() {
//calculateCamera();
lights();
background(0);

theBox.calculateWalk();

keyControl();


// Change height of the camera with mouseY
camera(eyeX, 3, eyeZ, // eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, // centerX, centerY, centerZ
0.0, 0.0, 1.0); // upX, upY, upZ

stroke(255,0,0);
line(-100, 0, 0, 100, 0, 0);//X
stroke(0,255,0);
line(0, -100, 0, 0, 100, 0);//Y
stroke(0,0,255);
line(0, 0, -100, 0, 0, 100);//Z
line (theBox.nextX, -2, theBox.nextZ, theBox.posX, -2, theBox.posZ);
pushMatrix();
translate(theBox.posX,0,theBox.posZ);
rotateY (-radians(theBox.degree));
box(2);
popMatrix();
pushMatrix();
sphere(0.1);
popMatrix();
}

void keyControl()
{
if (pressW==true) theBox.walk(true);
if (pressA==true) theBox.turn(false);
if (pressS==true) theBox.walk(false);
if (pressD==true) theBox.turn(true);
}

void keyPressed()
{
if (key=='w') pressW=true;
if (key=='a') pressA=true;
if (key=='s') pressS=true;
if (key=='d') pressD=true;

}


void keyReleased()
{
if (key=='w') pressW=false;
if (key=='a') pressA=false;
if (key=='s') pressS=false;
if (key=='d') pressD=false;
}

class MobileThing
{
float posX, posY, posZ, nextX, nextZ, prevX, prevZ, speed=0.3, degree=0;
MobileThing (float x, float y, float z)
{
posX = x;
posY = y;
posZ = z;
}
void calculateWalk()
{
nextX = posX + cos (radians(degree))*speed;
nextZ = posZ + sin (radians(degree))*speed;
prevX = posX - cos (radians(degree))*speed;
prevZ = posZ - sin (radians(degree))*speed;
}
void walk(boolean d)
{
if (d==true)
{
posX=nextX;
posZ=nextZ;
}
else
{
posX=prevX;
posZ=prevZ;
}
}
void turn (boolean a)
{
if (a==true) degree=degree-5;
else degree=degree+5;
}
}
Re: ZNear, does it work very near to the camera?
Reply #1 - Sep 26th, 2008, 10:54am
 
all i can think of is to adjust the fov to zoom in slightly, so that the camera doesn't have to get so close - another option is to try out the OCD camera library, this might behave better regarding z clipping.
Re: ZNear, does it work very near to the camera?
Reply #2 - Sep 26th, 2008, 4:32pm
 
Thanks for your comments Glenn. It was for one of your videos that I decide to try Processing Smiley

I added the OCD library but the things are the same: the cube vanishes before it reaches the camera. And I need the camera comes closer because this is for a First Person videogame experiment.

The current code it is

Code:

import damkjer.ocd.*;

Camera camera1;

float myZ=0, myX=0;
boolean pressW, pressA, pressS, pressD;
float step=0.5, nextX, nextZ, eyeX=0, eyeZ=20;

MobileThing theBox = new MobileThing(0, 0, 0);

void setup() {
size(700, 500, P3D);
fill(204);
camera1 = new Camera(this, 0, 0, 30, 0.1, 100); //Camera(parent, cameraX, cameraY, cameraZ, nearClip, farClip)

}


void draw() {
lights();
background(0);
camera1.feed();

theBox.calculateWalk();
keyControl();
stroke(255,0,0);
line(0, 0, 0, 100, 0, 0);//X
stroke(0,255,0);
line(0, 0, 0, 0, 100, 0);//Y
stroke(0,0,255);
line(0, 0, 0, 0, 0, 100);//Z
line (theBox.nextX, -2, theBox.nextZ, theBox.posX, -2, theBox.posZ);
pushMatrix();
translate(theBox.posX,0,theBox.posZ);
rotateY (-radians(theBox.degree));
box(2);
popMatrix();
}

void keyControl()
{
if (pressW==true) theBox.walk(true);
if (pressA==true) theBox.turn(false);
if (pressS==true) theBox.walk(false);
if (pressD==true) theBox.turn(true);
}

void keyPressed()
{
if (key=='w') pressW=true;
if (key=='a') pressA=true;
if (key=='s') pressS=true;
if (key=='d') pressD=true;

}


void keyReleased()
{
if (key=='w') pressW=false;
if (key=='a') pressA=false;
if (key=='s') pressS=false;
if (key=='d') pressD=false;
}

class MobileThing
{
float posX, posY, posZ, nextX, nextZ, prevX, prevZ, speed=0.3, degree=0;
MobileThing (float x, float y, float z)
{
posX = x;
posY = y;
posZ = z;
}
void calculateWalk()
{
nextX = posX + cos (radians(degree))*speed;
nextZ = posZ + sin (radians(degree))*speed;
prevX = posX - cos (radians(degree))*speed;
prevZ = posZ - sin (radians(degree))*speed;
}
void walk(boolean d)
{
if (d==true)
{
posX=nextX;
posZ=nextZ;
}
else
{
posX=prevX;
posZ=prevZ;
}
}
void turn (boolean a)
{
if (a==true) degree=degree-5;
else degree=degree+5;
}
}


Do you have another idea? Thanks again.
Re: ZNear, does it work very near to the camera?
Reply #3 - Sep 26th, 2008, 6:24pm
 
i'm not an expert in this area at all sadly, the only other thing I can think of trying is to reduce the overall scale of your objects, you might be able to get them closer to the camera that way.
Re: ZNear, does it work very near to the camera?
Reply #4 - Sep 26th, 2008, 8:28pm
 
hi, you can use the perspective() function to set the nearZ value.

you can try something like perspective(PI/3, width/height, 0.1, 1000);

just dont set the nearZ value to zero, i remember reading that it can mess things up.
Re: ZNear, does it work very near to the camera?
Reply #5 - Sep 26th, 2008, 9:32pm
 
Hello Glenn and Pelintra. Thanks for your feedback.

I tried both methods with same results Sad
I have reduced ZNear to 0.1 and 0.01 but as you can see in the images when the box comes closer to the camera it disappears.

The odd thing is that the distance to the camera is bigger than ZNear, in this screenshot the distance ZNear is 0.1 and the distance to the box should be 2.0 aprox.

...
...
Re: ZNear, does it work very near to the camera?
Reply #6 - Sep 26th, 2008, 10:57pm
 
glenn has pointed you in the right direction, but might've typo'd...  if you increase the scale of everything, eyez=200, box size = 20, walk speed = 3, etc, then it'll still look the same, but you'll be able to get right up on the box using something like perspective(fov,aspect,1,1000)

though note that rendering glitches can occur with very low znear values (by "wasting" zbuffer resolution) so don't set it any lower than you have to
Re: ZNear, does it work very near to the camera?
Reply #7 - Sep 27th, 2008, 1:37am
 
thats strange. i had some similar problems a while ago, and simply setting the nearZ value with the perspective function solved it. but i was using the opengl renderer and just noticed you're using the P3D. not sure if its related but maybe it would be worth trying with the opengl and see if that works.

*edit: the one application i mentioned before where i had some similar problems, i just tried changing the renderer to P3D and no matter how i set the values in the perspective it allways looked the same. so maybe that function simply doesn't work with P3D?
Re: ZNear, does it work very near to the camera?
Reply #8 - Sep 27th, 2008, 7:52am
 
well spotted pelintra, try opengl, this is what i was using when i had a similar problem/fix.
Re: ZNear, does it work very near to the camera?
Reply #9 - Sep 27th, 2008, 2:56pm
 
Solved!
I changed to OpenGL as you suggested and everything is fine now.
Thanks for your help guys.
Page Index Toggle Pages: 1