added ambient_occlusion global scene option to parser and vim keywords; added ambient_occlusion scene file

git-svn-id: svn://anubis/fart/trunk@263 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2010-06-29 19:37:53 +00:00
parent f5ac18d34f
commit 96f2b7d276
9 changed files with 69 additions and 4 deletions

View File

@ -202,6 +202,10 @@ void Scene::processOptions(refptr<Node> node)
{ {
setAmbientLight(Color((*it)->getVector())); setAmbientLight(Color((*it)->getVector()));
} }
else if ( typeid(**it) == typeid(AmbientOcclusionNode) )
{
m_ambient_occlusion_level = (*it)->getInteger();
}
} }
} }

View File

@ -30,6 +30,7 @@ Scene::Scene(const map<string, const char *> & options,
m_ambient_light = Color(0.2, 0.2, 0.2); m_ambient_light = Color(0.2, 0.2, 0.2);
m_max_depth = 10; m_max_depth = 10;
m_exposure = 1.0f; m_exposure = 1.0f;
m_ambient_occlusion_level = 0;
m_transforms.push(Transform()); m_transforms.push(Transform());
load(filename); load(filename);
@ -215,8 +216,12 @@ Color Scene::computePhong(const refptr<Material> material,
const Vector & surfacePoint, const Vector & surfacePoint,
const Vector & surfaceNormal) const Vector & surfaceNormal)
{ {
Color result = m_ambient_light * material->getAmbientColor() Color result = m_ambient_light * material->getAmbientColor();
* calculateAmbientOcclusion(Ray(surfacePoint, surfaceNormal).shift(1e-7)); if (m_ambient_occlusion_level > 0)
{
result *= calculateAmbientOcclusion(
Ray(surfacePoint, surfaceNormal).shift(1e-7));
}
Vector viewDirection = -viewRay.getDirection(); Vector viewDirection = -viewRay.getDirection();
double shininess = material->getShininess(); double shininess = material->getShininess();
@ -327,7 +332,8 @@ Color Scene::calculateLightContribution(const Ray & toLight,
Color Scene::calculateAmbientOcclusion(const Ray & surfaceNormal) Color Scene::calculateAmbientOcclusion(const Ray & surfaceNormal)
{ {
Color result(1, 1, 1); Color result(1, 1, 1);
const int nISteps = 30, nJSteps = 15; const int nISteps = m_ambient_occlusion_level * 6;
const int nJSteps = nISteps / 2;
int nRays = 0; int nRays = 0;
Vector perpX = surfaceNormal.getDirection().getPerpendicular().normalize(); Vector perpX = surfaceNormal.getDirection().getPerpendicular().normalize();
Vector perpY = (surfaceNormal.getDirection() * perpX).normalize(); Vector perpY = (surfaceNormal.getDirection() * perpX).normalize();

View File

@ -37,6 +37,7 @@ class Scene
int getWidth() { return m_width; } int getWidth() { return m_width; }
int getHeight() { return m_height; } int getHeight() { return m_height; }
int getMultisampleLevel() { return m_multisample_level; } int getMultisampleLevel() { return m_multisample_level; }
int getAmbientOcclusionLevel() { return m_ambient_occlusion_level; }
double getVFOV() { return m_vfov; } double getVFOV() { return m_vfov; }
protected: protected:
@ -79,6 +80,7 @@ class Scene
Color m_ambient_light; Color m_ambient_light;
int m_max_depth; int m_max_depth;
double m_exposure; double m_exposure;
int m_ambient_occlusion_level;
/* private data */ /* private data */
std::vector< refptr<Shape> > m_shapes; std::vector< refptr<Shape> > m_shapes;

View File

@ -187,6 +187,7 @@ int main(int argc, char * argv[])
cout << " Width: " << width << endl; cout << " Width: " << width << endl;
cout << " Height: " << height << endl; cout << " Height: " << height << endl;
cout << " Multisample Level: " << scene.getMultisampleLevel() << endl; cout << " Multisample Level: " << scene.getMultisampleLevel() << endl;
cout << " Ambient Occlusion Level: " << scene.getAmbientOcclusionLevel() << endl;
cout << " Vertical Field of View: " << scene.getVFOV() << endl; cout << " Vertical Field of View: " << scene.getVFOV() << endl;
cout << "----------------------------------------" << endl; cout << "----------------------------------------" << endl;
} }

View File

@ -68,6 +68,12 @@ class AmbientNode : public VectorNode
AmbientNode(refptr<Vector> vector) : VectorNode(vector) {} AmbientNode(refptr<Vector> vector) : VectorNode(vector) {}
}; };
class AmbientOcclusionNode : public IntegerNode
{
public:
AmbientOcclusionNode(int i) : IntegerNode(i) {}
};
class BoxNode : public Node class BoxNode : public Node
{ {
public: public:

View File

@ -40,6 +40,7 @@
-?[0-9]*\.[0-9]+ *yylval = new NumberNode(atof(yytext)); return REAL_NUMBER; -?[0-9]*\.[0-9]+ *yylval = new NumberNode(atof(yytext)); return REAL_NUMBER;
ambient return AMBIENT; ambient return AMBIENT;
ambient_occlusion return AMBIENT_OCCLUSION;
box return BOX; box return BOX;
camera return CAMERA; camera return CAMERA;
color return COLOR; color return COLOR;

View File

@ -59,6 +59,7 @@ static refptr<Node> parsed_scene_node;
%token REAL_NUMBER; %token REAL_NUMBER;
%token AMBIENT; %token AMBIENT;
%token AMBIENT_OCCLUSION;
%token BOX; %token BOX;
%token CAMERA; %token CAMERA;
%token COLOR; %token COLOR;
@ -314,6 +315,9 @@ options_item: WIDTH DEC_NUMBER {
| AMBIENT vector { | AMBIENT vector {
$$ = new AmbientNode($2->getVector()); $$ = new AmbientNode($2->getVector());
} }
| AMBIENT_OCCLUSION DEC_NUMBER {
$$ = new AmbientOcclusionNode($2->getInteger());
}
; ;
plane: PLANE LCURLY plane_items RCURLY { plane: PLANE LCURLY plane_items RCURLY {

View File

@ -0,0 +1,41 @@
scene
{
options
{
multisample 3
width 800
height 600
ambient_occlusion 5
}
camera
{
position <2, -3, 2>
look_at <0, 0, 1>
up <0, 0, 1>
}
light
{
position <200, -10, 50>
}
define material white
{
color <1, 1, 1>
}
sphere
{
radius 1
material white
translate <0, 0, 1>
}
plane
{
position <0, 0, 1>, 0
material white
}
}

View File

@ -10,7 +10,7 @@ endif
syn case match syn case match
syn keyword fartKeywords ambient color define diffuse exposure height jitter look_at material max_depth multisample options position radius reflectance rotate scale shininess size specular translate transparency union up vfov width syn keyword fartKeywords ambient ambient_occlusion color define diffuse exposure height jitter look_at material max_depth multisample options position radius reflectance rotate scale shininess size specular translate transparency union up vfov width
syn keyword fartObjects box camera cyl intersect light plane scene sphere subtract union syn keyword fartObjects box camera cyl intersect light plane scene sphere subtract union
syn match fartNumber "\(^\|\W\)\@<=[+-]\=\(\d\+\)\=\.\=\d\+\([eE][+-]\=\d\+\)\=" syn match fartNumber "\(^\|\W\)\@<=[+-]\=\(\d\+\)\=\.\=\d\+\([eE][+-]\=\d\+\)\="