10

I am currently learning OpenGL with shaders (3.3). There is one thing i can't seem to work out though. I have read that using built-in variables like gl_Position and gl_FragCoords is deprecated in OpenGL 3+, therefore I wanted to use my own output variable.

So instead of this:

#version 330\n
layout(location=0) in vec2 i_position;
out vec4 o_position;
void main() 
{
    gl_Position = vec4(i_position, 0.0, 1.0);
};

I wrote this:

#version 330\n
layout(location=0) in vec2 i_position;
out vec4 o_position;
void main() 
{
    o_position = vec4(i_position, 0.0, 1.0);
};

The shaders compile without problems in both cases, but the second code produces just blank screen. Do I need to somehow specify which variable is the output one?

michy04
  • 317
  • 3
  • 11
  • 4
    Neither of these [built-in variables](http://www.opengl.org/wiki/Built-in_Variable_(GLSL)) are deprecated. Prior to core profiles (compatibility profiles), there were builtins that reflected fixed pipeline attributes, like `gl_Color`, `gl_Normal`, etc. – Brett Hale Jun 25 '14 at 19:17
  • Oh I am sorry then. I have read that some of the `gl_*` variables were deprecated and thought that all of them were. Thank you for the clarification. Anyway, how do I make it work with the custom output variable? Or do you just use the gl_Position attribute to set the vertex position? – michy04 Jun 25 '14 at 19:23
  • 2
    You have to still use `gl_Position`. That variable is special, because it specifies coordinates that are used by fixed function blocks between vertex and fragment shaders (clipping, rasterization, etc). You can't replace it with a variable you define. In addition to the examples that @BrettHale already mentioned, `gl_FragColor`, which used to be the pre-defined output of the **fragment** shader, is also deprecated. – Reto Koradi Jun 25 '14 at 19:53
  • I see. I thought that if you can specify custom outputs in fragment shader you can do so in vertex shader, apparently I was wrong. Once again thank you for the clarification. I still have a lot to learn, but so far OpenGL has been great fun. – michy04 Jun 25 '14 at 20:03
  • You can do that, but `gl_Position` is part of `gl_PerVertex`. More precisely it is a non-optional part, there are still fixed-function parts of the render pipeline that require `gl_PerVertex.gl_Position`. `gl_FragCoord`, as you mention is actually calculated from that variable - after division by `w` and viewport transformation. – Andon M. Coleman Jun 26 '14 at 01:14

2 Answers2

33

This was mostly covered by the comments, but in the interest of having an answer in place, let me summarize which pre-defined GLSL variables are gone in the core profile, and which are still there.

In most cases, pre-defined variables were removed from core profile shaders as a direct consequence of large parts of the fixed function pipeline being removed from the API. There were a lot of uniform variables that reflected state that simply does not exist anymore. Typical examples include:

  • Fixed function transformation state (gl_ModelViewMatrix, gl_ProjectionMatrix, etc.).
  • Lighting parameters (gl_Light*).
  • Material parameters (gl_Material*).
  • Clip planes (gl_Clip*).

Similarly, vertex shader inputs that received fixed function vertex attributes are gone because only generic attributes are supported in the core profile. These include gl_Vertex, gl_Normal, gl_Color, etc.

There also used to be some pre-defined varying variables like gl_FrontColor, gl_BackColor, and gl_TexCoord that are all gone, and can easily be replaced by defining your own out/in variables.

Also gone are the pre-defined fragment shader outputs gl_FragColor and gl_FragData. Those are an interesting case since the reason is not feature deprecation. My understanding is that they were deprecated because they were not flexible enough to accommodate current functionality. Since the type of gl_FragColor was vec4, the output of the fragment shader would have to be vectors with float components. This does not work well if your render target has a different type, e.g. if you are rendering to an integer texture.

So what pre-defined variables are still left? While much less than before, there are still a few, which all relate to fixed function that is still in place in the programmable pipeline. Examples include:

  • The vertex coordinate interface between vertex shader and fragment shader, which primarily consists of the gl_Position output in the vertex shader and the gl_FragCoord input in the fragment shader. There are still a number of fixed function blocks sitting between vertex and fragment shaders, like clipping and rasterization. Those fixed function blocks operate on vertex coordinates, so the pre-defined variables still make sense.
  • Some variables related to instancing (gl_VertexID, gl_InstanceID).
  • gl_ClipDistance to support clipping with arbitrary planes, which is also still present in fixed function form.
  • gl_FrontFacing. The winding order of triangles is evaluated by fixed function, and made available to the fragment shader.

None of these lists are close to exhaustive, but I hope it's a useful overview, and provides some background. As always, the spec documents provide the final and full answer.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • 2
    Thanks everyone for their answers they really clarified a lot to me. OpenGL is quite a complex API so many things are not obvious at first. – michy04 Jun 26 '14 at 12:26
2

As our collegue just said, gl_Position is not deprecated. I just bought the book OpenGL Superbible 6th edition about OpenGL 4.3 and right on the first pages (page 18) this function is being used.

I can only recommend to get the book. It is written by the people designing OpenGL and its really didactic.

I was also lost like you before because I was trying to learn via internet or stackexchange, and after I got this book I am having a wonderful time with OpenGL. I think what I will learn in three weeks with the book will be more worth than 5 years learning from internet. OpenGL is not complicated and is not any "beast" as many people say as long you learn it from this book. If you learn it over internet, I am sure its more than a beast!

This book says right on the beginnig that he is not going to teach anything which is not core OpenGL, so you can be sure not to run into traps like the one you did.

You can buy this book used in Amazon for as cheap as 50 cents. I can only recommend it!

user5193682
  • 260
  • 1
  • 11
  • 1
    I think he did, if the first sentence. And then confirmed what I've suspected, the Internet is not a fulsome resource for understanding what is going on with the shaderized versions of OpenGL. Which is a shame, since OpenGL has a wiki. – Blair Houghton Nov 21 '15 at 23:01