2 #include "arb_shader_objects.h"
3 #include "arb_vertex_shader.h"
13 const char *standard_vertex_src[] =
15 "n", "attribute vec3 tangent;\n",
16 "n", "attribute vec3 binormal;\n",
17 "t|n", "varying vec2 v_texcoord;\n",
18 "s", "varying vec3 v_shadowcoord;\n",
19 "!lm", "varying vec4 v_color;\n",
20 "l!n", "varying vec3 v_normal;\n",
21 "l", "varying vec3 v_light_dir;\n",
22 "p", "varying vec3 v_eye_dir;\n",
25 0, "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
26 0, "\tgl_Position = gl_ProjectionMatrix*eye_pos;\n",
27 "l", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
28 "l!n", "\tv_normal = eye_normal;\n",
29 "n", "\tvec3 eye_tangent = gl_NormalMatrix*tangent;\n",
30 "n", "\tvec3 eye_binormal = gl_NormalMatrix*binormal;\n",
31 "l", "\tvec3 eye_light_dir = normalize(gl_LightSource[0].position.xyz-eye_pos.xyz*gl_LightSource[0].position.w);\n",
32 "n", "\tv_light_dir = vec3(dot(eye_tangent, eye_light_dir), dot(eye_binormal, eye_light_dir), dot(eye_normal, eye_light_dir));\n",
33 "l!n", "\tv_light_dir = eye_light_dir;\n",
34 "p", "\tvec3 eye_dir = -normalize(eye_pos.xyz);\n",
35 "pn", "\tv_eye_dir = vec3(dot(eye_tangent, eye_dir), dot(eye_binormal, eye_dir), dot(eye_normal, eye_dir));\n",
36 "p!n", "\tv_eye_dir = eye_dir;\n",
37 "t|n", "\tv_texcoord = gl_MultiTexCoord0.xy;\n",
38 "s", "\tv_shadowcoord = vec3(dot(gl_EyePlaneS[3], eye_pos), dot(gl_EyePlaneT[3], eye_pos), dot(gl_EyePlaneR[3], eye_pos));\n",
39 "!lm", "\tv_color = gl_Color;\n",
44 const char *standard_fragment_src[] =
46 "t", "uniform sampler2D texture;\n",
47 "n", "uniform sampler2D normalmap;\n",
48 "s", "uniform sampler2DShadow shadow;\n",
49 "t|n", "varying vec2 v_texcoord;\n",
50 "s", "varying vec3 v_shadowcoord;\n",
51 "!lm", "varying vec4 v_color;\n",
52 "l!n", "varying vec3 v_normal;\n",
53 "l", "varying vec3 v_light_dir;\n",
54 "p", "varying vec3 v_eye_dir;\n",
57 "n", "\tvec3 n_normal = texture2D(normalmap, v_texcoord).xyz*2.0-1.0;\n",
58 "l!n", "\tvec3 n_normal = normalize(v_normal);\n",
59 "l", "\tfloat l_diffuse = max(dot(n_normal, normalize(v_light_dir)), 0.0);\n",
60 "p", "\tvec3 half = normalize(v_eye_dir+v_light_dir);\n",
61 "p", "\tfloat l_specular = pow(max(dot(half, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
62 "s", "\tfloat l_shadow = shadow2D(shadow, v_shadowcoord).r;\n",
63 0, "\tgl_FragColor = ",
64 "!t!l!m", "vec4(1.0, 1.0, 1.0, 1.0)",
65 "t", "texture2D(texture, v_texcoord)",
69 "lm", "*gl_FrontLightProduct[0].diffuse",
71 "pm", "*gl_FrontLightProduct[0].specular",
74 "lm", "+gl_FrontLightModelProduct.sceneColor",
92 Program::Program(const StandardFeatures &features):
97 add_standard_shaders(features);
101 Program::Program(const string &vert, const string &frag):
106 attach_shader(*new Shader(VERTEX_SHADER, vert));
107 attach_shader(*new Shader(FRAGMENT_SHADER, frag));
113 static RequireExtension _ext("GL_ARB_shader_objects");
116 id = glCreateProgramObjectARB();
123 for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
126 glDeleteObjectARB(id);
129 void Program::attach_shader(Shader &shader)
131 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
133 glAttachObjectARB(id, shader.get_id());
134 shaders.push_back(&shader);
138 void Program::detach_shader(Shader &shader)
140 list<Shader *>::iterator i = remove(shaders.begin(), shaders.end(), &shader);
143 shaders.erase(i, shaders.end());
144 glDetachObjectARB(id, shader.get_id());
148 void Program::add_standard_shaders(const StandardFeatures &features)
150 string flags = features.create_flags();
151 string vertex_src = process_standard_source(standard_vertex_src, flags);
152 string fragment_src = process_standard_source(standard_fragment_src, flags);
153 attach_shader(*new Shader(VERTEX_SHADER, vertex_src));
154 attach_shader(*new Shader(FRAGMENT_SHADER, fragment_src));
157 string Program::process_standard_source(const char **source, const string &flags)
161 for(unsigned i=0; source[i+1]; i+=2)
167 for(const char *c=source[i]; *c; ++c)
169 if(*c>='a' && *c<='z')
171 bool found = (flags.find(*c)!=string::npos);
173 cond = (cond || found);
175 cond = (cond && !found);
177 cond = (cond && found);
188 result += source[i+1];
194 void Program::set_del_shaders(bool ds)
199 void Program::bind_attribute(unsigned index, const string &name)
201 static RequireExtension _ext("GL_ARB_vertex_shader");
202 glBindAttribLocationARB(id, index, name.c_str());
207 for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
208 if(!(*i)->is_compiled())
213 glLinkProgramARB(id);
215 glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
216 if(!(linked = value))
217 throw compile_error(get_info_log());
219 glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
220 for(int i=0; i<value; ++i)
225 glGetActiveUniformARB(id, i, 128, &len, &info.size, &info.type, name);
229 info.location = glGetUniformLocationARB(id, name);
230 uniforms[name] = info;
235 string Program::get_info_log() const
238 glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
240 glGetInfoLogARB(id, len+1, &len, log);
241 return string(log, len);
244 void Program::bind() const
247 throw invalid_operation("Program::bind");
249 if(!set_current(this))
252 glUseProgramObjectARB(id);
255 int Program::get_uniform_location(const string &n) const
257 map<string, UniformInfo>::const_iterator i = uniforms.find(n);
258 if(i==uniforms.end())
261 return i->second.location;
264 void Program::unbind()
269 glUseProgramObjectARB(0);
273 Program::StandardFeatures::StandardFeatures():
282 string Program::StandardFeatures::create_flags() const
304 Program::Loader::Loader(Program &p):
305 DataFile::ObjectLoader<Program>(p)
307 obj.set_del_shaders(true);
309 add("attribute", &Loader::attribute);
310 add("fragment_shader", &Loader::fragment_shader);
311 add("standard", &Loader::standard);
312 add("vertex_shader", &Loader::vertex_shader);
315 void Program::Loader::finish()
320 void Program::Loader::attribute(unsigned i, const string &n)
322 obj.bind_attribute(i, n);
325 void Program::Loader::fragment_shader(const string &src)
327 obj.attach_shader(*new Shader(FRAGMENT_SHADER, src));
330 void Program::Loader::standard()
332 StandardFeatures feat;
334 obj.add_standard_shaders(feat);
337 void Program::Loader::vertex_shader(const string &src)
339 obj.attach_shader(*new Shader(VERTEX_SHADER, src));
343 Program::StandardFeatures::Loader::Loader(StandardFeatures &f):
344 DataFile::ObjectLoader<StandardFeatures>(f)
346 add("lighting", &StandardFeatures::lighting);
347 add("material", &StandardFeatures::material);
348 add("normalmap", &StandardFeatures::normalmap);
349 add("shadow", &StandardFeatures::shadow);
350 add("specular", &StandardFeatures::specular);
351 add("texture", &StandardFeatures::texture);