Public Group

# Vectors with OpenGL

This topic is 4961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone. Sorry if I sound like a n00b. I'm learning vectors for OpenGL, and I don't understand the whole purpose of using vectors. Why can't I just specify the x, y, and z coordinates? And also, how do position vectors, direction vectors, and up vectors work? What is posVec.fX, posVec.fY, and posVec.fZ? Help~ I'm really confused... ;_; Thanks in advance...

##### Share on other sites
Hi riyunoa! What you must first understand is that a vector is much more than a set of (x, y, z) coordinates. If you did your Math, a vector has direction and magnitude. You need to specify direction some times:

You will learn about Normals in OpenGL. Say you have drawn a face and want to implement lighting. So which side of the face will light up? You must specify a normal with glNormal3f or the like. That specifies a vector which has direction coming out of the face, and the same side out of which it is coming out is lit up.

Again, when you are higher up and say you want to make a 3d world where monsters always come running for the player. A vector from the monster's position to the player's will make sure the monster is going the right way. This vector maybe obtained by subtracting their respectiove position vectors.

A position vector ofcourse is not much more than a specification of (x, y, z) coordinates. Well, I am sorry if this confused you any. I am not an expert, you should research this more (use Google), but I hope this helps you anyhow.

##### Share on other sites
Good question.

For the most part, you CAN just use (x,y,z) but it gets ugly FAST when you're working with a lot of vertices.

Not only will you encounter the need for what deavik just described, but it's also a MUCH nicer way to conceptualize what's going on in your scene.

I guess it can all come down to how much you like/dislike OO and objects..;)

hth,

##### Share on other sites
Quote:
 Original post by riyunoaAnd also, how do position vectors, direction vectors, and up vectors work? What is posVec.fX, posVec.fY, and posVec.fZ? Help~ I'm really confused... ;_;Thanks in advance...

That'd be for your camera position within the scene if I'm reading you right.

The up vector is ALWAYS (okay 99.9%) (0.0f, 1.0f, 0.0f)...ie. positive y-axis

##### Share on other sites
Alright, thanks so much!! Any particular sites that I can research on this confusing stuff?

And, I'm fine with programming and OO, but math stuff is a killer... @_#

##### Share on other sites
Oh yeah, I have another question.

In class today, we were told that the direction vector MUST have a length of 1.0. Why is that so? We had to declare a displacement vector, to sort of "stretch" the direction vector.

Why can't we just have a direction vector with a length of 3.0? And not normalize it?

Thanks again!!

##### Share on other sites
I hope I can explain this properly..

At a very basic level, I like to think about normalization as the same operations you need to perform on fractions (remember those? *grin*) in order to work with them.

To do any fraction math, you need to have the denominator right? Same thing with vectors. By normalizing the vector, you are in effect converting everyting to the same denominator.

When doing any matrix operations, it's also handy to have your vectors normalized, but that's a tale for another time. (first things first)..;)

hth,

##### Share on other sites
Quote:
 Original post by riyunoaOh yeah, I have another question.In class today, we were told that the direction vector MUST have a length of 1.0. Why is that so? We had to declare a displacement vector, to sort of "stretch" the direction vector.Why can't we just have a direction vector with a length of 3.0? And not normalize it? Thanks again!!

It's just a direction and it has no real length. You can have a "direction" with a length of 3.0. But if you want to do any type of operations with them like cross or dot products, it's best to normalize not just 1 vector but all vectors to 1.0 so the answer doesn't have any irrelevant "length" problems. Because it's all directions and angles not lengths.

##### Share on other sites
Ohhhhh~~ So there is no real "length"... I know that vectors have magnitude and direction... the "length" is the magnitude... @_#

So normalizing is to get them all to be the common denominator~~~

Gah, I need to find out more about this stuff... T.T *waits till math class*

Thanks so much you guys!

##### Share on other sites
Quote:
 Original post by riyunoaOh yeah, I have another question.In class today, we were told that the direction vector MUST have a length of 1.0. Why is that so? We had to declare a displacement vector, to sort of "stretch" the direction vector.Why can't we just have a direction vector with a length of 3.0? And not normalize it? Thanks again!!

You could have a direction vector of any length. It's just that most 3d APIs today expect you to pass normalized vectors to their matrices. The reason being that the for let's say gluLookat to make a camera matrix, all it has to to is "transpose" the object martrix generated when you pass 3 vectors to gluLookat as opposed to calculating the actual matrix inverse, which is would have a performance hit.
The transpose would be equal to the inverse in a matrix where the 3 vectors are perpendicular to each other. That's why glulookat does not give you the leeway to pass the right vector. ;*)

And most matrices should be homogenous that is : y'' + y + c = 0 instead of y'' + y + c = f(x)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015505
×