Author Topic: Non-uniform scaling  (Read 729 times)

Offline ned flanders

  • byte
  • *
  • Posts: 6
    • View Profile
Non-uniform scaling
« on: July 19, 2013, 07:09:08 pm »
Hi, noob question, wondering if there's an easy way to do non-uniform scaling, ie, where x,y,z, scaling factors are different.

My immediate need is simply to create a Primitive.Plane, which is of unit size, then scale as needed in the x,y directions.  For example, maybe I'd like a plane twice as wide as high.

Wait...just occurred to me...should I use the rotation matrix, and pass in values on the diagonal?

Offline ned flanders

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Non-uniform scaling
« Reply #1 on: July 19, 2013, 08:25:07 pm »
Well, I got it to work with the following code, in which I need not only non-uniform x-y scaling, but rotation baked in as well:

Code: [Select]
plane = Primitives.getPlane(1,1);

 Matrix scalingMat = new Matrix();
 scalingMat.set(0, 0, width);
 scalingMat.set(1, 1, height);

 Matrix rotMat = plane.getRotationMatrix();
 rotMat.rotateX((float)(Math.PI/2f));

 scalingMat.matMul(rotMat);

 plane.setRotationMatrix(scalingMat);

Let me know if there's an easier/better way for this.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Non-uniform scaling
« Reply #2 on: July 19, 2013, 09:14:47 pm »
That's ok, but it might screw up your normals so that lighting can become strange. You might want to add

Code: [Select]
plane.rotateMesh();
plane.clearRotation();

at the end to make the change permanent to the mesh.

Offline ned flanders

  • byte
  • *
  • Posts: 6
    • View Profile
Re: Non-uniform scaling
« Reply #3 on: July 19, 2013, 09:51:50 pm »
Thanks, makes perfect sense, I forgot about the normals.