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_program "flex"
check_program "bison"
check_lib ":libfl.a"
check_lib "pthread"
check_lib "freeimage"
end
env do |env|

View File

@ -6,6 +6,7 @@
#include "Scene.h"
#include "Light.h"
#include "util/stb_image.h"
#include "parser/parser.h"
#include "parser/nodes.h"
#include "util/Polygon.h"
@ -347,19 +348,23 @@ refptr<Material> Scene::processMaterial(refptr<Node> node)
else if ( typeid(**it) == typeid(TextureNode) )
{
string filename = (*it)->getString();
FIBITMAP * fib = NULL;
Texture * texture = NULL;
if (m_textures.find(filename) != m_textures.end())
{
/* texture already loaded */
fib = m_textures[filename];
texture = m_textures[filename];
}
else
{
fib = loadTexture((*it)->getString());
texture = loadTexture(filename);
if (texture != NULL)
{
m_textures[filename] = texture;
}
}
if (fib != NULL)
if (texture != NULL)
{
material->setTexture(fib);
material->setTexture(texture);
}
}
}
@ -811,30 +816,24 @@ void Scene::processShapeDefinition(refptr<Node> node)
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);
if (fif == FIF_UNKNOWN)
int width = 0;
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());
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;
cerr << "Error: image \"" << filename << "\" could not be loaded: "
<< stbi_failure_reason() << endl;
return NULL;
}
FIBITMAP * fib = FreeImage_Load(fif, filename.c_str(), 0);
if (fib == NULL)
{
cerr << "Error: image \"" << filename << "\" could not be loaded"
<< endl;
}
return fib;
Texture * texture = new Texture;
texture->width = width;
texture->height = height;
texture->channels = 4;
texture->data = data;
return texture;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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