Compare commits

...

5 Commits

13 changed files with 8053 additions and 63 deletions

View File

@ -2,9 +2,7 @@ configure do
check_cxx_compiler check_cxx_compiler
check_program "flex" check_program "flex"
check_program "bison" check_program "bison"
check_lib ":libfl.a"
check_lib "pthread" check_lib "pthread"
check_lib "freeimage"
end end
env do |env| env do |env|

View File

@ -6,6 +6,7 @@
#include "Scene.h" #include "Scene.h"
#include "Light.h" #include "Light.h"
#include "util/stb_image.h"
#include "parser/parser.h" #include "parser/parser.h"
#include "parser/nodes.h" #include "parser/nodes.h"
#include "util/Polygon.h" #include "util/Polygon.h"
@ -347,19 +348,23 @@ refptr<Material> Scene::processMaterial(refptr<Node> node)
else if ( typeid(**it) == typeid(TextureNode) ) else if ( typeid(**it) == typeid(TextureNode) )
{ {
string filename = (*it)->getString(); string filename = (*it)->getString();
FIBITMAP * fib = NULL; Texture * texture = NULL;
if (m_textures.find(filename) != m_textures.end()) if (m_textures.find(filename) != m_textures.end())
{ {
/* texture already loaded */ /* texture already loaded */
fib = m_textures[filename]; texture = m_textures[filename];
} }
else else
{ {
fib = loadTexture((*it)->getString()); texture = loadTexture(filename);
} if (texture != NULL)
if (fib != NULL)
{ {
material->setTexture(fib); m_textures[filename] = texture;
}
}
if (texture != NULL)
{
material->setTexture(texture);
} }
} }
} }
@ -811,30 +816,24 @@ void Scene::processShapeDefinition(refptr<Node> node)
m_transforms.pop(); m_transforms.pop();
} }
FIBITMAP * Scene::loadTexture(const std::string & filename) Texture * Scene::loadTexture(const std::string & filename)
{ {
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename.c_str(), 0); int width = 0;
if (fif == FIF_UNKNOWN) int height = 0;
int channels = 0;
/* request 4 channels so the data is always tightly packed RGBA */
unsigned char * data = stbi_load(filename.c_str(), &width, &height,
&channels, 4);
if (data == NULL)
{ {
fif = FreeImage_GetFIFFromFilename(filename.c_str()); cerr << "Error: image \"" << filename << "\" could not be loaded: "
if (fif == FIF_UNKNOWN) << stbi_failure_reason() << endl;
{
cerr << "Error: couldn't determine image format for \""
<< filename << "\"" << endl;
return NULL; return NULL;
} }
} Texture * texture = new Texture;
if (!FreeImage_FIFSupportsReading(fif)) texture->width = width;
{ texture->height = height;
cerr << "Error: image format of \"" << filename texture->channels = 4;
<< "\" does not support reading" << endl; texture->data = data;
return NULL; return texture;
}
FIBITMAP * fib = FreeImage_Load(fif, filename.c_str(), 0);
if (fib == NULL)
{
cerr << "Error: image \"" << filename << "\" could not be loaded"
<< endl;
}
return fib;
} }

View File

@ -7,11 +7,11 @@
#include <utility> /* pair */ #include <utility> /* pair */
#include <map> #include <map>
#include <algorithm> /* sort() */ #include <algorithm> /* sort() */
#include <functional> /* binary_function */
#include <typeinfo> /* typeid operator support */ #include <typeinfo> /* typeid operator support */
#include "Scene.h" #include "Scene.h"
#include "BMP.h" #include "BMP.h"
#include "util/stb_image.h"
#include "util/Color.h" #include "util/Color.h"
#include "shapes/Shape.h" #include "shapes/Shape.h"
#include "Light.h" #include "Light.h"
@ -75,12 +75,13 @@ Scene::Scene(const map<string, const char *> & options,
Scene::~Scene() Scene::~Scene()
{ {
/* clean up any textures loaded with freeimage */ /* clean up any textures loaded with stb_image */
for (std::map< std::string, FIBITMAP * >::iterator it = m_textures.begin(); for (std::map< std::string, Texture * >::iterator it = m_textures.begin();
it != m_textures.end(); it != m_textures.end();
it++) it++)
{ {
FreeImage_Unload(it->second); stbi_image_free(it->second->data);
delete it->second;
} }
} }

View File

@ -8,8 +8,6 @@
#include <utility> #include <utility>
#include <stack> #include <stack>
#include <FreeImage.h>
#include "util/refptr.h" #include "util/refptr.h"
#include "util/Ray.h" #include "util/Ray.h"
#include "util/Color.h" #include "util/Color.h"
@ -82,7 +80,7 @@ class Scene
std::vector<ShapeRef> processForNode(refptr<Node> node); std::vector<ShapeRef> processForNode(refptr<Node> node);
void processMaterialDefinition(refptr<Node> node); void processMaterialDefinition(refptr<Node> node);
void processShapeDefinition(refptr<Node> node); void processShapeDefinition(refptr<Node> node);
FIBITMAP * loadTexture(const std::string & filename); Texture * loadTexture(const std::string & filename);
/* rendering parameters */ /* rendering parameters */
int m_width; int m_width;
@ -104,7 +102,7 @@ class Scene
double m_sample_span; double m_sample_span;
double m_half_sample_span; double m_half_sample_span;
std::map< std::string, refptr<Material> > m_materials; std::map< std::string, refptr<Material> > m_materials;
std::map< std::string, FIBITMAP * > m_textures; std::map< std::string, Texture * > m_textures;
}; };
#endif #endif

