| 1 |
// -*-C++-*- |
| 2 |
#version 120 |
| 3 |
|
| 4 |
#extension GL_EXT_draw_instanced : enable |
| 5 |
|
| 6 |
// Based on object-instancing-lowspec.vert, with minor additions for instance color |
| 7 |
|
| 8 |
#define MODE_OFF 0 |
| 9 |
#define MODE_DIFFUSE 1 |
| 10 |
#define MODE_AMBIENT_AND_DIFFUSE 2 |
| 11 |
|
| 12 |
attribute vec3 instance_position; // (x,y,z) |
| 13 |
attribute vec4 instance_rotation_and_scale; // (heading, pitch, roll, scale) |
| 14 |
attribute vec4 instance_custom_attrib; // (r,g,b,a) |
| 15 |
|
| 16 |
varying vec4 diffuse_term; |
| 17 |
varying vec3 normal; |
| 18 |
varying vec4 ecPosition; |
| 19 |
|
| 20 |
varying vec4 instanceColor; |
| 21 |
|
| 22 |
uniform int colorMode; |
| 23 |
|
| 24 |
void setupShadows(vec4 eyeSpacePos); |
| 25 |
void apply_instance_transforms(inout vec3 position, inout vec3 normal, in vec3 instance_position, in vec4 instance_rotation_and_scale); |
| 26 |
|
| 27 |
void main() |
| 28 |
{ |
| 29 |
vec3 position = gl_Vertex.xyz; |
| 30 |
apply_instance_transforms(position, normal, instance_position, instance_rotation_and_scale); |
| 31 |
|
| 32 |
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, gl_Vertex.w); |
| 33 |
|
| 34 |
// Pass instance color to fragment shader from custom attribute |
| 35 |
instanceColor = instance_custom_attrib; |
| 36 |
|
| 37 |
/************* The following is copied from default.vert *************/ |
| 38 |
ecPosition = gl_ModelViewMatrix * gl_Vertex; |
| 39 |
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |
| 40 |
gl_TexCoord[2] = gl_TextureMatrix[2] * gl_MultiTexCoord2; |
| 41 |
|
| 42 |
vec4 ambient_color, diffuse_color; |
| 43 |
if (colorMode == MODE_DIFFUSE) { |
| 44 |
diffuse_color = gl_Color; |
| 45 |
ambient_color = gl_FrontMaterial.ambient; |
| 46 |
} else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) { |
| 47 |
diffuse_color = gl_Color; |
| 48 |
ambient_color = gl_Color; |
| 49 |
} else { |
| 50 |
diffuse_color = gl_FrontMaterial.diffuse; |
| 51 |
ambient_color = gl_FrontMaterial.ambient; |
| 52 |
} |
| 53 |
diffuse_term = diffuse_color * gl_LightSource[0].diffuse; |
| 54 |
vec4 constant_term = gl_FrontMaterial.emission + ambient_color * |
| 55 |
(gl_LightModel.ambient + gl_LightSource[0].ambient); |
| 56 |
// Super hack: if diffuse material alpha is less than 1, assume a |
| 57 |
// transparency animation is at work |
| 58 |
if (gl_FrontMaterial.diffuse.a < 1.0) |
| 59 |
diffuse_term.a = gl_FrontMaterial.diffuse.a; |
| 60 |
else |
| 61 |
diffuse_term.a = gl_Color.a; |
| 62 |
// Another hack for supporting two-sided lighting without using |
| 63 |
// gl_FrontFacing in the fragment shader. |
| 64 |
gl_FrontColor.rgb = constant_term.rgb; gl_FrontColor.a = 1.0; |
| 65 |
gl_BackColor.rgb = constant_term.rgb; gl_BackColor.a = 0.0; |
| 66 |
|
| 67 |
setupShadows(ecPosition); |
| 68 |
} |