Debian Codegraph @

Shaders/building-model-ALS-ultra.vert

raw
1 // -*- mode: C; -*-
2 // Licence: GPL v2
3 // © Emilian Huminiuc and Vivian Meazza 2011
4 #version 120
5
6 attribute vec3 instancePosition; // (x,y,z)
7 attribute vec3 instanceScale ; // (width, depth, height)
8 attribute vec3 attrib1; // Generic packed attributes
9 attribute vec3 attrib2;
10
11 varying vec3 rawpos;
12 varying vec3 VNormal;
13 varying vec3 VTangent;
14 varying vec3 VBinormal;
15 varying vec3 vViewVec;
16 varying vec3 vertVec;
17 varying vec3 reflVec;
18
19 varying float alpha;
20
21 attribute vec3 tangent;
22 attribute vec3 binormal;
23
24 uniform float pitch;
25 uniform float roll;
26 uniform float hdg;
27 uniform int refl_dynamic;
28 uniform int nmap_enabled;
29 uniform int shader_qual;
30 uniform int color_is_position;
31
32 const float EPSILON = 1e-7;
33
34 //////Fog Include///////////
35 // uniform int fogType;
36 // void fog_Func(int type);
37 ////////////////////////////
38
39 void setupShadows(vec4 eyeSpacePos);
40
41 void rotationMatrixPR(in float sinRx, in float cosRx, in float sinRy, in float cosRy, out mat4 rotmat)
42 {
43 rotmat = mat4( cosRy , sinRx * sinRy , cosRx * sinRy, 0.0,
44 0.0 , cosRx , -sinRx * cosRx, 0.0,
45 -sinRy, sinRx * cosRy, cosRx * cosRy , 0.0,
46 0.0 , 0.0 , 0.0 , 1.0 );
47 }
48
49 void rotationMatrixH(in float sinRz, in float cosRz, out mat4 rotmat)
50 {
51 rotmat = mat4( cosRz, -sinRz, 0.0, 0.0,
52 sinRz, cosRz, 0.0, 0.0,
53 0.0 , 0.0 , 1.0, 0.0,
54 0.0 , 0.0 , 0.0, 1.0 );
55 }
56
57 const float c_precision = 128.0;
58 const float c_precisionp1 = c_precision + 1.0;
59
60 vec3 float2vec(float value) {
61 vec3 val;
62 val.x = mod(value, c_precisionp1) / c_precision;
63 val.y = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision;
64 val.z = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision;
65 return val;
66 }
67
68 void main(void)
69 {
70 // Unpack generic attributes
71 vec3 attr1 = float2vec(attrib1.x);
72 vec3 attr2 = float2vec(attrib1.z);
73 vec3 attr3 = float2vec(attrib2.x);
74
75 // Determine the rotation for the building.
76 float sr = sin(6.28 * attr1.x);
77 float cr = cos(6.28 * attr1.x);
78
79 rawpos = gl_Vertex.xyz;
80 // Adjust the very top of the roof to match the rooftop scaling. This shapes
81 // the rooftop - gambled, gabled etc. These vertices are identified by gl_Color.z
82 rawpos.x = (1.0 - gl_Color.z) * rawpos.x + gl_Color.z * ((rawpos.x + 0.5) * attr3.z - 0.5);
83 rawpos.y = (1.0 - gl_Color.z) * rawpos.y + gl_Color.z * (rawpos.y * attrib2.y );
84
85 // Adjust pitch of roof to the correct height. These vertices are identified by gl_Color.z
86 // Scale down by the building height (instanceScale.z) because
87 // immediately afterwards we will scale UP the vertex to the correct scale.
88 rawpos.z = rawpos.z + gl_Color.z * attrib1.y / instanceScale.z;
89 rawpos = rawpos * instanceScale.xyz;
90
91 // Rotation of the building and movement into rawpos
92 rawpos.xy = vec2(dot(rawpos.xy, vec2(cr, sr)), dot(rawpos.xy, vec2(-sr, cr)));
93 rawpos = rawpos + instancePosition.xyz;
94 vec4 ecPosition = gl_ModelViewMatrix * vec4(rawpos, 1.0);
95
96 // Texture coordinates are stored as:
97 // - a separate offset (x0, y0) for the wall (wtex0x, wtex0y), and roof (rtex0x, rtex0y)
98 // - a semi-shared (x1, y1) so that the front and side of the building can have
99 // different texture mappings
100 //
101 // The vertex color value selects between them:
102 // gl_Color.x=1 indicates front/back walls
103 // gl_Color.y=1 indicates roof
104 // gl_Color.z=1 indicates top roof vertexs (used above)
105 // gl_Color.a=1 indicates sides
106 // Finally, the roof texture is on the right of the texture sheet
107 float wtex0x = attr1.y; // Front/Side texture X0
108 float wtex0y = attr1.z; // Front/Side texture Y0
109 float rtex0x = attr2.z; // Roof texture X0
110 float rtex0y = attr3.x; // Roof texture Y0
111 float wtex1x = attr2.x; // Front/Roof texture X1
112 float stex1x = attr3.y; // Side texture X1
113 float wtex1y = attr2.y; // Front/Roof/Side texture Y1
114
115 // We use the sign of the x texture coordinate to offset things later
116 // - roof texture coordinates are negative (0 -> -1)
117 // - wall texture coordinates are positive (0 -> 1)
118 // We want to nudge mtcx is the appropriate direction so that all of the
119 // offsets are applied in the correct direction
120 float mtcx = gl_MultiTexCoord0.x - sign(gl_Color.y - EPSILON) * EPSILON;
121
122 // Adjust the top texture coordinates to match roof shape
123 float is_roof_top_vertex = gl_Color.z;
124 float is_roof_bottom_vertex = 1.0 - is_roof_top_vertex;
125 float rooftop_scale_x = attr3.z;
126 float rooftop_scale_y = attrib2.y;
127
128 // Front/Back rooftop scaling differs from Left/Right scaling
129 float is_front_or_back = max(sign(EPSILON - abs(gl_Normal.y)), 0.0); // abs(gl_Normal.y) < EPSILON;
130
131 mtcx = is_front_or_back * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_y - 0.5)) +
132 (1.0 - is_front_or_back) * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_x - 0.5));
133
134 vec2 tex0 = vec2(sign(mtcx) * (gl_Color.x*wtex0x + gl_Color.y*rtex0x + gl_Color.a*wtex0x),
135 gl_Color.x*wtex0y + gl_Color.y*rtex0y + gl_Color.a*wtex0y);
136
137 vec2 tex1 = vec2(
138 // x-offsets
139 gl_Color.x*wtex1x + // main body front/back faces
140 gl_Color.a*stex1x + // main body left/right faces
141 gl_Color.y*(
142 is_front_or_back * wtex1x + // roof front/back faces
143 (1.0f - is_front_or_back) * stex1x // roof left/right faces
144 ),
145 // y-offsets
146 wtex1y
147 );
148
149 gl_TexCoord[0].x = tex0.x + mtcx * tex1.x;
150 gl_TexCoord[0].y = tex0.y + gl_MultiTexCoord0.y * tex1.y;
151
152 // Rotate the normal.
153 vec3 normal = gl_Normal;
154 // Rotate the normal as per the building.
155 normal.xy = vec2(dot(normal.xy, vec2(cr, sr)), dot(normal.xy, vec2(-sr, cr)));
156
157 VNormal = normalize(gl_NormalMatrix * normal);
158 vec3 n = normalize(normal);
159 vec3 tempTangent = cross(n, vec3(1.0,0.0,0.0));
160 vec3 tempBinormal = cross(n, tempTangent);
161
162 if (nmap_enabled > 0){
163 tempTangent = tangent;
164 tempBinormal = binormal;
165 }
166
167 VTangent = normalize(gl_NormalMatrix * tempTangent);
168 VBinormal = normalize(gl_NormalMatrix * tempBinormal);
169 vec3 t = tempTangent;
170 vec3 b = tempBinormal;
171
172 // Super hack: if diffuse material alpha is less than 1, assume a
173 // transparency animation is at work
174 if (gl_FrontMaterial.diffuse.a < 1.0)
175 alpha = gl_FrontMaterial.diffuse.a;
176 else
177 alpha = 1.0;
178
179 // Vertex in eye coordinates
180 vertVec = ecPosition.xyz;
181 vViewVec.x = dot(t, vertVec);
182 vViewVec.y = dot(b, vertVec);
183 vViewVec.z = dot(n, vertVec);
184
185 // calculate the reflection vector
186 vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0);
187 vec3 reflVec_stat = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz;
188 if (refl_dynamic > 0){
189 //prepare rotation matrix
190 mat4 RotMatPR;
191 mat4 RotMatH;
192 float _roll = roll;
193 if (_roll>90.0 || _roll < -90.0)
194 {
195 _roll = -_roll;
196 }
197 float cosRx = cos(radians(_roll));
198 float sinRx = sin(radians(_roll));
199 float cosRy = cos(radians(-pitch));
200 float sinRy = sin(radians(-pitch));
201 float cosRz = cos(radians(hdg));
202 float sinRz = sin(radians(hdg));
203 rotationMatrixPR(sinRx, cosRx, sinRy, cosRy, RotMatPR);
204 rotationMatrixH(sinRz, cosRz, RotMatH);
205 vec3 reflVec_dyn = (RotMatH * (RotMatPR * normalize(gl_ModelViewMatrixInverse * reflect_eye))).xyz;
206
207 reflVec = reflVec_dyn;
208 } else {
209 reflVec = reflVec_stat;
210 }
211
212 gl_FrontColor = gl_FrontMaterial.emission + vec4(1.0,1.0,1.0,1.0)
213 * (gl_LightModel.ambient + gl_LightSource[0].ambient);
214
215 gl_Position = gl_ModelViewProjectionMatrix * vec4(rawpos,1.0);
216
217 setupShadows(ecPosition);
218 }