Magik6k
Magik6k

Reputation: 125

GLSL vertex shader gl_Position value

I'm creating game that uses orthogonal view(2D). I'm trying to understand the value of gl_Position in vertex shader.

From what I understand x and y coordinates translate to screen position in range of -1 to 1, but I'm quite confused with role of the z and w, I only know that the w value should be set to 1.0

For the moment I just use gl_Position.xyw = vec3(Position, 1.0);, where Position is 2D vertex position

I use OpenGL 3.2.

Upvotes: 4

Views: 8366

Answers (1)

ratchet freak
ratchet freak

Reputation: 48176

Remember that openGL must also work for 3D and it's easier to expose the 3D details than to create a new interface for just 2D.

The Z component is to set the depth of the vertex, points outside -1,1 (after perspective divide) will not be drawn and for the values between -1,1 it will be checked against a depth buffer to see if the fragment is behind some previously drawn triangle and not draw it if it should be hidden.

The w component is for a perspective divide and allowing the GPU to interpolate the values in a perspective correct way. Otherwise the textures looks weird.

Upvotes: 4

Related Questions