View Full Version : GLSL - Why is gl_Vertex a vec4?

typewriter

03-03-2010, 09:29 AM

I can't seem to find anywhere in the orange book or on the internet.. Why is gl_Vertex a vec4? What is gl_Vertex.w?

I am implementing a spherical-coordinate deformer. When I convert from cartesian to spherical coords, what should happen to w?

pjcozzi

03-03-2010, 09:46 AM

gl_Vertex represents the position in homogeneous coordinates, so w is 1. In homogeneous coordinates, points have a w of 1 and vectors have a w of 0. So a point minus a point is a vector and so on. Note that valid values for w are just 0 and 1, so it doesn't make sense to add a point and a point.

Regards,

Patrick

typewriter

03-03-2010, 10:32 AM

Hmm.. OK that makes sense. But when I'm creating my own points inside the shader, should I make them vec4s? Because I've been doing it with vec3s. Do you need the W component for matrix operations?

Also then why is gl_Normal a vec3?

Ilian Dinev

03-03-2010, 10:37 AM

if you specify a float X, the vtx attribute will be {X,0,0,1}

if you specify a vec2, it'll be (X,Y,0,1}

{X,Y,Z,1}

{X,Y,Z,W}

bdysk

03-03-2010, 10:47 AM

you need the W component in matrix operations if you want to translate a point. The translation part is in the 4th column of a matrix, so the translation wont affect a vector with W=0.

One should mention, that in theory also values for W aside 0 and 1 are allowed in homogeneous coordinates.

W = 0 means a vector,

W != 0 means a point.

All points (WX, WY, WZ, W) in R^4 specify the same point (X,Y,Z) in R^3.

typewriter

03-03-2010, 12:00 PM

Interesting.. Thanks for the tips. This makes more sense now-- although the way I've been using vec3s seems to be OK since I am always assuming I know if its a point or a vector.

Now that I understand this, I can verify that my ignorance of the W component was NOT responsible for the issue I'm having.. Expect a math question next.. ;)

dorbie

03-04-2010, 02:27 AM

Remember that setting w in a shader can be different for a point vs a vector transformation. To transform a vector through a matrix you can set w to 0 and perform the multiply. Useful for normals for example. This has the equicalent effect of just doing the 3x3 and zeroing other terms like translation, but you don't have to worry about details like that at the conceptual level.

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.