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:
- 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 thegl_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.
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!
来源:https://stackoverflow.com/questions/24416589/glsl-using-custom-output-attribute-instead-of-gl-position