home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / renderable_ri.h < prev    next >
C/C++ Source or Header  |  2008-11-07  |  6KB  |  179 lines

  1. #ifndef K3DSDK_RENDERABLE_RI_H
  2. #define K3DSDK_RENDERABLE_RI_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2004, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.     \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "data.h"
  28. #include "k3d-i18n-config.h"
  29. #include "istream_ri.h"
  30. #include "irenderable_ri.h"
  31. #include "render_state_ri.h"
  32.  
  33. namespace k3d
  34. {
  35.  
  36. class idocument;
  37. class iproperty_collection;
  38.     
  39. namespace ri
  40. {
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // motion_begin
  44.  
  45. /// Convenience function that opens a RIB motion block iff motion-blur is enabled
  46. void motion_begin(const render_state& State);
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // motion_end
  50.  
  51. /// Convenience function that closes a RIB motion block iff motion-blur is enabled
  52. void motion_end(const render_state& State);
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // motion_blur
  56.  
  57. /// Convenience function that returns true iff motion-blurred rendering is enabled
  58. bool motion_blur(const render_state& State);
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // first_sample
  62.  
  63. /// Convenience function that returns true iff rendering the first sample in an image
  64. bool first_sample(const render_state& State);
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // last_sample
  68.  
  69. /// Convenience function that returns true iff rendering the last sample in an image
  70. bool last_sample(const render_state& State);
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // convert
  74.  
  75. /// Converts a k3d::matrix4 into a form usable with RenderMan
  76. const matrix convert(const k3d::matrix4& Matrix);
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // setup_material
  80.  
  81. void setup_material(iunknown* const Material, const render_state& State);
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // set_attributes
  85.  
  86. /// Extracts RenderMan data from a property collection, generating corresponding calls to RiAttribute()
  87. void set_attributes(iproperty_collection& Properties, istream& Engine);
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // set_options
  91.  
  92. /// Extracts RenderMan data from a property collection, generating corresponding calls to RiOption()
  93. void set_options(iproperty_collection& Properties, istream& Engine);
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // renderable
  97.  
  98. /// Adds a boilerplate implementation of k3d::ri::irender to a transformable base class, using the parameterized inheritance idiom
  99. template<typename base_t>
  100. class renderable :
  101.     public base_t,
  102.     public irenderable
  103. {
  104. public:
  105.     renderable(iplugin_factory& Factory, idocument& Document) :
  106.         base_t(Factory, Document),
  107.         m_render_final(init_owner(*this) + init_name("render_final") + init_label(_("Render")) + init_description(_("Controls whether this node will be visible in the final rendered image.")) + init_value(true)),
  108.         m_render_shadows(init_owner(*this) + init_name("render_shadows") + init_label(_("Render Shadows")) + init_description(_("Controls whether this node will be visible during shadow-map render passes.")) + init_value(true)),
  109.         m_motion_blur(init_owner(*this) + init_name("motion_blur") + init_label(_("Motion Blur")) + init_description("If enabled, this node will be rendered with motion-blur.") + init_value(false))
  110.     {
  111.     }
  112.  
  113.     void renderman_render(const render_state& State)
  114.     {
  115.         // If this is a normal pass and we're hidden, we're done ...
  116.         if(State.render_context == FINAL_FRAME && !m_render_final.pipeline_value())
  117.             return;
  118.         // If this is a shadow pass and we don't cast shadows, we're done ...
  119.         if(State.render_context == SHADOW_MAP && !m_render_shadows.pipeline_value())
  120.             return;
  121.  
  122.         // If this is the first sample in the frame, reset the sample list ...
  123.         if(first_sample(State))
  124.             m_motion_blur_samples.clear();
  125.  
  126.         // Push our current state onto the sample list ...
  127.         m_motion_blur_samples.push_back(base_t::matrix());
  128.  
  129.         // Only generate RIB on the last sample ...
  130.         if(last_sample(State))
  131.         {
  132.             State.stream.RiAttributeBegin();
  133.  
  134.             if(motion_blur(State) && m_motion_blur.pipeline_value())
  135.             {
  136.                 State.stream.RiMotionBeginV(State.sample_times);
  137.  
  138.                 for(unsigned int i = 0; i < m_motion_blur_samples.size(); ++i)
  139.                     State.stream.RiConcatTransform(convert(m_motion_blur_samples[i]));
  140.  
  141.                 State.stream.RiMotionEnd();
  142.             }
  143.             else
  144.             {
  145.                 State.stream.RiConcatTransform(convert(m_motion_blur_samples.front()));
  146.             }
  147.  
  148.             set_attributes(*this, State.stream);
  149.             on_renderman_render(State);
  150.  
  151.             State.stream.RiAttributeEnd();
  152.         }
  153.     }
  154.  
  155.     void renderman_render_complete(const render_state& State)
  156.     {
  157.         on_renderman_render_complete(State);
  158.     }
  159.  
  160. private:
  161.     virtual void on_renderman_render(const render_state& State) = 0;
  162.  
  163.     virtual void on_renderman_render_complete(const render_state& State)
  164.     {
  165.     }
  166.  
  167.     k3d_data(bool, data::immutable_name, data::change_signal, data::with_undo, data::local_storage, data::no_constraint, data::writable_property, data::with_serialization) m_render_final;
  168.     k3d_data(bool, data::immutable_name, data::change_signal, data::with_undo, data::local_storage, data::no_constraint, data::writable_property, data::with_serialization) m_render_shadows;
  169.     k3d_data(bool, data::immutable_name, data::change_signal, data::with_undo, data::local_storage, data::no_constraint, data::writable_property, data::with_serialization) m_motion_blur;
  170.     std::vector<k3d::matrix4> m_motion_blur_samples;
  171. };
  172.  
  173. } // namespace ri
  174.  
  175. } // namespace k3d
  176.  
  177. #endif // !K3DSDK_RENDERABLE_RI_H
  178.  
  179.