Compare commits

...
11 Commits
3 changed files with 57 additions and 22 deletions

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
# vim:filetype=python # vim:filetype=python
env = Environment(LIBS = ['GL']) env = Environment(LIBS = ['GL'], CXXFLAGS = ['-Wall', '-DGL_GLEXT_PROTOTYPES'])
env.Program('driver', Glob('*.cc')) env.Program('driver', Glob('*.cc'))
+40 -14
View File
@@ -1,6 +1,4 @@
#define GL_GLEXT_PROTOTYPES
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@@ -8,7 +6,6 @@
#include <ctype.h> /* isspace() */ #include <ctype.h> /* isspace() */
#include <string.h> /* strlen() */ #include <string.h> /* strlen() */
#include <stdlib.h> /* atoi */ #include <stdlib.h> /* atoi */
#include <GL/gl.h>
#include <vector> #include <vector>
#include <string> #include <string>
@@ -100,14 +97,8 @@ static void checkGLErrorLine(const char * function, int line)
/****** WFObj functions ******/ /****** WFObj functions ******/
WFObj::WFObj(loadfile_t lf, loadtexture_t lt) WFObj::WFObj()
{ {
m_loadfile = lf;
m_loadtexture = lt;
if (m_loadfile == NULL)
{
m_loadfile = loadfile;
}
if (m_valid) if (m_valid)
{ {
glDeleteBuffers(1, &m_data_vbo); glDeleteBuffers(1, &m_data_vbo);
@@ -123,7 +114,7 @@ WFObj::~WFObj()
void WFObj::clear() void WFObj::clear()
{ {
for (int i = 0; i < sizeof(m_vertices)/sizeof(m_vertices[0]); i++) for (size_t i = 0; i < sizeof(m_vertices)/sizeof(m_vertices[0]); i++)
m_vertices[i].clear(); m_vertices[i].clear();
m_faces.clear(); m_faces.clear();
m_materials.clear(); m_materials.clear();
@@ -132,8 +123,15 @@ void WFObj::clear()
m_current_material_name = ""; m_current_material_name = "";
} }
bool WFObj::load(const char *fname) bool WFObj::load(const char *fname, loadfile_t lf, loadtexture_t lt)
{ {
m_loadfile = lf;
m_loadtexture = lt;
if (m_loadfile == NULL)
{
m_loadfile = loadfile;
}
clear(); clear();
Buffer buff; Buffer buff;
@@ -341,7 +339,7 @@ WFObj::VertexRef WFObj::readVertexRef(const std::string ref)
void WFObj::loadMaterial(const std::string & name) void WFObj::loadMaterial(const std::string & name)
{ {
Buffer buff; Buffer buff;
string path = (name[0] != '/') ? basePath(m_path) + name : name; string path = resolvePath(name);
if (!m_loadfile(path.c_str(), buff)) if (!m_loadfile(path.c_str(), buff))
{ {
cerr << "WFObj: error: couldn't open material file '" << path << "'" cerr << "WFObj: error: couldn't open material file '" << path << "'"
@@ -420,9 +418,15 @@ void WFObj::loadMaterial(const std::string & name)
{ {
/* ignore these parameters */ /* ignore these parameters */
} }
else if (tokens[0] == "map_Kd")
{
m_materials[mat_name].texture = loadTexture(resolvePath(tokens[1]));
if (m_materials[mat_name].texture != 0)
m_materials[mat_name].flags |= Material::TEXTURE_BIT;
}
else else
{ {
cerr << "WFObj: error: unrecognized material directive: '" cerr << "WFObj: warning: unrecognized material directive: '"
<< tokens[0] << "'" << endl; << tokens[0] << "'" << endl;
} }
} }
@@ -458,6 +462,12 @@ bool WFObj::buildVBO()
it != m_faces.end(); it != m_faces.end();
it++) it++)
{ {
if (m_materials.find(it->first) == m_materials.end())
{
cerr << "Material '" << it->first << "' referenced in '"
<< m_path << "' is not defined." << endl;
return false;
}
for (vector<Face>::iterator fit = it->second.begin(); for (vector<Face>::iterator fit = it->second.begin();
fit != it->second.end(); fit != it->second.end();
fit++) fit++)
@@ -554,6 +564,22 @@ void WFObj::bindBuffers()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo);
} }
GLuint WFObj::loadTexture(const std::string & path)
{
if (m_loadtexture == NULL)
return 0;
if (m_textures.find(path) != m_textures.end())
return m_textures[path];
GLuint id = m_loadtexture(path.c_str());
m_textures[path] = id;
return id;
}
string WFObj::resolvePath(const string & name)
{
return (name[0] != '/') ? basePath(m_path) + name : name;
}
bool WFObj::VertexRef::operator<(const VertexRef & other) const bool WFObj::VertexRef::operator<(const VertexRef & other) const
{ {
if (vertex != other.vertex) if (vertex != other.vertex)
+16 -7
View File
@@ -2,7 +2,12 @@
#ifndef WFOBJ_H #ifndef WFOBJ_H
#define WFOBJ_H #define WFOBJ_H
#ifdef GL_INCLUDE_FILE
#include GL_INCLUDE_FILE
#else
#include <GL/gl.h> #include <GL/gl.h>
#endif
#include <vector> #include <vector>
#include <string> #include <string>
#include <map> #include <map>
@@ -14,7 +19,7 @@ public:
class Buffer class Buffer
{ {
public: public:
Buffer() : m_alloc(false) {} Buffer() : m_alloc(false) {data = NULL; length = 0;}
~Buffer() { if (m_alloc) delete data; } ~Buffer() { if (m_alloc) delete data; }
uint8_t *data; uint8_t *data;
size_t length; size_t length;
@@ -43,7 +48,7 @@ public:
GLfloat ambient[4]; GLfloat ambient[4];
GLfloat diffuse[4]; GLfloat diffuse[4];
GLfloat specular[4]; GLfloat specular[4];
std::string texture; GLuint texture;
int flags; int flags;
int first_vertex; int first_vertex;
int num_vertices; int num_vertices;
@@ -55,11 +60,11 @@ public:
enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES }; enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES };
/* constructors */ /* constructors */
WFObj(loadfile_t lf = NULL, loadtexture_t lt = NULL); WFObj();
~WFObj(); ~WFObj();
/* methods */ /* methods */
bool load(const char *fname); bool load(const char *fname, loadfile_t lf = NULL, loadtexture_t lt = NULL);
bool load(const Buffer &buff); bool load(const Buffer &buff);
const float * const getAABB() { return m_aabb; } const float * const getAABB() { return m_aabb; }
size_t getStride() { return sizeof(GLfloat) * m_n_floats_per_vref; } size_t getStride() { return sizeof(GLfloat) * m_n_floats_per_vref; }
@@ -69,6 +74,7 @@ public:
void bindBuffers(); void bindBuffers();
size_t getNumMaterials() { return m_num_materials; } size_t getNumMaterials() { return m_num_materials; }
std::map<std::string, Material> & getMaterials() { return m_materials; } std::map<std::string, Material> & getMaterials() { return m_materials; }
bool doTextures() { return m_do_textures; }
protected: protected:
/* types */ /* types */
@@ -86,9 +92,9 @@ protected:
{ {
public: public:
VertexRef() : vertex(0), texture(0), normal(0) {} VertexRef() : vertex(0), texture(0), normal(0) {}
int vertex; size_t vertex;
int texture; size_t texture;
int normal; size_t normal;
bool operator<(const VertexRef & other) const; bool operator<(const VertexRef & other) const;
}; };
@@ -109,6 +115,8 @@ protected:
std::string getLine(const Buffer & buff, size_t idx, size_t *update_idx); std::string getLine(const Buffer & buff, size_t idx, size_t *update_idx);
void loadMaterial(const std::string & name); void loadMaterial(const std::string & name);
bool buildVBO(); bool buildVBO();
GLuint loadTexture(const std::string & path);
std::string resolvePath(const std::string & name);
/* variables */ /* variables */
std::vector<Vertex> m_vertices[VERTEX_TYPES]; std::vector<Vertex> m_vertices[VERTEX_TYPES];
@@ -124,6 +132,7 @@ protected:
bool m_do_textures; bool m_do_textures;
size_t m_n_floats_per_vref; size_t m_n_floats_per_vref;
size_t m_num_materials; size_t m_num_materials;
std::map<std::string, GLuint> m_textures;
}; };
#endif #endif