Shader Coding - Best Practices: Difference between revisions

Jump to navigation Jump to search
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{WIP|Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html}}
{{WIP|Will be based on: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg35934.html}}
{{Rendering}}
== File Header ==
{{Note|It's a good idea to add a header to each shader detailing:
* filename
* purpose
* license
* author
* rendering pipeline (rembrandt/default)
* description
<syntaxhighlight lang="glsl">
// -*- mode: C; -*-
// urban.frag
// Licence: GPL v2
// Author: Frederic Bouvier.
//  Adapted from the paper by F. Policarpo et al. : Real-time Relief Mapping on Arbitrary Polygonal Surfaces
//  Adapted from the paper and sources by M. Drobot in GPU Pro : Quadtree Displacement Mapping with Height Blending
</syntaxhighlight>
}}
== Supported GLSL Versions ==
{{Note|Be aware that we currently '''_DO_NOT_''' support glsl <nowiki>> 1.2x. Please don't mix '''glsl''' flavours: please stick to glsl 1.2x.(even though support for it is largely </nowiki>''deprecated''). The transition to OpenGL 3.0 / glsl <nowiki>>=1.3x is a very big undertaking, and not backward compatible. It is always a good idea to declare the expected GLSL version at the top of each file</nowiki>:
<syntaxhighlight lang="glsl">
#version 120
</syntaxhighlight>
}}
== Enabling Extensions ==
{{Note|:To enable vendor specific extensions, use the '''#extension''' pragma at the top of the file, for example:
<syntaxhighlight lang="glsl">
#extension GL_ATI_shader_texture_lod : enable
#extension GL_ARB_shader_texture_lod : enable
</syntaxhighlight>
}}


== Vertex Shaders ==
== Vertex Shaders ==
Line 10: Line 46:
}
}
</syntaxhighlight>
</syntaxhighlight>
}}
{{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 ==


{{Note|To check if your shader is working, add this as the last line, it should turn all pixels black:
{{Note|To check if your shader is working, use a trivial  shader like this one or just add the last line to the end of the shader, it should turn all pixels black:
<syntaxhighlight lang="glsl">
<syntaxhighlight lang="glsl">
#version 120
void main() {
// turn all pixels into  black
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
gl_FragColor = vec4 (0.0,0.0,0.0,1.0);
}
</syntaxhighlight>
</syntaxhighlight>
}}
}}
Line 45: Line 69:
</syntaxhighlight>
</syntaxhighlight>
}}
}}
[[Category:Shader development]]
574

edits

Navigation menu