| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgfx/shaderexp.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2003 by Andrew Topp <designa@users.sourceforge.net>
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_GFX_SHADEREXP_H__
    00020 #define __CS_GFX_SHADEREXP_H__
    00021
    00026 #include "csextern.h"
    00027
    00028 #include "csutil/strhash.h"
    00029 #include "csutil/array.h"
    00030 #include "csutil/leakguard.h"
    00031 #include "csgeom/vector4.h"
    00032 #include "ivideo/shader/shader.h"
    00033
    00034 struct iObjectRegistry;
    00035 class csShaderVariable;
    00036 struct iShaderVariableContext;
    00037 struct iStringSet;
    00038 struct iDocumentNode;
    00039 struct cons;
    00040
    00044 class CS_CRYSTALSPACE_EXPORT csShaderExpression
    00045 {
    00046 public:
    00047   CS_LEAKGUARD_DECLARE (csShaderExpression);
    00048
    00049   struct oper_arg
    00050   {
    00051     uint8 type;
    00052
    00053     union
    00054     {
    00055       float num;
    00056       csStringID var;
    00057
    00058       // Illegal outside of a cons cell
    00059       int oper;
    00060       cons * cell;
    00061
    00062       // Special internal values
    00063       int acc;
    00064     };
    00065
    00066     csVector4 vec4;
    00067   };
    00068
    00069   struct oper
    00070   {
    00071     uint8 opcode, acc;
    00072     oper_arg arg1, arg2;
    00073   };
    00074
    00075   typedef csArray<oper> oper_array;
    00076   typedef csArray<oper_arg> arg_array;
    00077
    00078 private:
    00079   iObjectRegistry * obj_reg;
    00081   csRef<iShaderVarStack> stacks;
    00083   csRef<iStringSet> strset;
    00085   oper_array opcodes;
    00090   int accstack_max;
    00096   arg_array accstack;
    00097
    00099   bool parse_xml(cons *, iDocumentNode *);
    00101   bool parse_xml_atom(oper_arg &, csStringID, const char *, const char *);
    00103   bool parse_sexp(cons *, iDocumentNode *);
    00106   bool parse_sexp_form(const char *& text, cons *);
    00108   bool parse_sexp_atom(const char *& text, cons *);
    00110   bool parse_num_atom(const char *& text, oper_arg &);
    00111
    00113   bool compile_cons(const cons *, int & acc_top);
    00115   bool compile_make_vector(const cons *, int & acc_top, int acc);
    00116
    00118   bool eval_const(cons *&);
    00119
    00121   bool eval_oper(int oper, oper_arg arg1, oper_arg arg2, oper_arg & output);
    00123   bool eval_oper(int oper, oper_arg arg1, oper_arg & output);
    00125   bool eval_oper(int oper, oper_arg & output);
    00126
    00128   bool eval_add(const oper_arg & arg1, const oper_arg & arg2,
    00129         oper_arg & output) const;
    00131   bool eval_sub(const oper_arg & arg1, const oper_arg & arg2,
    00132         oper_arg & output) const;
    00134   bool eval_mul(const oper_arg & arg1, const oper_arg & arg2,
    00135         oper_arg & output) const;
    00137   bool eval_div(const oper_arg & arg1, const oper_arg & arg2,
    00138         oper_arg & output) const;
    00139
    00141   bool eval_elt1(const oper_arg & arg1, oper_arg & output) const;
    00143   bool eval_elt2(const oper_arg & arg1, oper_arg & output) const;
    00145   bool eval_elt3(const oper_arg & arg1, oper_arg & output) const;
    00147   bool eval_elt4(const oper_arg & arg1, oper_arg & output) const;
    00148
    00150   bool eval_sin(const oper_arg & arg1, oper_arg & output) const;
    00152   bool eval_cos(const oper_arg & arg1, oper_arg & output) const;
    00154   bool eval_tan(const oper_arg & arg1, oper_arg & output) const;
    00155
    00156   bool eval_arcsin(const oper_arg & arg1, oper_arg & output) const;
    00157   bool eval_arccos(const oper_arg & arg1, oper_arg & output) const;
    00158   bool eval_arctan(const oper_arg & arg1, oper_arg & output) const;
    00159
    00160   bool eval_dot(const oper_arg & arg1, const oper_arg & arg2,
    00161         oper_arg & output) const;
    00162   bool eval_cross(const oper_arg & arg1, const oper_arg & arg2,
    00163         oper_arg & output) const;
    00164   bool eval_vec_len(const oper_arg & arg1, oper_arg & output) const;
    00165   bool eval_normal(const oper_arg & arg1, oper_arg & output) const;
    00167   bool eval_floor(const oper_arg & arg1, oper_arg & output) const;
    00168
    00169   bool eval_pow(const oper_arg & arg1, const oper_arg & arg2,
    00170         oper_arg & output) const;
    00171   bool eval_min(const oper_arg & arg1, const oper_arg & arg2,
    00172         oper_arg & output) const;
    00173   bool eval_max(const oper_arg & arg1, const oper_arg & arg2,
    00174         oper_arg & output) const;
    00175
    00177   bool eval_time(oper_arg & output) const;
    00179   bool eval_frame(oper_arg & output) const;
    00180
    00182   bool eval_selt12(const oper_arg & arg1, const oper_arg & arg2,
    00183         oper_arg & output) const;
    00185   bool eval_selt34(const oper_arg & arg1, const oper_arg & arg2,
    00186         oper_arg & output) const;
    00188   bool eval_load(const oper_arg & arg1, oper_arg & output) const;
    00189
    00191   bool eval_variable(csShaderVariable *, oper_arg & out);
    00193   bool eval_argument(const oper_arg &, csShaderVariable *);
    00194
    00196   void destruct_cons(cons *) const;
    00198   void print_cons(const cons *) const;
    00200   void print_ops(const oper_array &) const;
    00202   void print_result(const oper_arg &) const;
    00203
    00204   /*inline*/static const char * GetTypeName (csStringID id)/* const*/;
    00205   /*{
    00206     return xmltypes.Request(id);
    00207   }*/
    00208   static const char* GetOperName (csStringID id);
    00209   static csStringID GetCommonTokenOp (const char* token);
    00210   static csStringID GetXmlTokenOp (const char* token);
    00211   static csStringID GetSexpTokenOp (const char* token);
    00212   static csStringID GetXmlType (const char* token);
    00213
    00214   csShaderVariable* ResolveVar (csStringID name);
    00215
    00216   mutable csString errorMsg;
    00217   void ParseError (const char* message, ...) const;
    00218   void EvalError (const char* message, ...) const;
    00219 public:
    00220   csShaderExpression(iObjectRegistry *);
    00221   ~csShaderExpression();
    00222
    00224   bool Parse(iDocumentNode *);
    00226
    00230   bool Evaluate(csShaderVariable *, csShaderVarStack& stacks);
    00231   bool Evaluate(csShaderVariable *, iShaderVarStack* stacks);
    00233
    00235   const char* GetError() const { return errorMsg; }
    00236 };
    00237
    00238 #endif