Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

renderer.h

Go to the documentation of this file.
00001 /*
00002     toxic - A Global Illumination Renderer
00003     Copyright (C) 2003-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of toxic.
00007 
00008     toxic is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     toxic is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with toxic; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef TOXIC_RENDERER_RENDERER_H
00024 #define TOXIC_RENDERER_RENDERER_H
00025 
00026 #include "common/math/point3.h"
00027 #include "common/math/real.h"
00028 #include "common/math/vector3.h"
00029 #include "framebuffer.h"
00030 #include "globals.h"
00031 #include "ilight.h"
00032 
00033 #include <cassert>
00034 
00035 namespace toxic {
00036 
00037     class Context;
00038     class Framebuffer;
00039     class Hit;
00040     class ICamera;
00041     class ISurfaceSampler;
00042     class ISurfaceShader;
00043     class PhotonMap;
00044     class Ray;
00045     class Scene;
00046     class ShadingData;
00047 
00048     class Renderer {
00049     public:
00050         Renderer();
00051         virtual ~Renderer();
00052 
00053         //! Sets the scene to render.
00054         void SetScene(const Scene *scene);
00055 
00056         //! Sets the camera.
00057         void SetCamera(const ICamera *camera);
00058 
00059         //! Sets the global photon map.
00060         void SetGlobalPhotonMap(const PhotonMap *global_pm);
00061 
00062         //! Sets the caustics photon map.
00063         void SetCausticsPhotonMap(const PhotonMap *cautics_pm);
00064 
00065         //! Sets the framebuffer and resets the render area.
00066         void SetFramebuffer(Framebuffer *framebuffer);
00067 
00068         //! Sets the render area to the rectangle (x0, y0)-(x1, y1). (x0, y0) are the
00069         //! coordinates of the upper left corner of the render area. (x1, y1) are the
00070         //! coordinates of the lower right corner of the render area. The given rectangle
00071         //! is clipped against the framebuffer boundaries. The framebuffer must be set
00072         //! prior to calling this method.
00073         //!\todo Should this method take a pair of sheep::Point2 (in NDC) instead of four ints?
00074         void SetRenderArea(int x0, int y0, int x1, int y1);
00075 
00076         //! Sets the render area to the full frame.
00077         void ResetRenderArea();
00078 
00079         //! Initializes the renderer. Must be called once just before calling the
00080         //! RenderNextPixel() method (and after having set the scene, the camera,
00081         //! the framebuffer and the render area).
00082         void Restart(const Context &context);
00083 
00084         //! Renders the next pixel. Does nothing if there is no more pixel to render.
00085         void RenderNextPixel(const Context &context);
00086 
00087         //! Methods for progress report.
00088         bool IsRenderComplete() const;
00089         int GetCompletedLines() const;
00090         sheep::Real GetProgress() const;
00091 
00092         void Annotate(const Context &context);
00093 
00094     private:
00095         const Scene *m_scene;
00096         const ICamera *m_camera;
00097 
00098         //! Taken from Jensen's book: "The global photon map contains all photons
00099         //! that hit diffuse surfaces in the model. The photons in the global
00100         //! photon map represent direct illumination, indirect illumination, and
00101         //! caustics (in path notation: L(S|D)*D)."
00102         const PhotonMap *m_global_pm;
00103 
00104         //! Jensen: "The caustics photon map contains photons that have been
00105         //! reflected or transmitted via one specular surface before hitting
00106         //! a diffuse surface. In the path notation these are LS+D."
00107         const PhotonMap *m_caustics_pm;
00108 
00109         Framebuffer *m_framebuffer;
00110 
00111         int m_area_x0, m_area_y0;               //!< Top-left corner of the render area.
00112         int m_area_x1, m_area_y1;               //!< Bottom-right corner of the render area.
00113 
00114         int m_x, m_y;                           //!< Coordinates of the next pixel to render.
00115         int m_completed_pixels;                 //!< Number of rendered pixels until now.
00116 
00117         //! Precomputed values.
00118         int m_area_pixels;                      //!< Number of pixels contained in the render area.
00119         sheep::Real m_inv_area_pixels;          //!< m_inv_area_pixels = 1.0 / m_area_pixels.
00120 
00121         //! Surface samplers.
00122         ISurfaceSampler *m_pixel_ss;            //!< Pixel surface sampler.
00123         ISurfaceSampler *m_arealight_ss;        //!< Area light surface sampler.
00124         ISurfaceSampler *m_primary_fg_hss;      //!< Hemisphere surface sampler when doing primary final gathering.
00125         ISurfaceSampler *m_secondary_fg_hss;    //!< Hemisphere surface sampler when doing secondary final gathering.
00126 
00127         //! Irradiance samples (area light sampling).
00128         ILight::IrradianceSampleVector m_irradiance_samples;
00129 
00130 #ifdef ENABLE_WHITTED_SAMPLING_CACHE
00131         Color3 *m_prev_pixels;
00132 #endif  // ENABLE_WHITTED_SAMPLING_CACHE
00133 
00134         void cleanup();
00135 
00136         //! Computes the radiance passing through the current pixel and stores it
00137         //! into the framebuffer.
00138         void render_pixel(const Context &context);
00139 
00140         void render_pixel_supersampling(const Context &context);
00141         void render_pixel_whitted(const Context &context);
00142 
00143         Color3 whitted_recursive_sampling(
00144             const Context &context,
00145             const sheep::Point2 &center,
00146             const sheep::Point2 &v0,
00147             const sheep::Point2 &v1,
00148             const sheep::Point2 &v2,
00149             const sheep::Point2 &v3,
00150             const Color3 &r0,
00151             const Color3 &r1,
00152             const Color3 &r2,
00153             const Color3 &r3,
00154             int recursion_level
00155         );  // not const because of trace()
00156 
00157         //! Traces a ray through the scene and returns the total reflected radiance
00158         //! along this ray. The ray is expressed in world space. The returned value
00159         //! is a radiance (W.m^-2.sr^-1).
00160         Color3 trace(
00161             const Context &context,
00162             const Ray &ray,
00163             int recursion_level
00164         );  // not const because of compute_direct_illumination()
00165 
00166         //! Computes the radiance reflected toward a given direction due to direct
00167         //! illumination of a given surface, at a given point. The point, the surface
00168         //! normal and the direction are expressed in world space. The direction is
00169         //! such as it leaves the surface, and is unit-length. The returned value is
00170         //! a radiance (W.m^-2.sr^-1).
00171         Color3 compute_direct_illumination(
00172             const Context &context,
00173             const sheep::Point3 &point,
00174             const sheep::Vector3 &geometric_normal,
00175             const sheep::Vector3 &shading_normal,
00176             const sheep::Vector3 &outgoing,
00177             const ISurfaceShader *surface_shader,
00178             const ShadingData &shadingdata
00179         );  // not const
00180 
00181         //! The returned value is a radiance (W.m^-2.sr^-1).
00182         Color3 compute_specular_reflections(
00183             const Context &context,
00184             const sheep::Point3 &point,
00185             const sheep::Vector3 &geometric_normal,
00186             const sheep::Vector3 &shading_normal,
00187             const sheep::Vector3 &outgoing,
00188             const ShadingData &shadingdata,
00189             int recursion_level
00190         );  // not const because of trace()
00191 
00192         //! The returned value is a radiance (W.m^-2.sr^-1).
00193         Color3 final_gathering(
00194             const Context &context,
00195             const sheep::Point3 &point,
00196             const sheep::Vector3 &geometric_normal,
00197             const sheep::Vector3 &shading_normal,
00198             const sheep::Vector3 &outgoing,
00199             const ShadingData &shadingdata,
00200             bool is_secondary = false
00201         ) const;
00202 
00203         //! The returned value is a radiance (W.m^-2.sr^-1).
00204         Color3 compute_caustics(
00205             const Context &context,
00206             const sheep::Point3 &point,
00207             const sheep::Vector3 &geometric_normal,
00208             const sheep::Vector3 &shading_normal,
00209             const sheep::Vector3 &outgoing,
00210             const ShadingData &shadingdata
00211         ) const;
00212 
00213         //! This is an auxiliary method used by the final_gathering() method.
00214         //! Computes the radiance reflected toward a given direction due to the photon
00215         //! density on a given surface, around a given point. The point, the surface
00216         //! normal and the outgoing direction are expressed in world space. The outgoing
00217         //! direction is such that it leaves the surface, and it is unit-length. The
00218         //! returned value is a radiance (W.m^-2.sr^-1).
00219         Color3 compute_indirect_illumination(
00220             const Context &context,
00221             const sheep::Point3 &point,
00222             const sheep::Vector3 &geometric_normal,
00223             const sheep::Vector3 &shading_normal,
00224             const sheep::Vector3 &outgoing,
00225             const ShadingData &shadingdata
00226         ) const;
00227     };
00228 
00229 #include "renderer.inl"
00230 
00231 }
00232 
00233 #endif  // !TOXIC_RENDERER_RENDERER_H

Generated on Tue May 11 01:31:52 2004 for toxic by doxygen 1.3.6