Author Topic: Having a hard time understanding uv coordinates in matrix  (Read 2774 times)

Offline SDD1965

  • byte
  • *
  • Posts: 5
    • View Profile
Having a hard time understanding uv coordinates in matrix
« on: August 31, 2015, 06:04:05 pm »
I've been looking like crazy online as well as on this site and I can't find any instruction on what the order of the float[16] in the matrix represent. I know the site tells me somewhere that each pair is the 1st, 2nd, etc., vertex but what I am looking for is a tutorial or example that tells me what happens as I change certain points.

Basically, by trial and error I have managed to come up with a squishy effect on a plane which is exactly what I was after but I really would like to understand what all the points represent and the effect they have on a plane.

I know that rotate, scale, and translate simplifies much of the most common effects but for certain actions it is better to have access to the texture matrix for additional effects and fine tuning.

For instance, at the moment what I am trying to do is create a swaying effect on a plane. Like a card rotating one direction and then the other on the y-axis. Even though I can do this with rotateY() I am already rotating the same object plane as part of a different effect, so I want to rotate the texture itself as a second independent rotation of the same object. That way I can use either one without resetting the other.

If there is an easier way to rotate the texture itself please let me know. But I would really love to find a tutorial on the float[16] of the matrix if it can be found anywhere.

Thank you in advance.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Having a hard time understanding uv coordinates in matrix
« Reply #1 on: August 31, 2015, 07:29:05 pm »
The float[16] array is just a quick way to get and set dumps from the matrix. There's no real point in fiddling around with it directly. What you seem to be looking for is a kind of explanation what each element of a matrix means, i.e. what each letter means in a matrix like

Code: [Select]
a b c d
e f g h
i j k l
m n o p

? The purpose of each element is defined by the way in which the multiplication matrix<->vector is defined. If you want to understand more about matrices, try to learn more about matrix*matrix and matrix*vector multiplications. A matrix is just a unified method for manipulating other matrices and vectors. You actually don't need a matrix for any of these, you could do it all by using "normal" math as well.

To rotate a texture, just use this: http://www.jpct.net/doc/com/threed/jpct/Object3D.html#setTextureMatrix(com.threed.jpct.Matrix)

Offline SDD1965

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Having a hard time understanding uv coordinates in matrix
« Reply #2 on: August 31, 2015, 09:42:24 pm »
Thanks Egon for the speedy reply.
Actually, when I mentioned the float[16] in the Matrix class I was asking because I was already using it to manipulate the texture using setTextureMatrix. After your reply I see that I must be using setTextureMatrix incorrectly and that dumping, changing, and setting the float[16] is not the way to go about it. I will try using setTextureMatrix with a matrix that was altered using other methods in the Matrix class. Thanks again.

Offline SDD1965

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Having a hard time understanding uv coordinates in matrix
« Reply #3 on: September 01, 2015, 12:09:47 am »
I need a little more help if you could.

I'm trying to rotate a texture, but the only change I get is a texture of double the width with no rotation at all. I am using GL2 if that matters.

Matrix matrix = new Matrix();
matrix.rotateY(5);
object3D.setTextureMatrix(matrix);

As mentioned earlier, I am just trying to figure out how to rotate a texture displayed inside a object3D.plane (picture a card rotating) but I do not want to rotate the object3D. Just the texture within the object3D. Can you tell me what  I'm missing in the code above? Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Having a hard time understanding uv coordinates in matrix
« Reply #4 on: September 01, 2015, 09:11:03 am »
Rotations on texture coordinates work in exactly the same way as rotations on objects do, except that it's in 2D and not 3D. If you want to rotate the texture's uv-coordinates to make the texture rotate, rotate the matrix around Z, not X or Y.

In addition, you might have to apply some translations to shift the rotation pivot into the center. Here's an example for desktop jPCT:

Code: [Select]
import java.awt.Color;

import org.lwjgl.opengl.Display;

import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;


public class Rotate
{

  public static void main(String[] args)
    throws Exception
  {
    FrameBuffer fb = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY);
    fb.disableRenderer(IRenderer.RENDERER_SOFTWARE);
    fb.enableRenderer(IRenderer.RENDERER_OPENGL);

    World world = new World();
    world.setAmbientLight(255, 255, 255);
    Object3D plane = Primitives.getPlane(2, 5);
    plane.compile();
    plane.build();

    Texture t = new Texture("logo.jpg");
    TextureManager.getInstance().addTexture("texture", t);
    plane.setTexture("texture");
    plane.translate(0, 0, 20);

    world.addObject(plane);

    float rot = 0f;

    while (!Display.isCloseRequested())
    {
      fb.clear(Color.BLUE);
      world.renderScene(fb);
      world.draw(fb);
      fb.update();
      fb.displayGLOnly();

      Matrix m = new Matrix();
      m.translate(-0.5f, -0.5f, -0.5f);
      m.rotateZ(rot);
      m.translate(0.5f, 0.5f, 0.5f);
      plane.setTextureMatrix(m);

      rot += 0.01f;

      Thread.sleep(10);
    }
  }
}


However, I'm not sure is this effect is what you really want to rotate some kind of card... ???

Offline SDD1965

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Having a hard time understanding uv coordinates in matrix
« Reply #5 on: September 01, 2015, 12:30:06 pm »
Ah, darn. Well that explains why  no amount of tweaking would rotate the Y-axis. Lol
Luckily only one of my effects needs to rotate on the y-axis so I'll just swap the effects out.
Thank you again for the prompt response and for the great site.  :D