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 & HelpSyntax Questions › Hard time understanding 3D example
Page Index Toggle Pages: 1
Hard time understanding 3D example (Read 881 times)
Hard time understanding 3D example
Sep 16th, 2009, 12:02pm
 
Hello,

I'm working on the example in pages 639-640 of "Processing: Creative Coding and Computational Art" (the Ira Greenberg book) and I'm having a tough time with the following code:

Quote:
float[][] originalVertices = new float[4][3];
float[][] transformedVertices = new float[4][3];
float angleX;
float angleY;
float angleZ;

void setup() {
  size(400, 400, P3D);
  float angle = 45;
  for (int i = 0; i < 4; i++){
    originalVertices[i][0] = cos(radians(angle))*50;
    originalVertices[i][1] = sin(radians(angle))*50;
    originalVertices[i][2] = 0;
    angle += 90.0;
  }
}

void draw() {
  background(100);
  myRotateX(2);
  myRotateY(6);
  myRotateZ(3);
  createRect();
}


void myRotateX(float deg) {
  angleX = angleX + deg;
}

void myRotateY(float deg) {
  angleY = angleY + deg;
}

void myRotateZ(float deg) {
  angleZ = angleZ + deg;
}


void createRect() {
  translate(width/2, height/2, 200);
  transformedVertices = rotateVertices();
  beginShape();
  for (int i = 0; i < 4; i++) {
    vertex(transformedVertices[i][0], transformedVertices[i][1], transformedVertices[i][2]);
  }
  endShape(CLOSE);
}


float[][] rotateVertices(){

  float[][] rotatedVertices_XAxis = new float[4][3];
  float[][] rotatedVertices_YAxis = new float[4][3];
  float[][] rotatedVertices_ZAxis = new float[4][3];

  for (int i = 0; i < 4; i++) {
    rotatedVertices_XAxis[i][0] = originalVertices[i][0]; 
    rotatedVertices_XAxis[i][1] = cos(radians(angleX))* originalVertices[i][1] - sin(radians(angleX)) * originalVertices[i][2];
    rotatedVertices_XAxis[i][2] = sin(radians(angleX))* originalVertices[i][1] + cos(radians(angleX)) * originalVertices[i][2];

    rotatedVertices_YAxis[i][1] = rotatedVertices_XAxis[i][1];
    rotatedVertices_YAxis[i][2] = cos(radians(angleY))* rotatedVertices_XAxis[i][2] - sin(radians(angleY)) * rotatedVertices_XAxis[i][0];
    rotatedVertices_YAxis[i][0] = sin(radians(angleY))* rotatedVertices_XAxis[i][2] + cos(radians(angleY)) * rotatedVertices_XAxis[i][0];

    rotatedVertices_ZAxis[i][0] = cos(radians(angleZ))* rotatedVertices_YAxis[i][0] - sin(radians(angleZ)) * rotatedVertices_YAxis[i][1];
    rotatedVertices_ZAxis[i][1] = sin(radians(angleZ))* rotatedVertices_YAxis[i][0] + cos(radians(angleZ)) * rotatedVertices_YAxis[i][1];
    rotatedVertices_ZAxis[i][2] = rotatedVertices_YAxis[i][2];
  }

  return rotatedVertices_ZAxis;

}



I can't quite picture what's going on in the rotateVertices() function, and the only explanation provided by the book is "...the rotateVertices() function should be straightforward." Anyone know what's going on?
Re: Hard time understanding 3D example
Reply #1 - Sep 16th, 2009, 12:32pm
 
Straight Forward? It might be a joke... Or simply referring to something that is commonly used in Computer Graphics. Maybe a place to start might be here:
http://en.wikipedia.org/wiki/Matrix_(mathematics)#Linear_equations

I think to really get this, you need to understand very well matrices manipulations first. In fact, float[4][3] is your matrix and where you have:
Code:
rotatedVertices_XAxis[i][0] = originalVertices[i][0]; 

this looks like a simplification of something more general where it is assumed that terms are null. You might have a look at the identity matrix. Myself, I find this form hard to understand, not intuitive.

Most people are using it as a black box.
Re: Hard time understanding 3D example
Reply #2 - Sep 16th, 2009, 1:41pm
 
there are three chunks in that rotateVertices call. here's the first bit:

rotatedVertices_XAxis[i][0] = originalVertices[i][0];
   rotatedVertices_XAxis[i][1] = cos(radians(angleX))* originalVertices[i][1] - sin(radians(angleX)) * originalVertices[i][2];
   rotatedVertices_XAxis[i][2] = sin(radians(angleX))* originalVertices[i][1] + cos(radians(angleX)) * originalVertices[i][2];

it's a matrix for rotating around the x axis. http://en.wikipedia.org/wiki/Rotation_matrix (the bit starting "Dimension Three").

it then goes on to multiply the result by a matrix representing a rotation around Y axis and the result of that by a matrix representing a rotation around Z axis. so you end up with something that rotates in space which is what you see when you run the code.

it's what rotateX(), rotateY() and rotateZ() is doing under the covers.

(the 4x3 doesn't relate to a transformation matrix, it's just 4 points each with an x, y and z)
Re: Hard time understanding 3D example
Reply #3 - Sep 16th, 2009, 2:22pm
 
You are right K, I'm used to think about the matrices each time I read float[4][4] or float[4][3]. And here, it implicitly use a simplified form that you find in general matrices. I'm confused, since I'm reading a lot of papers on rotations those days, everything to avoid|reduce cos-sin uses.
Re: Hard time understanding 3D example
Reply #4 - Sep 18th, 2009, 2:45pm
 
Thanks! It's so depressing when I realize just how much math I've forgotten.
Page Index Toggle Pages: 1