/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

Jump to content

Image of the Day

Today's #ScreenshotSaturday features Face Your Fears from Turtle Roc ... #virtualrealityshop #mobilevr #vrshop https://t.co/3qhSNAVNOZ
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.

Sign up now

Matrices general questions

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Danicco   Members   


Posted 21 March 2013 - 11:42 AM

I've a very old project in openGL that used glRotateglTranslate, and other glMatrix functions, and I'm updating it to the more modern approach of using matrices for calculations.


But I'm having some problems because I have very little to no experience with matrices (I always avoided it in my older project, I preferred using basic trigonometry because I found it to be easier), and I'd like to ask these questions:


I'm calculating, on each frame, this matrix:

mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
    glm::vec3(_camera.posX, _camera.posY, _camera.posZ), //Camera Position		
    glm::vec3(_camera.posX + _camera.directionX, _camera.posY - _camera.directionY, _camera.posZ - _camera.directionZ), //Eye Position
    glm::vec3(0, 1, 0) //Head Up
mat4 mModel = glm::mat4(1.0f);
mat4 mMVP = mProjection * mView * mModel;	

I'd like to change my object's position, so I tried this:

mat4 mModel = glm::mat4(1, 0, 0, object->posX, 
                        0, 1, 0, object->posY, 
		        0, 0, 1, object->posZ,
		        0, 0, 0, 1);

Because I read that the last column's represents the x, y, z translating positions of the plane.

But I guess I'm mistaken, because with this my object seems "stuck" and doesn't move at all (and my navigation stops).


I thought of multiplying it by a vec3(posX, posY, posZ), but then wouldn't the result be a another vec3? I read that when multiplying matrices such as 4x4 and a 4x2, the result is a 4x2, so I don't know how I can calculate this. I guess rotations will be even more complicated...


Another question I have is, I had a targetX, targetY, targetZ for my camera object, and I did this:

glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //go to the target
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //rotateX
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f); //rotateY
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //to go camera position

//Loop to draw all objects, assuming "camera position" as the new identity

And this way I could control where I was looking to, and change the zoom or switch to first person by making the target = position.

With the matrices' calculations I posted above, I don't know how I can achieve this, since the camera's position I send to the mView should change according to where I'm looking, and the center target as well right?

Any help on how I can achieve this?


Any info on how can I get to understand matrices and it's relations to the world space is really appreciated, I'm reading some tutorials about it but it still feels very overwhelming, too much information that I can't relate to the world's coordinates.



Edited by Danicco, 21 March 2013 - 11:44 AM.

#2 Danicco   Members   


Posted 21 March 2013 - 12:19 PM

I managed to get translation with a glm function, glm::translate(modelMatrix, positionVector), although I'm clueless what it is doing...


I got another question too, I added rotation with:

mat4 mModel = glm::mat4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);	
mat4 mModelRotateX = rotate(mModel, object->RotationX(), vec3(1.0, 0.0, 0.0));
mat4 mModelRotateY = rotate(mModel, object->RotationY(), vec3(0.0, 1.0, 0.0));
mat4 mModelRotateZ = rotate(mModel, object->RotationZ(), vec3(0.0, 0.0, 1.0));
mat4 mModelTranslate = translate(mModel, vec3(object->posX, object->posY, object->posZ));
mat4 mMVP = mProjection * mView * mModelTranslate * mModelRotateZ * mModelRotateY * mModelRotateX;

I think I could reduce the rotation matrices down to 1, but I don't know how, tips?


And for some reason, my object isn't rotating around it's center anymore (from my previous immediate drawing example), it's rotating around one of it's edges it seems. Why?


How can I control the "center" of the object like this, where I begin drawing? The vertexShader code is this:

#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;
void main()
	gl_Position = MVP * vec4(vertexPosition_modelspace, 1);

With MVP being the same as the mMVP in the main drawing loop.

I just noticed that ever since I switched from immediate drawing to VBOs, I'm not using any of the "object positions" values, so I don't know where it is assuming the object's center...

#3 incertia   Members   


Posted 21 March 2013 - 12:33 PM

Okay, so matrices. If you really want to know the deep and dark secrets that they contain, take a course in linear algebra and you will probably know more than you ever need to know about them. Anyways, I'll just be giving basic examples so that you can more easily understand what you read on the internet.

Okay, so a matrix looks something like

or [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]] because I can't use two sets of eqn tags for some reason.

Notice how it has 3 rows and 3 columns, so we say it is a 3x3 matrix. Thus, a 2x3 matrix would have 2 rows and 3 columns. This can be represented in code as a 2 dimensional array

We can also add two matrices by adding the elements in row i and column j of matrix 1 to row i and column j of matrix two. In essence, a[i][j] + b[i][j]. For example, [[1, 2, 3], [4, 5, 6]] + [[2, 3, 4], [3, 4, 5]] = [[3, 5, 7], [7, 9, 11]]. Adding is then fundamental to translations. If we want to translate a point 3 units in the x direction, we can just compute [[x], [y]] + [[3], [0]]. We can also scale matrices by a number, by scaling every element by that same number. This is useful for dilating an object with respect to its local origin.

Multiplying is a bit more complicated, but it can be done. If you think of row i of matrix 1 as some vector v1, and you think of column j of matrix 2 as some vector v2, then the entry in row i and column j of the resultant matrix is v1.v2 (add the products of the first elements, second elements, and so on). So a simple example would be [[1, 2]] * [[2], [1]] = [[4]]. We then arrive at an important conclusion about matrix multiplication. It is that the number of rows in the first matrix must be equivalent to the number of columns in the second matrix. Then, it is not hard to see that A * B is not necessarily the same as B * A, and both may not necessarily be defined for matrices A and B. Multiplying becomes the core of rotation, and transforming one system of coordinates to another (i.e. world space to screen space).

Due to 3D matrices being hard to do, I will simplify a general rotation example into 2D. If you actually get out some pencil and paper, and try to rotate a point (x, y) about the origin some angle t degrees about the origin, you can figure out by simple application of similar triangles that x' = x cos t + y sin t and y' = -x sin t + y cos t. Applying our knowledge of matrix multiplication, it is easily verifiable that [[cos t, -sin t], [sin t, cos t]] * [[x], [y]] = [[x'], [y']] is a valid way to compute the rotation of a point without hard coding a bunch of equations that are hard to maintain.

Hopefully this is decent enough as a brief introduction to matrices.

#4 RobTheBloke   Members   


Posted 21 March 2013 - 12:51 PM

mat4 mModel = glm::mat4(1, 0, 0, object->posX, 
                        0, 1, 0, object->posY, 
		        0, 0, 1, object->posZ,
		        0, 0, 0, 1);


Transpose that matrix. Translation should be at the bottom, not the right.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.