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?
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:
gl_ModelViewMatrix
, gl_ProjectionMatrix
, etc.).gl_Light*
).gl_Material*
).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:
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.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.