]> git.tdb.fi Git - libs/gl.git/commitdiff
System for generating standard shaders based on flags
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Apr 2011 12:42:49 +0000 (12:42 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Apr 2011 12:42:49 +0000 (12:42 +0000)
source/program.cpp
source/program.h

index 1f28e9565e19905e1770bce0de872621b25225f7..eb9481f2426ae7908f2a875aa9efa07ab7caed3e 100644 (file)
@@ -15,6 +15,78 @@ Distributed under the LGPL
 
 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 {
 
@@ -24,6 +96,15 @@ Program::Program():
        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)
 {
@@ -71,6 +152,52 @@ void Program::detach_shader(Shader &shader)
        }
 }
 
+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;
@@ -133,19 +260,56 @@ void Program::unbind()
 }
 
 
+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)
@@ -153,14 +317,28 @@ 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
index 63d43e0f800f65b9ac58b7df6017c6bf7b51588f..1e41e84423f8680ba787a83d8ecea5c45d1bc7c7 100644 (file)
@@ -21,12 +21,6 @@ class Shader;
 
 class Program: public Bindable<Program>
 {
-private:
-       unsigned id;
-       std::list<Shader *> shaders;
-       bool del_shaders;
-       bool linked;
-
 public:
        class Loader: public DataFile::ObjectLoader<Program>
        {
@@ -34,13 +28,43 @@ public:
                Loader(Program &);
 
        private:
-               void vertex_shader(const std::string &);
-               void fragment_shader(const std::string &);
-               void attribute(unsigned, const std::string &);
                virtual void finish();
+
+               void attribute(unsigned, const std::string &);
+               void fragment_shader(const std::string &);
+               void standard();
+               void vertex_shader(const std::string &);
+       };
+
+       struct StandardFeatures
+       {
+               class Loader: public DataFile::ObjectLoader<StandardFeatures>
+               {
+               public:
+                       Loader(StandardFeatures &);
+               };
+
+               bool texture;
+               bool material;
+               bool lighting;
+               bool specular;
+               bool normalmap;
+               bool shadow;
+
+               StandardFeatures();
+
+               std::string create_flags() const;
        };
 
+private:
+       unsigned id;
+       std::list<Shader *> shaders;
+       bool del_shaders;
+       bool linked;
+
+public:
        Program();
+       Program(const StandardFeatures &);
        Program(const std::string &, const std::string &);
 private:
        void init();
@@ -49,6 +73,10 @@ public:
 
        void attach_shader(Shader &shader);
        void detach_shader(Shader &shader);
+       void add_standard_shaders(const StandardFeatures &);
+private:
+       static std::string process_standard_source(const char **, const std::string &);
+public:
        const std::list<Shader *> &get_shaders() const { return shaders; }
        void set_del_shaders(bool);
        void bind_attribute(unsigned, const std::string &);