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
draw() resets Matrix? (Read 1938 times)
draw() resets Matrix?
May 9th, 2005, 8:26pm
 
Hi everyone,
This is the first time I post and I would like to start congratulating everyone behind Processing, it is a beautifully designed piece of software. I am using it from JBuilder to be able to access all the nicely crafted graphic functions, it is great fun...

My question:
I am trying to do something I have done before in Java with my own transformation matrices and also in C/C++ with OpenGL, that is, to rotate a model always relative to axes parallel to the screen, a sort of 'rolling' effect of 3D forms. I usually do it by not resetting the transformation Matrix and have it accumulate instead all the successive rotations, pushing the matrix, translating, drawing, and popping the matrix again at the end. It seems though that the draw() method resets the matrix every time, so I am doing a thing that does not look very nice, as it is to access the modelview Pmatrix in PGraphics3, copy it every time before exiting draw(), and restoring it in every new call to draw()...I may be doing something very stupid and there is a way to avoid all this?

By the way, I am sorry if perhaps I should have posted this in the integration topic, but I thought the answer may be useful to people also using the proper Processing editor.

Pablo.



Re: draw() resets Matrix?
Reply #1 - May 9th, 2005, 10:22pm
 
you should use beginCamera() and endCamera() around your matrix operations, which will let you futz with the camera matrix, which is what the model matrix is set to each time draw begins:
http://processing.org/reference/beginCamera_.html
Re: draw() resets Matrix?
Reply #2 - May 11th, 2005, 11:34am
 
Thanks,
I have followed your advice and used beginCamera(), endCamera() and I have the 'rolling' effect working.

I post the code here (translated from Java to Processing) just in case anyone wants to implement the effect too. It is a bit complicated, but in the other hand I see the reason for resetting the model matrix to the camera matrix, in general it makes much easier to understand what one is doing.
If you find something wrong, very backwards or a better solution, please let me know.

Thanks again,

Pablo

Here is the code:

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

float prevx, prevy = 0;
 float me_distance=20;

void setup()
{
   size(200, 200, P3D);
  noStroke();
  colorMode(RGB, 1);
  //we reset the camera here and the perspective as we want
  beginCamera();
  resetMatrix();
  endCamera();
  perspective(PI/3, float(width)/float(height), 0.1, 1000);
 
}

void draw()
{
 //colours and lights
 background(0.5f, 0.5f, 0.4f);
 directionalLight(1, 1, 1,20, 10, 20);
 lights();
 
 //changes to Camera matrix, we move away from the origin through translation
 beginCamera();    
 translate(0, 0, me_distance);
 endCamera();
 
 //we do the drawing here, all transformation will affect the model matrix, which
 //will anyway be reset to the camera matrix in every call to draw()
 scale(10);
 fill(0.9,0.2,0.2);
 box(1);

//here we undo the translation, (pop and push don't affect the camera matrix, so we do it 'manually' instead)
//this way, the next rotations we perform will have effect around the origin (not around our current position)
 beginCamera();
 translate(0, 0, -me_distance);
 endCamera();
}

void mouseDragged()
{
 float xtheta = -(prevy - mouseY) * (PI / width);
 float ytheta =  (prevx -  mouseX) * (PI / height);
 
 beginCamera();
 rotateY(ytheta);
 rotateX(xtheta);
 endCamera();

  prevx = mouseX;
  prevy = mouseY;
}

//to avoid jumps of rotation when mouse is pressed
void mousePressed()
{
prevx = mouseX;
prevy = mouseY;
}
Page Index Toggle Pages: 1