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 › Perspective, FOV and OPENGL
Page Index Toggle Pages: 1
Perspective, FOV and OPENGL (Read 3079 times)
Perspective, FOV and OPENGL
Mar 11th, 2009, 4:28pm
 
Hi, I have this piece of code to pan and rotate around a cube, and seems to be working perfectly in P3D, but when I try to run the sketch using OPENGL, the FOV doesn't reset when I reset the perspective.

Could anybody help me with this?

This is the code:

//-----------------------------------------------


import processing.opengl.*;

float alpha1 = 10.594999;
float alpha2 = 1.1074983;
float alpha1_ = 0;
float alpha2_ = 0;
float distance = 0.4113693;
int mouse_x, mouse_y;
float translate1 = 0.6165447;
float translate2 = 0.4113693;
float translate3 = 3.977392;
float fov = 0.5;

void setup(){

 size(1200,920,P3D);

}

void draw(){

 //--------------- This should reset the settings
 
 camera();
 perspective();
 background(0);
 lights();

 //--------------- After this, the box stays static

 box(50,50,50);

 //--------------- This rotates the scene
 
 camera(width/2.0 , height/2.0, distance *(height/2.0) / tan(PI*60.0 / 360.0), width/2.0,  height/2.0, -1 * (height/2.0) / tan(PI*60.0 / 360.0), 0, 1, 0);
 perspective(fov, float(width)/float(height), distance *((height/2.0) / tan(PI*60.0/360.0))/10.0, ((height/2.0) / tan(PI*60.0/360.0))*10.0);

 translate(width/2,height/2);

 //scale(2.5);

 alpha2_ = alpha2 + sin(frameCount/130.0)*0.00008;
 alpha1_ = alpha1 + sin(frameCount/123.0)*0.00008;
 rotateX(alpha2_);
 rotateZ(alpha1_);
 translate(translate1,translate2,translate3);

 //----------------------------------------------------- After this, the cube rotates with the mouse


 box(50,50,50);
 
}


void mousePressed ()
{
 mouse_x = mouseX;
 mouse_y = mouseY;  
}

void mouseDragged()
{
 if (mouseButton == LEFT)
 {
   alpha1 +=(mouse_x - mouseX)/100.0;    
   alpha2 +=(mouse_y - mouseY)/100.0;    
 }

 if (mouseButton == RIGHT)
 {
   float msx = (mouse_x - mouseX);
   float msy = (mouse_y - mouseY)*cos(alpha2);
   float msz = (mouse_y - mouseY);
   translate1 -=(  cos(alpha1)*msx + sin(alpha1)*msy)/2.50;    
   translate2 -=( -sin(alpha1)*msx + cos(alpha1)*msy)/2.50;
   translate3 +=( sin(alpha2)*msz)/2.50;
 }

 if (mouseButton == CENTER)
 {
   fov = fov * (mouseY - mouse_y + width) / width ;    
 };

 mouse_x=mouseX;
 mouse_y=mouseY;

}
Re: Perspective, FOV and OPENGL
Reply #1 - Mar 11th, 2009, 10:54pm
 
are you just wanting to see both views of the cube at the same time? the opengl version does seem to work for one frame, it flashes up momentarily.

try moving the background to the start of the draw and then change the order of the two bits so that you draw the moving scene first. doing this i get the same scene visible in both P3D and OPENGL

(actually, that's a lie - the rotating cube is a lot smaller in opengl)

oh, and translate(100, 100) before drawing the static cube or most of it will be off the top left of the screen
Re: Perspective, FOV and OPENGL
Reply #2 - Mar 12th, 2009, 5:40pm
 
Yes, but the FOV control (dragging the middle button) doesn't work then, that's why you get a smaller cube...

It looks like FOV only can change once per draw loop, or somthing similar. It's driving me nuts!
Re: Perspective, FOV and OPENGL
Reply #3 - Apr 22nd, 2010, 9:10pm
 
I'm running into a similar issue... did you ever find a solution?
Re: Perspective, FOV and OPENGL
Reply #4 - Apr 23rd, 2010, 12:30pm
 
I tried the original code that was pasted and the porblem appears to be the test for the CENTER button it doesn't seem to work. I changed it to this
Code:
  if (mouseButton == CENTER)
 {
   fov = fov * (mouseY - mouse_y + width) / width ;  
   println("FOV "+fov);  
 }

but when I tried it nothing was displayed. Change CENTER to LEFT and the FOV changes.

Putting this line in the mouse event handlers showed that the centre mouse button events were not detected Sad at least on my computer.

Code:
   println("Mouse " + mouseButton);

Page Index Toggle Pages: 1