| 1 |
// -*-C++-*- |
| 2 |
|
| 3 |
// Shader that uses OpenGL state values to do per-pixel lighting |
| 4 |
// |
| 5 |
// The only light used is gl_LightSource[0], which is assumed to be |
| 6 |
// directional. |
| 7 |
// |
| 8 |
// Diffuse colors come from the gl_Color, ambient from the material. This is |
| 9 |
// equivalent to osg::Material::DIFFUSE. |
| 10 |
#version 120 |
| 11 |
#extension GL_EXT_draw_instanced : enable |
| 12 |
#define MODE_OFF 0 |
| 13 |
#define MODE_DIFFUSE 1 |
| 14 |
#define MODE_AMBIENT_AND_DIFFUSE 2 |
| 15 |
|
| 16 |
attribute vec3 instancePosition; // (x,y,z) |
| 17 |
attribute vec3 instanceScale ; // (width, depth, height) |
| 18 |
attribute vec3 attrib1; // Generic packed attributes |
| 19 |
attribute vec3 attrib2; |
| 20 |
|
| 21 |
// The constant term of the lighting equation that doesn't depend on |
| 22 |
// the surface normal is passed in gl_{Front,Back}Color. The alpha |
| 23 |
// component is set to 1 for front, 0 for back in order to work around |
| 24 |
// bugs with gl_FrontFacing in the fragment shader. |
| 25 |
varying vec4 diffuse_term; |
| 26 |
varying vec3 normal; |
| 27 |
varying vec4 ecPosition; |
| 28 |
|
| 29 |
uniform int colorMode; |
| 30 |
|
| 31 |
const float EPSILON = 1e-7; |
| 32 |
|
| 33 |
////fog "include"//////// |
| 34 |
//uniform int fogType; |
| 35 |
// |
| 36 |
//void fog_Func(int type); |
| 37 |
///////////////////////// |
| 38 |
|
| 39 |
void setupShadows(vec4 eyeSpacePos); |
| 40 |
|
| 41 |
const float c_precision = 128.0; |
| 42 |
const float c_precisionp1 = c_precision + 1.0; |
| 43 |
|
| 44 |
vec3 float2vec(float value) { |
| 45 |
vec3 val; |
| 46 |
val.x = mod(value, c_precisionp1) / c_precision; |
| 47 |
val.y = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision; |
| 48 |
val.z = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision; |
| 49 |
return val; |
| 50 |
} |
| 51 |
|
| 52 |
void main() |
| 53 |
{ |
| 54 |
// Unpack generic attributes |
| 55 |
vec3 attr1 = float2vec(attrib1.x); |
| 56 |
vec3 attr2 = float2vec(attrib1.z); |
| 57 |
vec3 attr3 = float2vec(attrib2.x); |
| 58 |
|
| 59 |
// Determine the rotation for the building. |
| 60 |
float sr = sin(6.28 * attr1.x); |
| 61 |
float cr = cos(6.28 * attr1.x); |
| 62 |
|
| 63 |
vec3 position = gl_Vertex.xyz; |
| 64 |
// Adjust the very top of the roof to match the rooftop scaling. This shapes |
| 65 |
// the rooftop - gambled, gabled etc. These vertices are identified by gl_Color.z |
| 66 |
position.x = (1.0 - gl_Color.z) * position.x + gl_Color.z * ((position.x + 0.5) * attr3.z - 0.5); |
| 67 |
position.y = (1.0 - gl_Color.z) * position.y + gl_Color.z * (position.y * attrib2.y ); |
| 68 |
|
| 69 |
// Adjust pitch of roof to the correct height. These vertices are identified by gl_Color.z |
| 70 |
// Scale down by the building height (instanceScale.z) because |
| 71 |
// immediately afterwards we will scale UP the vertex to the correct scale. |
| 72 |
position.z = position.z + gl_Color.z * attrib1.y / instanceScale.z; |
| 73 |
position = position * instanceScale.xyz; |
| 74 |
|
| 75 |
// Rotation of the building and movement into position |
| 76 |
position.xy = vec2(dot(position.xy, vec2(cr, sr)), dot(position.xy, vec2(-sr, cr))); |
| 77 |
position = position + instancePosition.xyz; |
| 78 |
|
| 79 |
gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0); |
| 80 |
|
| 81 |
// Texture coordinates are stored as: |
| 82 |
// - a separate offset (x0, y0) for the wall (wtex0x, wtex0y), and roof (rtex0x, rtex0y) |
| 83 |
// - a semi-shared (x1, y1) so that the front and side of the building can have |
| 84 |
// different texture mappings |
| 85 |
// |
| 86 |
// The vertex color value selects between them: |
| 87 |
// gl_Color.x=1 indicates front/back walls |
| 88 |
// gl_Color.y=1 indicates roof |
| 89 |
// gl_Color.z=1 indicates top roof vertexs (used above) |
| 90 |
// gl_Color.a=1 indicates sides |
| 91 |
// Finally, the roof texture is on the right of the texture sheet |
| 92 |
float wtex0x = attr1.y; // Front/Side texture X0 |
| 93 |
float wtex0y = attr1.z; // Front/Side texture Y0 |
| 94 |
float rtex0x = attr2.z; // Roof texture X0 |
| 95 |
float rtex0y = attr3.x; // Roof texture Y0 |
| 96 |
float wtex1x = attr2.x; // Front/Roof texture X1 |
| 97 |
float stex1x = attr3.y; // Side texture X1 |
| 98 |
float wtex1y = attr2.y; // Front/Roof/Side texture Y1 |
| 99 |
|
| 100 |
// We use the sign of the x texture coordinate to offset things later |
| 101 |
// - roof texture coordinates are negative (0 -> -1) |
| 102 |
// - wall texture coordinates are positive (0 -> 1) |
| 103 |
// We want to nudge mtcx is the appropriate direction so that all of the |
| 104 |
// offsets are applied in the correct direction |
| 105 |
float mtcx = gl_MultiTexCoord0.x - sign(gl_Color.y - EPSILON) * EPSILON; |
| 106 |
|
| 107 |
// Adjust the top texture coordinates to match roof shape |
| 108 |
float is_roof_top_vertex = gl_Color.z; |
| 109 |
float is_roof_bottom_vertex = 1.0 - is_roof_top_vertex; |
| 110 |
float rooftop_scale_x = attr3.z; |
| 111 |
float rooftop_scale_y = attrib2.y; |
| 112 |
|
| 113 |
// Front/Back rooftop scaling differs from Left/Right scaling |
| 114 |
float is_front_or_back = max(sign(EPSILON - abs(gl_Normal.y)), 0.0); // abs(gl_Normal.y) < EPSILON; |
| 115 |
|
| 116 |
mtcx = is_front_or_back * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_y - 0.5)) + |
| 117 |
(1.0 - is_front_or_back) * (is_roof_bottom_vertex * (mtcx) + is_roof_top_vertex * ((mtcx + 0.5) * rooftop_scale_x - 0.5)); |
| 118 |
|
| 119 |
vec2 tex0 = vec2(sign(mtcx) * (gl_Color.x*wtex0x + gl_Color.y*rtex0x + gl_Color.a*wtex0x), |
| 120 |
gl_Color.x*wtex0y + gl_Color.y*rtex0y + gl_Color.a*wtex0y); |
| 121 |
|
| 122 |
vec2 tex1 = vec2( |
| 123 |
// x-offsets |
| 124 |
gl_Color.x*wtex1x + // main body front/back faces |
| 125 |
gl_Color.a*stex1x + // main body left/right faces |
| 126 |
gl_Color.y*( |
| 127 |
is_front_or_back * wtex1x + // roof front/back faces |
| 128 |
(1.0f - is_front_or_back) * stex1x // roof left/right faces |
| 129 |
), |
| 130 |
// y-offsets |
| 131 |
wtex1y |
| 132 |
); |
| 133 |
|
| 134 |
gl_TexCoord[0].x = tex0.x + mtcx * tex1.x; |
| 135 |
gl_TexCoord[0].y = tex0.y + gl_MultiTexCoord0.y * tex1.y; |
| 136 |
|
| 137 |
// Rotate the normal. |
| 138 |
normal = gl_Normal; |
| 139 |
normal.xy = vec2(dot(normal.xy, vec2(cr, sr)), dot(normal.xy, vec2(-sr, cr))); |
| 140 |
normal = gl_NormalMatrix * normal; |
| 141 |
|
| 142 |
vec4 ambient_color, diffuse_color; |
| 143 |
if (colorMode == MODE_DIFFUSE) { |
| 144 |
diffuse_color = vec4(1.0,1.0,1.0,1.0); |
| 145 |
ambient_color = gl_FrontMaterial.ambient; |
| 146 |
} else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) { |
| 147 |
diffuse_color = vec4(1.0,1.0,1.0,1.0); |
| 148 |
ambient_color = vec4(1.0,1.0,1.0,1.0); |
| 149 |
} else { |
| 150 |
diffuse_color = gl_FrontMaterial.diffuse; |
| 151 |
ambient_color = gl_FrontMaterial.ambient; |
| 152 |
} |
| 153 |
|
| 154 |
diffuse_term = diffuse_color * gl_LightSource[0].diffuse; |
| 155 |
vec4 constant_term = gl_FrontMaterial.emission + ambient_color * |
| 156 |
(gl_LightModel.ambient + gl_LightSource[0].ambient); |
| 157 |
// Super hack: if diffuse material alpha is less than 1, assume a |
| 158 |
// transparency animation is at work |
| 159 |
if (gl_FrontMaterial.diffuse.a < 1.0) |
| 160 |
diffuse_term.a = gl_FrontMaterial.diffuse.a; |
| 161 |
else |
| 162 |
diffuse_term.a = 1.0; |
| 163 |
// Another hack for supporting two-sided lighting without using |
| 164 |
// gl_FrontFacing in the fragment shader. |
| 165 |
gl_FrontColor.rgb = constant_term.rgb; gl_FrontColor.a = 1.0; |
| 166 |
gl_BackColor.rgb = constant_term.rgb; gl_BackColor.a = 0.0; |
| 167 |
//fogCoord = abs(ecPosition.z / ecPosition.w); |
| 168 |
//fog_Func(fogType); |
| 169 |
ecPosition = gl_ModelViewMatrix * vec4(position, 1.0); |
| 170 |
setupShadows(ecPosition); |
| 171 |
} |