📄 hotreadr.cpp
字号:
}
}
void CHOTReader::createDOFTree (GLbeadTree *root)
{
if (objData -> TotalDOF) {
GLint i, j, k;
k = objData -> TotalDOF + 1;
i = k * sizeof (GLbeadDOFTree);
j = k * sizeof (GLbeadDegreeOfFreedom);
j += i;
objData -> dofTree = (GLbeadDOFTree *) glAllocateMemory (j);
if (!objData -> dofTree) {
DisplayErrorMessage ((GLbyte *) "Can not allocate memory for DOF tree!");
return;
}
i += (GLint) objData -> dofTree;
GLbeadDegreeOfFreedom *dof = (GLbeadDegreeOfFreedom *) i;
for (i=0; i <= objData -> TotalDOF; i++) {
if (GlobalDOFTable[i]) {
dof[i] = *(GlobalDOFTable[i]);
glReleaseMemory ((char *) GlobalDOFTable[i]);
GlobalDOFTable[i] = &dof[i];
}
}
dof -> objectMatrix.M11 = dof -> objectMatrix.M22 =
dof -> objectMatrix.M33 = dof -> modelMatrix.M11 =
dof -> modelMatrix.M22 = dof -> modelMatrix.M33 = (GLfloat) 1.0;
dof -> dofIndex = 0;
dof -> modelFlag = 0;
objData -> objectFlag |= OBJECT_TYPE_DEGREEOFFREEDOM;
objData -> dofTree[0].dof = dof;
objData -> dofTree[0].Parent = objData -> dofTree[0].Child = objData -> dofTree[0].Sibling = 0;
objData -> dofTree[0].tree = root;
GLbeadDOFTree *dofptr, *dofRoot;
dofRoot = dofptr = 0;
getDOFSibling (root, &dofRoot, &dofptr);
getDOFChild (dofRoot, &objData -> dofTree[0]);
objData -> dofTree[0].Child = dofRoot;
SetVertexDofPtr ((void *) objData -> dofTree[0].dof);
// duplicate DOF vertices to avoid shared vertices being used by DOF polygon
dupVertexDOFptr (&objData -> dofTree[0]);
setVertexDOFptr (&objData -> dofTree[0]);
}
}
void CHOTReader::dupVertexDOFptr (GLbeadDOFTree *dofptr)
{
if (dofptr) {
dupVertexDOF (dofptr -> tree, dofptr -> dof);
dupVertexDOFptr (dofptr -> Sibling);
dupVertexDOFptr (dofptr -> Child);
}
}
void CHOTReader::dupVertexDOF (GLbeadTree *tree, GLbeadDegreeOfFreedom *dofptr)
{
if (tree) {
if (tree -> type != BEAD_TYPE_DOF) {
if (tree -> type == BEAD_TYPE_LINE_LIST) {
GLbeadTree *bead = (GLbeadTree *) tree -> beadElement;
while (bead) {
dupDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
bead = bead -> next;
}
}
else if ((tree -> type == BEAD_TYPE_POLYGON_TREE) ||
(tree -> type == BEAD_TYPE_POLYGONANDLINE_TREE)) {
dupVertexDOF_BSP ((GLbeadBSPTree *) tree -> beadElement, dofptr);
}
else if ((tree -> type == BEAD_TYPE_POLYGON) ||
(tree -> type == BEAD_TYPE_POLYGON_BSP)) {
dupDOFptr ((GLbeadPolygon *) tree -> beadElement, dofptr);
}
dupVertexDOF (tree -> next, dofptr);
dupVertexDOF (tree -> child, dofptr);
}
}
}
void CHOTReader::dupVertexDOF_BSP (GLbeadBSPTree *tree, GLbeadDegreeOfFreedom *dofptr)
{
if (tree) {
GLbeadTree *bead = tree -> face;
dupDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
dupVertexDOF_BSP (tree -> frontSide, dofptr);
dupVertexDOF_BSP (tree -> backSide, dofptr);
}
}
void CHOTReader::dupDOFptrsubface (GLbeadTree *bead, GLbeadDegreeOfFreedom *dofptr)
{
if (bead) {
dupDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
dupDOFptrsubface (bead -> next, dofptr);
}
}
void CHOTReader::dupDOFptr (GLbeadPolygon *poly, GLbeadDegreeOfFreedom *dofptr)
{
GLint i, j, k, l;
if (!dofptr) dofptr = objData -> dofTree[0].dof;
for (i = 0; i < poly -> totalVertices; i++) {
k = poly -> vertices[i];
GLvertexData *vtx = GetVertex (k);
GLvertex *vert = GetCoord (vtx -> vtxindex);
j = InsertVertex (vtx, vert, vtx -> flag | VERTEX_TYPE_DOF, (void *) dofptr);
if (j == -1) {
DisplayErrorMessage ((GLbyte *) "Can not duplicate DOF vertex");
return;
}
poly -> vertices[i] = j;
vtx -> flag &= ~VERTEX_TYPE_DYNAMIC;
if (dynamicvertex) {
DynamicVertexRecord *dvtx = dynamicvertex;
for (l=0; l < totaldynamicvertex;l++) {
if (dvtx -> index == k) dvtx -> index = j;
dvtx++;
}
}
}
if (poly -> totalVertices > 2) poly -> vertices[i] = poly -> vertices[0]; // close it
if (poly -> subface) dupDOFptrsubface (poly -> subface, dofptr);
}
void CHOTReader::setVertexDOFptr (GLbeadDOFTree *dofptr)
{
if (dofptr) {
setVertexDOF (dofptr -> tree, dofptr -> dof);
setVertexDOFptr (dofptr -> Sibling);
setVertexDOFptr (dofptr -> Child);
}
}
void CHOTReader::setVertexDOF (GLbeadTree *tree, GLbeadDegreeOfFreedom *dofptr)
{
if (tree) {
if (tree -> type != BEAD_TYPE_DOF) {
if (tree -> type == BEAD_TYPE_LINE_LIST) {
GLbeadTree *bead = (GLbeadTree *) tree -> beadElement;
while (bead) {
setDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
bead = bead -> next;
}
}
else if ((tree -> type == BEAD_TYPE_POLYGON_TREE) ||
(tree -> type == BEAD_TYPE_POLYGONANDLINE_TREE)) {
setVertexDOF_BSP ((GLbeadBSPTree *) tree -> beadElement, dofptr);
}
else if ((tree -> type == BEAD_TYPE_POLYGON) ||
(tree -> type == BEAD_TYPE_POLYGON_BSP)) {
setDOFptr ((GLbeadPolygon *) tree -> beadElement, dofptr);
}
setVertexDOF (tree -> next, dofptr);
setVertexDOF (tree -> child, dofptr);
}
}
}
void CHOTReader::setVertexDOF_BSP (GLbeadBSPTree *tree, GLbeadDegreeOfFreedom *dofptr)
{
if (tree) {
GLbeadTree *bead = tree -> face;
setDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
setVertexDOF_BSP (tree -> frontSide, dofptr);
setVertexDOF_BSP (tree -> backSide, dofptr);
}
}
void CHOTReader::setDOFptrsubface (GLbeadTree *bead, GLbeadDegreeOfFreedom *dofptr)
{
if (bead) {
setDOFptr ((GLbeadPolygon *) bead -> beadElement, dofptr);
setDOFptrsubface (bead -> next, dofptr);
}
}
void CHOTReader::setDOFptr (GLbeadPolygon *poly, GLbeadDegreeOfFreedom *dofptr)
{
GLint i, j;
for (i = 0; i < poly -> totalVertices; i++) {
j = poly -> vertices[i];
GLvertexData *vtx = GetVertex (j);
if (!(vtx -> flag & VERTEX_DOF_INIT_CENTER)) {
GLvertex *coord = GetCoord (vtx -> vtxindex);
coord -> x -= dofptr -> modelOrigin.x;
coord -> y -= dofptr -> modelOrigin.y;
coord -> z -= dofptr -> modelOrigin.z;
vtx -> flag |= VERTEX_DOF_INIT_CENTER;
}
}
CalculatePlane (poly);
if (poly -> subface) setDOFptrsubface (poly -> subface, dofptr);
}
void CHOTReader::getDOFChild (GLbeadDOFTree *dofptr, GLbeadDOFTree *dofparent)
{
while (dofptr) {
GLbeadDOFTree *headdof, *curdof;
headdof = curdof = 0;
dofptr -> Parent = dofparent;
getDOFSibling (dofptr -> tree, &headdof, &curdof);
dofptr -> Child = headdof;
if (headdof) getDOFChild (dofptr -> Child, dofptr);
dofptr = dofptr -> Sibling;
}
}
void CHOTReader::getDOFSibling (GLbeadTree *tree, GLbeadDOFTree **headdof, GLbeadDOFTree **dofptr)
{
if (tree) {
if (tree -> type == BEAD_TYPE_DOF) {
GLint dofIndex = (GLint) tree -> beadElement;
GLbeadDOFTree *curdof = &objData -> dofTree[dofIndex];
curdof -> dof = GlobalDOFTable[dofIndex];
curdof -> Parent = curdof -> Child = curdof -> Sibling = 0;
curdof -> tree = tree -> child;
if (*dofptr) {
GLbeadDOFTree *tempdof = *dofptr;
tempdof -> Sibling = curdof;
*dofptr = curdof;
}
else *headdof = *dofptr = curdof;
}
else getDOFSibling (tree -> child, headdof, dofptr);
getDOFSibling (tree -> next, headdof, dofptr);
}
}
GLint CHOTReader::readHeader ()
{
GLint header = findString ("HeaderBead");
if (header == -1) return 1;
in.movefileptr (header);
char str[256];
GLint i;
while (1) {
in.read ("%s", str);
i = GetStringType ((GLbyte *) str);
if (i == 'ZZZZ') break;
switch (i) {
case 'Vert': // VertexPool
in.read ("%s", str);
VertexListPos = findString (str);
break;
case 'Text': // TexturePool
in.read ("%s", str);
TextureListPos = findString (str);
break;
case 'Bead': // Bead List
in.read ("%s", str);
BeadListPos = findString (str);
break;
case 'Scal': // Scale
in.read ("%f", &objectScale);
break;
case 'Anim': // Animation
in.read ("%s", str);
animFlag = ReadAnsData (str);
break;
case 'Disa': // DisableMask
in.read ("%s", str);
objData -> objectFlag |= glConvertHexStringToInt ((GLbyte *) str);
break;
case 'TSet': // Texture Set
in.read ("%d", &objData -> TotalTextureSet);
break;
case 'Dyna': // Dynamic Vertex
in.read ("%s", str);
return ReadDynamicVertex (str);
case 'Patc': // Patch Color
in.read ("%s", str);
return ReadColorPatch (str);
}
}
return 0;
}
GLint CHOTReader::ReadDynamicVertex (char *str)
{
CStreamIO vtxfile;
GLint id;
if (!vtxfile.openread (str)) {
DisplayErrorMessage ((GLbyte *) "Can not open Dynamic Vertex file!");
return 1;
}
totaldynamicvertex = 0;
while (1) {
vtxfile.read ("%d", &id);
if (id == -1) break;
totaldynamicvertex++;
vtxfile.read ("%s", str);
}
dynamicvertex = (DynamicVertexRecord *) glAllocateMemory
(totaldynamicvertex*sizeof (DynamicVertexRecord));
if (dynamicvertex) {
vtxfile.movefileptr ();
DynamicVertexRecord *vtxptr = dynamicvertex;
while (1) {
vtxfile.read ("%d", &id);
if (id == -1) break;
vtxptr -> vtxnum = id;
vtxptr -> index = -1; // not initialized
vtxfile.read ("%s", vtxptr -> polyname);
strupr ((char *) vtxptr -> polyname);
vtxptr++;
}
}
vtxfile.closefile ();
if (!dynamicvertex) {
DisplayErrorMessage ((GLbyte *) "Can not allocate memory for Dynamic Vertex!");
return 1;
}
return 0;
}
GLint CHOTReader::ReadColorPatch (char *str)
{
CStreamIO patch;
if (!patch.openread (str)) {
DisplayErrorMessage ((GLbyte *) "Can not open Vertex Color Patch file!");
return 1;
}
GLint id;
GLfloat col;
totalcolorpatch = 0;
while (1) {
patch.read ("%d", &id);
if (id == -1) break;
totalcolorpatch++;
patch.read ("%s", str);
patch.read ("%f", &col);
patch.read ("%f", &col);
patch.read ("%f", &col);
patch.read ("%f", &col);
}
colorpatch = (ColorPatchRecord *) glAllocateMemory
(totalcolorpatch*sizeof (ColorPatchRecord));
if (colorpatch) {
patch.movefileptr ();
ColorPatchRecord *patchptr = colorpatch;
GLcolor rgbcolor;
GLuint color;
while (1) {
patch.read ("%d", &id);
if (id == -1) break;
patchptr -> vtxnum = id;
patch.read ("%s", patchptr -> polyname);
strupr ((char *) patchptr -> polyname);
patch.read ("%f", &rgbcolor.r);
patch.read ("%f", &rgbcolor.g);
patch.read ("%f", &rgbcolor.b);
patch.read ("%f", &rgbcolor.a);
color = (((GLint) (rgbcolor.a * 255.0f)) << 24) |
(((GLint) (rgbcolor.b * 255.0f)) << 16) |
(((GLint) (rgbcolor.g * 255.0f)) << 8) |
((GLint) (rgbcolor.r * 255.0f));
patchptr -> colorindex = CacheColor (color);
patchptr++;
}
}
patch.closefile ();
if (!colorpatch) {
DisplayErrorMessage ((GLbyte *) "Can not allocate memory for Vertex Color Patch!");
return 1;
}
return 0;
}
// return: -1 : VerticesList position is not set
// -2 : can not allocate memory for VerticesList
GLint CHOTReader::readVerticesList ()
{
if (VertexListPos == -1) return -1;
in.movefileptr (VertexListPos);
objData -> TotalVertices = countTotalString ("VertexNum");
in.movefileptr (VertexListPos);
GLvertexData vtx;
GLvertex coord;
char str[256];
GLint i, j, k, vtxnum;
in.read ("%s", str);
k = 0;
for (i=0; i < objData -> TotalVertices; i++) {
vtx.flag = 0;
j = GetStringType ((GLbyte *) str);
if (j == 'Vert') { // VertexNum
in.read ("%d", &vtxnum); // read index
in.read ("%s", str);
}
j = GetStringType ((GLbyte *) str);
if (j == 'Coor') { // Coordinate
in.read ("%f", &(coord.x));
in.read ("%f", &(coord.y));
in.read ("%f", &(coord.z));
in.read ("%s", str);
if (objectScale != (GLfloat) 1.0) {
coord.x *= objectScale;
coord.y *= objectScale;
coord.z *= objectScale;
}
if (coord.x > BeadVertexMax.x)
BeadVertexMax.x = coord.x;
if (coord.x < BeadVertexMin.x)
BeadVertexMin.x = coord.x;
if (coord.y > BeadVertexMax.y)
BeadVertexMax.y = coord.y;
if (coord.y < BeadVertexMin.y)
BeadVertexMin.y = coord.y;
if (coord.z > BeadVertexMax.z)
BeadVertexMax.z = coord.z;
if (coord.z < BeadVertexMin.z)
BeadVertexMin.z = coord.z;
}
j = GetStringType ((GLbyte *) str);
if (j == 'Norm') { // NormalCoordinate
in.read ("%f", &(vtx.vtxnormal.x));
in.read ("%f", &(vtx.vtxnormal.y));
in.read ("%f", &(vtx.vtxnormal.z));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -