Improve memory and instantiation time of random buildings.
[fg:toms-fgdata.git] / Shaders / building-ubershader.vert
1 // -*- mode: C; -*-
2 // Licence: GPL v2
3 // © Emilian Huminiuc and Vivian Meazza 2011
4 #version 120
5
6 varying vec3    rawpos;
7 varying vec3    VNormal;
8 varying vec3    VTangent;
9 varying vec3    VBinormal;
10 varying vec3    vViewVec;
11 varying vec3    reflVec;
12
13 varying float   alpha;
14
15 attribute       vec3    tangent;
16 attribute       vec3    binormal;
17
18 uniform float           pitch;
19 uniform float           roll;
20 uniform float           hdg;
21 uniform int             refl_dynamic;
22 uniform int             nmap_enabled;
23 uniform int             shader_qual;
24 uniform int                     rembrandt_enabled;
25 uniform int     color_is_position;
26
27 //////Fog Include///////////
28 // uniform      int     fogType;
29 // void fog_Func(int type);
30 ////////////////////////////
31
32 void    rotationMatrixPR(in float sinRx, in float cosRx, in float sinRy, in float cosRy, out mat4 rotmat)
33 {
34         rotmat = mat4(  cosRy , sinRx * sinRy , cosRx * sinRy,  0.0,
35                                                                         0.0   , cosRx        ,  -sinRx * cosRx, 0.0,
36                                                                         -sinRy, sinRx * cosRy,  cosRx * cosRy , 0.0,
37                                                                         0.0   , 0.0          ,  0.0           , 1.0 );
38 }
39
40 void    rotationMatrixH(in float sinRz, in float cosRz, out mat4 rotmat)
41 {
42         rotmat = mat4(  cosRz,  -sinRz, 0.0,    0.0,
43                                                                         sinRz,  cosRz,  0.0,    0.0,
44                                                                         0.0  ,  0.0  ,  1.0,    0.0,
45                                                                         0.0  ,  0.0  ,  0.0,    1.0 );
46 }
47
48 void    main(void)
49 {
50     float sr = sin(6.28 * gl_Color.a);
51     float cr = cos(6.28 * gl_Color.a);    
52     rawpos = gl_Vertex.xyz;
53   
54     // Rotation of the object and movement into position
55     rawpos.xy = vec2(dot(rawpos.xy, vec2(cr, sr)), dot(rawpos.xy, vec2(-sr, cr)));
56     rawpos = rawpos + gl_Color.xyz;    
57     
58                 vec4 ecPosition = gl_ModelViewMatrix * vec4(rawpos.x, rawpos.y, rawpos.z, 1.0);
59                 //fog_Func(fogType);
60
61     // Rotate the normal.
62     vec3 normal = gl_Normal;
63     normal.xy = vec2(dot(normal.xy, vec2(cr, sr)), dot(normal.xy, vec2(-sr, cr)));
64     normal = gl_NormalMatrix * normal;
65
66                 VNormal = normalize(normal);
67                 if (nmap_enabled > 0 && shader_qual > 2){
68                   VTangent = normalize(gl_NormalMatrix * tangent);
69                   VBinormal = normalize(gl_NormalMatrix * binormal);
70                 } else {
71                         VTangent = vec3(0.0);
72                         VBinormal = vec3 (0.0);
73                 }
74                 vec3 n = normalize(normal);
75                 vec3 t = cross(n, vec3(1.0,0.0,0.0));
76                 vec3 b = cross(n,t);
77
78                 // Super hack: if diffuse material alpha is less than 1, assume a
79                 // transparency animation is at work
80                 if (gl_FrontMaterial.diffuse.a < 1.0)
81                         alpha = gl_FrontMaterial.diffuse.a;
82                 else
83                         alpha = 1.0;
84
85     // Vertex in eye coordinates
86                 vec3 vertVec = ecPosition.xyz;
87                 vViewVec.x = dot(t, vertVec);
88                 vViewVec.y = dot(b, vertVec);
89                 vViewVec.z = dot(n, vertVec);
90
91                 // calculate the reflection vector
92                 vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0);
93                 vec3 reflVec_stat = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz;
94                 if (refl_dynamic > 0){
95                         //prepare rotation matrix
96                         mat4 RotMatPR;
97                         mat4 RotMatH;
98                         float _roll = roll;
99                         if (_roll>90.0 || _roll < -90.0)
100                         {
101                                 _roll = -_roll;
102                         }
103                         float cosRx = cos(radians(_roll));
104                         float sinRx = sin(radians(_roll));
105                         float cosRy = cos(radians(-pitch));
106                         float sinRy = sin(radians(-pitch));
107                         float cosRz = cos(radians(hdg));
108                         float sinRz = sin(radians(hdg));
109                         rotationMatrixPR(sinRx, cosRx, sinRy, cosRy, RotMatPR);
110                         rotationMatrixH(sinRz, cosRz, RotMatH);
111                         vec3 reflVec_dyn = (RotMatH * (RotMatPR * normalize(gl_ModelViewMatrixInverse * reflect_eye))).xyz;
112
113                         reflVec = reflVec_dyn;
114                 } else {
115                         reflVec = reflVec_stat;
116                 }
117
118                 if(rembrandt_enabled < 1){
119                 gl_FrontColor = gl_FrontMaterial.emission + vec4(1.0,1.0,1.0,1.0)
120                                           * (gl_LightModel.ambient + gl_LightSource[0].ambient);
121                 } else {
122                   gl_FrontColor = vec4(1.0,1.0,1.0,1.0);
123                 }
124                 gl_Position  = gl_ModelViewProjectionMatrix * vec4(rawpos,1.0);
125                 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
126 }