Shader Coding - Best Practices: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
mNo edit summary
Line 12: Line 12:
}}
}}


{{Note| ftransform() is no longer available since GLSL 1.40 and GLSL ES 1.0. Instead, the programmer has to manage the projection and modelview matrices explicitly in order to comply with the new OpenGL 3.1 standard.
<syntaxhighlight lang="glsl">
#version 140
uniform Transformation {
mat4 projection_matrix;
mat4 modelview_matrix;
};
in vec3 vertex;
void main(void) {
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);
}</syntaxhighlight>
}}


== Fragment Shaders ==
== Fragment Shaders ==

Revision as of 14:19, 22 February 2014

WIP.png Work in progress
This article or section will be worked on in the upcoming hours or days.
Note: Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html
See history for the latest developments.

Vertex Shaders

Note  For testing purposes, you can use a simple pass-through vertex shader:
#version 120
void main(void) {
        gl_Position = ftransform();
}
Note  ftransform() is no longer available since GLSL 1.40 and GLSL ES 1.0. Instead, the programmer has to manage the projection and modelview matrices explicitly in order to comply with the new OpenGL 3.1 standard.
#version 140
 
uniform Transformation {
	mat4 projection_matrix;
	mat4 modelview_matrix;
};
 
in vec3 vertex;
 
void main(void) {
	gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);
}

Fragment Shaders

Note  To check if your shader is working, add this as the last line, it should turn all pixels black:
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
Note  if4dnf: it's usually bad practice to do any kind of operations on gl_FragColor assignment. At most a vec4() swizzle is accepted, although even that one is treated differently based on the platform (some assign a temporary variable, some don't). Do wahtever you need in a separate vec4 variable and just assign it's value to gl_FragColor at the end.
Note  If the fragment isn't running, there should be an error message in the log file (I wish they'd still be written to the console). Might be that the texture isn't defined in the supporting framework for instance - the shader assumed that it is available, but you need to declare it in the matching C++ code/effect file first.
Note  This is how you can make a pixel darker:
gl_FragColor = color * 0.5;