Difference between revisions of "Shaders in Oolite"
(New page: '''Fragment shaders''' (also known, less accurately, as '''pixel shaders''') are programs which run on a graphics processing unit, which provide a more flexible alternative or supplement t...) |
m (Added note about compatibility-breaking modifications to come.) |
||
Line 2: | Line 2: | ||
Fragment shaders are supported in Oolite 1.67 for Mac OS X and later. At the time of writing, no released version of Oolite for other platforms supports shaders, but the next Windows release will. | Fragment shaders are supported in Oolite 1.67 for Mac OS X and later. At the time of writing, no released version of Oolite for other platforms supports shaders, but the next Windows release will. | ||
+ | |||
+ | The precise method for specifying shaders will change in the next release. However, the shader code will not need modification and updating will be simple. | ||
== An example == | == An example == |
Revision as of 18:21, 22 March 2007
Fragment shaders (also known, less accurately, as pixel shaders) are programs which run on a graphics processing unit, which provide a more flexible alternative or supplement to textures for specifying objects’ appearance. Fragment shaders can be implemented in a number of special-purpose programming languages; Oolite uses OpenGL Shading Language, also known as GLslang or GLSL.
Fragment shaders are supported in Oolite 1.67 for Mac OS X and later. At the time of writing, no released version of Oolite for other platforms supports shaders, but the next Windows release will.
The precise method for specifying shaders will change in the next release. However, the shader code will not need modification and updating will be simple.
An example
A full explanation of GLslang is beyond the scope of this article, but for illustrative purposes, here is an overview of the shader code in the Freaky Thargoids example OXP.
The shader code, which is specified in shipdata.plist, looks like this:
uniform sampler2D tex0; uniform sampler2D tex1; uniform float time; float wave(float t) { /* approximates a sine waveform by summing 4 triangular waveforms */ float s0 = t; s0 -= floor(s0); float sum = abs( s0 - 0.5); float s1 = t - 0.125; s1 -= floor(s1); sum += abs( s1 - 0.5) - 0.25; float s2 = t - 0.250; s2 -= floor(s2); sum += abs( s2 - 0.5) - 0.25; float s3 = t - 0.375; s3 -= floor(s3); sum += abs( s3 - 0.5) - 0.25; return sum; } void main() { float t1 = 1.5 * time + texture2D(tex1, gl_TexCoord[0].st).a; float effect = wave(t1); vec4 base = texture2D(tex0, gl_TexCoord[0].st); vec4 glow = texture2D(tex1, gl_TexCoord[0].st); gl_FragColor = gl_Color * base + effect * glow; }
The first section declares three uniform variables, which are used to pass information from Oolite to the shader. The two sampler2D
variables are used to read from the two textures used by the shader. The float
variable timer is a number which increases by 1.0 each second.
This is followed by a custom function wave(), which is an approximation of the trigonometric sine function].
The last part is the function main(), which is the function executed by the GPU. It first calculates a light intensity value, based on the time (multiplying by 1.5 gives a frequency of 1.5 pulsations per second, or 1/1.5 = 0.666… Hz) and the alpha channel of tex1, the glow and phase map texture. It then reads texture values from each texture, then combines them based on the calculated light value, and assigns the result to gl_FragColor, which is the colour of the generated pixel.
Uniform reference
The uniform variables currently provided by Oolite are:
Name | Type | Description |
---|---|---|
tex0 | sampler2D |
Sampler for the first texture. |
tex1 | sampler2D |
Sampler for the second texture. |
… | ||
texN | sampler2D |
Sampler for the N+1th texture. |
time | float |
Uniformly increasing timer, in seconds. |
engine_level | float |
Engine thrust. 0.0 for no movement, 1.0 for full thrust. Greater than 1.0 for injectors or hyperspeed? Untested. |
laser_heat_level | float |
Laser temperature, ranging from 0.0 to 1.0. |
hull_heat_level | float |
Hull temperature. 1.0 is damage level. (Not implemented in 1.67.1 or earlier.) |
Limitations
The current implementation does not support vertex shaders. It also does not provide the information necessary to implement the most common form of normal mapping.