Hey again everyone,

The past couple weeks I've been reading into matrices, but for the life of me I do not understand how you could use them efficiently for 3d programming. Been programming in java using netbeans and lib LWJGL. it has a matrix class in it.

If someone could guide me and give me some examples of how it is applied into a 3d environment.

Thanks in advance!

# Understanding how Matrix math is applied

Page 1 of 1## 11 Replies - 2822 Views - Last Post: 03 March 2012 - 10:23 AM

##
**Replies To:** Understanding how Matrix math is applied

### #2

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 01:19 PM

That is such a broad question with no quick answer. You need some kind of context in terms of implementation.

### #3

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 01:26 PM

Well that's more my issue. Take in a context of drawing a model with opengl (triangles)

Would you use glTranslatef(x, y, z); any more or would you base it off of matrix's. Im sure im over complicating this subject so.

Would I draw the trangles based off the matrix. I'm just not sure

(hehe i'm not even sure if I know what im talking about fully)

Would you use glTranslatef(x, y, z); any more or would you base it off of matrix's. Im sure im over complicating this subject so.

Would I draw the trangles based off the matrix. I'm just not sure

(hehe i'm not even sure if I know what im talking about fully)

### #4

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 02:04 PM

I should also mention that i do understand mathamatical equations between matrices. (add/sub/multiply). I do understand the concept of matrix math I just done know proper use of it for drawing a 3d environment

### #5

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 02:12 PM

Well the reality is, it is not as mutually exclusive as you think it is. Behind the scenes glTranslate *is* using matrices. So the question is more a matter of "should I roll my own translate method or using the GL version.

The answer is, if its a learning exercise, roll your own if you'd like. If its a real world project, by all means, don't re-invent the wheel. Chances are, you aren't going to out perform the OpenGL translate method, at least not by an amount that made the work investment worthwhile.

The answer is, if its a learning exercise, roll your own if you'd like. If its a real world project, by all means, don't re-invent the wheel. Chances are, you aren't going to out perform the OpenGL translate method, at least not by an amount that made the work investment worthwhile.

### #6

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 02:28 PM

Thanks for the reply Serapth.

Main reason I started looking into using a matrix is for 3d animation. Anything I was reading was hinting at the requirement of matrices.

I have created a model loader that loads in fine, and textures load in fine. The model draws amazingly. But when I went to start animation documentation was weak, at least for my understanding. But one thing everything I found about the subject was to use matrices. And I think this is where I'm getting lost. Cause the further I read into them (atleast the lib im using) it has options to rotate and translate.

But from what im gathering from you is continue using glTranslate and glRotate.

So with that said, how would one use (or would you have to) a Matrix for animation?

Main reason I started looking into using a matrix is for 3d animation. Anything I was reading was hinting at the requirement of matrices.

I have created a model loader that loads in fine, and textures load in fine. The model draws amazingly. But when I went to start animation documentation was weak, at least for my understanding. But one thing everything I found about the subject was to use matrices. And I think this is where I'm getting lost. Cause the further I read into them (atleast the lib im using) it has options to rotate and translate.

But from what im gathering from you is continue using glTranslate and glRotate.

So with that said, how would one use (or would you have to) a Matrix for animation?

### #7

## Re: Understanding how Matrix math is applied

Posted 01 March 2012 - 02:38 PM

blindstone, on 01 March 2012 - 09:28 PM, said:

Thanks for the reply Serapth.

Main reason I started looking into using a matrix is for 3d animation. Anything I was reading was hinting at the requirement of matrices.

I have created a model loader that loads in fine, and textures load in fine. The model draws amazingly. But when I went to start animation documentation was weak, at least for my understanding. But one thing everything I found about the subject was to use matrices. And I think this is where I'm getting lost. Cause the further I read into them (atleast the lib im using) it has options to rotate and translate.

But from what im gathering from you is continue using glTranslate and glRotate.

So with that said, how would one use (or would you have to) a Matrix for animation?

Main reason I started looking into using a matrix is for 3d animation. Anything I was reading was hinting at the requirement of matrices.

I have created a model loader that loads in fine, and textures load in fine. The model draws amazingly. But when I went to start animation documentation was weak, at least for my understanding. But one thing everything I found about the subject was to use matrices. And I think this is where I'm getting lost. Cause the further I read into them (atleast the lib im using) it has options to rotate and translate.

But from what im gathering from you is continue using glTranslate and glRotate.

So with that said, how would one use (or would you have to) a Matrix for animation?

Take a quick look at the documentation for glTranslate and you will see what I mean.

*glTranslate produces a translation by x y z . The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix, as if glMultMatrix were called with the following matrix for its argument:*

You are still using matrix math... its just behind the scenes. The x,y,z values you pass in are populating a matrix.

You are right though, you aren't going to get anywhere in 3D programming without a reasonably sound understanding of matrix math. The Kahn Academy is probably as good a place to start as anywhere. Their videos vary massively, but I have watched a few on very advanced subjects, that explained things extremely well.

Math is really not my strong suit anyways, and in refresher ( my last math class was University in the 90s... ), I discovered some Kahn instructors explained things better in 20 minutes, than my entire final year of high school advanced level courses, but truth is, I don't really think my teachers knew what they were teaching.

Another person who explained math ( and optimization ) EXTREMELY well, was Michael Abrash. He wrote a book called The Zen of Graphic Programming, which obsolete now, was absolutely amazing in what it taught. There are still few books like it, even though we have moved well past doing things like writing our own line drawing routines. There are a couple other books on math for game developers, that do the job to a varying degree.

### #8

## Re: Understanding how Matrix math is applied

Posted 02 March 2012 - 08:08 AM

after thinking about what you said overnight I think I have an understanding on what i need to do to get these animations working properly. Thanks man!

### #9

## Re: Understanding how Matrix math is applied

Posted 02 March 2012 - 08:18 AM

#include<xnamath.h> //Here is a directx 11 example in C++ this should be somewhat alike XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw (0.0f, 0.0f, 0.0f); XMMATRIX translationMat = XMMatrixTranslation (x, y, 0.0f ); XMMATRIX worldMat = rotationMat * translationMat; //XMMATRIX worldMat = XMMatrixIdentity( ); worldMat = XMMatrixTranspose( worldMat );

This post has been edited by **Toadill**: 02 March 2012 - 08:19 AM

### #10

## Re: Understanding how Matrix math is applied

Posted 02 March 2012 - 11:05 PM

To understand why matrices are helpful, take this example: you have ten objects, each of them you want to (1) scale it by a factor of 5, and (2) rotate it. You can go ahead and do that to each object, or you can make two matrices (one for scaling and one for rotating), multiply them together, and you end up with one matrix that can do what you want it to in one shot. Performance improved.

Although its confusing at first why you need them, once you get to know what's happening and how it works they are really sweet.

Although its confusing at first why you need them, once you get to know what's happening and how it works they are really sweet.

### #11

## Re: Understanding how Matrix math is applied

Posted 03 March 2012 - 09:19 AM

MasterOfBinary, on 03 March 2012 - 06:05 AM, said:

To understand why matrices are helpful, take this example: you have ten objects, each of them you want to (1) scale it by a factor of 5, and (2) rotate it. You can go ahead and do that to each object, or you can make two matrices (one for scaling and one for rotating), multiply them together, and you end up with one matrix that can do what you want it to in one shot. Performance improved.

Although its confusing at first why you need them, once you get to know what's happening and how it works they are really sweet.

Although its confusing at first why you need them, once you get to know what's happening and how it works they are really sweet.

This is indeed pretty much the essence on why 3D graphics use matrix transformations. There are cheaper ways to translate(vector addition)/rotate(you can rotate using a dot product)/scale(multiply vector by a scalar) the points in a model, But you can do all 3 at once by multiplying by a matrix. And in homogeneous coordinates the translate/rotate/scale matrix operations are associative. i.e.: A * (B *

**v**) = (A * B) *

**v**. with A, B matrix operations and

**v**a point. This means that we can multiply (chain) the operations for all points that must undergo a similar transformation (for example points from the same model, part of model) and then multiply all those point once by a single matrix.

This post has been edited by **Karel-Lodewijk**: 03 March 2012 - 09:21 AM

### #12

## Re: Understanding how Matrix math is applied

Posted 03 March 2012 - 10:23 AM

Thank you everyone for your helpful replies. Hopefully you will bare with me as i figure this out

Just wondering what i am doing incorrectly here.

At the moment I am rendering two positions (based on time), im not worried about the animation in between. As is i just want it to render starting at the basic position and the next screen is the point where the animation to finish.

I have a few pictures to give you an idea of whats going on

Screens:

Start position

What its doing (And should not be)

What it should look like

And the code purhapes someone can see something I do not (This is the render method)

I look forward to hearing from someone And thanks again for all your help!

Just wondering what i am doing incorrectly here.

At the moment I am rendering two positions (based on time), im not worried about the animation in between. As is i just want it to render starting at the basic position and the next screen is the point where the animation to finish.

I have a few pictures to give you an idea of whats going on

Screens:

Start position

What its doing (And should not be)

What it should look like

And the code purhapes someone can see something I do not (This is the render method)

ArrayList<Joint> joints = skeleton.Joints(); ArrayList<SkelAnim> skelanim = Test3d.displayExample.skelanim; int ji = 0; int fr = 0; if (_time.getTime() > 1.0f) fr = 1; if (_time.getTime() > 2.0f) { fr = 0; _time.reset(); } for (Joint j : joints) { for (SkelAnim sa : skelanim) { ArrayList<Frame> jp = sa.JointPos(); ArrayList<Frame> jr = sa.JointRot(); if(j.Parent() == null) { _jpmatrix.m03 = j.POS().x; _jpmatrix.m13 = j.POS().y; _jpmatrix.m23 = j.POS().z; } else { _jmatrix.m03 = j.POS().x; _jmatrix.m13 = j.POS().y; _jmatrix.m23 = j.POS().z; _jmatrix.mul(j._matrix(), _jpmatrix, _jmatrix); } _jmatrix.invert(); try{ _jmatrix.rotate(jr.get(fr).X(), new Vector3f(1.0f, 0.0f, 0.0f)); _jmatrix.rotate(jr.get(fr).Y(), new Vector3f(0.0f, 1.0f, 0.0f)); _jmatrix.rotate(jr.get(fr).Z(), new Vector3f(0.0f, 0.0f, 1.0f)); _jmatrix.invert(); }catch(Exception e){} for (Mesh m : mesh) { ArrayList<Face> face = m.Faces(); ArrayList<Vert> vert = m.Verts(); Texture texture = mats.get(m.MatIndex()).getTexture(); for (Face f : face) { GL11.glPushMatrix(); GL11.glTranslatef(pos.x , pos.y, pos.z); texture.bind(); GL11.glBegin(GL11.GL_TRIANGLES); if (sa.JointName().equals(j.Name()) && vert.get(f.pntOneVert()).joint() == ji) { Frame tempfr = null; try{ tempfr = jr.get(fr);} catch(Exception e){} if (tempfr != null) { _matrix.setZero(); _matrix.m03 = vert.get(f.pntOneVert()).X(); _matrix.m13 = vert.get(f.pntOneVert()).Y(); _matrix.m23 = vert.get(f.pntOneVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntOneVert()).uv0(), vert.get(f.pntOneVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); _matrix.setZero(); _matrix.m03 = vert.get(f.pntTwoVert()).X(); _matrix.m13 = vert.get(f.pntTwoVert()).Y(); _matrix.m23 = vert.get(f.pntTwoVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntTwoVert()).uv0(), vert.get(f.pntTwoVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); _matrix.setZero(); _matrix.m03 = vert.get(f.pntThreeVert()).X(); _matrix.m13 = vert.get(f.pntThreeVert()).Y(); _matrix.m23 = vert.get(f.pntThreeVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntThreeVert()).uv0(), vert.get(f.pntThreeVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); } else { _matrix.setZero(); _matrix.m03 = vert.get(f.pntOneVert()).X(); _matrix.m13 = vert.get(f.pntOneVert()).Y(); _matrix.m23 = vert.get(f.pntOneVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntOneVert()).uv0(), vert.get(f.pntOneVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); _matrix.setZero(); _matrix.m03 = vert.get(f.pntTwoVert()).X(); _matrix.m13 = vert.get(f.pntTwoVert()).Y(); _matrix.m23 = vert.get(f.pntTwoVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntTwoVert()).uv0(), vert.get(f.pntTwoVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); _matrix.setZero(); _matrix.m03 = vert.get(f.pntThreeVert()).X(); _matrix.m13 = vert.get(f.pntThreeVert()).Y(); _matrix.m23 = vert.get(f.pntThreeVert()).Z(); _matrix.mul(_matrix, _jmatrix, _matrix); GL11.glTexCoord2f(vert.get(f.pntThreeVert()).uv0(), vert.get(f.pntThreeVert()).uv1()); GL11.glVertex3f(_matrix.m03, _matrix.m13, _matrix.m23); } } GL11.glEnd(); GL11.glPopMatrix(); } } } _jmatrix = new Matrix4f(); ji++; }

I look forward to hearing from someone And thanks again for all your help!

Page 1 of 1