2015-07-22 19:14:50 +00:00
|
|
|
#ifndef BLENDERCONNECTION_HPP
|
|
|
|
#define BLENDERCONNECTION_HPP
|
2015-05-23 21:59:40 +00:00
|
|
|
|
2015-05-24 04:51:16 +00:00
|
|
|
#if _WIN32
|
2015-08-31 03:36:24 +00:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#endif
|
2015-09-27 04:35:36 +00:00
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
2015-05-24 04:51:16 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2015-05-23 21:59:40 +00:00
|
|
|
#include <unistd.h>
|
2015-05-24 04:51:16 +00:00
|
|
|
#endif
|
2015-05-23 21:59:40 +00:00
|
|
|
|
2015-07-22 19:14:50 +00:00
|
|
|
#include <stdint.h>
|
2015-08-03 02:05:04 +00:00
|
|
|
#include <stdio.h>
|
2015-05-24 22:19:28 +00:00
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
2015-10-12 05:19:04 +00:00
|
|
|
#include <iostream>
|
2015-10-17 02:06:27 +00:00
|
|
|
#include <unordered_map>
|
2015-05-24 22:19:28 +00:00
|
|
|
|
2015-07-28 02:25:33 +00:00
|
|
|
#include "HECL/HECL.hpp"
|
2015-11-14 02:26:06 +00:00
|
|
|
#include "HECL/HMDLMeta.hpp"
|
2015-10-09 02:08:10 +00:00
|
|
|
#include <Athena/Types.hpp>
|
2015-11-14 02:26:06 +00:00
|
|
|
#include <Athena/MemoryWriter.hpp>
|
2015-07-28 02:25:33 +00:00
|
|
|
|
|
|
|
namespace HECL
|
|
|
|
{
|
|
|
|
|
|
|
|
extern LogVisor::LogModule BlenderLog;
|
|
|
|
extern class BlenderConnection* SharedBlenderConnection;
|
2015-11-14 02:26:06 +00:00
|
|
|
class HMDLBuffers;
|
2015-07-28 02:25:33 +00:00
|
|
|
|
2015-07-22 19:14:50 +00:00
|
|
|
class BlenderConnection
|
2015-05-23 21:59:40 +00:00
|
|
|
{
|
2015-10-04 05:08:24 +00:00
|
|
|
public:
|
2015-11-21 01:13:06 +00:00
|
|
|
enum class BlendType
|
2015-10-04 05:08:24 +00:00
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
None,
|
|
|
|
Mesh,
|
|
|
|
Actor,
|
|
|
|
Area,
|
|
|
|
World,
|
|
|
|
MapArea,
|
|
|
|
MapUniverse
|
2015-10-04 05:08:24 +00:00
|
|
|
};
|
|
|
|
private:
|
2015-08-31 03:36:24 +00:00
|
|
|
bool m_lock = false;
|
2015-05-24 04:51:16 +00:00
|
|
|
#if _WIN32
|
|
|
|
HANDLE m_blenderProc;
|
|
|
|
#else
|
2015-05-23 21:59:40 +00:00
|
|
|
pid_t m_blenderProc;
|
2015-08-31 03:36:24 +00:00
|
|
|
#endif
|
2015-05-24 04:51:16 +00:00
|
|
|
int m_readpipe[2];
|
|
|
|
int m_writepipe[2];
|
2015-11-21 01:13:06 +00:00
|
|
|
BlendType m_loadedType = BlendType::None;
|
2015-10-07 01:16:54 +00:00
|
|
|
ProjectPath m_loadedBlend;
|
2015-09-06 20:08:23 +00:00
|
|
|
std::string m_startupBlend;
|
2015-05-24 22:19:28 +00:00
|
|
|
size_t _readLine(char* buf, size_t bufSz);
|
|
|
|
size_t _writeLine(const char* buf);
|
2015-10-02 04:06:45 +00:00
|
|
|
size_t _readBuf(void* buf, size_t len);
|
|
|
|
size_t _writeBuf(const void* buf, size_t len);
|
2015-05-24 22:19:28 +00:00
|
|
|
void _closePipe();
|
2015-05-23 21:59:40 +00:00
|
|
|
public:
|
2015-09-22 01:41:38 +00:00
|
|
|
BlenderConnection(int verbosityLevel=1);
|
2015-07-22 19:14:50 +00:00
|
|
|
~BlenderConnection();
|
2015-05-24 04:51:16 +00:00
|
|
|
|
2015-10-07 01:16:54 +00:00
|
|
|
bool createBlend(const ProjectPath& path, BlendType type);
|
2015-10-04 05:08:24 +00:00
|
|
|
BlendType getBlendType() const {return m_loadedType;}
|
2015-10-16 08:02:59 +00:00
|
|
|
bool openBlend(const ProjectPath& path, bool force=false);
|
2015-08-04 21:37:12 +00:00
|
|
|
bool saveBlend();
|
2015-08-05 22:59:59 +00:00
|
|
|
void deleteBlend();
|
2015-07-25 23:01:02 +00:00
|
|
|
|
2015-11-21 01:13:06 +00:00
|
|
|
enum class ANIMCurveType
|
2015-10-23 00:44:37 +00:00
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
Rotate,
|
|
|
|
Translate,
|
|
|
|
Scale
|
2015-10-23 00:44:37 +00:00
|
|
|
};
|
|
|
|
|
2015-07-25 23:01:02 +00:00
|
|
|
class PyOutStream : public std::ostream
|
|
|
|
{
|
|
|
|
friend class BlenderConnection;
|
|
|
|
BlenderConnection* m_parent;
|
2015-08-05 22:59:59 +00:00
|
|
|
bool m_deleteOnError;
|
2015-07-25 23:01:02 +00:00
|
|
|
struct StreamBuf : std::streambuf
|
|
|
|
{
|
2015-08-05 22:59:59 +00:00
|
|
|
PyOutStream& m_parent;
|
2015-07-28 02:25:33 +00:00
|
|
|
std::string m_lineBuf;
|
2015-08-05 22:59:59 +00:00
|
|
|
bool m_deleteOnError;
|
|
|
|
StreamBuf(PyOutStream& parent, bool deleteOnError)
|
|
|
|
: m_parent(parent), m_deleteOnError(deleteOnError) {}
|
2015-07-25 23:01:02 +00:00
|
|
|
StreamBuf(const StreamBuf& other) = delete;
|
|
|
|
StreamBuf(StreamBuf&& other) = default;
|
|
|
|
int_type overflow(int_type ch)
|
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
if (!m_parent.m_parent || !m_parent.m_parent->m_lock)
|
2015-08-05 22:59:59 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, "lock not held for PyOutStream writing");
|
2015-08-31 03:36:24 +00:00
|
|
|
if (ch != traits_type::eof() && ch != '\n' && ch != '\0')
|
2015-07-25 23:01:02 +00:00
|
|
|
{
|
2015-07-28 02:25:33 +00:00
|
|
|
m_lineBuf += char_type(ch);
|
|
|
|
return ch;
|
2015-07-25 23:01:02 +00:00
|
|
|
}
|
2015-08-31 03:36:24 +00:00
|
|
|
//printf("FLUSHING %s\n", m_lineBuf.c_str());
|
2015-08-05 22:59:59 +00:00
|
|
|
m_parent.m_parent->_writeLine(m_lineBuf.c_str());
|
2015-07-28 02:25:33 +00:00
|
|
|
char readBuf[16];
|
2015-08-05 22:59:59 +00:00
|
|
|
m_parent.m_parent->_readLine(readBuf, 16);
|
2015-07-28 02:25:33 +00:00
|
|
|
if (strcmp(readBuf, "OK"))
|
2015-08-05 22:59:59 +00:00
|
|
|
{
|
|
|
|
if (m_deleteOnError)
|
|
|
|
m_parent.m_parent->deleteBlend();
|
2015-07-28 02:25:33 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, "error sending '%s' to blender", m_lineBuf.c_str());
|
2015-08-05 22:59:59 +00:00
|
|
|
}
|
2015-07-28 02:25:33 +00:00
|
|
|
m_lineBuf.clear();
|
2015-07-25 23:01:02 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
} m_sbuf;
|
2015-08-05 22:59:59 +00:00
|
|
|
PyOutStream(BlenderConnection* parent, bool deleteOnError)
|
2015-09-03 01:43:20 +00:00
|
|
|
: std::ostream(&m_sbuf),
|
|
|
|
m_parent(parent),
|
2015-08-05 22:59:59 +00:00
|
|
|
m_deleteOnError(deleteOnError),
|
2015-09-03 01:43:20 +00:00
|
|
|
m_sbuf(*this, deleteOnError)
|
2015-07-25 23:01:02 +00:00
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
m_parent->m_lock = true;
|
2015-07-28 02:25:33 +00:00
|
|
|
m_parent->_writeLine("PYBEGIN");
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "READY"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to open PyOutStream with blender");
|
2015-07-25 23:01:02 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
PyOutStream(const PyOutStream& other) = delete;
|
|
|
|
PyOutStream(PyOutStream&& other)
|
2015-09-03 01:43:20 +00:00
|
|
|
: std::ostream(&m_sbuf), m_parent(other.m_parent), m_sbuf(std::move(other.m_sbuf))
|
2015-07-25 23:01:02 +00:00
|
|
|
{other.m_parent = nullptr;}
|
2015-08-05 22:59:59 +00:00
|
|
|
~PyOutStream() {close();}
|
|
|
|
void close()
|
2015-07-25 23:01:02 +00:00
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
if (m_parent && m_parent->m_lock)
|
2015-07-28 02:25:33 +00:00
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
m_parent->_writeLine("PYEND");
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "DONE"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to close PyOutStream with blender");
|
|
|
|
m_parent->m_lock = false;
|
2015-07-28 02:25:33 +00:00
|
|
|
}
|
2015-07-25 23:01:02 +00:00
|
|
|
}
|
2015-09-24 01:00:30 +00:00
|
|
|
#if __GNUC__
|
|
|
|
__attribute__((__format__ (__printf__, 2, 3)))
|
|
|
|
#endif
|
2015-08-03 02:05:04 +00:00
|
|
|
void format(const char* fmt, ...)
|
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
if (!m_parent || !m_parent->m_lock)
|
2015-08-05 22:59:59 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, "lock not held for PyOutStream::format()");
|
2015-08-03 02:05:04 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
char* result = nullptr;
|
2015-08-31 03:36:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int length = _vscprintf(fmt, ap);
|
|
|
|
result = (char*)malloc(length);
|
|
|
|
vsnprintf(result, length, fmt, ap);
|
|
|
|
#else
|
2015-08-03 02:05:04 +00:00
|
|
|
int length = vasprintf(&result, fmt, ap);
|
2015-08-31 03:36:24 +00:00
|
|
|
#endif
|
|
|
|
va_end(ap);
|
2015-08-03 02:05:04 +00:00
|
|
|
if (length > 0)
|
|
|
|
this->write(result, length);
|
|
|
|
free(result);
|
|
|
|
}
|
2015-10-24 01:21:58 +00:00
|
|
|
void linkBlend(const char* target, const char* objName, bool link=true);
|
|
|
|
void linkBackground(const char* target, const char* sceneName);
|
|
|
|
|
|
|
|
void AABBToBMesh(const atVec3f& min, const atVec3f& max)
|
|
|
|
{
|
|
|
|
format("bm = bmesh.new()\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.new((%f,%f,%f))\n"
|
|
|
|
"bm.verts.ensure_lookup_table()\n"
|
|
|
|
"bm.edges.new((bm.verts[0], bm.verts[1]))\n"
|
|
|
|
"bm.edges.new((bm.verts[0], bm.verts[2]))\n"
|
|
|
|
"bm.edges.new((bm.verts[0], bm.verts[4]))\n"
|
|
|
|
"bm.edges.new((bm.verts[3], bm.verts[1]))\n"
|
|
|
|
"bm.edges.new((bm.verts[3], bm.verts[2]))\n"
|
|
|
|
"bm.edges.new((bm.verts[3], bm.verts[7]))\n"
|
|
|
|
"bm.edges.new((bm.verts[5], bm.verts[1]))\n"
|
|
|
|
"bm.edges.new((bm.verts[5], bm.verts[4]))\n"
|
|
|
|
"bm.edges.new((bm.verts[5], bm.verts[7]))\n"
|
|
|
|
"bm.edges.new((bm.verts[6], bm.verts[2]))\n"
|
|
|
|
"bm.edges.new((bm.verts[6], bm.verts[4]))\n"
|
|
|
|
"bm.edges.new((bm.verts[6], bm.verts[7]))\n",
|
|
|
|
min.vec[0], min.vec[1], min.vec[2],
|
|
|
|
max.vec[0], min.vec[1], min.vec[2],
|
|
|
|
min.vec[0], max.vec[1], min.vec[2],
|
|
|
|
max.vec[0], max.vec[1], min.vec[2],
|
|
|
|
min.vec[0], min.vec[1], max.vec[2],
|
|
|
|
max.vec[0], min.vec[1], max.vec[2],
|
|
|
|
min.vec[0], max.vec[1], max.vec[2],
|
|
|
|
max.vec[0], max.vec[1], max.vec[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void centerView()
|
|
|
|
{
|
|
|
|
*this << "bpy.context.user_preferences.view.smooth_view = 0\n"
|
|
|
|
"for window in bpy.context.window_manager.windows:\n"
|
|
|
|
" screen = window.screen\n"
|
|
|
|
" for area in screen.areas:\n"
|
|
|
|
" if area.type == 'VIEW_3D':\n"
|
|
|
|
" for region in area.regions:\n"
|
|
|
|
" if region.type == 'WINDOW':\n"
|
|
|
|
" override = {'scene': bpy.context.scene, 'window': window, 'screen': screen, 'area': area, 'region': region}\n"
|
|
|
|
" bpy.ops.view3d.view_all(override)\n"
|
|
|
|
" break\n";
|
|
|
|
}
|
2015-09-01 00:26:35 +00:00
|
|
|
|
|
|
|
class ANIMOutStream
|
|
|
|
{
|
|
|
|
BlenderConnection* m_parent;
|
|
|
|
unsigned m_curCount = 0;
|
|
|
|
unsigned m_totalCount = 0;
|
|
|
|
bool m_inCurve = false;
|
|
|
|
public:
|
2015-10-23 00:44:37 +00:00
|
|
|
using CurveType = ANIMCurveType;
|
2015-09-01 00:26:35 +00:00
|
|
|
ANIMOutStream(BlenderConnection* parent)
|
|
|
|
: m_parent(parent)
|
|
|
|
{
|
|
|
|
m_parent->_writeLine("PYANIM");
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "ANIMREADY"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to open ANIMOutStream");
|
|
|
|
}
|
|
|
|
~ANIMOutStream()
|
|
|
|
{
|
|
|
|
char tp = -1;
|
|
|
|
m_parent->_writeBuf(&tp, 1);
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "ANIMDONE"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to close ANIMOutStream");
|
|
|
|
}
|
|
|
|
void changeCurve(CurveType type, unsigned crvIdx, unsigned keyCount)
|
|
|
|
{
|
|
|
|
if (m_curCount != m_totalCount)
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "incomplete ANIMOutStream for change");
|
|
|
|
m_curCount = 0;
|
|
|
|
m_totalCount = keyCount;
|
|
|
|
char tp = char(type);
|
|
|
|
m_parent->_writeBuf(&tp, 1);
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t ci;
|
|
|
|
uint32_t kc;
|
|
|
|
} info = {uint32_t(crvIdx), uint32_t(keyCount)};
|
|
|
|
m_parent->_writeBuf(reinterpret_cast<const char*>(&info), 8);
|
|
|
|
m_inCurve = true;
|
|
|
|
}
|
|
|
|
void write(unsigned frame, float val)
|
|
|
|
{
|
|
|
|
if (!m_inCurve)
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "changeCurve not called before write");
|
|
|
|
if (m_curCount < m_totalCount)
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t frm;
|
|
|
|
float val;
|
|
|
|
} key = {uint32_t(frame), val};
|
|
|
|
m_parent->_writeBuf(reinterpret_cast<const char*>(&key), 8);
|
|
|
|
++m_curCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "ANIMOutStream keyCount overflow");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ANIMOutStream beginANIMCurve()
|
|
|
|
{
|
|
|
|
return ANIMOutStream(m_parent);
|
|
|
|
}
|
2015-07-25 23:01:02 +00:00
|
|
|
};
|
2015-10-02 04:06:45 +00:00
|
|
|
PyOutStream beginPythonOut(bool deleteOnError=false)
|
2015-07-25 23:01:02 +00:00
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
if (m_lock)
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "lock already held for BlenderConnection::beginPythonOut()");
|
2015-08-05 22:59:59 +00:00
|
|
|
return PyOutStream(this, deleteOnError);
|
2015-07-25 23:01:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
class DataStream
|
|
|
|
{
|
|
|
|
friend class BlenderConnection;
|
|
|
|
BlenderConnection* m_parent;
|
|
|
|
DataStream(BlenderConnection* parent)
|
|
|
|
: m_parent(parent)
|
|
|
|
{
|
|
|
|
m_parent->m_lock = true;
|
|
|
|
m_parent->_writeLine("DATABEGIN");
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "READY"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to open DataStream with blender");
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
DataStream(const DataStream& other) = delete;
|
|
|
|
DataStream(DataStream&& other)
|
|
|
|
: m_parent(other.m_parent) {other.m_parent = nullptr;}
|
|
|
|
~DataStream() {close();}
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
if (m_parent && m_parent->m_lock)
|
|
|
|
{
|
|
|
|
m_parent->_writeLine("DATAEND");
|
|
|
|
char readBuf[16];
|
|
|
|
m_parent->_readLine(readBuf, 16);
|
|
|
|
if (strcmp(readBuf, "DONE"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to close DataStream with blender");
|
|
|
|
m_parent->m_lock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> getMeshList()
|
|
|
|
{
|
|
|
|
m_parent->_writeLine("MESHLIST");
|
|
|
|
uint32_t count;
|
|
|
|
m_parent->_readBuf(&count, 4);
|
|
|
|
std::vector<std::string> retval;
|
|
|
|
retval.reserve(count);
|
2015-10-12 04:38:49 +00:00
|
|
|
for (uint32_t i=0 ; i<count ; ++i)
|
2015-10-02 04:06:45 +00:00
|
|
|
{
|
|
|
|
char name[128];
|
|
|
|
m_parent->_readLine(name, 128);
|
|
|
|
retval.push_back(name);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-10-23 00:44:37 +00:00
|
|
|
/* Vector types with integrated stream reading constructor */
|
|
|
|
struct Vector2f
|
2015-10-02 04:06:45 +00:00
|
|
|
{
|
2015-10-23 00:44:37 +00:00
|
|
|
atVec2f val;
|
|
|
|
Vector2f() = default;
|
|
|
|
void read(BlenderConnection& conn) {conn._readBuf(&val, 8);}
|
|
|
|
Vector2f(BlenderConnection& conn) {read(conn);}
|
|
|
|
operator const atVec2f&() const {return val;}
|
|
|
|
};
|
|
|
|
struct Vector3f
|
|
|
|
{
|
|
|
|
atVec3f val;
|
|
|
|
Vector3f() = default;
|
|
|
|
void read(BlenderConnection& conn) {conn._readBuf(&val, 12);}
|
|
|
|
Vector3f(BlenderConnection& conn) {read(conn);}
|
|
|
|
operator const atVec3f&() const {return val;}
|
|
|
|
};
|
|
|
|
struct Vector4f
|
|
|
|
{
|
|
|
|
atVec4f val;
|
|
|
|
Vector4f() = default;
|
|
|
|
void read(BlenderConnection& conn) {conn._readBuf(&val, 16);}
|
|
|
|
Vector4f(BlenderConnection& conn) {read(conn);}
|
|
|
|
operator const atVec4f&() const {return val;}
|
|
|
|
};
|
|
|
|
struct Index
|
|
|
|
{
|
|
|
|
uint32_t val;
|
|
|
|
Index() = default;
|
|
|
|
void read(BlenderConnection& conn) {conn._readBuf(&val, 4);}
|
|
|
|
Index(BlenderConnection& conn) {read(conn);}
|
|
|
|
operator const uint32_t&() const {return val;}
|
|
|
|
};
|
2015-10-09 02:08:10 +00:00
|
|
|
|
2015-10-23 00:44:37 +00:00
|
|
|
/** Intermediate mesh representation prepared by blender from a single mesh object */
|
|
|
|
struct Mesh
|
|
|
|
{
|
2015-11-21 02:59:43 +00:00
|
|
|
HMDLTopology topology;
|
2015-10-22 02:01:08 +00:00
|
|
|
|
2015-10-09 02:08:10 +00:00
|
|
|
/* Cumulative AABB */
|
|
|
|
Vector3f aabbMin;
|
|
|
|
Vector3f aabbMax;
|
|
|
|
|
2015-11-09 19:44:47 +00:00
|
|
|
/** HECL source and metadata of each material */
|
2015-10-09 02:08:10 +00:00
|
|
|
struct Material
|
|
|
|
{
|
2015-10-14 23:06:47 +00:00
|
|
|
std::string name;
|
2015-10-09 02:08:10 +00:00
|
|
|
std::string source;
|
|
|
|
std::vector<ProjectPath> texs;
|
2015-10-17 02:06:27 +00:00
|
|
|
std::unordered_map<std::string, int32_t> iprops;
|
2015-10-09 02:08:10 +00:00
|
|
|
|
|
|
|
Material(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<std::vector<Material>> materialSets;
|
|
|
|
|
|
|
|
/* Vertex buffer data */
|
2015-10-03 01:53:45 +00:00
|
|
|
std::vector<Vector3f> pos;
|
|
|
|
std::vector<Vector3f> norm;
|
|
|
|
uint32_t colorLayerCount = 0;
|
2015-10-04 04:35:18 +00:00
|
|
|
std::vector<Vector3f> color;
|
2015-10-03 01:53:45 +00:00
|
|
|
uint32_t uvLayerCount = 0;
|
2015-10-04 04:35:18 +00:00
|
|
|
std::vector<Vector2f> uv;
|
2015-10-03 01:53:45 +00:00
|
|
|
|
|
|
|
/* Skinning data */
|
|
|
|
std::vector<std::string> boneNames;
|
|
|
|
struct SkinBind
|
|
|
|
{
|
|
|
|
uint32_t boneIdx;
|
|
|
|
float weight;
|
|
|
|
SkinBind(BlenderConnection& conn) {conn._readBuf(&boneIdx, 8);}
|
|
|
|
};
|
|
|
|
std::vector<std::vector<SkinBind>> skins;
|
2015-10-22 02:01:08 +00:00
|
|
|
std::vector<size_t> contiguousSkinVertCounts;
|
2015-10-02 04:06:45 +00:00
|
|
|
|
2015-11-09 19:44:47 +00:00
|
|
|
/** Islands of the same material/skinBank are represented here */
|
2015-10-03 01:53:45 +00:00
|
|
|
struct Surface
|
|
|
|
{
|
|
|
|
Vector3f centroid;
|
|
|
|
Index materialIdx;
|
|
|
|
Vector3f aabbMin;
|
|
|
|
Vector3f aabbMax;
|
|
|
|
Vector3f reflectionNormal;
|
|
|
|
uint32_t skinBankIdx;
|
|
|
|
|
2015-11-09 19:44:47 +00:00
|
|
|
/** Vertex indexing data (all primitives joined as degenerate tri-strip) */
|
2015-10-03 01:53:45 +00:00
|
|
|
struct Vert
|
2015-10-02 04:06:45 +00:00
|
|
|
{
|
2015-10-03 01:53:45 +00:00
|
|
|
uint32_t iPos;
|
|
|
|
uint32_t iNorm;
|
|
|
|
uint32_t iColor[4] = {uint32_t(-1)};
|
|
|
|
uint32_t iUv[8] = {uint32_t(-1)};
|
|
|
|
uint32_t iSkin;
|
2015-10-04 04:35:18 +00:00
|
|
|
uint32_t iBankSkin = -1;
|
2015-10-02 04:06:45 +00:00
|
|
|
|
2015-10-03 01:53:45 +00:00
|
|
|
Vert(BlenderConnection& conn, const Mesh& parent);
|
2015-11-14 02:26:06 +00:00
|
|
|
|
|
|
|
bool operator==(const Vert& other) const
|
|
|
|
{
|
|
|
|
if (iPos != other.iPos)
|
|
|
|
return false;
|
|
|
|
if (iNorm != other.iNorm)
|
|
|
|
return false;
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
if (iColor[i] != other.iColor[i])
|
|
|
|
return false;
|
|
|
|
for (int i=0 ; i<8 ; ++i)
|
|
|
|
if (iUv[i] != other.iUv[i])
|
|
|
|
return false;
|
|
|
|
if (iSkin != other.iSkin)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-02 04:06:45 +00:00
|
|
|
};
|
2015-10-03 01:53:45 +00:00
|
|
|
std::vector<Vert> verts;
|
2015-10-02 04:06:45 +00:00
|
|
|
|
2015-10-04 04:35:18 +00:00
|
|
|
Surface(BlenderConnection& conn, Mesh& parent, int skinSlotCount);
|
2015-10-02 04:06:45 +00:00
|
|
|
};
|
2015-10-03 01:53:45 +00:00
|
|
|
std::vector<Surface> surfaces;
|
|
|
|
|
2015-10-04 04:35:18 +00:00
|
|
|
struct SkinBanks
|
2015-10-03 01:53:45 +00:00
|
|
|
{
|
2015-11-14 02:26:06 +00:00
|
|
|
struct Bank
|
|
|
|
{
|
|
|
|
std::vector<uint32_t> m_skinIdxs;
|
|
|
|
std::vector<uint32_t> m_boneIdxs;
|
|
|
|
|
|
|
|
void addSkins(const Mesh& parent, const std::vector<uint32_t>& skinIdxs)
|
|
|
|
{
|
|
|
|
for (uint32_t sidx : skinIdxs)
|
|
|
|
{
|
|
|
|
m_skinIdxs.push_back(sidx);
|
|
|
|
for (const SkinBind& bind : parent.skins[sidx])
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
for (uint32_t bidx : m_boneIdxs)
|
|
|
|
{
|
|
|
|
if (bidx == bind.boneIdx)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
m_boneIdxs.push_back(bind.boneIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t lookupLocalBoneIdx(uint32_t boneIdx) const
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<m_boneIdxs.size() ; ++i)
|
|
|
|
if (m_boneIdxs[i] == boneIdx)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::vector<Bank> banks;
|
|
|
|
std::vector<Bank>::iterator addSkinBank(int skinSlotCount)
|
2015-10-03 01:53:45 +00:00
|
|
|
{
|
2015-10-04 04:35:18 +00:00
|
|
|
banks.emplace_back();
|
2015-10-19 03:27:58 +00:00
|
|
|
if (skinSlotCount > 0)
|
2015-11-14 02:26:06 +00:00
|
|
|
banks.back().m_skinIdxs.reserve(skinSlotCount);
|
2015-10-04 04:35:18 +00:00
|
|
|
return banks.end() - 1;
|
2015-10-03 01:53:45 +00:00
|
|
|
}
|
2015-11-14 02:26:06 +00:00
|
|
|
uint32_t addSurface(const Mesh& mesh, const Surface& surf, int skinSlotCount);
|
2015-10-03 01:53:45 +00:00
|
|
|
} skinBanks;
|
2015-10-02 04:06:45 +00:00
|
|
|
|
2015-10-22 02:01:08 +00:00
|
|
|
using SurfProgFunc = std::function<void(int)>;
|
2015-11-14 02:26:06 +00:00
|
|
|
Mesh(BlenderConnection& conn, HMDLTopology topology, int skinSlotCount, SurfProgFunc& surfProg);
|
2015-10-22 02:01:08 +00:00
|
|
|
|
|
|
|
Mesh getContiguousSkinningVersion() const;
|
2015-11-14 02:26:06 +00:00
|
|
|
|
|
|
|
/** Prepares mesh representation for indexed access on modern APIs.
|
|
|
|
* Mesh must remain resident for accessing reference members
|
|
|
|
*/
|
|
|
|
HMDLBuffers getHMDLBuffers() const;
|
2015-10-02 04:06:45 +00:00
|
|
|
};
|
|
|
|
|
2015-10-22 02:01:08 +00:00
|
|
|
|
2015-11-14 02:26:06 +00:00
|
|
|
static const char* MeshOutputModeString(HMDLTopology topology)
|
2015-10-22 02:01:08 +00:00
|
|
|
{
|
|
|
|
static const char* STRS[] = {"TRIANGLES", "TRISTRIPS"};
|
2015-11-21 01:13:06 +00:00
|
|
|
return STRS[int(topology)];
|
2015-10-22 02:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-23 00:44:37 +00:00
|
|
|
/** Compile mesh by context (MESH blends only) */
|
2015-11-14 02:26:06 +00:00
|
|
|
Mesh compileMesh(HMDLTopology topology, int skinSlotCount=10,
|
2015-10-22 02:01:08 +00:00
|
|
|
Mesh::SurfProgFunc surfProg=[](int){})
|
2015-10-04 04:35:18 +00:00
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
if (m_parent->m_loadedType != BlendType::Mesh)
|
2015-10-23 00:44:37 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, _S("%s is not a MESH blend"),
|
|
|
|
m_parent->m_loadedBlend.getAbsolutePath().c_str());
|
|
|
|
|
2015-10-04 04:35:18 +00:00
|
|
|
char req[128];
|
2015-10-22 02:01:08 +00:00
|
|
|
snprintf(req, 128, "MESHCOMPILE %s %d",
|
2015-11-14 02:26:06 +00:00
|
|
|
MeshOutputModeString(topology), skinSlotCount);
|
2015-10-04 04:35:18 +00:00
|
|
|
m_parent->_writeLine(req);
|
|
|
|
|
|
|
|
char readBuf[256];
|
|
|
|
m_parent->_readLine(readBuf, 256);
|
|
|
|
if (strcmp(readBuf, "OK"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to cook mesh: %s", readBuf);
|
|
|
|
|
2015-11-14 02:26:06 +00:00
|
|
|
return Mesh(*m_parent, topology, skinSlotCount, surfProg);
|
2015-10-04 04:35:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:44:37 +00:00
|
|
|
/** Compile mesh by name (AREA blends only) */
|
2015-11-14 02:26:06 +00:00
|
|
|
Mesh compileMesh(const std::string& name, HMDLTopology topology, int skinSlotCount=10,
|
2015-10-22 02:01:08 +00:00
|
|
|
Mesh::SurfProgFunc surfProg=[](int){})
|
2015-10-02 04:06:45 +00:00
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
if (m_parent->m_loadedType != BlendType::Area)
|
2015-10-23 00:44:37 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, _S("%s is not an AREA blend"),
|
|
|
|
m_parent->m_loadedBlend.getAbsolutePath().c_str());
|
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
char req[128];
|
2015-10-22 02:01:08 +00:00
|
|
|
snprintf(req, 128, "MESHCOMPILENAME %s %s %d", name.c_str(),
|
2015-11-14 02:26:06 +00:00
|
|
|
MeshOutputModeString(topology), skinSlotCount);
|
2015-10-02 04:06:45 +00:00
|
|
|
m_parent->_writeLine(req);
|
|
|
|
|
|
|
|
char readBuf[256];
|
|
|
|
m_parent->_readLine(readBuf, 256);
|
|
|
|
if (strcmp(readBuf, "OK"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to cook mesh '%s': %s", name.c_str(), readBuf);
|
|
|
|
|
2015-11-14 02:26:06 +00:00
|
|
|
return Mesh(*m_parent, topology, skinSlotCount, surfProg);
|
2015-10-02 04:06:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:44:37 +00:00
|
|
|
/** Compile all meshes into one (AREA blends only) */
|
2015-11-14 02:26:06 +00:00
|
|
|
Mesh compileAllMeshes(HMDLTopology topology, int skinSlotCount=10, float maxOctantLength=5.0,
|
2015-10-22 02:01:08 +00:00
|
|
|
Mesh::SurfProgFunc surfProg=[](int){})
|
2015-10-02 04:06:45 +00:00
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
if (m_parent->m_loadedType != BlendType::Area)
|
2015-10-23 00:44:37 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, _S("%s is not an AREA blend"),
|
|
|
|
m_parent->m_loadedBlend.getAbsolutePath().c_str());
|
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
char req[128];
|
2015-10-22 02:01:08 +00:00
|
|
|
snprintf(req, 128, "MESHCOMPILEALL %s %d %f",
|
2015-11-14 02:26:06 +00:00
|
|
|
MeshOutputModeString(topology),
|
2015-10-22 02:01:08 +00:00
|
|
|
skinSlotCount, maxOctantLength);
|
2015-10-02 04:06:45 +00:00
|
|
|
m_parent->_writeLine(req);
|
|
|
|
|
|
|
|
char readBuf[256];
|
|
|
|
m_parent->_readLine(readBuf, 256);
|
|
|
|
if (strcmp(readBuf, "OK"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to cook all meshes: %s", readBuf);
|
|
|
|
|
2015-11-14 02:26:06 +00:00
|
|
|
return Mesh(*m_parent, topology, skinSlotCount, surfProg);
|
2015-10-02 04:06:45 +00:00
|
|
|
}
|
2015-10-23 00:44:37 +00:00
|
|
|
|
|
|
|
/** Intermediate actor representation prepared by blender from a single HECL actor blend */
|
|
|
|
struct Actor
|
|
|
|
{
|
|
|
|
struct Armature
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
struct Bone
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
Vector3f origin;
|
|
|
|
int32_t parent = -1;
|
|
|
|
std::vector<int32_t> children;
|
|
|
|
Bone(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<Bone> bones;
|
|
|
|
Bone* lookupBone(const char* name)
|
|
|
|
{
|
|
|
|
for (Bone& b : bones)
|
|
|
|
if (!b.name.compare(name))
|
|
|
|
return &b;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Armature(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<Armature> armatures;
|
|
|
|
|
|
|
|
struct Subtype
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
ProjectPath mesh;
|
|
|
|
int32_t armature = -1;
|
|
|
|
std::vector<std::pair<std::string, ProjectPath>> overlayMeshes;
|
|
|
|
Subtype(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<Subtype> subtypes;
|
|
|
|
|
|
|
|
struct Action
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
float interval;
|
|
|
|
bool additive;
|
|
|
|
std::vector<int32_t> frames;
|
|
|
|
struct Channel
|
|
|
|
{
|
|
|
|
std::string boneName;
|
|
|
|
uint32_t attrMask;
|
|
|
|
struct Key
|
|
|
|
{
|
|
|
|
Vector4f rotation;
|
|
|
|
Vector3f position;
|
|
|
|
Vector3f scale;
|
|
|
|
Key(BlenderConnection& conn, uint32_t attrMask);
|
|
|
|
};
|
|
|
|
std::vector<Key> keys;
|
|
|
|
Channel(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<Channel> channels;
|
|
|
|
std::vector<std::pair<Vector3f, Vector3f>> subtypeAABBs;
|
|
|
|
Action(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
std::vector<Action> actions;
|
|
|
|
|
|
|
|
Actor(BlenderConnection& conn);
|
|
|
|
};
|
|
|
|
|
|
|
|
Actor compileActor()
|
|
|
|
{
|
2015-11-21 01:13:06 +00:00
|
|
|
if (m_parent->m_loadedType != BlendType::Actor)
|
2015-10-23 00:44:37 +00:00
|
|
|
BlenderLog.report(LogVisor::FatalError, _S("%s is not an ACTOR blend"),
|
|
|
|
m_parent->m_loadedBlend.getAbsolutePath().c_str());
|
|
|
|
|
|
|
|
m_parent->_writeLine("ACTORCOMPILE");
|
|
|
|
|
|
|
|
char readBuf[256];
|
|
|
|
m_parent->_readLine(readBuf, 256);
|
|
|
|
if (strcmp(readBuf, "OK"))
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "unable to compile actor: %s", readBuf);
|
|
|
|
|
|
|
|
return Actor(*m_parent);
|
|
|
|
}
|
2015-10-02 04:06:45 +00:00
|
|
|
};
|
|
|
|
DataStream beginData()
|
|
|
|
{
|
|
|
|
if (m_lock)
|
|
|
|
BlenderLog.report(LogVisor::FatalError, "lock already held for BlenderConnection::beginDataIn()");
|
|
|
|
return DataStream(this);
|
|
|
|
}
|
|
|
|
|
2015-05-24 04:51:16 +00:00
|
|
|
void quitBlender();
|
2015-07-28 02:25:33 +00:00
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
static BlenderConnection& SharedConnection()
|
2015-07-28 02:25:33 +00:00
|
|
|
{
|
|
|
|
if (!SharedBlenderConnection)
|
2015-09-22 01:41:38 +00:00
|
|
|
SharedBlenderConnection = new BlenderConnection(HECL::VerbosityLevel);
|
2015-07-28 02:25:33 +00:00
|
|
|
return *SharedBlenderConnection;
|
|
|
|
}
|
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
void closeStream()
|
2015-08-16 23:01:35 +00:00
|
|
|
{
|
|
|
|
if (m_lock)
|
|
|
|
deleteBlend();
|
|
|
|
}
|
|
|
|
|
2015-10-02 04:06:45 +00:00
|
|
|
static void Shutdown()
|
2015-07-28 02:25:33 +00:00
|
|
|
{
|
2015-08-16 23:01:35 +00:00
|
|
|
if (SharedBlenderConnection)
|
|
|
|
{
|
|
|
|
SharedBlenderConnection->closeStream();
|
|
|
|
SharedBlenderConnection->quitBlender();
|
|
|
|
delete SharedBlenderConnection;
|
|
|
|
SharedBlenderConnection = nullptr;
|
|
|
|
BlenderLog.report(LogVisor::Info, "BlenderConnection Shutdown Successful");
|
|
|
|
}
|
2015-07-28 02:25:33 +00:00
|
|
|
}
|
2015-08-16 23:01:35 +00:00
|
|
|
|
2015-05-23 21:59:40 +00:00
|
|
|
};
|
|
|
|
|
2015-11-14 02:26:06 +00:00
|
|
|
class HMDLBuffers
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Surface;
|
|
|
|
private:
|
|
|
|
friend struct BlenderConnection::DataStream::Mesh;
|
2015-11-16 04:30:06 +00:00
|
|
|
HMDLBuffers(HMDLMeta&& meta,
|
2015-11-14 02:26:06 +00:00
|
|
|
size_t vboSz, const std::vector<atUint32>& iboData,
|
|
|
|
std::vector<Surface>&& surfaces,
|
|
|
|
const BlenderConnection::DataStream::Mesh::SkinBanks& skinBanks)
|
2015-11-16 04:30:06 +00:00
|
|
|
: m_meta(std::move(meta)),
|
2015-11-14 02:26:06 +00:00
|
|
|
m_vboSz(vboSz), m_vboData(new uint8_t[vboSz]),
|
|
|
|
m_iboSz(iboData.size()*4), m_iboData(new uint8_t[iboData.size()*4]),
|
|
|
|
m_surfaces(std::move(surfaces)), m_skinBanks(skinBanks)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Athena::io::MemoryWriter w(m_iboData.get(), m_iboSz);
|
|
|
|
w.enumerateLittle(iboData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
2015-11-16 04:30:06 +00:00
|
|
|
HMDLMeta m_meta;
|
2015-11-14 02:26:06 +00:00
|
|
|
size_t m_vboSz;
|
|
|
|
std::unique_ptr<uint8_t[]> m_vboData;
|
|
|
|
size_t m_iboSz;
|
|
|
|
std::unique_ptr<uint8_t[]> m_iboData;
|
|
|
|
|
|
|
|
struct Surface
|
|
|
|
{
|
|
|
|
Surface(const BlenderConnection::DataStream::Mesh::Surface& origSurf,
|
|
|
|
atUint32 start, atUint32 count)
|
|
|
|
: m_origSurf(origSurf), m_start(start), m_count(count) {}
|
|
|
|
const BlenderConnection::DataStream::Mesh::Surface& m_origSurf;
|
|
|
|
atUint32 m_start;
|
|
|
|
atUint32 m_count;
|
|
|
|
};
|
|
|
|
std::vector<Surface> m_surfaces;
|
|
|
|
|
|
|
|
const BlenderConnection::DataStream::Mesh::SkinBanks& m_skinBanks;
|
|
|
|
};
|
|
|
|
|
2015-07-28 02:25:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 19:14:50 +00:00
|
|
|
#endif // BLENDERCONNECTION_HPP
|