using namespace std;
+namespace {
+
+const char *standard_vertex_src[] =
+{
+ "n", "attribute vec3 tangent;\n",
+ "n", "attribute vec3 binormal;\n",
+ "t|n", "varying vec2 v_texcoord;\n",
+ "s", "varying vec3 v_shadowcoord;\n",
+ "!lm", "varying vec4 v_color;\n",
+ "l!n", "varying vec3 v_normal;\n",
+ "l", "varying vec3 v_light_dir;\n",
+ "p", "varying vec3 v_eye_dir;\n",
+ 0, "void main()\n",
+ 0, "{\n",
+ 0, "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
+ 0, "\tgl_Position = gl_ProjectionMatrix*eye_pos;\n",
+ "l", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
+ "l!n", "\tv_normal = eye_normal;\n",
+ "n", "\tvec3 eye_tangent = gl_NormalMatrix*tangent;\n",
+ "n", "\tvec3 eye_binormal = gl_NormalMatrix*binormal;\n",
+ "l", "\tvec3 eye_light_dir = normalize(gl_LightSource[0].position.xyz-eye_pos.xyz*gl_LightSource[0].position.w);\n",
+ "n", "\tv_light_dir = vec3(dot(eye_tangent, eye_light_dir), dot(eye_binormal, eye_light_dir), dot(eye_normal, eye_light_dir));\n",
+ "l!n", "\tv_light_dir = eye_light_dir;\n",
+ "p", "\tvec3 eye_dir = -normalize(eye_pos.xyz);\n",
+ "pn", "\tv_eye_dir = vec3(dot(eye_tangent, eye_dir), dot(eye_binormal, eye_dir), dot(eye_normal, eye_dir));\n",
+ "p!n", "\tv_eye_dir = eye_dir;\n",
+ "t|n", "\tv_texcoord = gl_MultiTexCoord0.xy;\n",
+ "s", "\tv_shadowcoord = vec3(dot(gl_EyePlaneS[3], eye_pos), dot(gl_EyePlaneT[3], eye_pos), dot(gl_EyePlaneR[3], eye_pos));\n",
+ "!lm", "\tv_color = gl_Color;\n",
+ 0, "}",
+ 0, 0
+};
+
+const char *standard_fragment_src[] =
+{
+ "t", "uniform sampler2D texture;\n",
+ "n", "uniform sampler2D normalmap;\n",
+ "s", "uniform sampler2DShadow shadow;\n",
+ "t|n", "varying vec2 v_texcoord;\n",
+ "s", "varying vec3 v_shadowcoord;\n",
+ "!lm", "varying vec4 v_color;\n",
+ "l!n", "varying vec3 v_normal;\n",
+ "l", "varying vec3 v_light_dir;\n",
+ "p", "varying vec3 v_eye_dir;\n",
+ 0, "void main()\n",
+ 0, "{\n",
+ "n", "\tvec3 n_normal = texture2D(normalmap, v_texcoord).xyz*2.0-1.0;\n",
+ "l!n", "\tvec3 n_normal = normalize(v_normal);\n",
+ "l", "\tfloat l_diffuse = max(dot(n_normal, normalize(v_light_dir)), 0.0);\n",
+ "p", "\tvec3 half = normalize(v_eye_dir+v_light_dir);\n",
+ "p", "\tfloat l_specular = pow(max(dot(half, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
+ "s", "\tfloat l_shadow = shadow2D(shadow, v_shadowcoord).r;\n",
+ 0, "\tgl_FragColor = ",
+ "!t!l!m", "vec4(1.0, 1.0, 1.0, 1.0)",
+ "t", "texture2D(texture, v_texcoord)",
+ "l|mt", "*",
+ "!lm", "v_color",
+ "l", "((l_diffuse",
+ "lm", "*gl_FrontLightProduct[0].diffuse",
+ "p", "+l_specular",
+ "pm", "*gl_FrontLightProduct[0].specular",
+ "l", ")",
+ "s", "*l_shadow",
+ "lm", "+gl_FrontLightModelProduct.sceneColor",
+ "l", ")",
+ 0, ";\n",
+ 0, "}\n",
+ 0, 0
+};
+
+}
+
namespace Msp {
namespace GL {
init();
}
+Program::Program(const StandardFeatures &features):
+ del_shaders(true)
+{
+ init();
+
+ add_standard_shaders(features);
+ link();
+}
+
Program::Program(const string &vert, const string &frag):
del_shaders(true)
{
}
}
+void Program::add_standard_shaders(const StandardFeatures &features)
+{
+ string flags = features.create_flags();
+ string vertex_src = process_standard_source(standard_vertex_src, flags);
+ string fragment_src = process_standard_source(standard_fragment_src, flags);
+ attach_shader(*new Shader(VERTEX_SHADER, vertex_src));
+ attach_shader(*new Shader(FRAGMENT_SHADER, fragment_src));
+}
+
+string Program::process_standard_source(const char **source, const string &flags)
+{
+ string result;
+
+ for(unsigned i=0; source[i+1]; i+=2)
+ {
+ if(source[i])
+ {
+ bool cond = true;
+ char oper = '&';
+ for(const char *c=source[i]; *c; ++c)
+ {
+ if(*c>='a' && *c<='z')
+ {
+ bool found = (flags.find(*c)!=string::npos);
+ if(oper=='|')
+ cond = (cond || found);
+ else if(oper=='!')
+ cond = (cond && !found);
+ else if(oper=='&')
+ cond = (cond && found);
+ oper = '&';
+ }
+ else
+ oper = *c;
+ }
+
+ if(!cond)
+ continue;
+ }
+
+ result += source[i+1];
+ }
+
+ return result;
+}
+
void Program::set_del_shaders(bool ds)
{
del_shaders = ds;
}
+Program::StandardFeatures::StandardFeatures():
+ texture(false),
+ material(false),
+ lighting(false),
+ specular(false),
+ normalmap(false),
+ shadow(false)
+{ }
+
+string Program::StandardFeatures::create_flags() const
+{
+ string flags;
+ if(texture)
+ flags += 't';
+ if(material)
+ flags += 'm';
+ if(lighting)
+ {
+ flags += 'l';
+ if(specular)
+ flags += 'p';
+ if(normalmap)
+ flags += 'n';
+ }
+ if(shadow)
+ flags += 's';
+
+ return flags;
+}
+
+
Program::Loader::Loader(Program &p):
DataFile::ObjectLoader<Program>(p)
{
obj.set_del_shaders(true);
- add("vertex_shader", &Loader::vertex_shader);
- add("fragment_shader", &Loader::fragment_shader);
add("attribute", &Loader::attribute);
+ add("fragment_shader", &Loader::fragment_shader);
+ add("standard", &Loader::standard);
+ add("vertex_shader", &Loader::vertex_shader);
}
-void Program::Loader::vertex_shader(const string &src)
+void Program::Loader::finish()
{
- obj.attach_shader(*new Shader(VERTEX_SHADER, src));
+ obj.link();
+}
+
+void Program::Loader::attribute(unsigned i, const string &n)
+{
+ obj.bind_attribute(i, n);
}
void Program::Loader::fragment_shader(const string &src)
obj.attach_shader(*new Shader(FRAGMENT_SHADER, src));
}
-void Program::Loader::attribute(unsigned i, const string &n)
+void Program::Loader::standard()
{
- obj.bind_attribute(i, n);
+ StandardFeatures feat;
+ load_sub(feat);
+ obj.add_standard_shaders(feat);
}
-void Program::Loader::finish()
+void Program::Loader::vertex_shader(const string &src)
{
- obj.link();
+ obj.attach_shader(*new Shader(VERTEX_SHADER, src));
+}
+
+
+Program::StandardFeatures::Loader::Loader(StandardFeatures &f):
+ DataFile::ObjectLoader<StandardFeatures>(f)
+{
+ add("lighting", &StandardFeatures::lighting);
+ add("material", &StandardFeatures::material);
+ add("normalmap", &StandardFeatures::normalmap);
+ add("shadow", &StandardFeatures::shadow);
+ add("specular", &StandardFeatures::specular);
+ add("texture", &StandardFeatures::texture);
}
} // namespace GL