Page 1 of 1

What is the vec4 position returned by a vertex shader?

Posted: Thu Nov 10, 2016 8:09 pm
by Zarty55
Just a quick question. I thought it would be vec2 or vec3. What does the components of that vector means? And also, what kind of transformation is the transform_projection doing? Thanks!

Re: What is the vec4 position returned by a vertex shader?

Posted: Thu Nov 10, 2016 8:21 pm
by raidho36
The GPU uses 3d coordinates anyway, and you need a 4 dimensional vector to go along with 3+1d projection matrix. It's components are simply x, y and z coordinates, and a w additional projection value which you shouldn't touch.

The projection sets up a matrix such that multiplying world coordinates vector by this matrix yields screen coordinates. It has to do with object positions.

Re: What is the vec4 position returned by a vertex shader?

Posted: Thu Nov 10, 2016 10:27 pm
by bartbes
As some extra explanation, using 4 coordinates to describe 3-dimensional points means you can easily do translation. To do the same in 2 dimensions, you only need 3 coordinates, but OpenGL works with 3-dimensional coordinates. For the maths to work, the 4th coordinate needs to be 1, though OpenGL divides all other coordinates by it in case it isn't.

The transform_projection passed to the vertex shader represents all transformations you have done using love.graphics.translate, love.graphics.scale, etc, and finally the "base" projection that turns the input vector into a vector representing its position in clip space (which ends up being its position on the screen I know this isn't strictly true, leave me be, opengl wizards).

Re: What is the vec4 position returned by a vertex shader?

Posted: Fri Nov 11, 2016 1:00 am
by Zarty55
Thanks for the answers guys :)

Ok, I've read somewhere else about the 4th component (And it's related to either a true Z value or a trick where 0 is scaling and 1 means translating? I'm confused about this). But the 3rd component is the one I'm not understanding. I tried to modify it and it simply makes whatever I'm drawing disappear if it's bigger than 1 or smaller than -1. My thought was that the vertex shader was actually the last thing that would happen to the vertex and after that it would draw the polygon on the screen. But the fact that it is a vec4 makes it seems to me that something else is happening after that. Is there a diagram that represents the general proccess so I can locate myself in all this?

If it's too complicated to explain maybe you guys could give me a website to read about it?

Edit: Ok I've found some openGL pipeline diagram. What I want to know is if Love changes any of that?

Re: What is the vec4 position returned by a vertex shader?

Posted: Fri Nov 11, 2016 10:05 am
by raidho36
It's hard-wired in the GPU and cannot be changed.

The reason why graphics disappear if you modify z value too much is because camera clipping still applies, and apparently it's set such that only graphics within [-1, 1] regions show up. Not that it matters, because 2d graphics is rendered in specific order and blended, so the z value is practically discarded.