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 Matrix parameters
|
---|
83 |
|
---|
84 | Here are some examples of passing matrices to GLSL mat2, mat3, mat4 uniforms:
|
---|
85 |
|
---|
86 | @example
|
---|
87 | material exampleGLSLmatixUniforms
|
---|
88 | {
|
---|
89 | technique matrix_passing
|
---|
90 | {
|
---|
91 | pass examples
|
---|
92 | {
|
---|
93 | vertex_program_ref myVertexShader
|
---|
94 | {
|
---|
95 | // mat4 uniform
|
---|
96 | param_named OcclusionMatrix matrix4x4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
|
---|
97 | // or
|
---|
98 | param_named ViewMatrix float16 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
|
---|
99 |
|
---|
100 | // mat3
|
---|
101 | param_named TextRotMatrix float9 1 0 0 0 1 0 0 0 1
|
---|
102 | }
|
---|
103 |
|
---|
104 | fragment_program_ref myFragmentShader
|
---|
105 | {
|
---|
106 | // mat2 uniform
|
---|
107 | param_named skewMatrix float4 0.5 0 -0.5 1.0
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | @end example
|
---|
114 |
|
---|
115 | @subheading Matrix parameters
|
---|
116 |
|
---|
117 | Here are some examples of passing matrices to GLSL mat2, mat3, mat4 uniforms:
|
---|
118 |
|
---|
119 | @example
|
---|
120 | material exampleGLSLmatixUniforms
|
---|
121 | {
|
---|
122 | technique matrix_passing
|
---|
123 | {
|
---|
124 | pass examples
|
---|
125 | {
|
---|
126 | vertex_program_ref myVertexShader
|
---|
127 | {
|
---|
128 | // mat4 uniform
|
---|
129 | param_named OcclusionMatrix matrix4x4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
|
---|
130 | // or
|
---|
131 | param_named ViewMatrix float16 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
|
---|
132 |
|
---|
133 | // mat3
|
---|
134 | param_named TextRotMatrix float9 1 0 0 0 1 0 0 0 1
|
---|
135 | }
|
---|
136 |
|
---|
137 | fragment_program_ref myFragmentShader
|
---|
138 | {
|
---|
139 | // mat2 uniform
|
---|
140 | param_named skewMatrix float4 0.5 0 -0.5 1.0
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | @end example
|
---|
147 |
|
---|
148 | @subheading Accessing OpenGL states in GLSL
|
---|
149 | 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. @*@* |
---|