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 & HelpOpenGL and 3D Libraries › Camera positioning, orbit
Page Index Toggle Pages: 1
Camera positioning, orbit (Read 2518 times)
Camera positioning, orbit
Mar 17th, 2006, 6:23pm
 
Hi all,

I have a problem controlling the camera. My objective is simply to orbit the object of interest using cursor keys.

Code:


/* starting camera coordinates */
float z = 500;
float x = 100;
float y = 100;

float theta = PI/3;

void setup(){
size(512,512,P3D);
camera(x,y,z, 0,0,0, 0,-1,0); // init camera
}

void draw(){
background(50);

pushMatrix();
box(100,100,100);
popMatrix();

}

void keyPressed() {
if(key == CODED) {
if (keyCode == UP){
y = (y * cos(theta)) - (x * sin(theta));
}
if (keyCode == DOWN){
y = (y * cos(theta)) + (x * sin(theta));
}
if (keyCode == LEFT){
x = (x * cos(theta)) - (y * sin(theta));
}
if (keyCode == RIGHT){
x = (x * cos(theta)) + (y * sin(theta));
}

camera(x,y,z, 0,0,0, 0,-1,0); // reposition camera
}

}


.. the above does not work as desired, and I would really appreciate (read: desperately need) some guidance with this :(

Thank you for your time,
Re: Camera positioning, orbit
Reply #1 - Oct 12th, 2006, 7:42pm
 
I implemented a rotation for the camera which works fine with P3D, but NOT with OPENGL , I don't know why ...any help regarding the OPENGL is very much appreciated

here is my code, I hope it helps you since it works fine with P3D:

//-----------------------------------------------------
import processing.opengl.*;
import javax.media.opengl.*;

float cameraX = 0,cameraY=0,cameraZ=0,targetX=0,targetY=0,targetZ=0,N_cameraX=0,N_cameraY=1,N_ca
meraZ=0;
float cameraAlpha = 30, cameraBeta=30;
float cameraRadius = 20;

//GL gl;

void setup(){
 size(800,800,P3D);
 //size(800,800,OPENGL);
 background(0,0,0);
// gl = ((PGraphicsGL) g).gl;
 
 }
 
 
void draw(){
background(0,0,100);
//gl.glClear(GL.GL_DEPTH_BUFFER_BIT);

camera();

 cameraMove();
camera( cameraX, cameraY, cameraZ, targetX, targetY, targetZ, N_cameraX, N_cameraY, N_cameraZ);
 fill(255,0,0);
//lights();
 box(4);
 }
 
 
 void cameraMove(){

 cameraX = cameraRadius * cos(radians(cameraAlpha))*cos(radians(cameraBeta));
 cameraY = cameraRadius * sin(radians(cameraAlpha));
 cameraZ = cameraRadius *cos(radians(cameraAlpha)) *sin(radians(cameraBeta));

 if (cameraY > 0 && cameraY< cameraRadius)
 {
 N_cameraX= 0;//-cameraX;
 N_cameraY = -cameraY ;//-cameraY + cameraRadius/cameraY;
 N_cameraZ = 0;//-cameraZ;
 }
 
 println("cameraX = " + cameraX);
 println("cameraY = " + cameraY);
 println("cameraZ = " + cameraZ);
 println("NcameraX = " + N_cameraX);
 println("NcameraY = " + N_cameraY);
 println("NcameraZ = " + N_cameraZ);
 }
 
void keyPressed(){
 if (keyCode == DOWN)
 cameraAlpha -= 1;
 if (keyCode == UP)
  cameraAlpha += 1;
    if (keyCode == LEFT)
  cameraBeta -= 1;
 if (keyCode== RIGHT)
cameraBeta += 1;
 
if (key == 'z')
 cameraRadius -=1;
 if (key == 'Z')
 cameraRadius +=1;
 
if(key == 'r' || key == 'R'){
cameraX = 0;
cameraY=0;
cameraZ=0;
targetX=0;
targetY=0;
targetZ=0;
N_cameraX=0;
N_cameraY=1;
N_cameraZ=0;
cameraAlpha = 30;
cameraBeta=30;
cameraRadius = 20;
  }  
 }
 
 
void mouseDragged(){
 if(mouseButton == RIGHT){
 cameraX -= (pmouseX-mouseX)/2;
 cameraY -= (pmouseY-mouseY)/2;
 targetX -= (pmouseX-mouseX)/2;
 targetY -= (pmouseY-mouseY)/2;
 }
 
  if(mouseButton == LEFT){
 cameraAlpha -= pmouseY - mouseY;
  cameraBeta += pmouseX - mouseX;
 }
 
 }
Re: Camera positioning, orbit
Reply #2 - Nov 22nd, 2009, 1:45pm
 
some help for OPENGL....

Code :
 //-----------------------------------------------------
 import processing.opengl.*;
 import javax.media.opengl.*;

 float cameraX = 0,cameraY=0,cameraZ=0,targetX=0,targetY=0,targetZ=0,N_cameraX=0,N_cameraY=1,N_ca
meraZ=0;
 float cameraAlpha = 150/*30*/, cameraBeta= 150/*30*/;
 float cameraRadius = 200;
 Debug debug;

 void setup(){
   size(800,800,OPENGL);
   background(0);
   PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
   debug = new Debug(pgl);
  }


 void draw(){
   background(0);
   PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
   GL gl = pgl.gl;
   debug.show(pgl);
   camera();
   cameraMove();
   camera( cameraX, cameraY, cameraZ,  0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
   lights();
   sphere(40);
 }


 void cameraMove(){
   cameraX = cameraRadius * cos(radians(cameraAlpha))*cos(radians(cameraBeta));
   cameraY = cameraRadius * sin(radians(cameraAlpha));  
   cameraZ = cameraRadius *cos(radians(cameraAlpha)) *sin(radians(cameraBeta));

  if (cameraY > 0 && cameraY< cameraRadius) {
    N_cameraX= 0;//-cameraX;
    N_cameraY = -cameraY ;//-cameraY + cameraRadius/cameraY;
    N_cameraZ = 0;//-cameraZ;
  }

  println("cameraX = " + cameraX);
  println("cameraY = " + cameraY);
  println("cameraZ = " + cameraZ);
  println("NcameraX = " + N_cameraX);
  println("NcameraY = " + N_cameraY);
  println("NcameraZ = " + N_cameraZ);
}

 void keyPressed(){
   if (keyCode == DOWN) cameraAlpha -= 1;
   if (keyCode == UP) cameraAlpha += 1;
   if (keyCode == LEFT) cameraBeta -= 1;
   if (keyCode== RIGHT) cameraBeta += 1;
   if (key == 'z') cameraRadius -=1;
   if (key == 'Z') cameraRadius +=1;

   if(key == 'r' || key == 'R'){
     cameraX = 0;
     cameraY=0;
     cameraZ=0;
     targetX=0;
     targetY=0;
     targetZ=0;
     N_cameraX=0;
     N_cameraY=1;
     N_cameraZ=0;
     cameraAlpha = 30;
     cameraBeta=30;
     cameraRadius = 20;
 }  
}


 void mouseDragged(){
  if(mouseButton == RIGHT){
      cameraX -= (pmouseX-mouseX)/2;
      cameraY -= (pmouseY-mouseY)/2;
      targetX -= (pmouseX-mouseX)/2;
      targetY -= (pmouseY-mouseY)/2;
  }
 
   if(mouseButton == LEFT){
     cameraAlpha -= pmouseY - mouseY;
     cameraBeta += pmouseX - mouseX;
  }
 
}



 public class Debug  {
   
   public void show(PGraphicsOpenGL pgl) {
       draw_axes(pgl);
   }
   public Debug(PGraphicsOpenGL pgl) {
       
   }
   
 
   public void draw_axes (PGraphicsOpenGL pgl){
     GL gl;
     gl = pgl.beginGL();  
     gl.glPushMatrix();
     gl.glColor3f(0.,255.,0.);
     gl.glBegin(GL.GL_LINES);
     gl.glVertex3f(0, 0, 0);
     gl.glVertex3f(250, 0, 0);
     gl.glEnd();
     gl.glColor3f(255.,0.,0.);
     gl.glBegin(GL.GL_LINES);
     gl.glVertex3f(0, 250, 0);
     gl.glVertex3f(0, 0, 0);
     gl.glEnd();
     gl.glColor3f(0.,0.,255.);
     gl.glBegin(GL.GL_LINES);
     gl.glVertex3f(0, 0, 250);
     gl.glVertex3f(0, 0, 0);
     gl.glEnd();
     gl.glPopMatrix();
     pgl.endGL();  
 
  }
 }
Page Index Toggle Pages: 1