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

Comparing libgender/shader.h (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

555 }; 555 };
556 556
557 typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader; 557 typedef shader_object<GL_VERTEX_SHADER_ARB> vertex_shader;
558 typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader; 558 typedef shader_object<GL_FRAGMENT_SHADER_ARB> fragment_shader;
559 559
560 template<typename T>
561 struct sl_append
562 {
563 T t;
564
565 sl_append (const T &t) : t(t) { }
566
567 void operator ()() const
568 {
569 cur->push_back (t);
570 }
571 };
572
573 template<int length>
574 struct sl_string
575 {
576 char str[length];
577
578 void operator ()() const
579 {
580 cur->push_back (*new fragment_string_i (str));
581 }
582 };
583
584 // only floats
585 struct sl_float
586 {
587 const GLfloat c;
588
589 sl_float (GLfloat c) : c(c) { }
590
591 void operator ()() const
592 {
593 char s[64];
594 sprintf (s, "%g", c);
595 cur->append_string (s);
596 }
597 };
598
599 template<class A, class B>
600 inline sl_expr< sl_concat2<A, B> >
601 concat (const A &a, const B &b)
602 {
603 typedef sl_concat2<A, B> expr;
604 return sl_expr<expr> (expr (a, b));
605 }
606
607 template<class A, class B, class C>
608 inline sl_expr< sl_concat3<A, B, C> >
609 concat (const A &a, const B &b, const C &c)
610 {
611 typedef sl_concat3<A, B, C> expr;
612 return sl_expr<expr> (expr (a, b, c));
613 }
614
615 template<class A, class B, class C, class D>
616 inline sl_expr< sl_concat4<A, B, C, D> >
617 concat (const A &a, const B &b, const C &c, const D &d)
618 {
619 typedef sl_concat4<A, B, C, D> expr;
620 return sl_expr<expr> (expr (a, b, c, d));
621 }
622
623 template<typename expr>
624 struct sl_convert
625 {
626 typedef sl_expr<expr> T;
627 static inline const T &convert (const T &e)
628 {
629 return e;
630 }
631 };
632
633 template<>
634 struct sl_convert<GLfloat>
635 {
636 typedef sl_expr<sl_float> T;
637 static inline const T convert (GLfloat f)
638 {
639 return sl_float (f);
640 }
641 };
642
643 template<>
644 struct sl_convert<vec3>
645 {
646 typedef sl_expr< sl_string<256> > T;
647 static inline const T convert (const vec3 &v)
648 {
649 sl_string<256> s;
650 sprintf (s.str, "vec3 (%g, %g, %g)", v.x, v.y, v.z);
651 return s;
652 }
653 };
654
655 template<>
656 struct sl_convert<vec4>
657 {
658 typedef sl_expr< sl_string<256> > T;
659 static inline const T convert (const vec4 &v)
660 {
661 sl_string<256> s;
662 sprintf (s.str, "vec4 (%g, %g, %g, %g)", v.x, v.y, v.z, v.w);
663 return s;
664 }
665 };
666
667 template<>
668 template<class V>
669 struct sl_convert< var_ref<V> >
670 {
671 typedef sl_expr< sl_append< var_ref<V> > > T;
672 static inline const T convert (const var_ref<V> &v)
673 {
674 return sl_append< var_ref<V> > (v);
675 }
676 };
677
678 template<>
679 struct sl_convert<glvar>
680 {
681 typedef sl_expr< sl_append<glvar> > T;
682 static inline const T convert (const glvar &v)
683 {
684 return sl_append<glvar> (v);
685 }
686 };
687
688 template<>
689 template<const char *strtype>
690 struct sl_convert< temp_ref<strtype> >
691 {
692 typedef sl_expr< sl_append< temp_ref<strtype> > > T;
693 static inline const T convert (const temp_ref<strtype> &v)
694 {
695 return sl_expr< sl_append< temp_ref<strtype> > >(
696 sl_append< temp_ref<strtype> > (v)
697 );
698 }
699 };
700
701 template<class fragment, typename expr>
702 inline void sl_assign (const fragment &f, const expr &e)
703 {
704 cur->append_const (" ");
705 cur->append (f);
706 cur->append_const (" = ");
707 sl_convert<expr>::convert (e) ();
708 cur->append_const (";\n");
709 }
710
711 template<class type>
712 template<typename expr>
713 inline const auto_lvalue_ref0<type> &auto_lvalue_ref0<type>::operator =(const expr &e) const
714 {
715 sl_assign (*this, e);
716 return *this;
717 }
718
719 template<class type, typename arg1>
720 template<typename expr>
721 inline const auto_lvalue_ref1<type,arg1> &auto_lvalue_ref1<type,arg1>::operator =(const expr &e) const
722 {
723 sl_assign (*this, e);
724 return *this;
725 }
726
727 template<const char *strtype>
728 template<typename expr>
729 inline const temp_ref<strtype> &temp_ref<strtype>::operator =(const expr &e) const
730 {
731 sl_assign (*this, e);
732 return *this;
733 }
734
735 struct sl_append_const_string
736 {
737 fragment_const_string str;
738 sl_append_const_string (const char *s)
739 : str (s)
740 { }
741
742 void operator ()() const
743 {
744 cur->push_back (str);
745 }
746 };
747
748# define SHADER_BINOP(op, str) \
749 extern const sl_append_const_string str_ ## str; \
750 template<typename A, typename B> \
751 inline const sl_expr< sl_concat3< typename sl_convert<A>::T, \
752 sl_append_const_string, \
753 typename sl_convert<B>::T > > \
754 operator op(const A &a, const B &b) \
755 { \
756 return concat (sl_convert<A>::convert (a), str_ ## str, sl_convert<B>::convert (b)); \
757 }
758
759 SHADER_BINOP (+, plus);
760 SHADER_BINOP (-, minus);
761 SHADER_BINOP (*, mul);
762 SHADER_BINOP (/, div);
763
764# undef SHADER_BINOP
765
560 void debdebdebdebug ();//D 766 void debdebdebdebug ();//D
561} 767}
562 768
563#endif 769#endif
564 770

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines