Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not vec3 for OpenGL ES 2.0 gl_Position?

I am new to OpenGL ES 2.0, and cannot understand the following simplest shader:

attribute vec4 vPosition; void main() {    gl_Position = vPosition; } 

My question is, since a position would be a vector of (x, y, z), why is gl_Position a vec4 instead of vec3?

like image 523
peter Avatar asked Mar 30 '12 13:03

peter


People also ask

Why is gl_Position a vec4?

In the last assignment statement, gl_Position is the special built-in variable that is used in the vertex shader to give the coordinates of the vertex. gl_Position is of type vec4, requiring four numbers, because the coordinates are specified as homogeneous coordinates (Subsection 3.5. 3).

What is gl_Position in OpenGL?

gl_Position is a built-in vertex shader output variable, whose type is defined by the OpenGL specification to be a vec4 . position is a vertex shader attribute, and since the introduction of programmable shaders, you (as the developer) are in full control of its format.


2 Answers

The w in vec4(x, y, z, w) is used for clipping, and plays its part while linear algebra transformations are applied to the position.

By default, this should be set to 1.0.

See here for some more info: http://web.archive.org/web/20160408103910/http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for-iOS-chapter-4.html

like image 150
Matisse VerDuyn Avatar answered Oct 18 '22 02:10

Matisse VerDuyn


If you provide your vertices to the shader directly in clip space, you could just pass x,y,z and add 1 as the w component in that shader.

attribute vec3 vPosition; // vec3 instead of vec4 void main() {    gl_Position = vec4 (vPosition, 1.0); } 
like image 25
Drout Avatar answered Oct 18 '22 02:10

Drout