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",
23 "r", "vec4 transform_vertex(vec4);\n",
24 "lr", "vec3 transform_normal(vec3);\n",
27 "r", "\tvec4 eye_pos = transform_vertex(gl_Vertex);\n",
28 "!r", "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
29 0, "\tgl_Position = gl_ProjectionMatrix*eye_pos;\n",
30 "lr", "\tvec3 eye_normal = transform_normal(gl_Normal);\n",
31 "l!r", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
32 "l!n", "\tv_normal = eye_normal;\n",
33 "n", "\tvec3 eye_tangent = gl_NormalMatrix*tangent;\n",
34 "n", "\tvec3 eye_binormal = gl_NormalMatrix*binormal;\n",
35 "l", "\tvec3 eye_light_dir = normalize(gl_LightSource[0].position.xyz-eye_pos.xyz*gl_LightSource[0].position.w);\n",
36 "n", "\tv_light_dir = vec3(dot(eye_tangent, eye_light_dir), dot(eye_binormal, eye_light_dir), dot(eye_normal, eye_light_dir));\n",
37 "l!n", "\tv_light_dir = eye_light_dir;\n",
38 "p", "\tvec3 eye_dir = -normalize(eye_pos.xyz);\n",
39 "pn", "\tv_eye_dir = vec3(dot(eye_tangent, eye_dir), dot(eye_binormal, eye_dir), dot(eye_normal, eye_dir));\n",
40 "p!n", "\tv_eye_dir = eye_dir;\n",
41 "t|n", "\tv_texcoord = gl_MultiTexCoord0.xy;\n",
42 "s", "\tv_shadowcoord = vec3(dot(gl_EyePlaneS[3], eye_pos), dot(gl_EyePlaneT[3], eye_pos), dot(gl_EyePlaneR[3], eye_pos));\n",
43 "!lm", "\tv_color = gl_Color;\n",
48 const char *standard_fragment_src[] =
50 "t", "uniform sampler2D texture;\n",
51 "n", "uniform sampler2D normalmap;\n",
52 "s", "uniform sampler2DShadow shadow;\n",
53 "t|n", "varying vec2 v_texcoord;\n",
54 "s", "varying vec3 v_shadowcoord;\n",
55 "!lm", "varying vec4 v_color;\n",
56 "l!n", "varying vec3 v_normal;\n",
57 "l", "varying vec3 v_light_dir;\n",
58 "p", "varying vec3 v_eye_dir;\n",
61 "n", "\tvec3 n_normal = texture2D(normalmap, v_texcoord).xyz*2.0-1.0;\n",
62 "l!n", "\tvec3 n_normal = normalize(v_normal);\n",
63 "l", "\tfloat l_diffuse = max(dot(n_normal, normalize(v_light_dir)), 0.0);\n",
64 "p", "\tvec3 half_dir = normalize(v_eye_dir+v_light_dir);\n",
65 "p", "\tfloat l_specular = pow(max(dot(half_dir, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
66 "s", "\tfloat l_shadow = shadow2D(shadow, v_shadowcoord).r;\n",
67 0, "\tgl_FragColor = ",
68 "!t!l!m", "vec4(1.0, 1.0, 1.0, 1.0)",
69 "t", "texture2D(texture, v_texcoord)",
73 "lm", "*gl_FrontLightProduct[0].diffuse",
75 "pm", "*gl_FrontLightProduct[0].specular",
78 "lm", "+gl_FrontLightModelProduct.sceneColor",
96 Program::Program(const StandardFeatures &features):
101 add_standard_shaders(features);
105 Program::Program(const string &vert, const string &frag):
110 attach_shader(*new Shader(VERTEX_SHADER, vert));
111 attach_shader(*new Shader(FRAGMENT_SHADER, frag));
117 static RequireExtension _ext("GL_ARB_shader_objects");
120 id = glCreateProgramObjectARB();
127 for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
130 glDeleteObjectARB(id);
133 void Program::attach_shader(Shader &shader)
135 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
137 glAttachObjectARB(id, shader.get_id());
138 shaders.push_back(&shader);
142 void Program::detach_shader(Shader &shader)
144 list<Shader *>::iterator i = remove(shaders.begin(), shaders.end(), &shader);
147 shaders.erase(i, shaders.end());
148 glDetachObjectARB(id, shader.get_id());
152 void Program::add_standard_shaders(const StandardFeatures &features)
154 string flags = features.create_flags();
155 string vertex_src = process_standard_source(standard_vertex_src, flags);
156 string fragment_src = process_standard_source(standard_fragment_src, flags);
157 attach_shader(*new Shader(VERTEX_SHADER, vertex_src));
158 attach_shader(*new Shader(FRAGMENT_SHADER, fragment_src));
161 string Program::process_standard_source(const char **source, const string &flags)
165 for(unsigned i=0; source[i+1]; i+=2)
171 for(const char *c=source[i]; *c; ++c)
173 if(*c>='a' && *c<='z')
175 bool found = (flags.find(*c)!=string::npos);
177 cond = (cond || found);
179 cond = (cond && !found);
181 cond = (cond && found);
192 result += source[i+1];
198 void Program::set_del_shaders(bool ds)
203 void Program::bind_attribute(unsigned index, const string &name)
205 static RequireExtension _ext("GL_ARB_vertex_shader");
206 glBindAttribLocationARB(id, index, name.c_str());
211 for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
212 if(!(*i)->is_compiled())
217 glLinkProgramARB(id);
219 glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
220 if(!(linked = value))
221 throw compile_error(get_info_log());
223 glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
224 for(int i=0; i<value; ++i)
229 glGetActiveUniformARB(id, i, 128, &len, &info.size, &info.type, name);
233 info.location = glGetUniformLocationARB(id, name);
234 uniforms[name] = info;
239 string Program::get_info_log() const
242 glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
243 char *buf = new char[len+1];
244 glGetInfoLogARB(id, len+1, &len, buf);
245 string log(buf, len);
250 void Program::bind() const
253 throw invalid_operation("Program::bind");
255 if(!set_current(this))
258 glUseProgramObjectARB(id);
261 int Program::get_uniform_location(const string &n) const
263 map<string, UniformInfo>::const_iterator i = uniforms.find(n);
264 if(i==uniforms.end())
267 return i->second.location;
270 void Program::unbind()
275 glUseProgramObjectARB(0);
279 Program::StandardFeatures::StandardFeatures():
289 string Program::StandardFeatures::create_flags() const
313 Program::Loader::Loader(Program &p):
314 DataFile::ObjectLoader<Program>(p)
316 obj.set_del_shaders(true);
318 add("attribute", &Loader::attribute);
319 add("fragment_shader", &Loader::fragment_shader);
320 add("standard", &Loader::standard);
321 add("vertex_shader", &Loader::vertex_shader);
324 void Program::Loader::finish()
329 void Program::Loader::attribute(unsigned i, const string &n)
331 obj.bind_attribute(i, n);
334 void Program::Loader::fragment_shader(const string &src)
336 obj.attach_shader(*new Shader(FRAGMENT_SHADER, src));
339 void Program::Loader::standard()
341 StandardFeatures feat;
343 obj.add_standard_shaders(feat);
346 void Program::Loader::vertex_shader(const string &src)
348 obj.attach_shader(*new Shader(VERTEX_SHADER, src));
352 Program::StandardFeatures::Loader::Loader(StandardFeatures &f):
353 DataFile::ObjectLoader<StandardFeatures>(f)
355 add("lighting", &StandardFeatures::lighting);
356 add("material", &StandardFeatures::material);
357 add("normalmap", &StandardFeatures::normalmap);
358 add("shadow", &StandardFeatures::shadow);
359 add("specular", &StandardFeatures::specular);
360 add("texture", &StandardFeatures::texture);
361 add("transform", &StandardFeatures::transform);