ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/shader.C
(Generate patch)

Comparing libgender/shader.C (file contents):
Revision 1.2 by root, Sat Oct 23 21:47:02 2004 UTC vs.
Revision 1.3 by root, Sat Oct 23 21:53:06 2004 UTC

221 printf ("INFOLOG<%s>\n", infolog); 221 printf ("INFOLOG<%s>\n", infolog);
222 abort (); 222 abort ();
223 } 223 }
224 } 224 }
225 225
226 template<typename T>
227 struct sl_append
228 {
229 T t;
230
231 sl_append (const T &t) : t(t) { }
232
233 void operator ()() const
234 {
235 cur->push_back (t);
236 }
237 };
238
239 template<int length>
240 struct sl_string
241 {
242 char str[length];
243
244 void operator ()() const
245 {
246 cur->push_back (*new fragment_string_i (str));
247 }
248 };
249
250 // only floats
251 struct sl_float
252 {
253 const GLfloat c;
254
255 sl_float (GLfloat c) : c(c) { }
256
257 void operator ()() const
258 {
259 char s[64];
260 sprintf (s, "%g", c);
261 cur->append_string (s);
262 }
263 };
264
265 template<class A, class B>
266 inline sl_expr< sl_concat2<A, B> >
267 concat (const A &a, const B &b)
268 {
269 typedef sl_concat2<A, B> expr;
270 return sl_expr<expr> (expr (a, b));
271 }
272
273 template<class A, class B, class C>
274 inline sl_expr< sl_concat3<A, B, C> >
275 concat (const A &a, const B &b, const C &c)
276 {
277 typedef sl_concat3<A, B, C> expr;
278 return sl_expr<expr> (expr (a, b, c));
279 }
280
281 template<class A, class B, class C, class D>
282 inline sl_expr< sl_concat4<A, B, C, D> >
283 concat (const A &a, const B &b, const C &c, const D &d)
284 {
285 typedef sl_concat4<A, B, C, D> expr;
286 return sl_expr<expr> (expr (a, b, c, d));
287 }
288
289 template<typename expr>
290 struct sl_convert
291 {
292 typedef sl_expr<expr> T;
293 static inline const T &convert (const T &e)
294 {
295 return e;
296 }
297 };
298
299 template<>
300 struct sl_convert<GLfloat>
301 {
302 typedef sl_expr<sl_float> T;
303 static inline const T convert (GLfloat f)
304 {
305 return sl_float (f);
306 }
307 };
308
309 template<>
310 struct sl_convert<vec3>
311 {
312 typedef sl_expr< sl_string<256> > T;
313 static inline const T convert (const vec3 &v)
314 {
315 sl_string<256> s;
316 sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z);
317 return s;
318 }
319 };
320
321 template<>
322 struct sl_convert<vec4>
323 {
324 typedef sl_expr< sl_string<256> > T;
325 static inline const T convert (const vec4 &v)
326 {
327 sl_string<256> s;
328 sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w);
329 return s;
330 }
331 };
332
333 template<>
334 template<class V>
335 struct sl_convert< var_ref<V> >
336 {
337 typedef sl_expr< sl_append< var_ref<V> > > T;
338 static inline const T convert (const var_ref<V> &v)
339 {
340 return sl_append< var_ref<V> > (v);
341 }
342 };
343
344 template<>
345 struct sl_convert<glvar>
346 {
347 typedef sl_expr< sl_append<glvar> > T;
348 static inline const T convert (const glvar &v)
349 {
350 return sl_append<glvar> (v);
351 }
352 };
353
354 template<>
355 template<const char *strtype>
356 struct sl_convert< temp_ref<strtype> >
357 {
358 typedef sl_expr< sl_append< temp_ref<strtype> > > T;
359 static inline const T convert (const temp_ref<strtype> &v)
360 {
361 return sl_expr< sl_append< temp_ref<strtype> > >(
362 sl_append< temp_ref<strtype> > (v)
363 );
364 }
365 };
366
367 template<class fragment, typename expr>
368 inline void sl_assign (const fragment &f, const expr &e)
369 {
370 cur->append_const (" ");
371 cur->append (f);
372 cur->append_const (" = ");
373 sl_convert<expr>::convert (e) ();
374 cur->append_const (";\n");
375 }
376
377 template<class type>
378 template<typename expr>
379 inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const
380 {
381 sl_assign (*this, e);
382 return *this;
383 }
384
385 template<class type, typename arg1>
386 template<typename expr>
387 inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const
388 {
389 sl_assign (*this, e);
390 return *this;
391 }
392
393 template<const char *strtype>
394 template<typename expr>
395 inline const temp_ref<strtype> &temp_ref<strtype>::operator =(const expr &e) const
396 {
397 sl_assign (*this, e);
398 return *this;
399 }
400
401 struct sl_append_const_string
402 {
403 fragment_const_string str;
404 sl_append_const_string (const char *s)
405 : str (s)
406 { }
407
408 void operator ()() const
409 {
410 cur->push_back (str);
411 }
412 };
413
414# define SHADER_BINOP(op, str) \
415 extern const sl_append_const_string str_ ## str; \
416 template<typename A, typename B> \
417 inline const sl_expr< sl_concat3< typename sl_convert<A>::T, \
418 sl_append_const_string, \
419 typename sl_convert<B>::T > > \
420 operator op(const A &a, const B &b) \
421 { \
422 return concat (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
423 }
424
425 const sl_append_const_string str_plus (" + "); 226 const sl_append_const_string str_plus (" + ");
426 const sl_append_const_string str_minus (" - "); 227 const sl_append_const_string str_minus (" - ");
427 const sl_append_const_string str_mul (" * "); 228 const sl_append_const_string str_mul (" * ");
428 const sl_append_const_string str_div (" / "); 229 const sl_append_const_string str_div (" / ");
429 230
430 SHADER_BINOP (+, plus);
431 SHADER_BINOP (-, minus);
432 SHADER_BINOP (*, mul);
433 SHADER_BINOP (/, div);
434
435# undef SHADER_BINOP
436
437 const sl_append_const_string str_rpar (")"); 231 const sl_append_const_string str_rpar (")");
438 232
439 void debdebdebdebug ()//D 233 void debdebdebdebug ()//D
440 { 234 {
441 vertex_shader vsh; 235 vertex_shader vsh;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines