pablo_miranda
YaBB Newbies
Offline
Posts: 2
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; }