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 › understanding the 3D camera
Page Index Toggle Pages: 1
understanding the 3D camera (Read 206 times)
understanding the 3D camera
Jan 12th, 2009, 12:02am
 
I've been trying draw a rectangle perpendicular to a movable camera. Was thinking that if the camera held its distance to the point where it was pointing and the rectangle would rotate as much as the camera then it should show up without perspective. Now when the camera tilts though, the rectangle appears further away and skewed.

Code:


float h2;
float fov;
float fov2;
float cameraY;
float cameraZ;
float xRot;
int boxW;

void setup() {
size(200, 200, P3D);
h2 = height/2;
fov = PI / 3;// 60°
fov2 = fov / 2;
cameraZ = h2 / (float) tan(fov / 2);
boxW = 25;
}

void draw(){
background(200);

xRot = map(mouseY,0,height,fov2*-1,fov2);
cameraY = cameraZ*sin(xRot);
camera(0, cameraY, cameraZ, 0, 0, 0, 0, 1, 0);

noFill();
stroke(50,200,50);
box(boxW);

rectMode(CENTER);
rect(0,0,width, height);

rotateX(2*PI-xRot);
stroke(255,50,50);
rect(0,0,width, height);
}


Is there some fundamental aspect of the 3D camera which has passed me by?
Re: understanding the 3D camera
Reply #1 - Jan 12th, 2009, 12:26am
 
…it's good to take a break.
Mistook the camera's distance to the viewpoint as the z-axis value.

Code:

float h2;
float fov;
float fov2;
float d;
float cameraY;
float cameraZ;
float xRot;
int boxW;

void setup() {
size(200, 200, P3D);
h2 = height/2;
fov = PI / 3;// 60°
fov2 = fov / 2;
d = h2 / (float) tan(fov / 2);
boxW = 25;
}

void draw(){
background(200);

xRot = map(mouseY,0,height,fov2*-1,fov2);
cameraY = d*sin(xRot);
cameraZ = d*cos(xRot);
camera(0, cameraY, cameraZ, 0, 0, 0, 0, 1, 0);

noFill();
stroke(50,200,50);
box(boxW);

rectMode(CENTER);
rect(0,0,width, height);

rotateX(2*PI-xRot);
stroke(255,50,50);
rect(0,0,width, height);
}
Page Index Toggle Pages: 1