0

How do you modify or override the perspective division calculation in OpenGL?

Also is it possible to use x, y, z and w as variables in matrices in OpenGL?

1 Answers1

1

How do you modify or override the perspective division calculation in OpenGL?

You can't. It's one of the few "hardwired" (or actually hardcoded on modern GPUs) things in OpenGL. But that's no problem. Just set gl_Position.w = 1 at the very end of the vertex shader, which will effectively make the perspective division a no-op. However you then also have no perspective, so you'd have to implement that in your vertex shader as well. You could write gl_Position /= gl_Position.w; as the very last statement in the vertex shader to do the perspective divide yourself, and bypass the built-in divide.

Also is it possible to use x, y, z and w as variables in matrices in OpenGL?

I'm not quite sure what you mean by that.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thank you for the help. Would this need a custom vertex shader? In regard to the x, y, z and w, I wanted to multiply x by z using a matrix (y and z will be multiplied by 1). – user2689489 Aug 29 '13 at 14:49
  • @user2689489: Of course this requires a custom vertex shader. But with modern OpenGL you need a vertex shader anyway. Well, regarding the other part, how about you write down as mathematical equation (forget about programming languages for that), what you want to calculate. – datenwolf Aug 29 '13 at 15:36
  • Thank you; I'll try that when I have access to OpenGL. The equation would be this http://i39.tinypic.com/330cj8p.png (multiply x by z so there is no perspective effect in x (thus cancelling x/z), or change it so that it is x/(z/2) and thus giving a reduced perspective/convergence effect. I don't know how possible this is geometrically. – user2689489 Aug 30 '13 at 12:17
  • Well, just write that down, i.e. `gl_Position.x /= gl_Position.z`. As for using x, y, z, w elements, those are defines for vectors only. However matrices are just a bunch of vectors, so let `mat4 M;` you can write `M[1].z` if you'd like to. – datenwolf Aug 30 '13 at 12:53
  • Would this be possible using glMultMatrixd in Python with OpenGL 1.4? – user2689489 Aug 30 '13 at 14:13
  • @user2689489: No. glMultiMatrix modifies the transformation matrices not in the way you require it. You could do a glGet to retrive the current matrix, and add '1' the XZ element and use `glLoadMatrix` to upload that to OpenGL again. But why bother: Nobody should use the fixed function pipeline in new projects anyway, and ideally you're doing the matrix math with a real matrix library and only loading the final value to OpenGL using glUniform (or with glLoadMatrix) if you really have to. – datenwolf Aug 30 '13 at 15:21
  • Note that dividing by "w" in the vertex shader is not quite the same: interpolation of varying variables will not be perspective correct. For example textures will not properly converge with distance, appearing awkwardly triangulated. I presume this is why it's still a fixed function; the original "w" needs to be available to the rasterizer. – jwelsh Mar 22 '15 at 23:42