Compare commits

..

No commits in common. "8f09415b37115f9b0a3b01df8fc00bc0262a733d" and "deb16eeef7b9161e4b2c3259829c815808d29a9b" have entirely different histories.

13 changed files with 63 additions and 8053 deletions

View File

@ -2,7 +2,9 @@ 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,7 +6,6 @@
#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"
@ -348,23 +347,19 @@ 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();
Texture * texture = NULL; FIBITMAP * fib = NULL;
if (m_textures.find(filename) != m_textures.end()) if (m_textures.find(filename) != m_textures.end())
{ {
/* texture already loaded */ /* texture already loaded */
texture = m_textures[filename]; fib = m_textures[filename];
} }
else else
{ {
texture = loadTexture(filename); fib = loadTexture((*it)->getString());
if (texture != NULL)
{
m_textures[filename] = texture;
}
} }
if (texture != NULL) if (fib != NULL)
{ {
material->setTexture(texture); material->setTexture(fib);
} }
} }
} }
@ -816,24 +811,30 @@ void Scene::processShapeDefinition(refptr<Node> node)
m_transforms.pop(); m_transforms.pop();
} }
Texture * Scene::loadTexture(const std::string & filename) FIBITMAP * Scene::loadTexture(const std::string & filename)
{ {
int width = 0; FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename.c_str(), 0);
int height = 0; if (fif == FIF_UNKNOWN)
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)
{ {
cerr << "Error: image \"" << filename << "\" could not be loaded: " fif = FreeImage_GetFIFFromFilename(filename.c_str());
<< stbi_failure_reason() << endl; if (fif == FIF_UNKNOWN)
{
cerr << "Error: couldn't determine image format for \""
<< filename << "\"" << endl;
return NULL;
}
}
if (!FreeImage_FIFSupportsReading(fif))
{
cerr << "Error: image format of \"" << filename
<< "\" does not support reading" << endl;
return NULL; return NULL;
} }
Texture * texture = new Texture; FIBITMAP * fib = FreeImage_Load(fif, filename.c_str(), 0);
texture->width = width; if (fib == NULL)
texture->height = height; {
texture->channels = 4; cerr << "Error: image \"" << filename << "\" could not be loaded"
texture->data = data; << endl;
return texture; }
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,13 +75,12 @@ Scene::Scene(const map<string, const char *> & options,
Scene::~Scene() Scene::~Scene()
{ {
/* clean up any textures loaded with stb_image */ /* clean up any textures loaded with freeimage */
for (std::map< std::string, Texture * >::iterator it = m_textures.begin(); for (std::map< std::string, FIBITMAP * >::iterator it = m_textures.begin();
it != m_textures.end(); it != m_textures.end();
it++) it++)
{ {
stbi_image_free(it->second->data); FreeImage_Unload(it->second);
delete it->second;
} }
} }

View File

@ -8,6 +8,8 @@
#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"
@ -80,7 +82,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);
Texture * loadTexture(const std::string & filename); FIBITMAP * loadTexture(const std::string & filename);
/* rendering parameters */ /* rendering parameters */
int m_width; int m_width;
@ -102,7 +104,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, Texture * > m_textures; std::map< std::string, FIBITMAP * > m_textures;
}; };
#endif #endif

View File

@ -10,6 +10,8 @@
#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"
@ -186,6 +188,8 @@ 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);
@ -334,6 +338,8 @@ int main(int argc, char * argv[])
cout << endl; cout << endl;
} }
FreeImage_DeInitialise();
exit(0); exit(0);
return 0; return 0;
} }

View File

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

View File

@ -19,6 +19,11 @@ 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,6 +1,7 @@
#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;
@ -27,6 +28,9 @@ 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,7 +2,8 @@
#ifndef MATERIAL_H #ifndef MATERIAL_H
#define MATERIAL_H MATERIAL_H #define MATERIAL_H MATERIAL_H
#include "Texture.h" #include <FreeImage.h>
#include "Color.h" #include "Color.h"
class Material class Material
@ -54,8 +55,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(Texture * texture) { m_texture = texture; } void setTexture(FIBITMAP * fib) { m_texture = fib; }
Texture * getTexture() const { return m_texture; } FIBITMAP * getTexture() const { return m_texture; }
protected: protected:
Color m_ambient_color; Color m_ambient_color;
@ -65,7 +66,7 @@ class Material
double m_reflectance; double m_reflectance;
double m_refraction; double m_refraction;
double m_transparency; double m_transparency;
Texture * m_texture; FIBITMAP * m_texture;
}; };
#endif #endif

View File

@ -1,16 +0,0 @@
#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(); refptr<T>();
refptr(T * ptr); refptr<T>(T * ptr);
refptr(const refptr<T> & orig); refptr<T>(const refptr<T> & orig);
refptr & operator=(const refptr<T> & orig); refptr<T> & operator=(const refptr<T> & orig);
refptr & operator=(T * ptr); refptr<T> & operator=(T * ptr);
~refptr(); ~refptr<T>();
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; }

View File

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

File diff suppressed because it is too large Load Diff