-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#include <cstring>
#include <fstream>
#include <boost/algorithm/string/trim.hpp>
#include <zlib.h>
-#include <stlplus/strings/string_utilities.hpp>
-
#include "debug.hh"
#include "log.hh"
#include "mesh.hh"
// TODO: this file needs to be cleaned up
-
-#define AC3D_FORMAT_VERSION (0x0b)
-#define ZLIB_BUF_SIZE (262114)
+#define AC3D_FORMAT_VERSION 0x0b
+#define ZLIB_BUF_SIZE 262114
namespace moof {
MOOF_REGISTER_RESOURCE(mesh, ac, models);
-
static std::string read_string(std::istream& stream)
{
std::string str;
char atom;
- do // skip to the next non-space character
- {
- stream.get(atom);
- }
+ do stream.get(atom);
while (stream && std::isspace(atom));
if (atom == '"')
{
- do
- {
+ do {
stream.get(atom);
if (atom == '"') break;
str += atom;
}
else
{
- do
- {
+ do {
stream.get(atom);
if (std::isspace(atom)) break;
str += atom;
return str;
}
-inline int read_hex(std::istream& stream)
+static int read_hex(std::istream& stream)
{
int hex;
std::ios::fmtflags flags = stream.flags();
return hex;
}
-inline vector2 read_pair(std::istream& stream)
+static vector2 read_pair(std::istream& stream)
{
vector2 triplet;
stream >> triplet[0] >> triplet[1];
return triplet;
}
-inline vector3 read_triplet(std::istream& stream)
+static vector3 read_triplet(std::istream& stream)
{
vector3 triplet;
stream >> triplet[0] >> triplet[1] >> triplet[2];
return triplet;
}
-inline vector4 read_color(std::istream& stream)
+static vector4 read_color(std::istream& stream)
{
vector4 color;
stream >> color[0] >> color[1] >> color[2];
return color;
}
-
void mesh::import(std::istream& stream)
{
- std::string atom;
-
- object_ptr obj;
+ std::string atom;
+ object_ptr obj;
std::stack<int> kids;
// read and verify the AC3D header
//face.triangles.push_back(vert);
//}
-
-
mesh::mesh(const std::string& path)
{
std::ifstream file(path.c_str(), std::ifstream::in |
- std::ifstream::binary);
+ std::ifstream::binary);
if (!file) throw std::runtime_error("cannot find mesh file");
// if we can read the header, the file isn't compressed
{
log_info("text mesh detected");
file.seekg(std::ios::beg);
-
import(file);
}
else
zstream.next_in = Z_NULL;
int result = inflateInit2(&zstream, 32+MAX_WBITS);
- if (result != Z_OK) throw std::runtime_error("zlib init error");
+ if (result != Z_OK)
+ throw std::runtime_error("zlib init error");
- do
- {
+ do {
file.read(in, sizeof(in));
zstream.next_in = (Bytef*)in;
zstream.avail_in = file.gcount();
if (zstream.avail_in == 0) break;
- do
- {
+ do {
zstream.next_out = (Bytef*)out;
zstream.avail_out = sizeof(out);
result = inflate(&zstream, Z_NO_FLUSH);
switch (result)
{
- case Z_NEED_DICT:
- case Z_DATA_ERROR:
- case Z_MEM_ERROR:
- inflateEnd(&zstream);
- throw std::runtime_error("zlib inflate error");
- case Z_STREAM_ERROR:
- throw std::runtime_error("zlib stream error");
+ case Z_NEED_DICT:
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ inflateEnd(&zstream);
+ throw std::runtime_error("zlib inflate error");
+ case Z_STREAM_ERROR:
+ throw std::runtime_error("zlib stream error");
}
int inflated = sizeof(out) - zstream.avail_out;
while(result != Z_STREAM_END);
inflateEnd(&zstream);
-
import(stream);
}
}
-
void mesh::draw(scalar alpha) const
{
glEnableClientState(GL_VERTEX_ARRAY);
// TODO: disable vertex array?
}
-
void mesh::set_material(int index) const
{
set_material(materials_[index]);
glMaterial(GL_FRONT, GL_SHININESS, material.shininess);
}
-
void mesh::object::draw(scalar alpha, bool recurse) const
{
glVertexPointer(verts);
{
std::vector<object_ptr>::const_iterator jt;
for (jt = kids.begin(); jt != kids.end(); ++jt)
- {
(*jt)->draw(alpha);
- }
}
}
-
//class mesh_resource_loader
//{
//public: