| 1 |
// -*- mode: C; -*- |
| 2 |
// Licence: GPL v2 |
| 3 |
// © Emilian Huminiuc and Vivian Meazza 2011 |
| 4 |
#version 120 |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
// the file is directly copied from model-combined.eff except the (three) WINGFLEX to END WINGLFEX parts |
| 9 |
// and the subsequent gl_Vertex integrations |
| 10 |
// I didnt find how to do a more beautiful way (including model-combined and only adding the wingflexer part) |
| 11 |
// if you know how to do this, please do :) |
| 12 |
|
| 13 |
varying vec3 rawpos; |
| 14 |
varying vec3 VNormal; |
| 15 |
varying vec3 VTangent; |
| 16 |
varying vec3 VBinormal; |
| 17 |
varying vec3 vViewVec; |
| 18 |
varying vec3 reflVec; |
| 19 |
varying vec3 vertVec; |
| 20 |
|
| 21 |
varying float alpha; |
| 22 |
|
| 23 |
attribute vec3 tangent; |
| 24 |
attribute vec3 binormal; |
| 25 |
|
| 26 |
uniform float pitch; |
| 27 |
uniform float roll; |
| 28 |
uniform float hdg; |
| 29 |
uniform int refl_dynamic; |
| 30 |
uniform int nmap_enabled; |
| 31 |
uniform int shader_qual; |
| 32 |
|
| 33 |
// WINGFLEX |
| 34 |
uniform int wingflex_type; |
| 35 |
uniform float body_width; |
| 36 |
uniform float wingflex_alpha; |
| 37 |
uniform float wingflex_trailing_alpha; |
| 38 |
uniform float wingsweep_factor; |
| 39 |
uniform float wingflex_z; |
| 40 |
uniform float wing_span; |
| 41 |
uniform float rotation_x1; |
| 42 |
uniform float rotation_y1; |
| 43 |
uniform float rotation_z1; |
| 44 |
uniform float rotation_x2; |
| 45 |
uniform float rotation_y2; |
| 46 |
uniform float rotation_z2; |
| 47 |
uniform float rotation_rad; |
| 48 |
// END WINGFLEX |
| 49 |
|
| 50 |
//////Fog Include/////////// |
| 51 |
// uniform int fogType; |
| 52 |
// void fog_Func(int type); |
| 53 |
//////////////////////////// |
| 54 |
|
| 55 |
// WINGFLEX |
| 56 |
vec2 calc_deflection(float y){ |
| 57 |
float distance; |
| 58 |
float bwh = body_width/2; |
| 59 |
if(y < bwh && y > -bwh){ |
| 60 |
//this part does not move |
| 61 |
distance = 0; |
| 62 |
}else if(y > bwh){ |
| 63 |
distance = y - bwh; |
| 64 |
}else if(y < -bwh){ |
| 65 |
distance = y + bwh; |
| 66 |
} |
| 67 |
float max_dist = (wing_span-body_width)/2; |
| 68 |
float deflection = wingflex_z * (distance*distance)/(max_dist*max_dist); |
| 69 |
float delta_y; |
| 70 |
if(y<0){ |
| 71 |
delta_y = deflection/wing_span; |
| 72 |
}else{ |
| 73 |
delta_y = -deflection/wing_span; |
| 74 |
} |
| 75 |
vec2 returned = vec2 ( deflection, delta_y ); |
| 76 |
return returned; |
| 77 |
} |
| 78 |
// END WINGFLEX |
| 79 |
|
| 80 |
void setupShadows(vec4 eyeSpacePos); |
| 81 |
|
| 82 |
void rotationMatrixPR(in float sinRx, in float cosRx, in float sinRy, in float cosRy, out mat4 rotmat) |
| 83 |
{ |
| 84 |
rotmat = mat4( cosRy , sinRx * sinRy , cosRx * sinRy, 0.0, |
| 85 |
0.0 , cosRx , -sinRx * cosRx, 0.0, |
| 86 |
-sinRy, sinRx * cosRy, cosRx * cosRy , 0.0, |
| 87 |
0.0 , 0.0 , 0.0 , 1.0 ); |
| 88 |
} |
| 89 |
|
| 90 |
void rotationMatrixH(in float sinRz, in float cosRz, out mat4 rotmat) |
| 91 |
{ |
| 92 |
rotmat = mat4( cosRz, -sinRz, 0.0, 0.0, |
| 93 |
sinRz, cosRz, 0.0, 0.0, |
| 94 |
0.0 , 0.0 , 1.0, 0.0, |
| 95 |
0.0 , 0.0 , 0.0, 1.0 ); |
| 96 |
} |
| 97 |
|
| 98 |
void main(void) |
| 99 |
{ |
| 100 |
// WINGFLEX |
| 101 |
vec4 vertex = gl_Vertex; |
| 102 |
|
| 103 |
if ( wingflex_type == 0 ) { |
| 104 |
vec2 deflection = calc_deflection(vertex.y); |
| 105 |
|
| 106 |
vertex.z += deflection[0]; |
| 107 |
vertex.y += deflection[1]; |
| 108 |
|
| 109 |
if(rotation_rad != 0){ |
| 110 |
vec2 defl1=calc_deflection(rotation_y1); |
| 111 |
vec2 defl2=calc_deflection(rotation_y2); |
| 112 |
float rot_y1 = rotation_y1; |
| 113 |
float rot_z1 = rotation_z1; |
| 114 |
float rot_y2 = rotation_y2; |
| 115 |
float rot_z2 = rotation_z2; |
| 116 |
rot_y1 -= defl1[1]; |
| 117 |
rot_z1 += defl1[0]; |
| 118 |
rot_y2 -= defl2[1]; |
| 119 |
rot_z2 += defl2[0]; |
| 120 |
//Calculate rotation |
| 121 |
vec3 normal; |
| 122 |
normal[0]=rotation_x2-rotation_x1; |
| 123 |
normal[1]=rot_y2-rot_y1; |
| 124 |
normal[2]=rot_z2-rot_z1; |
| 125 |
normal = normalize(normal); |
| 126 |
float tmp = (1-cos(rotation_rad)); |
| 127 |
mat4 rotation_matrix = mat4( |
| 128 |
pow(normal[0],2)*tmp+cos(rotation_rad), normal[1]*normal[0]*tmp-normal[2]*sin(rotation_rad), normal[2]*normal[0]*tmp+normal[1]*sin(rotation_rad), 0.0, |
| 129 |
normal[0]*normal[1]*tmp+normal[2]*sin(rotation_rad), pow(normal[1],2)*tmp+cos(rotation_rad), normal[2]*normal[1]*tmp-normal[0]*sin(rotation_rad), 0.0, |
| 130 |
normal[0]*normal[2]*tmp-normal[1]*sin(rotation_rad), normal[1]*normal[2]*tmp+normal[0]*sin(rotation_rad), pow(normal[2],2)*tmp+cos(rotation_rad), 0.0, |
| 131 |
0.0, 0.0, 0.0, 1.0 |
| 132 |
); |
| 133 |
vec4 old_point; |
| 134 |
old_point[0]=vertex.x; |
| 135 |
old_point[1]=vertex.y; |
| 136 |
old_point[2]=vertex.z; |
| 137 |
old_point[3]=1.0; |
| 138 |
rotation_matrix[3][0] = rotation_x1 - rotation_x1*rotation_matrix[0][0] - rot_y1*rotation_matrix[1][0] - rot_z1*rotation_matrix[2][0]; |
| 139 |
rotation_matrix[3][1] = rot_y1 - rotation_x1*rotation_matrix[0][1] - rot_y1*rotation_matrix[1][1] - rot_z1*rotation_matrix[2][1]; |
| 140 |
rotation_matrix[3][2] = rot_z1 - rotation_x1*rotation_matrix[0][2] - rot_y1*rotation_matrix[1][2] - rot_z1*rotation_matrix[2][2]; |
| 141 |
vec4 new_point=rotation_matrix*old_point; |
| 142 |
vertex.x=new_point[0]; |
| 143 |
vertex.y=new_point[1]; |
| 144 |
vertex.z=new_point[2]; |
| 145 |
} |
| 146 |
|
| 147 |
} else if (wingflex_type == 1 ) { |
| 148 |
float arm_reach = 4.8; |
| 149 |
|
| 150 |
float x_factor = max((abs(vertex.x) - body_width),0); |
| 151 |
float y_factor = max(vertex.y,0.0); |
| 152 |
float flex_factor1 = wingflex_alpha * (1.0 - wingsweep_factor); |
| 153 |
float flex_factor2 = wingflex_trailing_alpha * (1.0 -wingsweep_factor); |
| 154 |
|
| 155 |
|
| 156 |
if (flex_factor1<0.0) {flex_factor1 *=0.7;} |
| 157 |
if (flex_factor2<0.0) {flex_factor1 *=0.7;} |
| 158 |
|
| 159 |
// basic flapping motion is linear to arm_reach, then parabolic |
| 160 |
|
| 161 |
float intercept_point = 0.1 * arm_reach * arm_reach * flex_factor1; |
| 162 |
|
| 163 |
if (x_factor < arm_reach) |
| 164 |
{ |
| 165 |
vertex.z += x_factor/arm_reach * intercept_point; |
| 166 |
} |
| 167 |
|
| 168 |
else |
| 169 |
{ |
| 170 |
vertex.z += 0.1 * x_factor * x_factor * flex_factor1; |
| 171 |
} |
| 172 |
|
| 173 |
// upward stroke is slightly forward-swept, downward stroke a bit backward |
| 174 |
vertex.y += -0.25 * abs(x_factor) * flex_factor1; |
| 175 |
|
| 176 |
//trailing edge lags the motion |
| 177 |
vertex.z += 0.2 * y_factor * x_factor * flex_factor2; |
| 178 |
|
| 179 |
|
| 180 |
// if the wings are folded, we sweep them back |
| 181 |
vertex.y += 0.5 * x_factor * wingsweep_factor; |
| 182 |
float sweep_x = 0.5; |
| 183 |
if (vertex.x > 0.0) {sweep_x = - 0.5;} |
| 184 |
|
| 185 |
vertex.x+= sweep_x * (1.0 + 0.5 *x_factor) * wingsweep_factor; |
| 186 |
|
| 187 |
|
| 188 |
} |
| 189 |
// END WINGFLEX |
| 190 |
|
| 191 |
rawpos = vertex.xyz; |
| 192 |
vec4 ecPosition = gl_ModelViewMatrix * vertex; |
| 193 |
//rawpos = gl_Vertex.xyz; |
| 194 |
//vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex; |
| 195 |
//fog_Func(fogType); |
| 196 |
|
| 197 |
VNormal = normalize(gl_NormalMatrix * gl_Normal); |
| 198 |
|
| 199 |
vec3 n = normalize(gl_Normal); |
| 200 |
vec3 tempTangent = cross(n, vec3(1.0,0.0,0.0)); |
| 201 |
vec3 tempBinormal = cross(n, tempTangent); |
| 202 |
|
| 203 |
if (nmap_enabled > 0){ |
| 204 |
tempTangent = tangent; |
| 205 |
tempBinormal = binormal; |
| 206 |
} |
| 207 |
|
| 208 |
VTangent = normalize(gl_NormalMatrix * tempTangent); |
| 209 |
VBinormal = normalize(gl_NormalMatrix * tempBinormal); |
| 210 |
vec3 t = tempTangent; |
| 211 |
vec3 b = tempBinormal; |
| 212 |
|
| 213 |
// Super hack: if diffuse material alpha is less than 1, assume a |
| 214 |
// transparency animation is at work |
| 215 |
if (gl_FrontMaterial.diffuse.a < 1.0) |
| 216 |
alpha = gl_FrontMaterial.diffuse.a; |
| 217 |
else |
| 218 |
alpha = gl_Color.a; |
| 219 |
|
| 220 |
// Vertex in eye coordinates |
| 221 |
vertVec = ecPosition.xyz; |
| 222 |
vViewVec.x = dot(t, vertVec); |
| 223 |
vViewVec.y = dot(b, vertVec); |
| 224 |
vViewVec.z = dot(n, vertVec); |
| 225 |
|
| 226 |
// calculate the reflection vector |
| 227 |
vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0); |
| 228 |
vec3 reflVec_stat = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz; |
| 229 |
if (refl_dynamic > 0){ |
| 230 |
//prepare rotation matrix |
| 231 |
mat4 RotMatPR; |
| 232 |
mat4 RotMatH; |
| 233 |
float _roll = roll; |
| 234 |
if (_roll>90.0 || _roll < -90.0) |
| 235 |
{ |
| 236 |
_roll = -_roll; |
| 237 |
} |
| 238 |
float cosRx = cos(radians(_roll)); |
| 239 |
float sinRx = sin(radians(_roll)); |
| 240 |
float cosRy = cos(radians(-pitch)); |
| 241 |
float sinRy = sin(radians(-pitch)); |
| 242 |
float cosRz = cos(radians(hdg)); |
| 243 |
float sinRz = sin(radians(hdg)); |
| 244 |
rotationMatrixPR(sinRx, cosRx, sinRy, cosRy, RotMatPR); |
| 245 |
rotationMatrixH(sinRz, cosRz, RotMatH); |
| 246 |
vec3 reflVec_dyn = (RotMatH * (RotMatPR * normalize(gl_ModelViewMatrixInverse * reflect_eye))).xyz; |
| 247 |
|
| 248 |
reflVec = reflVec_dyn; |
| 249 |
} else { |
| 250 |
reflVec = reflVec_stat; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
gl_FrontColor = gl_FrontMaterial.emission + gl_Color |
| 255 |
* (gl_LightModel.ambient + gl_LightSource[0].ambient); |
| 256 |
|
| 257 |
gl_Position = gl_ModelViewProjectionMatrix * vertex; |
| 258 |
//gl_Position = ftransform(); |
| 259 |
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |
| 260 |
|
| 261 |
|
| 262 |
setupShadows(ecPosition); |
| 263 |
} |