[657] | 1 | @subheading OpenGL GLSL
|
---|
| 2 | OpenGL GLSL has a similar language syntax to HLSL but is tied to the OpenGL API. The are a few benefits over Cg in that it only requires the OpenGL render system plugin, not any additional plugins. Declaring a OpenGL GLSL program is similar to Cg but simpler. Here's an example:
|
---|
| 3 | @example
|
---|
| 4 | vertex_program myGLSLVertexProgram glsl
|
---|
| 5 | {
|
---|
| 6 | source myGLSLVertexProgram.txt
|
---|
| 7 | }
|
---|
| 8 | @end example
|
---|
| 9 | In GLSL, no entry point needs to be defined since it is always 'main()' and there is no target definition since GLSL source is compiled into native GPU code and not intermediate assembly. @*@*
|
---|
| 10 |
|
---|
| 11 | GLSL supports the use of modular shaders. This means you can write GLSL external functions that can be used in multiple shaders.
|
---|
| 12 |
|
---|
| 13 | @example
|
---|
| 14 | vertex_program myExteranalGLSLFunction1 glsl
|
---|
| 15 | {
|
---|
| 16 | source myExternalGLSLfunction1.txt
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | vertex_program myExteranalGLSLFunction2 glsl
|
---|
| 20 | {
|
---|
| 21 | source myExternalGLSLfunction2.txt
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | vertex_program myGLSLVertexProgram1 glsl
|
---|
| 25 | {
|
---|
| 26 | source myGLSLfunction.txt
|
---|
| 27 | attach myExteranalGLSLFunction1 myExteranalGLSLFunction2
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | vertex_program myGLSLVertexProgram2 glsl
|
---|
| 31 | {
|
---|
| 32 | source myGLSLfunction.txt
|
---|
| 33 | attach myExteranalGLSLFunction1
|
---|
| 34 | }
|
---|
| 35 | @end example
|
---|
| 36 |
|
---|
| 37 | External GLSL functions are attached to the program that needs them by using 'attach' and including the names of all external programs required on the same line seperated by spaces. This can be done for both vertex and fragment programs.
|
---|
| 38 |
|
---|
| 39 | @subheading GLSL Texture Samplers
|
---|
| 40 | To pass texture unit index values from the material script to texture samplers in glsl use 'int' type named parameters. See the example below:@*
|
---|
| 41 |
|
---|
| 42 | excerpt from GLSL example.frag source:
|
---|
| 43 | @example
|
---|
| 44 | varying vec2 UV;
|
---|
| 45 | uniform sampler2D diffuseMap;
|
---|
| 46 |
|
---|
| 47 | void main(void)
|
---|
| 48 | {
|
---|
| 49 | gl_FragColor = texture2D(diffuseMap, UV);
|
---|
| 50 | }
|
---|
| 51 | @end example
|
---|
| 52 |
|
---|
| 53 | In material script:
|
---|
| 54 | @example
|
---|
| 55 | fragment_program myFragmentShader glsl
|
---|
| 56 | {
|
---|
| 57 | source example.frag
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | material exampleGLSLTexturing
|
---|
| 61 | {
|
---|
| 62 | technique
|
---|
| 63 | {
|
---|
| 64 | pass
|
---|
| 65 | {
|
---|
| 66 | fragment_program_ref myFragmentShader
|
---|
| 67 | {
|
---|
| 68 | param_named diffuseMap int 0
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | texture_unit
|
---|
| 72 | {
|
---|
| 73 | texture myTexture.jpg 2d
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | @end example
|
---|
| 79 |
|
---|
| 80 | An index value of 0 refers to the first texture unit in the pass, an index value of 1 refers to the second unit in the pass and so on.@*@*
|
---|
| 81 |
|
---|
| 82 | @subheading Accessing OpenGL states in GLSL
|
---|
| 83 | GLSL can access most of the GL states directly so you do not need to pass these states through @ref{param_named_auto} in the material script. This includes lights, material state, and all the matrices used in the openGL state ie model view matrix, worldview projection matrix etc. @*@* |
---|