Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenGL vertex shaders, what is w, and why do I divide by it?

void main(void) {   vec4 clipCoord = glModelViewProjectionmatrix * gl_Vertex;   gl_Position = clipCoord;    gl_FrontColor = gl_Color;    vec3 ndc = clipCoord.xyz / clipCoord.w; 

So the clipCoord is just doing standard fixed pipeline transforms. Why do I divide by w, and what do I get from this?

like image 636
anon Avatar asked Mar 11 '10 05:03

anon


People also ask

What is vUV in vertex shader?

Varying: Varying variables are values created by the vertex shader and transmitted to the pixel shader. Here the vertex shader will transmit a vUV (a simple copy of uv ) value to the pixel shader. This means that a pixel is defined here with a position and a texture coordinates.

What is W in 3D graphics?

So traditionally, in 3D graphics, directional lights are differentiated from point lights by the value of W in the position vector of the light. If W=1, then it is a point light. If W=0, then it is a directional light.

What is the purpose of a vertex shader?

The vertex shader is used to transform the attributes of vertices (points of a triangle) such as color, texture, position and direction from the original color space to the display space. It allows the original objects to be distorted or reshaped in any manner.

What is OpenGL vertex shader?

The Vertex Shader is the programmable Shader stage in the rendering pipeline that handles the processing of individual vertices. Vertex shaders are fed Vertex Attribute data, as specified from a vertex array object by a drawing command.


1 Answers

W is the fourth coordinate of a three dimensional vertex; This vertex is called homogeneous vertex coordinate.

In few words, the W component is a factor which divides the other vector components. When W is 1.0, the homogeneous vertex coordinates are "normalized". To compare two vertices, you should normalize the W value to 1.0.

Think of the vertex (1,1,1,1). Now increase the W value (w > 1.0). The normalized position is scaling! and it is going to the origin. Think of the vertex (1,1,1,1). Now decrease the W value (W < 1.0). The normalized position is going to an infinite point.

Apart from scaling vertex coordinates, the W coordinate is necessary since you have to multiply a 4x4 matrix (the model view and/or the projection matrices) with a 4x1 matrix (the vertex).

Of course, the Red Book is the definite guide:

Red Book Appendix

like image 55
Luca Avatar answered Sep 21 '22 01:09

Luca