LWJGL Forum

Programming => OpenGL => Topic started by: mrgibson on June 23, 2013, 05:43:46

Title: [SOLVED] JAVA AffineTransform and OPENGL
Post by: mrgibson on June 23, 2013, 05:43:46
I do some translation and zooming in a JAVA affine transform, what is the best way to use it in OPENGL (with LWJGL) ?
(I also use shaders)


Thank you,
mrgibson

Title: Re: JAVA AffineTransform and OPENGL
Post by: quew8 on June 23, 2013, 09:12:32
You would have to extract the matrix from the affine transform and set it to your OpenGL shader. Unfortunately, as far as I know, you cannot get the matrix from the affine transform so...

Fortunately for you, translation and scaling matrices are very simple.
Translation : 1 0 0 tx Where the translation vector is (tx, ty, tz)
                  0 1 0 ty
                  0 0 1 tz
                  0 0 0 1

Scaling : sx  0   0   0 Where sx, sy, sz are the respective x, y and z scaling coefficients.
             0   sy  0   0
             0   0    sz  0
             0   0    0    1

However a sound knowledge of transformation matrices is essential for advanced OpenGL so I would advice a bit of research. I found http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/ (http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/) to be an excellent starter.
Title: Re: JAVA AffineTransform and OPENGL
Post by: mrgibson on June 25, 2013, 20:46:41
Nice link.

I think I could extract the matrix using Affinetransform.getMatrix() function.

I guess I could send it to my shader then.
Title: Re: JAVA AffineTransform and OPENGL
Post by: quew8 on June 26, 2013, 17:40:00
Sure you could. I must have missed that function (I did look through the javadoc). One thing to be careful of - make sure you get the column major / row major order the right way round.
Title: Re: JAVA AffineTransform and OPENGL
Post by: mrgibson on June 29, 2013, 04:24:06
This is working nicely, I ported all my code from Graphics2D to OpenGL.
I also discovered glScissor(), really simple to use for clipping regions.

Here a small video of the result:

http://www.youtube.com/watch?v=ib_3F06mwIY&feature=youtu.be
Title: Re: [SOLVED] JAVA AffineTransform and OPENGL
Post by: quew8 on June 29, 2013, 10:27:57
That's great work for an OpenGL beginner. You would be shocked how many people have problems with this stuff.