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
Controlling cameras. (Read 1452 times)
Controlling cameras.
Aug 1st, 2007, 9:17pm
 
Hi all, first post - glad to be here!
First of all I am confident that my failure to make this work stems from my basic misunderstanding of the openGL system itself.
What I am attempting to do is to add a camera control to fly around this sketch, which generates boxes (posted below). My main problem is that whether I'm doing camera(), rotate() or similar things, it messes up the pattern generated inside the loop.

Please help me understand the mechanisms involved in taming this simple beast - thanks!

Andreas.

Code:
import processing.opengl.*;
void setup(){
size(800, 800, OPENGL);
}
void draw()
{
float m = millis();
fill(m % 255, 108, 0, 100);
// here we establish that every 20 ms we want a new box.
if(m%20==0) {
// Right - now we're starting the loop of events to
// create our dispersed boxes.
loop();
translate(m%620+100, m%709+50, (m%100)-50);
box(90);
if (m >= 30000) {
noLoop();
}
}
}
// What would it take to build a real-time movable camera within this sketch?
// Being a n00b, right now all camera/rotate actions I try affects the creation of boxes.

Re: Controlling cameras.
Reply #1 - Aug 2nd, 2007, 12:36am
 
To control the camera, simply use... the camera() instruction. And forget loop() and noLoop() as long as you did not explore all the possibilities of Processing without using these calls...

Here is one example of what you should looking for.
Note that I put the Z axis up to allow camera rotation on x and y axis.

import processing.opengl.*;
void setup(){
  size(800, 800, OPENGL);
}
float alpha=0;

void draw()
{
 float x,y;
background(0);

    x=width/2 * (1+cos(alpha));
    y=height/2 * (1-sin(alpha));
   camera(x,y, (height/2.0) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 0, 0, 0, 1);
   alpha+=0.1;
   
   translate(width/2,height/2);
   box(90);
     translate(100,0);
     box(50);
}
Re: Controlling cameras.
Reply #2 - Aug 2nd, 2007, 12:56am
 
Thank you for the reply, Olivier,
but the point I was getting at is that in a normal sketch, such as yours, I fully understand how to move around it - but whenever I have an open-ended autonomous system such as my silly example all camera movements mess up the generation.

I am just trying to separate the two processes... To be honest I don't understand how camera()could be able to affect the outcome of my sketch, but it certainly does...

And regarding the loop, it is used to stop the generation after a set amount of time - Once I hit 0.001 FPS it'll be even more difficult to move the camera.

If Processing can be made to accept camera movements in my sketch without messing up the generation of boxes I really would love to see it (and I know it can, I'm just hoping for people to accept the challenge!)

Thanks in advance,
a.
Re: Controlling cameras.
Reply #3 - Aug 2nd, 2007, 3:30am
 
first read up on the coordinate system:
http://processing.org/reference/environment/index.html#Coordinates
(that's the page you get from help -> getting started)

which will tell you that z goes negative, so you need to translate back before drawing, otherwise your objects are flying into the screen and being clipped.

next, get your head around rotate/translate and how they work.. there are lots of examples online in the reference section, and included with the download.

finally, once things make more sense, try playing with the camera (read the reference, check out the examples). also check out the ocd camera library, which lots of people use to get better camera control.
Re: Controlling cameras.
Reply #4 - Aug 2nd, 2007, 10:05am
 
You've got a significant problem with your code, which is probably causing you to think the genreation is being messed up.

You're only drawing each box once. It looks like you may think that you're adding boxes to a list to draw each frame, but it doesn't work like that. Each box is drawn once, and after that it's just a bunch of pixels on screen that wont move if you mve the camera. All moving the camera will achieve is that the box drawn in a frame will be affected. You'll also have no problem with the sketch slowing down, since you'll not ever be drawing more than one box a frame.

To do what I think you want to do, you'll need to create an array of sets fo coordinates, and redraw each box each frame, that way you'll see all boxes move with the camera movements.

So some pseudo code of what you'll need to do:

Code:

coord[] boxes;

class coords
{
float x,y,z;
coords(float _x, float // etc etc...
}

void setup()
{
//normal setup stuff;
boxes=new coord[0];
}

void draw()
{
background(0); // need to clear screen or there'll be a mess
if(boxes.length<1000) // limit number of boxes
{
boxes=(coord[])append(boxes,new coord(x,y,z));
}

// position camera here.


//now draw the boxes
for(int i=0;i<boxes.length;i++)
{
pushMatrix();
translate(boxes[i].x,boxes[i].y,boxes[i].z);
box(90);
popMatrix();
}
}
Re: Controlling cameras.
Reply #5 - Aug 10th, 2007, 12:51am
 
Thanks John, you've nailed it!

I am coming from a Jitter background, where objects such as jit.gl.sketch handle internal command lists that do all that crap for you. I guess there's no avoiding learning how to stack up an array of values anyway Smiley

Thanks for replying... I was getting very confused, since I thought I understood how to manipulate the camera... and I *did*! It was everything else that was wrong, hehe.

Cheers,
Andreas.
Page Index Toggle Pages: 1