SHARE
TWEET

Untitled

a guest Aug 24th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. EXPORT GameApi::MT GameApi::MaterialsApi::phong(EveryApi &ev, MT nxt, float light_dir_x, float light_dir_y, float light_dir_z, unsigned int ambient, unsigned int highlight, float pow)
  3. {
  4.   Material *mat = find_material(e, nxt);
  5.   return add_material(e, new PhongMaterial(ev, mat, light_dir_x, light_dir_y, light_dir_z, ambient, highlight, pow));
  6. }
  7.  
  8. class PhongMaterial : public MaterialForward
  9. {
  10. public:
  11.   PhongMaterial(GameApi::EveryApi &ev, Material *next, float light_dir_x, float light_dir_y, float light_dir_z, unsigned int ambient, unsigned int highlight, float pow) : ev(ev), next(next), light_dir_x(light_dir_x), light_dir_y(light_dir_y), light_dir_z(light_dir_z), ambient(ambient), highlight(highlight), pow(pow) { }
  12.   virtual GameApi::ML mat2(GameApi::P p) const
  13.   {
  14.     GameApi::P p0 = ev.polygon_api.recalculate_normals(p);
  15.     //GameApi::P p00 = ev.polygon_api.smooth_normals2(p0);
  16.     //GameApi::P p1 = ev.polygon_api.color(p0, 0xff000000);
  17.     GameApi::ML ml;
  18.     ml.id = next->mat(p0.id);
  19.     GameApi::ML sh = ev.polygon_api.phong_shader(ev, ml, light_dir_x, light_dir_y, light_dir_z, ambient, highlight, pow);
  20.     return sh;
  21.   }
  22.   virtual GameApi::ML mat2_inst(GameApi::P p, GameApi::PTS pts) const
  23.   {
  24.     GameApi::P p0 = ev.polygon_api.recalculate_normals(p);
  25.     //GameApi::P p00 = ev.polygon_api.smooth_normals2(p0);
  26.     //GameApi::P p1 = ev.polygon_api.color(p0, 0xff000000);
  27.     GameApi::ML ml;
  28.     ml.id = next->mat_inst(p0.id, pts.id);
  29.     GameApi::ML sh = ev.polygon_api.phong_shader(ev, ml, light_dir_x, light_dir_y, light_dir_z, ambient, highlight, pow);
  30.     return sh;
  31.  
  32.   }
  33.   virtual GameApi::ML mat2_inst2(GameApi::P p, GameApi::PTA pta) const
  34.   {
  35.     GameApi::P p0 = ev.polygon_api.recalculate_normals(p);
  36.     //GameApi::P p00 = ev.polygon_api.smooth_normals2(p0);
  37.     //GameApi::P p1 = ev.polygon_api.color(p0, 0xff000000);
  38.     GameApi::ML ml;
  39.     ml.id = next->mat_inst2(p0.id, pta.id);
  40.     GameApi::ML sh = ev.polygon_api.phong_shader(ev, ml, light_dir_x, light_dir_y, light_dir_z, ambient, highlight, pow);
  41.     return sh;
  42.  
  43.   }
  44.   virtual GameApi::ML mat_inst_fade(GameApi::P p, GameApi::PTS pts, bool flip, float start_time, float end_time) const
  45.   {
  46.     GameApi::P p0 = ev.polygon_api.recalculate_normals(p);
  47.     //GameApi::P p00 = ev.polygon_api.smooth_normals2(p0);
  48.     //GameApi::P p1 = ev.polygon_api.color(p0, 0xff000000);
  49.     GameApi::ML ml;
  50.     ml.id = next->mat_inst_fade(p0.id, pts.id, flip, start_time, end_time);
  51.     GameApi::ML sh = ev.polygon_api.phong_shader(ev, ml, light_dir_x, light_dir_y, light_dir_z, ambient, highlight, pow);
  52.     return sh;
  53.  
  54.   }
  55.  
  56. private:
  57.   GameApi::EveryApi &ev;
  58.   Material *next;
  59.   float light_dir_x, light_dir_y, light_dir_z;
  60.   unsigned int ambient, highlight;
  61.   float pow;
  62. };
  63.  
  64. class PhongShaderML : public MainLoopItem
  65. {
  66. public:
  67.   PhongShaderML(GameApi::Env &env, GameApi::EveryApi &ev, MainLoopItem *next, Vector light_dir, unsigned int ambient, unsigned int highlight, float pow) : env(env), ev(ev), next(next), light_dir(light_dir), ambient(ambient), highlight(highlight), pow(pow)
  68.   {
  69.     firsttime = true;
  70.     sh.id = -1;
  71.   }
  72.   int shader_id() { if (sh.id != -1) return sh.id; return next->shader_id();
  73.   }
  74.   void handle_event(MainLoopEvent &e)
  75.   {
  76.     next->handle_event(e);
  77.   }
  78.   void Prepare() { next->Prepare(); }
  79.   void execute(MainLoopEnv &e)
  80.   {
  81.     MainLoopEnv ee = e;
  82.      if (firsttime)
  83.       {
  84.     firsttime = false;
  85. #if 1
  86.     GameApi::US vertex;
  87.     vertex.id = ee.us_vertex_shader;
  88.     if (vertex.id==-1) {
  89.       GameApi::US a0 = ev.uber_api.v_empty();
  90.       //GameApi::US a1 = ev.uber_api.v_colour(a0);
  91.       ee.us_vertex_shader = a0.id;
  92.     }
  93.     vertex.id = ee.us_vertex_shader;
  94.     vertex = ev.uber_api.v_phong(vertex);
  95.     //GameApi::US a2 = ev.uber_api.v_passall(a4v);
  96.     ee.us_vertex_shader = vertex.id;
  97.  
  98.     GameApi::US fragment;
  99.     fragment.id = ee.us_fragment_shader;
  100.     if (fragment.id==-1) {
  101.       GameApi::US a0 = ev.uber_api.f_empty(false);
  102.       //GameApi::US a1 = ev.uber_api.f_colour(a0);
  103.       ee.us_fragment_shader = a0.id;
  104.     }
  105.     fragment.id = ee.us_fragment_shader;
  106.     if (ambient)
  107.       fragment = ev.uber_api.f_phong(fragment);
  108.     ee.us_fragment_shader = fragment.id;
  109. #endif
  110.       }
  111.  
  112.     int sh_id = next->shader_id();
  113.     sh.id = sh_id;
  114.     //std::cout << "sh_id" << sh_id << std::endl;
  115.     if (sh_id!=-1)
  116.       {
  117.     //GameApi::SH sh;
  118.     ev.shader_api.use(sh);
  119.  
  120.  
  121.     ev.shader_api.set_var(sh, "light_dir", light_dir.dx, light_dir.dy, light_dir.dz);
  122.     ev.shader_api.set_var(sh, "level1_color",
  123.                   ((ambient&0xff0000)>>16)/255.0,
  124.                   ((ambient&0xff00)>>8)/255.0,
  125.                   ((ambient&0xff))/255.0,
  126.                   ((ambient&0xff000000)>>24)/255.0);
  127.     ev.shader_api.set_var(sh, "level2_color",
  128.                   ((highlight&0xff0000)>>16)/255.0,
  129.               ((highlight&0xff00)>>8)/255.0,
  130.                   ((highlight&0xff))/255.0,
  131.               ((highlight&0xff000000)>>24)/255.0);
  132.     ev.shader_api.set_var(sh, "hilight", pow);
  133.       }
  134.  
  135.     GameApi::M m = add_matrix2( env, e.in_MV); //ev.shader_api.get_matrix_var(sh, "in_MV");
  136.     GameApi::M m1 = add_matrix2(env, e.in_T); //ev.shader_api.get_matrix_var(sh, "in_T");
  137.     GameApi::M m3 = add_matrix2(env, e.in_P); //ev.shader_api.get_matrix_var(sh, "in_T");
  138.     GameApi::M m2 = add_matrix2(env, e.in_N); //ev.shader_api.get_matrix_var(sh, "in_N");
  139.     ev.shader_api.set_var(sh, "in_MV", m);
  140.     ev.shader_api.set_var(sh, "in_T", m1);
  141.     ev.shader_api.set_var(sh, "in_N", m2);
  142.     ev.shader_api.set_var(sh, "in_P", m3);
  143.     ev.shader_api.set_var(sh, "time", e.time);
  144.     ev.shader_api.set_var(sh, "in_POS", e.in_POS);
  145.  
  146.     next->execute(ee);
  147.     ev.shader_api.unuse(sh);
  148.   }
  149. private:
  150.   GameApi::Env &env;
  151.   GameApi::EveryApi &ev;
  152.   MainLoopItem *next;
  153.   Vector light_dir;
  154.   GameApi::SH sh;
  155.   bool firsttime;
  156.   unsigned int ambient;
  157.   unsigned int highlight;
  158.   float pow;
  159. };
  160.  
  161. GameApi::US GameApi::UberShaderApi::v_phong(US us)
  162. {
  163.   ShaderCall *next = find_uber(e, us);
  164.   return add_uber(e, new V_ShaderCallFunction("phong", next,"EX_NORMAL2 EX_LIGHTPOS2 LIGHTDIR IN_NORMAL"));
  165. }
  166. GameApi::US GameApi::UberShaderApi::f_phong(US us)
  167. {
  168.   ShaderCall *next = find_uber(e, us);
  169.   return add_uber(e, new F_ShaderCallFunction("phong", next,"EX_NORMAL2 EX_LIGHTPOS2 LEVELS"));
  170. }
  171. class F_ShaderCallFunction : public ShaderCall
  172. {
  173. public:
  174.   F_ShaderCallFunction(std::string funcname, ShaderCall *next, std::string defines) : funcname(funcname), next(next),defines(defines) { }
  175.   int index(int base) const {
  176.     id = next->index(base)+1;
  177.     return id;
  178.   }
  179.   std::string func_call() const
  180.   {
  181.     std::string out;
  182.     out+=next->func_call();
  183.     std::stringstream ss;
  184.     int i = id;
  185.     ss << i+1;
  186.     std::stringstream ss2;
  187.     ss2 << i;
  188.     out+="vec4 rgb";
  189.     out+=ss.str();
  190.     out+=" = ";
  191.     out+=funcname;
  192.     out+="(rgb";
  193.     out+=ss2.str();
  194.     out+=");\n";
  195.     return out;
  196.   }
  197.   std::string define_strings() const {
  198.     std::string s1 = defines;
  199.     std::string s2 = next->define_strings();
  200.     std::string m = (s1=="" ||s2=="") ? "" : " ";
  201.     return s1 + m + s2;
  202.   }
  203. private:
  204.   std::string funcname;
  205.   ShaderCall *next;
  206.   mutable int id;
  207.   std::string defines;
  208. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top