CShaderFong.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Sonny Chan
40  \version 3.2.0 $Rev: 2015 $
41  */
42 //==============================================================================
43 
44 //---------------------------------------------------------------------------
45 #ifndef CShaderFong
46 #define CShaderFong
47 //---------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 //---------------------------------------------------------------------------
50 
51 //---------------------------------------------------------------------------
52 namespace chai3d {
53 //---------------------------------------------------------------------------
54 
55 const std::string C_SHADER_FONG_VERT =
56 " \n"
57 " attribute vec3 aPosition; \n"
58 " attribute vec3 aNormal; \n"
59 " attribute vec3 aTexCoord; \n"
60 " attribute vec4 aColor; \n"
61 " attribute vec3 aTangent; \n"
62 " attribute vec3 aBitangent; \n"
63 " \n"
64 " // vertex position and normal in eye coordinate space \n"
65 " varying vec4 vPosition; \n"
66 " varying vec3 vNormal; \n"
67 " \n"
68 " //---------------------------------------------------------------------- \n"
69 " // Main vertex shader code. \n"
70 " //---------------------------------------------------------------------- \n"
71 " \n"
72 " void main(void) \n"
73 " { \n"
74 " // pass along a transformed vertex position, normal, and texture \n"
75 " vPosition = gl_ModelViewMatrix * gl_Vertex; \n"
76 " vNormal = gl_NormalMatrix * aNormal; \n"
77 " gl_TexCoord[0] = gl_TextureMatrix[0] * vec4(aTexCoord, 1.0); \n"
78 " \n"
79 " // transform the vertex to get shadow map texture coordinates \n"
80 " float s = dot(gl_EyePlaneS[1], vPosition); \n"
81 " float t = dot(gl_EyePlaneT[1], vPosition); \n"
82 " float r = dot(gl_EyePlaneR[1], vPosition); \n"
83 " float q = dot(gl_EyePlaneQ[1], vPosition); \n"
84 " gl_TexCoord[1] = vec4(s, t, r, q); \n"
85 " \n"
86 " // fixed function vertex transform \n"
87 " gl_Position = ftransform(); \n"
88 " } \n"
89 " \n";
90 
91 const std::string C_SHADER_FONG_FRAG =
92 " \n"
93 "// interpolated vertex position in eye coordinate space (from vertex shader) \n"
94 " varying vec4 vPosition; \n"
95 " varying vec3 vNormal; \n"
96 " \n"
97 " // shadow (depth) map \n"
98 " uniform sampler2DShadow uShadowMap; \n"
99 " \n"
100 " //---------------------------------------------------------------------- \n"
101 " // Computes lighting power and attenuation (Section 2.14.1 of specification) \n"
102 " //---------------------------------------------------------------------- \n"
103 " \n"
104 " float attenuation(vec3 p, int i) \n"
105 " { \n"
106 " vec4 p_l = gl_LightSource[i].position; \n"
107 " if (p_l.w == 0.0) return 1.0; \n"
108 " \n"
109 " float d = distance(p, p_l.xyz); \n"
110 " float k0 = gl_LightSource[i].constantAttenuation; \n"
111 " float k1 = gl_LightSource[i].linearAttenuation; \n"
112 " float k2 = gl_LightSource[i].quadraticAttenuation; \n"
113 " \n"
114 " return 1.0 / (k0 + k1*d + k2*d*d); \n"
115 " } \n"
116 " \n"
117 " float spotlight(vec3 p, int i) \n"
118 " { \n"
119 " if (gl_LightSource[i].spotCosCutoff < 0.0) return 1.0; \n"
120 " \n"
121 " vec4 p_l = gl_LightSource[i].position; \n"
122 " if (p_l.w == 0.0) return 1.0; \n"
123 " \n"
124 " vec3 v = normalize(p - p_l.xyz); \n"
125 " vec3 s = normalize(gl_LightSource[i].spotDirection); \n"
126 " \n"
127 " float cosine = max(dot(v, s), 0.0); \n"
128 " if (cosine >= gl_LightSource[i].spotCosCutoff) \n"
129 " return pow(cosine, gl_LightSource[i].spotExponent); \n"
130 " else return 0.0; \n"
131 " } \n"
132 " \n"
133 " \n"
134 " //---------------------------------------------------------------------- \n"
135 " // Computes phong shading based on current light and material properties. \n"
136 " //---------------------------------------------------------------------- \n"
137 " \n"
138 " vec4 shade(vec3 p, vec3 v, vec3 n) \n"
139 " { \n"
140 " vec3 Ie = gl_FrontMaterial.emission.rgb; \n"
141 " vec3 Ia = gl_FrontLightModelProduct.sceneColor.rgb; \n"
142 " vec3 Il = vec3(0.0); \n"
143 " \n"
144 " for (int i = 0; i < gl_MaxLights; ++i) \n"
145 " { \n"
146 " vec4 p_l = gl_LightSource[i].position; \n"
147 " vec3 l = normalize(p_l.xyz - p * p_l.w); \n"
148 " vec3 h = normalize(l + v); \n"
149 " float s_m = gl_FrontMaterial.shininess; \n"
150 " \n"
151 " float cosNL = max(dot(n, l), 0.0); \n"
152 " float cosNH = max(dot(n, h), 0.0); \n"
153 " \n"
154 " vec3 phong = gl_FrontLightProduct[i].ambient.rgb \n"
155 " + cosNL * gl_FrontLightProduct[i].diffuse.rgb \n"
156 " + pow(cosNH, s_m) * gl_FrontLightProduct[i].specular.rgb; \n"
157 " Il += attenuation(p, i) * spotlight(p, i) * phong; \n"
158 " } \n"
159 " \n"
160 " float alpha = gl_FrontMaterial.diffuse.a; \n"
161 " return vec4(Ie + Ia + Il, alpha); \n"
162 " } \n"
163 " \n"
164 " \n"
165 " //---------------------------------------------------------------------- \n"
166 " // Main fragment shader code. \n"
167 " //---------------------------------------------------------------------- \n"
168 " \n"
169 " void main(void) \n"
170 " { \n"
171 " vec3 view = normalize(-vPosition.xyz); \n"
172 " vec3 normal = normalize(vNormal); \n"
173 " vec4 shadow = shadow2DProj(uShadowMap, gl_TexCoord[1]); \n"
174 " gl_FragColor = vec4(shade(vPosition.xyz, view, normal).rgb, shadow.a); \n"
175 " } \n";
176 
177 
178 //---------------------------------------------------------------------------
179 } // namespace chai3d
180 //---------------------------------------------------------------------------
181 
182 //---------------------------------------------------------------------------
183 #endif
184 //---------------------------------------------------------------------------
const std::string C_SHADER_FONG_FRAG
Definition: CShaderFong.h:91
const std::string C_SHADER_FONG_VERT
Definition: CShaderFong.h:55
Implements option settings for CHAI3D.
Definition: CAudioBuffer.cpp:56