YafaRay Core  v3.2.0-ALPHA-10-g60452c5
gradientback.cc
Go to the documentation of this file.
1 /****************************************************************************
2  * gradientback.cc: a background using a simple color gradient
3  * This is part of the yafray package
4  * Copyright (C) 2006 Mathias Wein
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #include <yafray_config.h>
22 
23 #include <core_api/environment.h>
24 #include <core_api/background.h>
25 #include <core_api/params.h>
26 #include <core_api/scene.h>
27 #include <core_api/light.h>
28 
30 
32 {
33  public:
34  gradientBackground_t(color_t gzcol, color_t ghcol, color_t szcol, color_t shcol, bool ibl, bool with_caustic);
35  virtual color_t operator() (const ray_t &ray, renderState_t &state, bool from_postprocessed=false) const;
36  virtual color_t eval(const ray_t &ray, bool from_postprocessed=false) const;
37  virtual ~gradientBackground_t();
39  bool hasIBL() { return withIBL; }
40  bool shootsCaustic() { return shootCaustic; }
41  protected:
43  bool withIBL;
46 };
47 
48 gradientBackground_t::gradientBackground_t(color_t gzcol, color_t ghcol, color_t szcol, color_t shcol, bool ibl, bool with_caustic):
49 gzenith(gzcol), ghoriz(ghcol), szenith(szcol), shoriz(shcol), withIBL(ibl), shootCaustic(with_caustic)
50 {
51  // Empty
52 }
53 
55 {
56  // Empty
57 }
58 
59 color_t gradientBackground_t::operator() (const ray_t &ray, renderState_t &state, bool from_postprocessed) const
60 {
61  return eval(ray);
62 }
63 
64 color_t gradientBackground_t::eval(const ray_t &ray, bool from_postprocessed) const
65 {
66  color_t color;
67 
68  float blend = ray.dir.z;
69 
70  if(blend >= 0.f)
71  {
72  color = blend * szenith + (1.f - blend) * shoriz;
73  }
74  else
75  {
76  blend = -blend;
77  color = blend * gzenith + (1.f - blend) * ghoriz;
78  }
79 
80  if(color.minimum() < 1e-6f) color = color_t(1e-5f);
81 
82  return color;
83 }
84 
86 {
87  color_t gzenith, ghoriz, szenith(0.4f, 0.5f, 1.f), shoriz(1.f);
88  float p = 1.0;
89  bool bgl = false;
90  int bglSam = 16;
91  bool castShadows = true;
92  bool caus = true;
93  bool diff = true;
94 
95  params.getParam("horizon_color", shoriz);
96  params.getParam("zenith_color", szenith);
97  gzenith = szenith;
98  ghoriz = shoriz;
99  params.getParam("horizon_ground_color", ghoriz);
100  params.getParam("zenith_ground_color", gzenith);
101  params.getParam("ibl", bgl);
102  params.getParam("ibl_samples", bglSam);
103  params.getParam("power", p);
104  params.getParam("cast_shadows", castShadows);
105  params.getParam("with_caustic", caus);
106  params.getParam("with_diffuse", diff);
107 
108  background_t *gradBG = new gradientBackground_t(gzenith*p, ghoriz*p, szenith*p, shoriz*p, bgl, true);
109 
110  if(bgl)
111  {
112  paraMap_t bgp;
113  bgp["type"] = std::string("bglight");
114  bgp["samples"] = bglSam;
115  bgp["with_caustic"] = caus;
116  bgp["with_diffuse"] = diff;
117  bgp["cast_shadows"] = castShadows;
118 
119  light_t *bglight = render.createLight("GradientBackground_bgLight", bgp);
120 
121  bglight->setBackground(gradBG);
122 
123  if(bglight) render.getScene()->addLight(bglight);
124  }
125 
126  return gradBG;
127 }
128 
129 extern "C"
130 {
131 
133  {
134  render.registerFactory("gradientback",gradientBackground_t::factory);
135  }
136 
137 }
vector3d_t dir
Definition: ray.h:19
virtual void registerFactory(const std::string &name, light_factory_t *f)
Definition: environment.cc:997
scene_t * getScene()
Definition: environment.h:57
float minimum() const
Definition: color.h:113
YAFRAYPLUGIN_EXPORT void registerPlugin(renderEnvironment_t &render)
virtual color_t operator()(const ray_t &ray, renderState_t &state, bool from_postprocessed=false) const
get the background color for a given ray
Definition: gradientback.cc:59
#define __BEGIN_YAFRAY
Definition: ray.h:11
bool addLight(light_t *l)
Definition: scene.cc:730
virtual color_t eval(const ray_t &ray, bool from_postprocessed=false) const
Definition: gradientback.cc:64
#define YAFRAYPLUGIN_EXPORT
Definition: color.h:49
Definition: light.h:29
float z
Definition: vector3d.h:92
light_t * createLight(const std::string &name, paraMap_t &params)
Definition: environment.cc:246
bool getParam(const std::string &name, T &val) const
template function to get a value, available types are those of parameter_t::getVal() ...
Definition: params.h:82
static background_t * factory(paraMap_t &, renderEnvironment_t &)
Definition: gradientback.cc:85
gradientBackground_t(color_t gzcol, color_t ghcol, color_t szcol, color_t shcol, bool ibl, bool with_caustic)
Definition: gradientback.cc:48
virtual ~gradientBackground_t()
Definition: gradientback.cc:54
virtual void setBackground(background_t *bg)
This method must be called right after the factory is called on a background light or the light will ...
Definition: light.h:63
#define __END_YAFRAY