From 31544a5d4aec9be151ff2a9f0c205c3d76283614 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 10:02:45 -0400 Subject: [PATCH 1/8] remove files in preparation for transition to wfobj/cfs_gen submodules --- FileLoader.h | 88 ------ LoadFile.cc | 48 ---- LoadFile.h | 25 -- TextureLoader.h | 18 -- WFObj.cc | 706 ------------------------------------------------ WFObj.h | 96 ------- genLoadFile.pl | 60 ---- 7 files changed, 1041 deletions(-) delete mode 100644 FileLoader.h delete mode 100644 LoadFile.cc delete mode 100644 LoadFile.h delete mode 100644 TextureLoader.h delete mode 100644 WFObj.cc delete mode 100644 WFObj.h delete mode 100755 genLoadFile.pl diff --git a/FileLoader.h b/FileLoader.h deleted file mode 100644 index 8a6cffb..0000000 --- a/FileLoader.h +++ /dev/null @@ -1,88 +0,0 @@ - -#ifndef FILELOADER_H -#define FILELOADER_H FILELOADER_H - -#include -#include - -class FileLoader -{ - public: - class Path - { - public: - std::string fullPath; - std::string shortPath; - Path() { } - Path(const std::string & full_path, - const std::string & short_path) - : fullPath(full_path), shortPath(short_path) { } - std::string toString() const - { - std::string ret; - if (shortPath != "") - ret += shortPath + ","; - if (fullPath != "") - ret += fullPath; - return ret; - } - }; - - class Buffer - { - public: - char * data; - int size; - Buffer(int sz) - { - size = sz; - data = new char[size]; - _refcnt = new int; - *_refcnt = 1; - m_alloced = true; - } - Buffer(char * data, int sz) - { - size = sz; - this->data = data; - m_alloced = false; - } - void copy(const Buffer & other) - { - data = other.data; - size = other.size; - m_alloced = other.m_alloced; - if (m_alloced) - { - _refcnt = other._refcnt; - (*_refcnt)++; - } - } - Buffer(const Buffer & other) { copy(other); } - Buffer & operator=(const Buffer & other) - { - copy(other); - return *this; - } - ~Buffer() - { - if (m_alloced) - { - (*_refcnt)--; - if (*_refcnt < 1) - { - delete _refcnt; - delete data; - } - } - } - protected: - int * _refcnt; - bool m_alloced; - }; - - virtual int getSize(const Path & path) = 0; - virtual Buffer load(const Path & path) = 0; -}; - -#endif diff --git a/LoadFile.cc b/LoadFile.cc deleted file mode 100644 index 7bfb944..0000000 --- a/LoadFile.cc +++ /dev/null @@ -1,48 +0,0 @@ - -#include - -#include "LoadFile.h" - -using namespace std; - -/* The data section is generated by a perl script. The contents of the - * included file are part of this logical program unit, which is why it - * is directly included instead of compiling it separately. - */ -#include "LoadFile-gen.inc" - -LoadFile::LoadFile() -{ - for (unsigned int i = 0; - i < sizeof(LoadFileData)/sizeof(LoadFileData[0]); - i++) - { - m_filemap[LoadFileData[i].filename] = &LoadFileData[i]; - } -} - -FileLoader::Buffer LoadFile::load(const FileLoader::Path & path) -{ - map::iterator it = m_filemap.find(path.shortPath); - - if (it == m_filemap.end()) - { - cerr << "Warning: LoadFile::load(" - << path.shortPath << ") did not find file" - << endl; - return Buffer(0); - } - - return Buffer((char *) it->second->data, it->second->length); -} - -int LoadFile::getSize(const FileLoader::Path & path) -{ - map::iterator it = m_filemap.find(path.shortPath); - - if (it == m_filemap.end()) - return 0; - - return it->second->length; -} - diff --git a/LoadFile.h b/LoadFile.h deleted file mode 100644 index 7a5845f..0000000 --- a/LoadFile.h +++ /dev/null @@ -1,25 +0,0 @@ - -#ifndef LOADFILE_H -#define LOADFILE_H - -#include -#include -#include "FileLoader.h" - -typedef struct { - const char * filename; - unsigned char * data; - int length; -} fileref_t; - -class LoadFile : public FileLoader -{ - public: - LoadFile(); - int getSize(const Path & path); - Buffer load(const Path & path); - protected: - std::map< std::string, fileref_t * > m_filemap; -}; - -#endif diff --git a/TextureLoader.h b/TextureLoader.h deleted file mode 100644 index a7b9ca5..0000000 --- a/TextureLoader.h +++ /dev/null @@ -1,18 +0,0 @@ - -#ifndef TEXTURELOADER_H -#define TEXTURELOADER_H TEXTURELOADER_H - -#include -#include -#include "FileLoader.h" - -class TextureLoader -{ - public: - virtual GLuint load( - const FileLoader::Path & path, - FileLoader & fileLoader, bool mipmaps = true, - int mode = GL_DECAL, int quality = 1) = 0; -}; - -#endif diff --git a/WFObj.cc b/WFObj.cc deleted file mode 100644 index 146c80b..0000000 --- a/WFObj.cc +++ /dev/null @@ -1,706 +0,0 @@ - -#include -#include -#include -#include -#include // isspace() -#include // strlen() -#include -#include -#include -#include -#include -#include -#include -#include "WFObj.h" -using namespace std; - -#define WHITESPACE " \n\r\t\v" -//#define DEBUGGL - -/****** static functions ******/ - -static string trimString(string s) -{ - size_t lastpos = s.find_last_not_of(WHITESPACE); - if (lastpos == string::npos) - return ""; - s.erase(lastpos + 1); - s.erase(0, s.find_first_not_of(WHITESPACE)); - return s; -} - -static string stripFirstToken(string & input) -{ - size_t firstnonspace = input.find_first_not_of(WHITESPACE); - if (firstnonspace == string::npos) - return ""; - size_t spaceafter = input.find_first_of(WHITESPACE, firstnonspace); - string token = input.substr(firstnonspace, spaceafter - firstnonspace); - input.erase(0, spaceafter); - return token; -} - -static vector splitString(const string & str, char delim) -{ - vector ret; - string s = str; - size_t pos; - while ( (pos = s.find(delim)) != string::npos ) - { - string t = s.substr(0, pos); - ret.push_back(t); - s.erase(0, pos + 1); - } - if (s != "") - ret.push_back(s); - return ret; -} - -static string basePath(const string & str) -{ - string path = str; - size_t pos; - if ( (pos = path.find_last_of("/\\")) != string::npos ) - { - path.erase(pos + 1); - return path; - } - return ""; -} - -//#define DEBUG_GL_ERROR -#ifdef DEBUG_GL_ERROR -#define checkGLError() checkGLErrorLine(__FUNCTION__, __LINE__) -static void checkGLErrorLine(const char * function, int line) -{ - GLenum err = glGetError(); - if (err != 0) - { - cerr << "gl error in " << function - << ": " << err << " (0x" << hex << err << ") at line " - << dec << line << endl; - } -} -#else -#define checkGLError() -#endif - - -/****** WFObj functions ******/ - -WFObj::WFObj() -{ - init(); -} - -WFObj::WFObj(FileLoader & fileLoader) -{ - init(&fileLoader); -} - -WFObj::WFObj(TextureLoader & textureLoader) -{ - init(NULL, &textureLoader); -} - -WFObj::WFObj(FileLoader & fileLoader, TextureLoader & textureLoader) -{ - init(&fileLoader, &textureLoader); -} - -void WFObj::init (FileLoader * fileLoader, - TextureLoader * textureLoader) -{ - m_fileLoader = fileLoader; - if (m_fileLoader == NULL) - { - m_fileLoader = new WFFileLoader(); - m_iCreatedFileLoader = true; - } - else - { - m_iCreatedFileLoader = false; - } - m_textureLoader = textureLoader; -} - -WFObj::~WFObj() -{ - if (m_iCreatedFileLoader) - delete m_fileLoader; -} - -void WFObj::clear() -{ - m_data = std::vector< std::vector >(); - m_loadedVertex = false; -} - -bool WFObj::load(const FileLoader::Path & path) -{ - clear(); - - FileLoader::Buffer buff = m_fileLoader->load(path); - if (buff.size <= 0) - return false; - - m_path = path; - string str(buff.data, buff.size); - stringstream istr(str, ios_base::in); - load(istr, buff.size); - - return true; -} - -bool WFObj::load(std::istream & istr, unsigned int size) -{ - char buf[size+1]; - - string buildup; - while (istr.good()) - { - istr.getline(buf, size+1); - string input = trimString(buf); - int sz = input.size(); - if (sz == 0 || input[0] == '#') - continue; - if (input[sz-1] == '\\') - { - input[sz-1] = ' '; - buildup = input; - continue; - } - if (buildup != "") - input = buildup + input; - buildup = ""; - processInputLine(input); - } - return true; -} - -void WFObj::processInputLine(const std::string & input) -{ - string line = input; - vector lineParts; - for (;;) - { - string token = stripFirstToken(line); - if (token == "") - break; - lineParts.push_back(token); - } - if (lineParts.size() > 0) - m_data.push_back(lineParts); -} - - -GLuint WFObj::render(bool doTextureInfo, bool enableBlending) -{ - checkGLError(); - GLuint list = glGenLists(1); - glNewList(list, GL_COMPILE); - int len = m_data.size(); - enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES }; - vector vertices[VERTEX_TYPES]; - int numVertsLast = 0; - bool inFace = false; - bool inMaterial = false; - string currentMaterialName; - WFMtl material(this); - for (int i = 0; i < len; i++) - { - string type = m_data[i][0]; - if (type == "v") - { - Vertex v = readVertex(m_data[i]); - updateAABB(v.getData()); - vertices[VERTEX].push_back(v); - } - else if (type == "vt") - vertices[VERTEX_TEXTURE].push_back(readVertex(m_data[i])); - else if (type == "vn") - vertices[VERTEX_NORMAL].push_back(readVertex(m_data[i])); - else if (type == "f") - { - int numVerts = m_data[i].size() - 1; - if (inFace && (numVerts != numVertsLast || numVertsLast > 4)) - { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; - } - if (!inFace) - { - if (numVerts == 3) - { -#ifdef DEBUGGL - cout << "glBegin(GL_TRIANGLES)" << endl; -#endif - glBegin(GL_TRIANGLES); - } - else if (numVerts == 4) - { -#ifdef DEBUGGL - cout << "glBegin(GL_QUADS)" << endl; -#endif - glBegin(GL_QUADS); - } - else - { -#ifdef DEBUGGL - cout << "glBegin(GL_POLYGON)" << endl; -#endif - glBegin(GL_POLYGON); - } - inFace = true; - } - for (int v = 1; v <= numVerts; v++) - { - int vertexIndices[3] = {0, 0, 0}; - parseVertexIndices(m_data[i][v], vertexIndices); - for (int j = 0; j < 3; j++) - { - if (vertexIndices[j] < 0) - vertexIndices[j] += vertices[j].size() + 1; - } - bool valid = true; - for (int j = 0; j < 3; j++) - { - if (vertexIndices[j] > (int) vertices[j].size()) - { - valid = false; - break; - } - } - if (vertexIndices[VERTEX] <= 0) - valid = false; - if (!valid) - continue; - if (vertexIndices[VERTEX_NORMAL] != 0) - { - /* a normal is present */ -#ifdef DEBUGGL - cout << " glNormal3f(" << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [0] << ", " << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [1] << ", " << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [2] << ")" << endl; -#endif - glNormal3fv(vertices[VERTEX_NORMAL] - [vertexIndices[VERTEX_NORMAL]-1].getData()); - } - if (vertexIndices[VERTEX_TEXTURE] != 0) - { - /* a texture coordinate is present */ -#ifdef DEBUGGL - cout << " glTexCoord2f(" << - vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1] - [0] << ", " << - vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1] - [1] << ')' << endl; -#endif - glTexCoord2fv(vertices[VERTEX_TEXTURE] - [vertexIndices[VERTEX_TEXTURE]-1].getData()); - } -#ifdef DEBUGGL - cout << " glVertex3f(" << - vertices[VERTEX][vertexIndices[VERTEX]-1][0] << ", " << - vertices[VERTEX][vertexIndices[VERTEX]-1][1] << ", " << - vertices[VERTEX][vertexIndices[VERTEX]-1][2] << ")" << - " [" << vertexIndices[VERTEX] << "]" << endl; -#endif - glVertex3fv(vertices[VERTEX][vertexIndices[VERTEX]-1].getData()); - } - - numVertsLast = numVerts; - } - else if (type == "usemtl") - { - if (inFace) - { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; - } - if (inMaterial) - { - material.renderEnd(currentMaterialName, doTextureInfo, - enableBlending); - inMaterial = false; - } - if (m_data[i].size() >= 2) - { - currentMaterialName = m_data[i][1]; - material.renderBegin(currentMaterialName, doTextureInfo, - enableBlending); - inMaterial = true; - } - } - else if (type == "mtllib") - { - if (m_data[i].size() >= 2) - { - FileLoader::Path path( - basePath(m_path.fullPath) + m_data[i][1], - m_data[i][1]); - material.load(path); - } - } - } - if (inFace) - { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; - } - if (inMaterial) - { - material.renderEnd(currentMaterialName, doTextureInfo, enableBlending); - inMaterial = false; - } - glEndList(); - checkGLError(); - return list; -} - -void WFObj::updateAABB(const float * const vertex) -{ - if (m_loadedVertex) - { - if (vertex[0] < m_aabb[0]) - m_aabb[0] = vertex[0]; - else if (vertex[0] > m_aabb[3]) - m_aabb[3] = vertex[0]; - if (vertex[1] < m_aabb[1]) - m_aabb[1] = vertex[1]; - else if (vertex[1] > m_aabb[4]) - m_aabb[4] = vertex[1]; - if (vertex[2] < m_aabb[2]) - m_aabb[2] = vertex[2]; - else if (vertex[2] > m_aabb[5]) - m_aabb[5] = vertex[2]; - } - else - { - m_aabb[0] = m_aabb[3] = vertex[0]; - m_aabb[1] = m_aabb[4] = vertex[1]; - m_aabb[2] = m_aabb[5] = vertex[2]; - m_loadedVertex = true; - } - m_width = m_aabb[3] - m_aabb[0]; - m_depth = m_aabb[4] - m_aabb[1]; - m_height = m_aabb[5] - m_aabb[2]; -} - -WFObj::Vertex WFObj::readVertex(const vector & parts) -{ - int partslen = parts.size(); - Vertex v; - for (int i = 1; i < partslen && i <= 4; i++) - { - sscanf(parts[i].c_str(), "%f", &v[i - 1]); - } - return v; -} - -void WFObj::parseVertexIndices(const string & vtxref, int * ret) -{ - vector parts = splitString(vtxref, '/'); - int num = parts.size(); - for (int i = 0; i < num && i < 3; i++) - sscanf(parts[i].c_str(), "%d", ret + i); -} - - - - -/****** WFMtl functions ******/ - -void WFObj::WFMtl::clear() -{ - m_data = map< string, vector< vector > >(); - m_currentMaterialName = ""; - m_attributesPushed = false; -} - -bool WFObj::WFMtl::load(const FileLoader::Path & path) -{ - clear(); - - FileLoader::Buffer buff = m_obj->m_fileLoader->load(path); - if (buff.size <= 0) - return false; - - m_path = path; - string str(buff.data, buff.size); - stringstream istr(str, ios_base::in); - load(istr, buff.size); - - return true; -} - -bool WFObj::WFMtl::load(std::istream & istr, unsigned int size) -{ - char buf[size+1]; - - string buildup; - while (istr.good()) - { - istr.getline(buf, size+1); - string input = trimString(buf); - int sz = input.size(); - if (sz == 0 || input[0] == '#') - continue; - if (input[sz-1] == '\\') - { - input[sz-1] = ' '; - buildup = input; - continue; - } - if (buildup != "") - input = buildup + input; - buildup = ""; - processInputLine(input); - } - return true; -} - -void WFObj::WFMtl::processInputLine(const std::string & input) -{ - string line = input; - vector lineParts; - for (;;) - { - string token = stripFirstToken(line); - if (token == "") - break; - lineParts.push_back(token); - } - if (lineParts.size() > 0) - { - if ( (lineParts.size() >= 2) && (lineParts[0] == "newmtl") ) - { - m_currentMaterialName = lineParts[1]; - } - else if (m_currentMaterialName != "") - { - m_data[m_currentMaterialName].push_back(lineParts); - } - } -} - -void WFObj::WFMtl::renderBegin(const string & mtlname, bool doTextureInfo, - bool enableBlending) -{ - map< string, vector< vector > >::iterator it = m_data.find(mtlname); - if (it == m_data.end()) - return; - vector< vector > & stmts = it->second; - int num_stmts = stmts.size(); - bool foundTexture = false; - bool didSomething = false; - checkGLError(); - for (int i = 0; i < num_stmts; i++) - { - string & type = stmts[i][0]; - if (type == "Ka") /* set ambient color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_AMBIENT, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_AMBIENT, mat); - didSomething = true; - } - } - else if (type == "Kd") /* set diffuse color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_DIFFUSE, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_DIFFUSE, mat); - didSomething = true; - } - } - else if (type == "Ks") /* set specular color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_SPECULAR, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_SPECULAR, mat); - didSomething = true; - } - } - else if (type == "Ns") /* set shininess */ - { - if (stmts[i].size() == 2) - { - pushAttributes(); - GLfloat shininess = 0.0f; - sscanf(stmts[i][1].c_str(), "%f", &shininess); -#ifdef DEBUGGL - cout << " glMaterialf(GL_FRONT, GL_SHININESS, " - << shininess << ")" << endl; -#endif - glMaterialf(GL_FRONT, GL_SHININESS, shininess); - didSomething = true; - } - } - else if (type == "map_Kd") /* load a diffuse texture */ - { - if (doTextureInfo) - { - if (stmts[i].size() == 2) - { - if (m_obj->m_textureLoader != NULL) - { - FileLoader::Path path( - basePath(m_path.fullPath + stmts[i][1]), - stmts[i][1]); - GLuint tex = m_obj->m_textureLoader->load(path, - *m_obj->m_fileLoader); - if (tex > 0) - { - pushAttributes(); /* jh 2009-11-16 */ -#ifdef DEBUGGL - cout << " glBindTexture(GL_TEXTURE_2D, " << tex << ")" << endl; -#endif - glBindTexture(GL_TEXTURE_2D, tex); - foundTexture = true; - didSomething = true; - } - } - } - } - } - } - - checkGLError(); - if (didSomething) - { - pushAttributes(); - if (doTextureInfo) - { - if (enableBlending) - { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - } - if (foundTexture) - { -#ifdef DEBUGGL - cout << " glEnable(GL_TEXTURE_2D)" << endl; -#endif - glEnable(GL_TEXTURE_2D); - } - else - { -#ifdef DEBUGGL - cout << " glDisable(GL_TEXTURE_2D)" << endl; -#endif - glDisable(GL_TEXTURE_2D); - } - } - } - checkGLError(); -} - -void WFObj::WFMtl::pushAttributes() -{ - if (m_attributesPushed) - return; - m_attributesPushed = true; -#ifdef DEBUGGL - cout << " glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT)" - << endl; -#endif - checkGLError(); - glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); - checkGLError(); -} - -void WFObj::WFMtl::renderEnd(const string & mtlname, bool doTextureInfo, - bool enableBlending) -{ - map< string, vector< vector > >::iterator it = m_data.find(mtlname); - if (it == m_data.end()) - return; - if (m_attributesPushed) - { -#ifdef DEBUGGL - cout << " glPopAttrib()" << endl; -#endif - checkGLError(); - glPopAttrib(); - checkGLError(); - m_attributesPushed = false; - } -} - - -/****** WFFileLoader functions ******/ - -int WFObj::WFFileLoader::getSize(const Path & path) -{ - struct stat st; - - if (path.fullPath == "") - return -1; - if (stat(path.fullPath.c_str(), &st)) - return -2; - return st.st_size; -} - -FileLoader::Buffer WFObj::WFFileLoader::load(const Path & path) -{ - int size = getSize(path); - if (size > 0) - { - int fd = open(path.fullPath.c_str(), O_RDONLY); - if (fd > 0) - { - Buffer buf(size); - int num_read = read(fd, buf.data, size); - close(fd); - if (num_read > 0) - return buf; - } - } - return Buffer(0); -} diff --git a/WFObj.h b/WFObj.h deleted file mode 100644 index 24c973e..0000000 --- a/WFObj.h +++ /dev/null @@ -1,96 +0,0 @@ - -#ifndef WFOBJ_H -#define WFOBJ_H - -#include "FileLoader.h" -#include "TextureLoader.h" -#include -#include -#include -#include - -class WFObj -{ -public: - WFObj(); - WFObj(FileLoader & fileLoader); - WFObj(TextureLoader & textureLoader); - WFObj(FileLoader & fileLoader, TextureLoader & textureLoader); - ~WFObj(); - - class WFMtl - { - public: - WFMtl(WFObj * obj) { m_obj = obj; } - bool load(const FileLoader::Path & path); - void renderBegin(const std::string & mtlname, - bool doTextureInfo = true, - bool enableBlending = false); - void renderEnd(const std::string & mtlname, - bool doTextureInfo = true, - bool enableBlending = false); - - protected: - /* methods */ - void clear(); - void processInputLine(const std::string & input); - void pushAttributes(); - bool load(std::istream & istr, unsigned int size); - - /* variables */ - std::map< std::string, std::vector< std::vector > > m_data; - std::string m_currentMaterialName; - bool m_attributesPushed; - FileLoader::Path m_path; - WFObj * m_obj; - }; - - class WFFileLoader : public FileLoader - { - public: - virtual int getSize(const Path & path); - virtual Buffer load(const Path & path); - }; - - bool load(const FileLoader::Path & path); - GLuint render(bool doTextureInfo = true, - bool enableBlending = false); - const float * const getAABB() { return m_aabb; } - int getWidth() { return m_width; } - int getDepth() { return m_depth; } - int getHeight() { return m_height; } - -protected: - /* types */ - class Vertex - { - public: - float operator[](int idx) const { return data[idx]; } - float & operator[](int idx) { return data[idx]; } - float * getData() { return data; } - private: - float data[4]; - }; - - /* methods */ - void init(FileLoader * fileLoader = NULL, - TextureLoader * textureLoader = NULL); - void clear(); - void processInputLine(const std::string & input); - Vertex readVertex(const std::vector & parts); - void parseVertexIndices(const std::string & vtxref, int * ret); - void updateAABB(const float * const vertex); - bool load(std::istream & istr, unsigned int size); - - /* variables */ - std::vector< std::vector > m_data; - FileLoader::Path m_path; - float m_aabb[6]; - int m_width, m_depth, m_height; - bool m_loadedVertex; - FileLoader * m_fileLoader; - TextureLoader * m_textureLoader; - bool m_iCreatedFileLoader; -}; - -#endif diff --git a/genLoadFile.pl b/genLoadFile.pl deleted file mode 100755 index f5880f4..0000000 --- a/genLoadFile.pl +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Getopt::Long; - -my $root = '.'; -GetOptions('root=s' => \$root); -$root = "$root/" if ($root ne ''); - -my @fileList = @ARGV; -if ($#fileList < 0) -{ - print "$0 \n"; - exit(42); -} - -my %fileData; -my $index = 0; - -open(OUTPUT, '>', 'LoadFile-gen.inc'); -foreach my $fileName (@fileList) -{ - local $/; - open(FILE, '<', $fileName); - my $fileContents = ; - close(FILE); - my $length = length($fileContents); - my @fileContents = split(//, $fileContents); - my $cname = "dat$index"; - print OUTPUT "\nstatic unsigned char ${cname} [] = {\n"; - for (my $byteNum = 0; $byteNum <= $#fileContents; $byteNum++) - { - print OUTPUT " " if ($byteNum % 12 == 0); - printf OUTPUT ("0x%02x", ord($fileContents[$byteNum])); - print OUTPUT ", " unless ($byteNum == $#fileContents); - print OUTPUT "\n" if ($byteNum % 12 == 11); - } - print OUTPUT "\n};\n"; - $index++; - my $shortName = $fileName; - $shortName =~ s/^$root//; - $fileData{$shortName} = [$cname, $length]; -} - -print OUTPUT "\nfileref_t LoadFileData[] = {\n"; - -my @fileNames = keys(%fileData); -for (my $fileIndex = 0; $fileIndex <= $#fileNames; $fileIndex++) -{ - my $fileName = $fileNames[$fileIndex]; - printf OUTPUT (' {"%s", %s, %s}', - $fileName, - $fileData{$fileName}->[0], - $fileData{$fileName}->[1]); - print OUTPUT "," unless ($fileIndex == $#fileNames); - print OUTPUT "\n"; -} -print OUTPUT "};\n"; -close(OUTPUT); From 9782d105fe6794a03af0d2e96bdd5f7415a80d44 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 10:04:53 -0400 Subject: [PATCH 2/8] add wfobj/cfs_gen submodules --- .gitmodules | 6 ++++++ cfs_gen | 1 + wfobj | 1 + 3 files changed, 8 insertions(+) create mode 100644 .gitmodules create mode 160000 cfs_gen create mode 160000 wfobj diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..beee7d0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "wfobj"] + path = wfobj + url = ../util/wfobj.git +[submodule "cfs_gen"] + path = cfs_gen + url = ../util/cfs_gen.git diff --git a/cfs_gen b/cfs_gen new file mode 160000 index 0000000..6954147 --- /dev/null +++ b/cfs_gen @@ -0,0 +1 @@ +Subproject commit 6954147f53d57724b7666bf4a1226e17ca64f734 diff --git a/wfobj b/wfobj new file mode 160000 index 0000000..e4dc0f4 --- /dev/null +++ b/wfobj @@ -0,0 +1 @@ +Subproject commit e4dc0f4d3627ab08e20d1be2dce41981e5771cca From e0169b779e67f0854ae9e3989a7479b489e424de Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 10:34:46 -0400 Subject: [PATCH 3/8] update build environment to use cfs_gen --- .gitignore | 3 ++- SConstruct | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 6aad486..90600b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dwss modes.h -LoadFile-gen.inc +cfs.cc +cfs.h diff --git a/SConstruct b/SConstruct index a17a495..c20de7b 100644 --- a/SConstruct +++ b/SConstruct @@ -1,16 +1,17 @@ # vim:syntax=python import os +import re +from subprocess import * install_dir = '/usr/lib/gnome-screensaver/gnome-screensaver' desktop_dir = os.getenv('HOME') + '/.local/share/applications/screensavers' -load_files = ['logo/dwlogo.obj', 'logo/dwlogo.mtl'] +load_files = ['logo/dwlogo.obj', 'logo/dwlogo.mtl', Glob('shaders/*')] env = Environment(CPPPATH = ['.']) env.ParseConfig("pkg-config --cflags --libs glib-2.0 gdk-2.0 atk gtk+-2.0 gtkglext-1.0") -genLoadFile = Builder(action = 'perl genLoadFile.pl --root=logo $SOURCES') -env.Append(BUILDERS = {'LoadFile' : genLoadFile}) +# modes builder def gen_modes(target, source, env): f = open(str(target[0]), 'w') @@ -22,10 +23,27 @@ def gen_modes(target, source, env): genModes = Builder(action = gen_modes) env.Append(BUILDERS = {'Modes' : genModes}) +# CFS builder + +def CFS(target, source, env): + source_list = [] + for s in source: + source_list.append(str(s)) + Popen(['./cfs_gen/cfs_gen.py', str(target[0])] + source_list).wait() + return None + +def CFS_emitter(target, source, env): + target.append(re.sub(r'\.cc?', '.h', str(target[0]))) + return target, source + +env.Append(BUILDERS = {'CFS' : Builder(action = CFS, emitter = CFS_emitter)}) + +# source file list + sources = [Glob('*.c'), Glob('*.cc'), Glob('LoadFile/*.cc'), Glob('modes/*.cc')] -env.LoadFile('LoadFile-gen.inc', load_files) -env.Depends('LoadFile-gen.inc', 'genLoadFile.pl') +env.CFS('cfs.cc', load_files) +env.Depends('cfs.cc', 'cfs_gen/cfs_gen.py') env.Modes('modes.h', Glob('modes/*.h')) From 00b9723aac7ab5920ff3ca33626b2f86ded86d33 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 10:43:18 -0400 Subject: [PATCH 4/8] add glslUtil submodule --- .gitmodules | 3 +++ glslUtil | 1 + 2 files changed, 4 insertions(+) create mode 160000 glslUtil diff --git a/.gitmodules b/.gitmodules index beee7d0..14f8521 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "cfs_gen"] path = cfs_gen url = ../util/cfs_gen.git +[submodule "glslUtil"] + path = glslUtil + url = ../util/glslUtil.git diff --git a/glslUtil b/glslUtil new file mode 160000 index 0000000..0d78e3c --- /dev/null +++ b/glslUtil @@ -0,0 +1 @@ +Subproject commit 0d78e3cf4f606953087cdccae989f805e07ac0e3 From 55bf10c2825eba81032079b8d1e1751067410c75 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 11:35:23 -0400 Subject: [PATCH 5/8] reorganize source a bit, building but not rendering LogoBoxes --- LogoBox.cc | 68 ------------ SConstruct | 11 +- shaders/obj.fp | 39 +++++++ shaders/obj.vp | 13 +++ .../GnomeScreensaver.cc | 0 GnomeScreensaver.h => src/GnomeScreensaver.h | 0 src/LogoBox.cc | 102 ++++++++++++++++++ LogoBox.h => src/LogoBox.h | 5 +- src/WFObjLoadFile.cc | 16 +++ src/WFObjLoadFile.h | 9 ++ gs-theme-window.c => src/gs-theme-window.c | 0 gs-theme-window.h => src/gs-theme-window.h | 0 12 files changed, 191 insertions(+), 72 deletions(-) delete mode 100644 LogoBox.cc create mode 100644 shaders/obj.fp create mode 100644 shaders/obj.vp rename GnomeScreensaver.cc => src/GnomeScreensaver.cc (100%) rename GnomeScreensaver.h => src/GnomeScreensaver.h (100%) create mode 100644 src/LogoBox.cc rename LogoBox.h => src/LogoBox.h (80%) create mode 100644 src/WFObjLoadFile.cc create mode 100644 src/WFObjLoadFile.h rename gs-theme-window.c => src/gs-theme-window.c (100%) rename gs-theme-window.h => src/gs-theme-window.h (100%) diff --git a/LogoBox.cc b/LogoBox.cc deleted file mode 100644 index 459a29d..0000000 --- a/LogoBox.cc +++ /dev/null @@ -1,68 +0,0 @@ - -/* - * Author: Josh Holtrop - * DornerWorks ScreenSaver - * This module can be used to create "LogoBox" objects - * which consist of a 3D DW logo - */ - -#include -#include "WFObj.h" -#include "LogoBox.h" -#include "LoadFile.h" -#include /* memcpy() */ - -#include -using namespace std; - -static GLuint _logoList = 0; -static GLuint _drawList; -static LoadFile loadFile; -static float _width, _depth, _height; - -/* - * construct a LogoBox object - * The first time the constructor is called it loads the - * Alias Wavefront object model. Subsequent calls will - * reuse the same module. - */ -LogoBox::LogoBox() -{ - if (_logoList == 0) - { - WFObj obj(loadFile); - if (obj.load(FileLoader::Path("", "dwlogo.obj"))) - { - _logoList = obj.render(false); - if (_logoList == 0) - { - cerr << "Error rendering dwlogo.obj" << endl; - } - const float * aabb = obj.getAABB(); - - float c_x = (aabb[0] + aabb[3]) / 2.0f; - float c_y = (aabb[1] + aabb[4]) / 2.0f; - float c_z = (aabb[2] + aabb[5]) / 2.0f; - - _width = aabb[3] - aabb[0]; - _depth = aabb[4] - aabb[1]; - _height = aabb[5] - aabb[2]; - - _drawList = glGenLists(1); - glNewList(_drawList, GL_COMPILE); - glPushMatrix(); - glTranslatef(-c_x, -c_y, -c_z); - glCallList(_logoList); - glPopMatrix(); - glEndList(); - } - else - { - cerr << "Error loading dwlogo.obj" << endl; - } - } - m_drawList = _drawList; - m_width = _width; - m_depth = _depth; - m_height = _height; -} diff --git a/SConstruct b/SConstruct index c20de7b..15a90f7 100644 --- a/SConstruct +++ b/SConstruct @@ -10,6 +10,7 @@ load_files = ['logo/dwlogo.obj', 'logo/dwlogo.mtl', Glob('shaders/*')] env = Environment(CPPPATH = ['.']) env.ParseConfig("pkg-config --cflags --libs glib-2.0 gdk-2.0 atk gtk+-2.0 gtkglext-1.0") +env.Append(CPPFLAGS = ['-Isrc', '-DGL_GLEXT_PROTOTYPES']) # modes builder @@ -40,7 +41,15 @@ env.Append(BUILDERS = {'CFS' : Builder(action = CFS, emitter = CFS_emitter)}) # source file list -sources = [Glob('*.c'), Glob('*.cc'), Glob('LoadFile/*.cc'), Glob('modes/*.cc')] +sources = [ + 'cfs.cc', + Glob('src/*.c'), + Glob('src/*.cc'), + Glob('LoadFile/*.cc'), + Glob('modes/*.cc'), + Glob('wfobj/*.cc'), + Glob('glslUtil/*.c'), + ] env.CFS('cfs.cc', load_files) env.Depends('cfs.cc', 'cfs_gen/cfs_gen.py') diff --git a/shaders/obj.fp b/shaders/obj.fp new file mode 100644 index 0000000..35dc833 --- /dev/null +++ b/shaders/obj.fp @@ -0,0 +1,39 @@ + +#ifdef GL_FRAGMENT_PRECISION_HIGH +precision highp float; +#else +precision mediump float; +#endif + +uniform vec4 ambient, diffuse, specular; +uniform float shininess; + +varying vec3 pos_i; +varying vec3 normal_i; + +void main(void) +{ + vec3 n, lightDir; + vec4 color; + float NdotL, RdotEye; + + lightDir = normalize(vec3(-0.4, 0, -0.9)); + color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */ + n = normalize(normal_i); + + NdotL = max(dot(n, -lightDir), 0.0); + + if (NdotL > 0.0) + { + /* diffuse component */ + color += diffuse * NdotL; + /* specular component */ + RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n))); + if (RdotEye > 0.0) + { + color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0); + } + } + + gl_FragColor = color; +} diff --git a/shaders/obj.vp b/shaders/obj.vp new file mode 100644 index 0000000..7d88a5f --- /dev/null +++ b/shaders/obj.vp @@ -0,0 +1,13 @@ + +attribute vec3 pos; +attribute vec3 normal; + +varying vec3 pos_i; +varying vec3 normal_i; + +void main(void) +{ + gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1); + pos_i = gl_Position.xyz; + normal_i = gl_NormalMatrix * normal; +} diff --git a/GnomeScreensaver.cc b/src/GnomeScreensaver.cc similarity index 100% rename from GnomeScreensaver.cc rename to src/GnomeScreensaver.cc diff --git a/GnomeScreensaver.h b/src/GnomeScreensaver.h similarity index 100% rename from GnomeScreensaver.h rename to src/GnomeScreensaver.h diff --git a/src/LogoBox.cc b/src/LogoBox.cc new file mode 100644 index 0000000..bec312a --- /dev/null +++ b/src/LogoBox.cc @@ -0,0 +1,102 @@ +/* + * Author: Josh Holtrop + * DornerWorks ScreenSaver + * This module can be used to create "LogoBox" objects + * which consist of a 3D DW logo + */ + +#include + +#include + +#include "wfobj/WFObj.h" +#include "LogoBox.h" +#include "cfs.h" +#include "glslUtil/glslUtil.h" +#include "WFObjLoadFile.h" + +using namespace std; + +static WFObj obj; +static GLuint program; +static GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc; +enum Locations { + LOC_POSITION, + LOC_NORMAL +}; +static float _width, _depth, _height; +static bool loaded = false; + +/* + * construct a LogoBox object + * The first time the constructor is called it loads the + * Alias Wavefront object model. Subsequent calls will + * reuse the same module. + */ +LogoBox::LogoBox() +{ + if (!loaded) + { + if (obj.load("logo/dwlogo.obj", WFObjLoadFile)) + { + const float * aabb = obj.getAABB(); + + float c_x = (aabb[0] + aabb[3]) / 2.0f; + float c_y = (aabb[1] + aabb[4]) / 2.0f; + float c_z = (aabb[2] + aabb[5]) / 2.0f; + + _width = aabb[3] - aabb[0]; + _depth = aabb[4] - aabb[1]; + _height = aabb[5] - aabb[2]; + + loaded = loadShaders(); + } + else + { + cerr << "Error loading dwlogo.obj" << endl; + } + } + m_width = _width; + m_depth = _depth; + m_height = _height; +} + +bool LogoBox::loadShaders() +{ + const guAttribBinding bindings[] = { + {LOC_POSITION, "pos"}, + {LOC_NORMAL, "normal"}, + {0, NULL} + }; + const char *v_shader_src = (const char *) getFile("shaders/obj.vp", NULL); + const char *f_shader_src = (const char *) getFile("shaders/obj.fp", NULL); + if (v_shader_src == NULL || f_shader_src == NULL) + { + cerr << "Error reading shader source files" << endl; + return false; + } + program = guMakeProgramFromSource(v_shader_src, f_shader_src, bindings); + if (program == 0) + { + cerr << "Error creating shaders." << endl; + return false; + } + + ambient_loc = glGetUniformLocation(program, "ambient"); + diffuse_loc = glGetUniformLocation(program, "diffuse"); + specular_loc = glGetUniformLocation(program, "specular"); + shininess_loc = glGetUniformLocation(program, "shininess"); + + glUseProgram(program); + /* set up a default material */ + glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0); + glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); + glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); + glUniform1f(shininess_loc, 85.0); +} + +void LogoBox::draw() +{ + if (!loaded) + return; +} diff --git a/LogoBox.h b/src/LogoBox.h similarity index 80% rename from LogoBox.h rename to src/LogoBox.h index 66fc056..92f7b87 100644 --- a/LogoBox.h +++ b/src/LogoBox.h @@ -1,4 +1,3 @@ - /* * Author: Josh Holtrop * DornerWorks screensaver @@ -14,12 +13,12 @@ class LogoBox { public: LogoBox(); - void draw() { glCallList(m_drawList); } + void draw(); float getWidth() const { return m_width; } float getDepth() const { return m_depth; } float getHeight() const { return m_height; } protected: - GLuint m_drawList; + bool loadShaders(); float m_width, m_depth, m_height; }; diff --git a/src/WFObjLoadFile.cc b/src/WFObjLoadFile.cc new file mode 100644 index 0000000..78355ac --- /dev/null +++ b/src/WFObjLoadFile.cc @@ -0,0 +1,16 @@ + +#include /* memcpy() */ + +#include "WFObjLoadFile.h" +#include "cfs.h" + +bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff) +{ + unsigned int length; + const unsigned char *dat = getFile(fname, &length); + if (dat == NULL) + return false; + buff.alloc(length); + memcpy(buff.data, dat, length); + return true; +} diff --git a/src/WFObjLoadFile.h b/src/WFObjLoadFile.h new file mode 100644 index 0000000..4414faf --- /dev/null +++ b/src/WFObjLoadFile.h @@ -0,0 +1,9 @@ + +#ifndef WFOBJLOADFILE_H +#define WFOBJLOADFILE_H + +#include "wfobj/WFObj.h" + +bool WFObjLoadFile(const char *fname, WFObj::Buffer & buff); + +#endif /* WFOBJLOADFILE_H */ diff --git a/gs-theme-window.c b/src/gs-theme-window.c similarity index 100% rename from gs-theme-window.c rename to src/gs-theme-window.c diff --git a/gs-theme-window.h b/src/gs-theme-window.h similarity index 100% rename from gs-theme-window.h rename to src/gs-theme-window.h From 4cfa0893f65fff1339ccc0bf224b724d52be3494 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 12:05:26 -0400 Subject: [PATCH 6/8] fix build using dwss.cc for main() instead of wfobj/driver - whoops --- SConstruct | 2 +- dwss.cc => src/dwss.cc | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename dwss.cc => src/dwss.cc (100%) diff --git a/SConstruct b/SConstruct index 15a90f7..c7f69d4 100644 --- a/SConstruct +++ b/SConstruct @@ -47,7 +47,7 @@ sources = [ Glob('src/*.cc'), Glob('LoadFile/*.cc'), Glob('modes/*.cc'), - Glob('wfobj/*.cc'), + Glob('wfobj/WFObj.cc'), Glob('glslUtil/*.c'), ] diff --git a/dwss.cc b/src/dwss.cc similarity index 100% rename from dwss.cc rename to src/dwss.cc From d556ef69bf367a3df09fc2a0a85c0a4d37639d09 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 12:11:24 -0400 Subject: [PATCH 7/8] fill out LogoBox::draw(), rendering logos with shaders now --- src/LogoBox.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/LogoBox.cc b/src/LogoBox.cc index bec312a..bdd8526 100644 --- a/src/LogoBox.cc +++ b/src/LogoBox.cc @@ -93,10 +93,45 @@ bool LogoBox::loadShaders() glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); glUniform1f(shininess_loc, 85.0); + return true; +} + +static void renderMaterial(const WFObj::Material & m) +{ + if (m.flags & WFObj::Material::SHININESS_BIT) + glUniform1f(shininess_loc, m.shininess); + if (m.flags & WFObj::Material::AMBIENT_BIT) + glUniform4fv(ambient_loc, 1, &m.ambient[0]); + if (m.flags & WFObj::Material::DIFFUSE_BIT) + glUniform4fv(diffuse_loc, 1, &m.diffuse[0]); + if (m.flags & WFObj::Material::SPECULAR_BIT) + glUniform4fv(specular_loc, 1, &m.specular[0]); + if (m.flags & WFObj::Material::TEXTURE_BIT) + { + cerr << "error: textured materials not implemented yet" << endl; + } } void LogoBox::draw() { if (!loaded) return; + obj.bindBuffers(); + glUseProgram(program); + glEnableVertexAttribArray(LOC_POSITION); + glEnableVertexAttribArray(LOC_NORMAL); + int stride = obj.getStride(); + glVertexAttribPointer(LOC_POSITION, 3, GL_FLOAT, GL_FALSE, + stride, (GLvoid *) obj.getVertexOffset()); + glVertexAttribPointer(LOC_NORMAL, 3, GL_FLOAT, GL_FALSE, + stride, (GLvoid *) obj.getNormalOffset()); + for (map::iterator it = obj.getMaterials().begin(); + it != obj.getMaterials().end(); + it++) + { + renderMaterial(it->second); + glDrawElements(GL_TRIANGLES, it->second.num_vertices, + GL_UNSIGNED_SHORT, + (GLvoid *) (sizeof(GLushort) * it->second.first_vertex)); + } } From 4207934753cb7c68b4a10c8e15ec0f52b5a238f2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 11 Oct 2011 12:12:28 -0400 Subject: [PATCH 8/8] fix line endings on LogoBox sources --- src/LogoBox.cc | 274 ++++++++++++++++++++++++------------------------- src/LogoBox.h | 50 ++++----- 2 files changed, 162 insertions(+), 162 deletions(-) diff --git a/src/LogoBox.cc b/src/LogoBox.cc index bdd8526..8319b8e 100644 --- a/src/LogoBox.cc +++ b/src/LogoBox.cc @@ -1,137 +1,137 @@ -/* - * Author: Josh Holtrop - * DornerWorks ScreenSaver - * This module can be used to create "LogoBox" objects - * which consist of a 3D DW logo - */ - -#include - -#include - -#include "wfobj/WFObj.h" -#include "LogoBox.h" -#include "cfs.h" -#include "glslUtil/glslUtil.h" -#include "WFObjLoadFile.h" - -using namespace std; - -static WFObj obj; -static GLuint program; -static GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc; -enum Locations { - LOC_POSITION, - LOC_NORMAL -}; -static float _width, _depth, _height; -static bool loaded = false; - -/* - * construct a LogoBox object - * The first time the constructor is called it loads the - * Alias Wavefront object model. Subsequent calls will - * reuse the same module. - */ -LogoBox::LogoBox() -{ - if (!loaded) - { - if (obj.load("logo/dwlogo.obj", WFObjLoadFile)) - { - const float * aabb = obj.getAABB(); - - float c_x = (aabb[0] + aabb[3]) / 2.0f; - float c_y = (aabb[1] + aabb[4]) / 2.0f; - float c_z = (aabb[2] + aabb[5]) / 2.0f; - - _width = aabb[3] - aabb[0]; - _depth = aabb[4] - aabb[1]; - _height = aabb[5] - aabb[2]; - - loaded = loadShaders(); - } - else - { - cerr << "Error loading dwlogo.obj" << endl; - } - } - m_width = _width; - m_depth = _depth; - m_height = _height; -} - -bool LogoBox::loadShaders() -{ - const guAttribBinding bindings[] = { - {LOC_POSITION, "pos"}, - {LOC_NORMAL, "normal"}, - {0, NULL} - }; - const char *v_shader_src = (const char *) getFile("shaders/obj.vp", NULL); - const char *f_shader_src = (const char *) getFile("shaders/obj.fp", NULL); - if (v_shader_src == NULL || f_shader_src == NULL) - { - cerr << "Error reading shader source files" << endl; - return false; - } - program = guMakeProgramFromSource(v_shader_src, f_shader_src, bindings); - if (program == 0) - { - cerr << "Error creating shaders." << endl; - return false; - } - - ambient_loc = glGetUniformLocation(program, "ambient"); - diffuse_loc = glGetUniformLocation(program, "diffuse"); - specular_loc = glGetUniformLocation(program, "specular"); - shininess_loc = glGetUniformLocation(program, "shininess"); - - glUseProgram(program); - /* set up a default material */ - glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0); - glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); - glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); - glUniform1f(shininess_loc, 85.0); - return true; -} - -static void renderMaterial(const WFObj::Material & m) -{ - if (m.flags & WFObj::Material::SHININESS_BIT) - glUniform1f(shininess_loc, m.shininess); - if (m.flags & WFObj::Material::AMBIENT_BIT) - glUniform4fv(ambient_loc, 1, &m.ambient[0]); - if (m.flags & WFObj::Material::DIFFUSE_BIT) - glUniform4fv(diffuse_loc, 1, &m.diffuse[0]); - if (m.flags & WFObj::Material::SPECULAR_BIT) - glUniform4fv(specular_loc, 1, &m.specular[0]); - if (m.flags & WFObj::Material::TEXTURE_BIT) - { - cerr << "error: textured materials not implemented yet" << endl; - } -} - -void LogoBox::draw() -{ - if (!loaded) - return; - obj.bindBuffers(); - glUseProgram(program); - glEnableVertexAttribArray(LOC_POSITION); - glEnableVertexAttribArray(LOC_NORMAL); - int stride = obj.getStride(); - glVertexAttribPointer(LOC_POSITION, 3, GL_FLOAT, GL_FALSE, - stride, (GLvoid *) obj.getVertexOffset()); - glVertexAttribPointer(LOC_NORMAL, 3, GL_FLOAT, GL_FALSE, - stride, (GLvoid *) obj.getNormalOffset()); - for (map::iterator it = obj.getMaterials().begin(); - it != obj.getMaterials().end(); - it++) - { - renderMaterial(it->second); - glDrawElements(GL_TRIANGLES, it->second.num_vertices, - GL_UNSIGNED_SHORT, - (GLvoid *) (sizeof(GLushort) * it->second.first_vertex)); - } -} +/* + * Author: Josh Holtrop + * DornerWorks ScreenSaver + * This module can be used to create "LogoBox" objects + * which consist of a 3D DW logo + */ + +#include + +#include + +#include "wfobj/WFObj.h" +#include "LogoBox.h" +#include "cfs.h" +#include "glslUtil/glslUtil.h" +#include "WFObjLoadFile.h" + +using namespace std; + +static WFObj obj; +static GLuint program; +static GLint ambient_loc, diffuse_loc, specular_loc, shininess_loc; +enum Locations { + LOC_POSITION, + LOC_NORMAL +}; +static float _width, _depth, _height; +static bool loaded = false; + +/* + * construct a LogoBox object + * The first time the constructor is called it loads the + * Alias Wavefront object model. Subsequent calls will + * reuse the same module. + */ +LogoBox::LogoBox() +{ + if (!loaded) + { + if (obj.load("logo/dwlogo.obj", WFObjLoadFile)) + { + const float * aabb = obj.getAABB(); + + float c_x = (aabb[0] + aabb[3]) / 2.0f; + float c_y = (aabb[1] + aabb[4]) / 2.0f; + float c_z = (aabb[2] + aabb[5]) / 2.0f; + + _width = aabb[3] - aabb[0]; + _depth = aabb[4] - aabb[1]; + _height = aabb[5] - aabb[2]; + + loaded = loadShaders(); + } + else + { + cerr << "Error loading dwlogo.obj" << endl; + } + } + m_width = _width; + m_depth = _depth; + m_height = _height; +} + +bool LogoBox::loadShaders() +{ + const guAttribBinding bindings[] = { + {LOC_POSITION, "pos"}, + {LOC_NORMAL, "normal"}, + {0, NULL} + }; + const char *v_shader_src = (const char *) getFile("shaders/obj.vp", NULL); + const char *f_shader_src = (const char *) getFile("shaders/obj.fp", NULL); + if (v_shader_src == NULL || f_shader_src == NULL) + { + cerr << "Error reading shader source files" << endl; + return false; + } + program = guMakeProgramFromSource(v_shader_src, f_shader_src, bindings); + if (program == 0) + { + cerr << "Error creating shaders." << endl; + return false; + } + + ambient_loc = glGetUniformLocation(program, "ambient"); + diffuse_loc = glGetUniformLocation(program, "diffuse"); + specular_loc = glGetUniformLocation(program, "specular"); + shininess_loc = glGetUniformLocation(program, "shininess"); + + glUseProgram(program); + /* set up a default material */ + glUniform4f(ambient_loc, 0.2, 0.2, 0.2, 1.0); + glUniform4f(diffuse_loc, 1.0, 0.6, 0.0, 1.0); + glUniform4f(specular_loc, 1.0, 1.0, 1.0, 1.0); + glUniform1f(shininess_loc, 85.0); + return true; +} + +static void renderMaterial(const WFObj::Material & m) +{ + if (m.flags & WFObj::Material::SHININESS_BIT) + glUniform1f(shininess_loc, m.shininess); + if (m.flags & WFObj::Material::AMBIENT_BIT) + glUniform4fv(ambient_loc, 1, &m.ambient[0]); + if (m.flags & WFObj::Material::DIFFUSE_BIT) + glUniform4fv(diffuse_loc, 1, &m.diffuse[0]); + if (m.flags & WFObj::Material::SPECULAR_BIT) + glUniform4fv(specular_loc, 1, &m.specular[0]); + if (m.flags & WFObj::Material::TEXTURE_BIT) + { + cerr << "error: textured materials not implemented yet" << endl; + } +} + +void LogoBox::draw() +{ + if (!loaded) + return; + obj.bindBuffers(); + glUseProgram(program); + glEnableVertexAttribArray(LOC_POSITION); + glEnableVertexAttribArray(LOC_NORMAL); + int stride = obj.getStride(); + glVertexAttribPointer(LOC_POSITION, 3, GL_FLOAT, GL_FALSE, + stride, (GLvoid *) obj.getVertexOffset()); + glVertexAttribPointer(LOC_NORMAL, 3, GL_FLOAT, GL_FALSE, + stride, (GLvoid *) obj.getNormalOffset()); + for (map::iterator it = obj.getMaterials().begin(); + it != obj.getMaterials().end(); + it++) + { + renderMaterial(it->second); + glDrawElements(GL_TRIANGLES, it->second.num_vertices, + GL_UNSIGNED_SHORT, + (GLvoid *) (sizeof(GLushort) * it->second.first_vertex)); + } +} diff --git a/src/LogoBox.h b/src/LogoBox.h index 92f7b87..81461ce 100644 --- a/src/LogoBox.h +++ b/src/LogoBox.h @@ -1,25 +1,25 @@ -/* - * Author: Josh Holtrop - * DornerWorks screensaver - * The LogoBox class - */ - -#ifndef LOGOBOX_H -#define LOGOBOX_H - -#include - -class LogoBox -{ -public: - LogoBox(); - void draw(); - float getWidth() const { return m_width; } - float getDepth() const { return m_depth; } - float getHeight() const { return m_height; } -protected: - bool loadShaders(); - float m_width, m_depth, m_height; -}; - -#endif +/* + * Author: Josh Holtrop + * DornerWorks screensaver + * The LogoBox class + */ + +#ifndef LOGOBOX_H +#define LOGOBOX_H + +#include + +class LogoBox +{ +public: + LogoBox(); + void draw(); + float getWidth() const { return m_width; } + float getDepth() const { return m_depth; } + float getHeight() const { return m_height; } +protected: + bool loadShaders(); + float m_width, m_depth, m_height; +}; + +#endif