beginning migrate to shaders
add a few submodules generate C file store with cfs_gen remove FileLoader/TextureLoader add initial color shaders for wfobj objects
This commit is contained in:
parent
55fee6b9a9
commit
8028654ff2
3
.gitignore
vendored
3
.gitignore
vendored
@ -9,7 +9,8 @@
|
|||||||
/stderr.txt
|
/stderr.txt
|
||||||
/sdl_keymap.h
|
/sdl_keymap.h
|
||||||
/sdl_keymap.cc
|
/sdl_keymap.cc
|
||||||
/ag_lua.cc
|
/cfs.cc
|
||||||
|
/cfs.h
|
||||||
|
|
||||||
# /libsrc/
|
# /libsrc/
|
||||||
/libsrc/*.blend1
|
/libsrc/*.blend1
|
||||||
|
12
.gitmodules
vendored
Normal file
12
.gitmodules
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[submodule "cfs_gen"]
|
||||||
|
path = cfs_gen
|
||||||
|
url = ../util/cfs_gen.git
|
||||||
|
[submodule "wfobj"]
|
||||||
|
path = wfobj
|
||||||
|
url = ../util/wfobj.git
|
||||||
|
[submodule "refptr"]
|
||||||
|
path = refptr
|
||||||
|
url = ../util/refptr.git
|
||||||
|
[submodule "TextureCache"]
|
||||||
|
path = TextureCache
|
||||||
|
url = ../util/TextureCache.git
|
@ -1,88 +0,0 @@
|
|||||||
|
|
||||||
#ifndef FILELOADER_H
|
|
||||||
#define FILELOADER_H FILELOADER_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
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
|
|
51
SConstruct
51
SConstruct
@ -16,7 +16,6 @@ if os.sep == '\\':
|
|||||||
platform = 'PLATFORM_WINDOWS'
|
platform = 'PLATFORM_WINDOWS'
|
||||||
else:
|
else:
|
||||||
platform = 'PLATFORM_LINUX'
|
platform = 'PLATFORM_LINUX'
|
||||||
subdirs = ['WFObj', 'PhyObj', 'TextureCache', 'OdeWorld']
|
|
||||||
|
|
||||||
env = Environment()
|
env = Environment()
|
||||||
env.ParseConfig('pkg-config --cflags --libs lua5.1')
|
env.ParseConfig('pkg-config --cflags --libs lua5.1')
|
||||||
@ -37,38 +36,40 @@ env.Append(CCFLAGS = ['-O2', '-Wall'])
|
|||||||
if DEBUG:
|
if DEBUG:
|
||||||
env.Append(CCFLAGS = ['-g'])
|
env.Append(CCFLAGS = ['-g'])
|
||||||
|
|
||||||
sources = [Glob('src/*.cc'), 'ag_lua.cc', 'sdl_keymap.cc']
|
sources = [
|
||||||
for sd in subdirs:
|
'cfs.cc',
|
||||||
sources += [Glob(sd + '/*.c'), Glob(sd + '/*.cc')]
|
'sdl_keymap.cc',
|
||||||
|
Glob('src/*.cc'),
|
||||||
|
'OdeWorld/OdeWorld.cc',
|
||||||
|
'PhyObj/PhyObj.cc',
|
||||||
|
'TextureCache/TextureCache.cc',
|
||||||
|
'wfobj/WFObj.cc',
|
||||||
|
]
|
||||||
|
|
||||||
def F2C(target, source, env):
|
def CFS(target, source, env):
|
||||||
f = open(str(target[0]), 'w')
|
source_list = []
|
||||||
for s in source:
|
for s in source:
|
||||||
c_name = re.sub(r'\W', '_', str(s).split('/')[-1])
|
source_list.append(str(s))
|
||||||
f.write('unsigned char %s[] = {' % c_name)
|
Popen(['./cfs_gen/cfs_gen.py', str(target[0])] + source_list).wait()
|
||||||
src = open(str(s), 'r')
|
|
||||||
s_len = 0
|
|
||||||
while 1:
|
|
||||||
if s_len % 12 == 0:
|
|
||||||
f.write('\n ')
|
|
||||||
ch = src.read(1)
|
|
||||||
if len(ch) < 1:
|
|
||||||
break
|
|
||||||
s_len += 1
|
|
||||||
f.write('0x%02x, ' % ord(ch))
|
|
||||||
f.write('0x00\n')
|
|
||||||
src.close()
|
|
||||||
f.write('};\n')
|
|
||||||
f.write('unsigned int %s_len = %d;\n' % (c_name, s_len))
|
|
||||||
f.close()
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def CFS_emitter(target, source, env):
|
||||||
|
target.append(re.sub(r'\.cc?', '.h', str(target[0])))
|
||||||
|
return target, source
|
||||||
|
|
||||||
def bt(cmd):
|
def bt(cmd):
|
||||||
return Popen(cmd, shell = True, stdout = PIPE).communicate()[0]
|
return Popen(cmd, shell = True, stdout = PIPE).communicate()[0]
|
||||||
|
|
||||||
env.Append(BUILDERS = {'F2C' : Builder(action = F2C)})
|
env.Append(BUILDERS = {'CFS' : Builder(action = CFS, emitter = CFS_emitter)})
|
||||||
|
|
||||||
env.F2C('ag_lua.cc', 'src/ag.lua')
|
cfs_sources = [
|
||||||
|
'src/ag.lua',
|
||||||
|
'shaders/wfobj.f.glsl',
|
||||||
|
'shaders/wfobj.v.glsl',
|
||||||
|
]
|
||||||
|
|
||||||
|
env.CFS('cfs.cc', cfs_sources)
|
||||||
|
env.Depends('cfs.cc', 'cfs_gen/cfs_gen.py')
|
||||||
env.Command('sdl_keymap.cc', 'gen-sdl-keymap.pl', 'perl gen-sdl-keymap.pl')
|
env.Command('sdl_keymap.cc', 'gen-sdl-keymap.pl', 'perl gen-sdl-keymap.pl')
|
||||||
env.Program(target, sources)
|
env.Program(target, sources)
|
||||||
|
|
||||||
|
1
TextureCache
Submodule
1
TextureCache
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 93490cb3d1e103e73ece437a3dc452c54efccc46
|
@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
TARGET := TextureCache.o
|
|
||||||
SOURCES := $(wildcard *.cc)
|
|
||||||
HEADERS := $(wildcard *.h)
|
|
||||||
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
$(TARGET): $(SOURCES) $(HEADERS)
|
|
||||||
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCES)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-rm -f *~ *.o
|
|
@ -1,143 +0,0 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <SDL_image.h>
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <GL/glu.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include "TextureCache.h"
|
|
||||||
#include "TextureLoader/TextureLoader.h"
|
|
||||||
#include "FileLoader/FileLoader.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//#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
|
|
||||||
|
|
||||||
TextureCache::~TextureCache()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint TextureCache::load(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality)
|
|
||||||
{
|
|
||||||
string filename = path.shortPath;
|
|
||||||
if (filename == "")
|
|
||||||
filename = path.fullPath;
|
|
||||||
if (filename == "")
|
|
||||||
return 0;
|
|
||||||
map<string,GLuint>::iterator it = m_cache.find(filename);
|
|
||||||
if (it != m_cache.end())
|
|
||||||
{
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
GLuint tex = loadTexture(path, fileLoader, mipmaps, mode, quality);
|
|
||||||
if (tex > 0)
|
|
||||||
{
|
|
||||||
m_cache[filename] = tex;
|
|
||||||
}
|
|
||||||
return tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint TextureCache::loadTexture(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
GLuint texture;
|
|
||||||
FileLoader::Buffer buff = fileLoader.load(path);
|
|
||||||
if (buff.size <= 0)
|
|
||||||
return 0;
|
|
||||||
SDL_RWops * ops = SDL_RWFromMem(buff.data, buff.size);
|
|
||||||
SDL_Surface * temp = IMG_Load_RW(ops, true);
|
|
||||||
if (!temp)
|
|
||||||
{
|
|
||||||
cerr << "Failed to load image ('" << path.fullPath << "', '"
|
|
||||||
<< path.shortPath << "'): " << IMG_GetError() << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_PixelFormat fmt;
|
|
||||||
fmt.palette = NULL;
|
|
||||||
fmt.BitsPerPixel = 32;
|
|
||||||
fmt.BytesPerPixel = 4;
|
|
||||||
fmt.Rmask = 0x000000FF;
|
|
||||||
fmt.Gmask = 0x0000FF00;
|
|
||||||
fmt.Bmask = 0x00FF0000;
|
|
||||||
fmt.Amask = 0xFF000000;
|
|
||||||
fmt.Rshift = 0;
|
|
||||||
fmt.Gshift = 8;
|
|
||||||
fmt.Bshift = 16;
|
|
||||||
fmt.Ashift = 24;
|
|
||||||
|
|
||||||
SDL_Surface * texsurf = SDL_ConvertSurface(temp, &fmt, SDL_SWSURFACE);
|
|
||||||
SDL_FreeSurface(temp);
|
|
||||||
if (!texsurf)
|
|
||||||
{
|
|
||||||
cerr << "Image was not converted properly!" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
unsigned int * pixels = new unsigned int[texsurf->w * texsurf->h];
|
|
||||||
int y;
|
|
||||||
unsigned int dstOffset = texsurf->w * (texsurf->h - 1);
|
|
||||||
unsigned int srcOffset = 0;
|
|
||||||
for (y = 0; y < texsurf->h; y++)
|
|
||||||
{
|
|
||||||
memcpy(pixels + dstOffset,
|
|
||||||
((unsigned int *)texsurf->pixels) + srcOffset,
|
|
||||||
texsurf->w << 2);
|
|
||||||
dstOffset -= texsurf->w;
|
|
||||||
srcOffset += texsurf->w;
|
|
||||||
}
|
|
||||||
glGenTextures(1, &texture);
|
|
||||||
checkGLError();
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
checkGLError();
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, 4, texsurf->w, texsurf->h, 0,
|
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
|
|
||||||
if (quality > 0)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
mipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
|
|
||||||
checkGLError();
|
|
||||||
|
|
||||||
if (mipmaps)
|
|
||||||
{
|
|
||||||
checkGLError();
|
|
||||||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
|
|
||||||
texsurf->w, texsurf->h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
checkGLError();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_FreeSurface(texsurf);
|
|
||||||
delete[] pixels;
|
|
||||||
checkGLError();
|
|
||||||
return texture;
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
|
|
||||||
#ifndef TEXTURECACHE_H
|
|
||||||
#define TEXTURECACHE_H TEXTURECACHE_H
|
|
||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
#include "TextureLoader/TextureLoader.h"
|
|
||||||
#include "FileLoader/FileLoader.h"
|
|
||||||
|
|
||||||
class TextureCache : public TextureLoader
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~TextureCache();
|
|
||||||
virtual GLuint load(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps = true,
|
|
||||||
int mode = GL_DECAL, int quality = 1);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/* methods */
|
|
||||||
GLuint loadTexture(const FileLoader::Path & path,
|
|
||||||
FileLoader & fileLoader, bool mipmaps,
|
|
||||||
int mode, int quality);
|
|
||||||
|
|
||||||
/* data */
|
|
||||||
std::map< std::string, GLuint > m_cache;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,18 +0,0 @@
|
|||||||
|
|
||||||
#ifndef TEXTURELOADER_H
|
|
||||||
#define TEXTURELOADER_H TEXTURELOADER_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include "FileLoader/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
|
|
@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
TARGET := WFObj.o
|
|
||||||
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
%.o: %.cc
|
|
||||||
$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-rm -f *~ *.o
|
|
@ -1,18 +0,0 @@
|
|||||||
|
|
||||||
CXX := g++
|
|
||||||
CXXFLAGS := -O2
|
|
||||||
SOURCE := WFObj.cc driver.cc
|
|
||||||
OBJS := $(SOURCE:.cc=.o)
|
|
||||||
LDFLAGS := -lGL
|
|
||||||
TARGET := driver
|
|
||||||
|
|
||||||
all: $(TARGET)
|
|
||||||
|
|
||||||
$(TARGET): $(OBJS)
|
|
||||||
$(CXX) -o $@ $^ $(LDFLAGS)
|
|
||||||
|
|
||||||
%.o: %.cc %.hh
|
|
||||||
$(CXX) -c -o $@ $< $(CXXFLAGS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
-rm -f *~ *.o $(TARGET)
|
|
703
WFObj/WFObj.cc
703
WFObj/WFObj.cc
@ -1,703 +0,0 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <ctype.h> // isspace()
|
|
||||||
#include <string.h> // strlen()
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#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<string> splitString(const string & str, char delim)
|
|
||||||
{
|
|
||||||
vector<string> 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<std::string> >();
|
|
||||||
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<string> 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<Vertex> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WFObj::Vertex WFObj::readVertex(const vector<string> & 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<string> 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<string> > >();
|
|
||||||
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<string> 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<string> > >::iterator it = m_data.find(mtlname);
|
|
||||||
if (it == m_data.end())
|
|
||||||
return;
|
|
||||||
vector< vector<string> > & 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<string> > >::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);
|
|
||||||
}
|
|
@ -1,92 +0,0 @@
|
|||||||
|
|
||||||
#ifndef WFOBJ_H
|
|
||||||
#define WFOBJ_H
|
|
||||||
|
|
||||||
#include "FileLoader/FileLoader.h"
|
|
||||||
#include "TextureLoader/TextureLoader.h"
|
|
||||||
#include <GL/gl.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
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<std::string> > > 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; }
|
|
||||||
|
|
||||||
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<std::string> & 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<std::string> > m_data;
|
|
||||||
FileLoader::Path m_path;
|
|
||||||
float m_aabb[6];
|
|
||||||
bool m_loadedVertex;
|
|
||||||
FileLoader * m_fileLoader;
|
|
||||||
TextureLoader * m_textureLoader;
|
|
||||||
bool m_iCreatedFileLoader;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
1
cfs_gen
Submodule
1
cfs_gen
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 6954147f53d57724b7666bf4a1226e17ca64f734
|
1
refptr
Submodule
1
refptr
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit d5c4876ff8bd674c9d788dfa58eeae35249f3edc
|
@ -1,99 +0,0 @@
|
|||||||
|
|
||||||
#ifndef REFPTR_H
|
|
||||||
#define REFPTR_H REFPTR_H
|
|
||||||
|
|
||||||
/* Author: Josh Holtrop
|
|
||||||
* Purpose: Provide a reference-counting pointer-like first order
|
|
||||||
* C++ object that will free the object it is pointing to when
|
|
||||||
* all references to it have been destroyed.
|
|
||||||
* This implementation does not solve the circular reference problem.
|
|
||||||
* I was not concerned with that when developing this class.
|
|
||||||
*/
|
|
||||||
#include <stdlib.h> /* NULL */
|
|
||||||
|
|
||||||
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>();
|
|
||||||
T & operator*() const { return *m_ptr; }
|
|
||||||
T * operator->() const { return m_ptr; }
|
|
||||||
bool isNull() const { return m_ptr == NULL; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void cloneFrom(const refptr<T> & orig);
|
|
||||||
void destroy();
|
|
||||||
|
|
||||||
T * m_ptr;
|
|
||||||
int * m_refCount;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr()
|
|
||||||
{
|
|
||||||
m_ptr = NULL;
|
|
||||||
m_refCount = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr(T * ptr)
|
|
||||||
{
|
|
||||||
m_ptr = ptr;
|
|
||||||
m_refCount = new int;
|
|
||||||
*m_refCount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
cloneFrom(orig);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
cloneFrom(orig);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T> & refptr<T>::operator=(T * ptr)
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
m_ptr = ptr;
|
|
||||||
m_refCount = new int;
|
|
||||||
*m_refCount = 1;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
this->m_ptr = orig.m_ptr;
|
|
||||||
this->m_refCount = orig.m_refCount;
|
|
||||||
if (m_refCount != NULL)
|
|
||||||
(*m_refCount)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::~refptr()
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void refptr<T>::destroy()
|
|
||||||
{
|
|
||||||
if (m_refCount != NULL)
|
|
||||||
{
|
|
||||||
if (*m_refCount <= 1)
|
|
||||||
{
|
|
||||||
delete m_ptr;
|
|
||||||
delete m_refCount;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
(*m_refCount)--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
39
shaders/wfobj.f.glsl
Normal file
39
shaders/wfobj.f.glsl
Normal file
@ -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;
|
||||||
|
}
|
16
shaders/wfobj.v.glsl
Normal file
16
shaders/wfobj.v.glsl
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform mat4 modelview;
|
||||||
|
|
||||||
|
attribute vec3 pos;
|
||||||
|
attribute vec3 normal;
|
||||||
|
|
||||||
|
varying vec3 pos_i;
|
||||||
|
varying vec3 normal_i;
|
||||||
|
|
||||||
|
void main(void)
|
||||||
|
{
|
||||||
|
gl_Position = projection * modelview * vec4(pos, 1);
|
||||||
|
pos_i = gl_Position.xyz;
|
||||||
|
normal_i = normal;
|
||||||
|
}
|
@ -13,7 +13,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "ag.h"
|
#include "ag.h"
|
||||||
#include "ag_lua.h"
|
#include "cfs.h"
|
||||||
#include "anaglym.h"
|
#include "anaglym.h"
|
||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
#include "WFObj/WFObj.h"
|
#include "WFObj/WFObj.h"
|
||||||
@ -86,8 +86,7 @@ namespace ag
|
|||||||
};
|
};
|
||||||
luaL_register(L, "ag", functions);
|
luaL_register(L, "ag", functions);
|
||||||
|
|
||||||
ag_lua[ag_lua_len - 1] = '\0';
|
luaL_loadstring(L, (const char *) getFile("src/ag.lua"));
|
||||||
luaL_loadstring(L, (const char *) ag_lua);
|
|
||||||
|
|
||||||
lua_pcall(L, 0, 0, 0);
|
lua_pcall(L, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
#ifndef AG_LUA_H
|
|
||||||
#define AG_LUA_H
|
|
||||||
|
|
||||||
extern unsigned char ag_lua[];
|
|
||||||
extern unsigned int ag_lua_len;
|
|
||||||
|
|
||||||
#endif
|
|
1
wfobj
Submodule
1
wfobj
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 1ecc6d749be3bc3f5dc0de230b5e8e3ca079377a
|
Loading…
x
Reference in New Issue
Block a user