CXO2's picture

GL.Translate() GL.Rotate() GL.Scale() vs GL.LoadMatrix() Which one better?

Okay
Let's straight to the point, Recently i was wondering to decide whether my Sprite class will use matrix4 and GL.LoadMatrix() or just simply use GL.Translate(), GL.Rotate(), GL.Scale()

I tested both, but i cant see any different at all..

So the question is.. which one is better (pros n cons)?

Something like this:

                GL.PushMatrix();
 
                GL.Scale(new Vector3(Scale.X, Scale.Y, 0.0f));
                GL.Translate(new Vector3((Width * Origin.X), (Height * Origin.Y), 0f));
                GL.Rotate(Rotation, new Vector3(0f, 0f, 1f));
 
                // Drawing Code Here
 
                GL.PopMatrix();
                GL.LoadIdentity();

or this..

                GL.PushMatrix();
 
                Matrix4 Matrix, translation, rotation, scale;
 
                translation = Matrix4.CreateTranslation(new Vector3((Width * Origin.X), (Height * Origin.Y), 0f));
                rotation = Matrix4.CreateFromAxisAngle(new Vector3(0f, 0f, 1f), Rotation);
                scale = Matrix4.CreateScale(new Vector3(Scale.X, Scale.Y, 0.0f));
 
                Matrix = scale * rotation * rotation;
 
                GL.MatrixMode(MatrixMode.ModelView);
                GL.LoadIdentity();
 
                GL.LoadMatrix(ref Matrix);
 
                // Drawing Code Here
 
                GL.PopMatrix();
                GL.LoadIdentity();

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

If you are using OpenGL 1.x/2.x, there is no practical difference between the two.

The matrix stack (GL.Rotate etc) is no longer available on modern OpenGL versions (including OpenGL ES and OpenGL 3.x/4.x). Using Matrix4.Create* will make your code easier to port there.

CXO2's picture

I see (I even just know its called Matrix Stack lol)
oh anyway, is there any different on performance?

the Fiddler's picture

Possibly, but I have not benchmarked this.

CXO2's picture

Thanks for the info Fiddler!
Probably I will measure the performance difference later.

Anyway i find GL.PushMatrix(), GL.PopMatrix(), GL.MatrixMode(), and GL.Identity() are deprecated, is there any workaround about this?
AFAIK the usage of GL.LoadMatrix() is:

                GL.PushMatrix();
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
 
                GL.LoadMatrix(ref Matrix);
                GL.PopMatrix();

We can't use GL.LoadMatrix() before Specifying which the current matrix (with GL.MatrixMode()) right?
(Sorry for too much questions here)

the Fiddler's picture
Quote:

Anyway i find GL.PushMatrix(), GL.PopMatrix(), GL.MatrixMode(), and GL.Identity() are deprecated, is there any workaround about this?

Yes: use OpenTK.Matrix4 to setup matrices and use GLSL shaders to render your graphics. If you are keen on learning modern 3d graphics, you should move to shaders as soon as possible. It takes a bit more work to get started with shaders, but they are much *much* better in the long run.

OpenTK examples might prove useful here.

If you have a modern Ati or Nvidia graphics card, I would also suggest using OpenTK.Graphics.ES30 instead of OpenTK.Graphics.OpenGL. OpenGL ES is a smaller and cleaner subset of OpenGL that is a much better fit to modern GPUs. This way, you won't waste time learning arcane 20-year old OpenGL commands and, as a bonus, you will be able to port your code on Android and iOS very easily.

This is up to you though. I'm sure many people will disagree, but I do feel that discussing the finer points of the 1995 matrix stack is not useful in 2014.

Quote:

We can't use GL.LoadMatrix() before Specifying which the current matrix (with GL.MatrixMode()) right?

The matrix stack is stateful. You set the "current" matrix with GL.MatrixMode and then use the rest of the matrix commands to modify the current matrix.

Your example should be like this instead:

GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadMatrix(ref matrix);
// render
GL.PopMatrix();
CXO2's picture

I see, so the answer is Shader
Well I just have interest on 2D, but i have class for handling Shader (fragment shader and vertex shader) and it works perfectly now (I guess I will share it later haha)

Thanks for so meaningful Information Fiddler!

Oh Anyway

the Fiddler wrote:
Quote:

We can't use GL.LoadMatrix() before Specifying which the current matrix (with GL.MatrixMode()) right?

The matrix stack is stateful. You set the "current" matrix with GL.MatrixMode and then use the rest of the matrix commands to modify the current matrix.

Your example should be like this instead:

GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadMatrix(ref matrix);
// render
GL.PopMatrix();

Sorry for the wrong example code.
That what i was meant haha