marblematerial.cpp

Go to the documentation of this file.
00001 #include "marblematerial.h"
00002 #include <Magick++.h>
00003 #include <math.h>
00004 #include <iostream>
00005 #include "color.h"
00006 
00007 using Magick::Color;
00008 
00009 
00010 MarbleMaterial::StaticInit MarbleMaterial::m_init;
00011 
00012 
00013 
00014 void MarbleMaterial::choose_materials(double noise_amount, Material ** one  , Material ** two) const
00015 {
00016     *one = NULL;
00017     *two = NULL;
00018 
00019     if(noise_amount < m_break_point)
00020     {
00021         *one = m_material_one_a;
00022         *two = m_material_one_b;
00023     }
00024     else
00025     {
00026         *one = m_material_two_a;
00027         *two = m_material_two_b;
00028     }
00029 }
00030 
00031 double MarbleMaterial::interpolate_value(double noise_amount, double one, double two) const
00032 {
00033     double scale = 0.0;
00034 
00035     if(noise_amount < m_break_point)
00036     {
00037         scale = noise_amount * m_material_one_scale;
00038     }
00039     else
00040     {
00041         scale = (noise_amount - m_break_point) * m_material_two_scale;
00042     }
00043     return one * scale + two * (1.0 - scale);
00044 }
00045 
00046 Color MarbleMaterial::get_color(const Point3D& intersection_point) const 
00047 { 
00048     // TODO:  Rewokr this in light of no longer using global color map in
00049     // Scene.
00050 
00051     // !!!THREAD SAFETY ALERT!!!
00052     // we are using one global color here.  This
00053     // will absolutely break if we go multi-threaded.
00054     //  When that day comes, we will either need to
00055     // revisit this strategy or mangle the key
00056     // based on the thread id
00057 
00058     const char * noiseColorKey = "__MARBLE_WORKING_COLOR";
00059 
00060     Scene * scene = Scene::get_instance();
00061     // This just constructs a new color using Magick::Color(std::string).
00062     Color retVal(scene->get_color(noiseColorKey));
00063     if(!retVal.isValid())
00064     {
00065         retVal = Color("red");
00066     }
00067 
00068     double noise = m_noise.get_noise(intersection_point * m_noise_scale);
00069     Material * one = NULL;
00070     Material * two = NULL;
00071     choose_materials(noise, &one, &two);
00072 
00073     // @todo TODO: This probably could be done better.
00074 
00075     double scale = 0.0;
00076 
00077     if(noise < m_break_point)
00078     {
00079         scale = noise * m_material_one_scale;
00080     }
00081     else
00082     {
00083         scale = (noise - m_break_point) * m_material_two_scale;
00084     }
00085 
00086     Color color_one = (one->get_color(intersection_point) );
00087     Color color_two = (two->get_color(intersection_point) );
00088 
00089     return (color_one * scale) + (color_two * (1.0-scale) );
00090 
00091 }
00092 
00093 bool MarbleMaterial::is_light(const Point3D& intersection_point) const 
00094 { 
00095     return false; 
00096 }
00097 
00098 void MarbleMaterial::set_is_light(bool v) 
00099 { 
00100 }
00101 
00102 double MarbleMaterial::get_diffuse(const Point3D& intersection_point) const 
00103 { 
00104     Material * one = NULL;
00105     Material * two = NULL;
00106     double noise = m_noise.get_noise(intersection_point * m_noise_scale);
00107     choose_materials(noise, &one, &two);
00108     return interpolate_value(noise, one->get_diffuse(intersection_point), two->get_diffuse(intersection_point));
00109 }
00110 
00111 void MarbleMaterial::set_diffuse(double diffuse) 
00112 { 
00113 }
00114 
00115 double MarbleMaterial::get_reflection(const Point3D& intersection_point) const 
00116 { 
00117     Material * one = NULL;
00118     Material * two = NULL;
00119     double noise = m_noise.get_noise(intersection_point * m_noise_scale);
00120     choose_materials(noise, &one, &two);
00121     return interpolate_value(noise, one->get_reflection(intersection_point), two->get_reflection(intersection_point));
00122 }
00123 
00124 double MarbleMaterial::get_reflectivity(const Point3D& intersection_point) const 
00125 { 
00126     Material * one = NULL;
00127     Material * two = NULL;
00128     double noise = m_noise.get_noise(intersection_point* m_noise_scale);
00129     choose_materials(noise, &one, &two);
00130     return interpolate_value(noise, one->get_reflectivity(intersection_point), two->get_reflectivity(intersection_point));
00131 }
00132 
00133 double MarbleMaterial::get_refraction_index(const Point3D& intersection_point) const 
00134 { 
00135     // This would seem sketchy at best.  Perhaps better
00136     // would be to make material one the authority...
00137     Material * one = NULL;
00138     Material * two = NULL;
00139     double noise = m_noise.get_noise(intersection_point* m_noise_scale);
00140     choose_materials(noise, &one, &two);
00141     return interpolate_value(noise, one->get_refraction_index(intersection_point), two->get_refraction_index(intersection_point));
00142 }
00143 
00144 double MarbleMaterial::get_opacity(const Point3D& intersection_point) const 
00145 { 
00146     Material * one = NULL;
00147     Material * two = NULL;
00148     double noise = m_noise.get_noise(intersection_point* m_noise_scale);
00149     choose_materials(noise, &one, &two);
00150     return interpolate_value(noise, one->get_opacity(intersection_point), two->get_opacity(intersection_point));
00151 }
00152 
00153 void MarbleMaterial::set_opacity(double opacity) 
00154 {
00155 }
00156 

Generated on Tue Oct 30 22:12:15 2007 for mbrt by  doxygen 1.5.2