PDA

View Full Version : vector with 4 components and matrix 4x4

 Chirone05-30-2010, 09:16 AMI've been learning OpenGL ES 2.0 recently and it uses vec4 and mat4, which are vectors with 4 components and matrices with 4 columns and 4 rows. it makes a bit of sense as to why these have 4 components... but i am confused. for color it makes sense for a vector to have 4 components, red, green, blue and alpha then position/directions have x, y, z, and w? what is the w? i've tried to google but it always corrects me asking if i want to be searching for vectors with only x y and z. and with texture coordinates, while they are 2d, i don't understand what the other two components are for... it has s, t, p, q. somewhere it said that it's normally s,t,r,q but r had to be replaced with p since r is used in color. i assume the r part of texture coordinates is for 3d textures but i'm not sure what q is for. i hope this makes sense because i have no idea how to ask this question...
05-30-2010, 10:12 AM
Um... as far as I know, vectors don't have to have four components at all. You simply choose whether you want to use vec2, vec3 or vec4 depending on the data they represent and the calculation you're doing.

That's with GLSL though. I've never touched ES.

helios01
05-30-2010, 10:39 AM
4x4 Matrices are used for transformations in a 3D space. Translation in 3d space demands a 4x4 matrix because adding a vector3 to each point is not linear, there is no matrix that can move the origin (0,0,0) using a 3x3 matrix. So to move a bunch a points in 3D space by multiplying a matrix, we need a 4x4 matrix.

Specifically, we have the origin be (0,0,0,1). Then we can translate something in 3D space by (x,y,z) by applying/multiplying a matrix of the form:
| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| x y z 1 |

Chirone
05-30-2010, 11:08 AM
Elyaradine, yeah, i know you can also use vec2 and vec3, but i'm still curious to know what the fourth parameter is used for and what it actually does for both position/direction and texture vectors

helios01, thanks, that cleared up a few things

i suppose the fourth parameter in the vectors is used because you can't multiply or add a vec3 to a mat4?

helios01
05-30-2010, 11:29 AM
That's correct, we can only multiply matrices axb with matrices bxc.

Mic_Ma
05-30-2010, 12:17 PM
Look up homogeneous coordinates, or quaternions, both of which use 4-vectors (for different reasons).

ThE_JacO
05-30-2010, 11:10 PM
Elyaradine, yeah, i know you can also use vec2 and vec3, but i'm still curious to know what the fourth parameter is used for and what it actually does for both position/direction and texture vectors

helios01, thanks, that cleared up a few things

i suppose the fourth parameter in the vectors is used because you can't multiply or add a vec3 to a mat4?
As mentioned already, you might want to look into homogeneous coordinates (http://en.wikipedia.org/wiki/Homogeneous_coordinates) and quaternions. The 4th component is usually employed to be able to have xw, yw, zw into a vectorial form containing only scalars, 4x4 and 4x3 (assuming no homogenization) matrices are probably the most common use in CG.
Perspective projections and vectors with a bundled factor are other common uses.

There's also a large number of cases where you use quadrivectors that don't necessarily just involve having a convenient matricial form, and having that granule and all the basic operations implemented in an API comes in handy very often.

Next time around though you probably want to post this kind of questions in the graphics programming forums, mostly to reach the right intended audience :) Not that it's bad to occasionally have a topical thread in GD :p

Chirone
05-31-2010, 12:01 AM
thanks for the tips and info Mic_Ma and ThE_JacO, i totally forgot about homogeneous coordinates

it's been 5 years since i did applied math and hadn't had to use it until recently...

CGTalk Moderation
05-31-2010, 12:01 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.