View File

@ -10,8 +10,6 @@
#include <string> #include <string>
#include <map> #include <map>
#include <FreeImage.h>
#include "Scene.h" #include "Scene.h"
#include "distrib/distrib.h" #include "distrib/distrib.h"
#include "BMP.h" #include "BMP.h"
@ -188,8 +186,6 @@ int main(int argc, char * argv[])
srand(time(NULL)); srand(time(NULL));
FreeImage_Initialise(FALSE);
const char * filename = argv[optind]; const char * filename = argv[optind];
client_options.push_back(filename); client_options.push_back(filename);
Scene scene(scene_options, filename); Scene scene(scene_options, filename);
@ -338,8 +334,6 @@ int main(int argc, char * argv[])
cout << endl; cout << endl;
} }
FreeImage_DeInitialise();
exit(0); exit(0);
return 0; return 0;
} }

View File

@ -1,5 +1,6 @@
%option nounput %option nounput
%option noyywrap
%option bison-locations %option bison-locations
%{ %{

View File

@ -19,11 +19,6 @@ extern FILE * yyin;
void errFunc(const char * str, YYLTYPE * yyllocp); void errFunc(const char * str, YYLTYPE * yyllocp);
int yywrap()
{
return 1;
}
static refptr<Node> parsed_scene_node; static refptr<Node> parsed_scene_node;
refptr<Scope> parser_scope; refptr<Scope> parser_scope;

View File

@ -1,7 +1,6 @@
#include "Shape.h" #include "Shape.h"
#include <algorithm> /* sort() */ #include <algorithm> /* sort() */
#include <functional> /* binary_function */
#include <utility> #include <utility>
using namespace std; using namespace std;
@ -28,9 +27,6 @@ void Shape::setMaterial(refptr<Material> material)
} }
class BoolIntersectionComparator class BoolIntersectionComparator
: public std::binary_function<Shape::BoolIntersection,
Shape::BoolIntersection,
bool>
{ {
public: public:
BoolIntersectionComparator(const Vector & refPoint) BoolIntersectionComparator(const Vector & refPoint)

View File

@ -2,8 +2,7 @@
#ifndef MATERIAL_H #ifndef MATERIAL_H
#define MATERIAL_H MATERIAL_H #define MATERIAL_H MATERIAL_H
#include <FreeImage.h> #include "Texture.h"
#include "Color.h" #include "Color.h"
class Material class Material
@ -55,8 +54,8 @@ class Material
void setTransparency(double t) { m_transparency = t; } void setTransparency(double t) { m_transparency = t; }
double getTransparency() const { return m_transparency; } double getTransparency() const { return m_transparency; }
void setTexture(FIBITMAP * fib) { m_texture = fib; } void setTexture(Texture * texture) { m_texture = texture; }
FIBITMAP * getTexture() const { return m_texture; } Texture * getTexture() const { return m_texture; }
protected: protected:
Color m_ambient_color; Color m_ambient_color;
@ -66,7 +65,7 @@ class Material
double m_reflectance; double m_reflectance;
double m_refraction; double m_refraction;
double m_transparency; double m_transparency;
FIBITMAP * m_texture; Texture * m_texture;
}; };
#endif #endif

16
src/util/Texture.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef TEXTURE_H
#define TEXTURE_H TEXTURE_H
/* A loaded image texture. Pixel data is stored as tightly packed RGBA
* bytes (4 channels) and is owned by this struct -- free it with
* stbi_image_free(). */
struct Texture
{
int width;
int height;
int channels;
unsigned char * data;
};
#endif

View File

@ -8,12 +8,12 @@ template <typename T>
class refptr class refptr
{ {
public: public:
refptr<T>(); refptr();
refptr<T>(T * ptr); refptr(T * ptr);
refptr<T>(const refptr<T> & orig); refptr(const refptr<T> & orig);
refptr<T> & operator=(const refptr<T> & orig); refptr & operator=(const refptr<T> & orig);
refptr<T> & operator=(T * ptr); refptr & operator=(T * ptr);
~refptr<T>(); ~refptr();
T & operator*() const; T & operator*() const;
T * operator->() const; T * operator->() const;
bool isNull() const { return m_ptr == NULL; } bool isNull() const { return m_ptr == NULL; }

5
src/util/stb_image.cc Normal file
View File

@ -0,0 +1,5 @@
/* Single translation unit that compiles the stb_image implementation. */
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7988
src/util/stb_image.h Normal file

File diff suppressed because it is too large Load Diff