Using a matrix to generate a new vector position
Okay, basically I'm just trying to calculate the new location for a point after applying a few translates and rotates to the modelview matrix in OpenGL. Here's what I have:
The matrix, by using glGetFloatv(GL_MODELVIEW_MATRIX, mat)
A vector, which is just an (x, y, z) coordinate
Normally I'd just multiply the vector by the matrix, but the matrix is 4x4 in OpenGL and my vector is 3x1. How can I calculate the new position for the vector using that matrix?

Like say for example my vector is (0, 0, 0), and I run these transformations:
glTranslatef(0, 4, 0)
glRotatef(90, 0, 0, 1) // rotate to the right 90 degrees
glTranslatef(3, 0, 0)
How could it calculate that the new vector position is (3, 4, 0)?
Thanks in advance!
The matrix, by using glGetFloatv(GL_MODELVIEW_MATRIX, mat)
A vector, which is just an (x, y, z) coordinate
Normally I'd just multiply the vector by the matrix, but the matrix is 4x4 in OpenGL and my vector is 3x1. How can I calculate the new position for the vector using that matrix?

Like say for example my vector is (0, 0, 0), and I run these transformations:
glTranslatef(0, 4, 0)
glRotatef(90, 0, 0, 1) // rotate to the right 90 degrees
glTranslatef(3, 0, 0)
How could it calculate that the new vector position is (3, 4, 0)?
Thanks in advance!
imikedaman Wrote:Okay, basically I'm just trying to calculate the new location for a point after applying a few translates and rotates to the modelview matrix in OpenGL. Here's what I have:
The matrix, by using glGetFloatv(GL_MODELVIEW_MATRIX, mat)
A vector, which is just an (x, y, z) coordinate
Normally I'd just multiply the vector by the matrix, but the matrix is 4x4 in OpenGL and my vector is 3x1. How can I calculate the new position for the vector using that matrix?

Like say for example my vector is (0, 0, 0), and I run these transformations:
glTranslatef(0, 4, 0)
glRotatef(90, 0, 0, 1) // rotate to the right 90 degrees
glTranslatef(3, 0, 0)
How could it calculate that the new vector position is (3, 4, 0)?
Thanks in advance!
OpenGL uses what is called a homogeneous coordinate system. Which (pardon my bastardization) is like tacking on an extra dimension to points. The idea can be pretty clearly illustrated when you embed a 2D plane in 3space, but when you're embedding 3space into 4space like OpenGL does, it's pretty hard to visualize or illustrate. But anyway, here is how it works:
In homogeneous coordinates the point (x, y, z) can be represented as (ax, ay, az, a) for any nonzero constant a. In other words, the points (ax, ay, az, a) and (bx, by, bz, b) are equal if a and b are nonzero real numbers.
In order to multiply your vector by the matrix you must first represent it as a homogeneous point. So if your vector is (x, y, z), you can represent it as (x, y, z, 1). Multiply this vector by the matrix. What you'll get is (x_prime, y_prime, z_prime, c). In order to turn the resultant vector into a normal 3D vector you need to divide each component by c so that you get (x_prime / c, y_prime / c, z_prime / c, 1). Your answer is then (x_prime / c, y_prime / c, z_prime / c).
EDIT: Actually, do you have to divide by c? I just realized that with what OpenGL uses the modelview matrix for, you might not have to (c might always be 1).
Since w is always 1 from what I can tell, doing that is functionally equivalent to what I was previously doing, which is just adding the last column directly to the x, y, and z values for the vector, since that's where the translation values apparently go. However, that doesn't seem to work right. :/
Unless there's a glitch somewhere else in my code... I better go check.
Unless there's a glitch somewhere else in my code... I better go check.
Quote:... since that's where the translation values apparently go.that doesn't mean that you should do anything differently, its simply a 4x4 matrix multiplied by a 4x1 matrix.
Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
imikedaman Wrote:Since w is always 1 from what I can tell, doing that is functionally equivalent to what I was previously doing, which is just adding the last column directly to the x, y, and z values for the vector, since that's where the translation values apparently go. However, that doesn't seem to work right. :/
Unless there's a glitch somewhere else in my code... I better go check.
Maybe you aren't grabbing the entries of the matrix correctly?
m[12], m[13], and m[14] should be the translational entries of the modelview matrix. m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], and m[10] are the affine entries of the matrix.
It's definitely a bug elsewhere in the code, and not with how I was composing the transformations. After running a simple test (start at the origin, translate up 4 units, rotate right 90 degrees, translate "up" 3 units), it got the correct answer of (4, 3, 0). And now begins the long and tedious process of figuring out where I messed up.
EDIT: Yep, I was using 12, 13, and 14 for the translation entries. Thanks for your help.
EDIT: Yep, I was using 12, 13, and 14 for the translation entries. Thanks for your help.
Aha, that wasn't so long, nor was it particularly tedious. It turns out I was effectively calling glTranslate in the wrong place, making everything increasingly distored as everything propagated down the recursive function. Now everything seems to be working fine!
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Get Z vector of rotation matrix?  mikey  17  7,778 
Feb 28, 2010 12:52 PM Last Post: mikey 

generate MD5 strings with ObjectiveC?  fernandovalente  3  4,514 
Jan 27, 2010 09:29 AM Last Post: Hog 

Key sequence to generate endoffile signal?  WhatMeWorry  14  8,964 
May 7, 2009 07:25 AM Last Post: Robin Forder 

new position with vector  pander007  3  3,805 
Oct 13, 2007 11:49 AM Last Post: Nevada 

geting window position  marko  2  2,884 
Jul 24, 2005 02:32 AM Last Post: marko 