📄 regiscol.cpp
字号:
//-----------------------------------------------------------------------------
#include "regiscol.h"
//-----------------------------------------------------------------------------
GLbeadTree *beadRoot;
GLint GlobalErrorFlag;
char inString[256], beadName[256];
GLint polyvertices[100];
ColorPatchRecord *colorpatch;
GLint totalcolorpatch;
CStreamIO in;
CFileIO out;
GLint VertexListPos, BeadListPos;
GLint VertexIndex;
GLint TotalColor;
ColorTableTree *rootColor;
ColorTableList *colorList, *currentList;
//-----------------------------------------------------------------------------
inline GLint GetStringType (GLbyte *str)
{
union {
char c[4];
GLint l;
} type;
char c;
type.l = (GLint) *((GLint *) str);
c = type.c[0];
type.c[0] = type.c[3];
type.c[3] = c;
c = type.c[1];
type.c[1] = type.c[2];
type.c[2] = c;
return type.l;
}
//-----------------------------------------------------------------------------
void main (int argc, char* argv[])
{
struct _finddata_t c_file;
long hFile;
char lowerCase[_MAX_PATH];
char wildcard[6] = "*.HOT";
char colfile[80], hotfile[80];
if (argc < 2) {
puts ("\nRegisCol v1.0 by Erick Jap\n");
puts ("Usage: RegisCol colorlist [hotfile]");
puts ("where: colorlist is the color list file");
puts (" hotfile is hot file to be registered");
puts ("If hotfile is not specified, all hot files will be registered");
exit (1);
}
strcpy (colfile, argv[1]);
hotfile[0] = 0;
if (argc > 2) {
glConcatFileExtension ((GLbyte *) hotfile, (GLbyte *) argv[2], (GLbyte *) "HOT");
}
colorList = currentList = 0;
rootColor = 0;
TotalColor = 0;
if (in.openread (colfile, 1)) {
GLint i;
if( in.read ("%d", &i)) {
while (i--) {
GLuint color;
in.read ("%d", &color);
InsertColor (color);
}
}
in.closefile();
}
if (!out.openwrite (colfile, 1)) {
printf ("Can not open color output file %s\n", colfile);
exit (1);
}
if (hotfile[0]) {
strlwr(hotfile);
printf ("Registering %s color\n", hotfile);
readHOT (hotfile);
}
else {
if( (hFile = _findfirst( wildcard, &c_file )) != -1L ) {
do {
if(c_file.attrib & _A_SUBDIR) {
// Skip sub directories
}
else {
strcpy( lowerCase, c_file.name );
strlwr( lowerCase );
printf ("Registering %s color\n", lowerCase);
readHOT (lowerCase);
}
} while( _findnext( hFile, &c_file ) == 0 );
}
}
out.writedata ((char *) &TotalColor, 4);
while (colorList) {
ColorTableList *colptr = colorList;
out.writedata ((char *) &colptr -> color, 4);
colorList = colorList -> next;
glReleaseMemory ((char *) colptr);
}
RemoveColor (rootColor);
_findclose(hFile);
out.closefile (1);
}
GLint InsertColor (GLcolor *col)
{
GLuint color = (((GLint) (col -> a * 255.0f)) << 24) |
(((GLint) (col -> b * 255.0f)) << 16) |
(((GLint) (col -> g * 255.0f)) << 8) |
(((GLint) (col -> r * 255.0f)));
return InsertColor (color);
}
GLint InsertColor (GLuint color)
{
if (FindColorInTree(rootColor, color)) return 1;
ColorTableTree *newcol = (ColorTableTree *) glAllocateMemory (sizeof (ColorTableTree));
if (!newcol) {
printf ("Can not allocate color table memory!\n");
return 0;
}
ColorTableList *newlist = (ColorTableList *) glAllocateMemory (sizeof (ColorTableList));
if (!newlist) {
printf ("Can not allocate color list memory!\n");
return 0;
}
TotalColor++;
newlist -> color = color;
newlist -> next = 0;
if (colorList) {
currentList -> next = newlist;
currentList = newlist;
}
else {
colorList = currentList = newlist;
}
newcol -> color = color;
newcol -> left = 0;
newcol -> right = 0;
if (rootColor) InsertColorInTree (rootColor, newcol);
else rootColor = newcol;
return 2;
}
void RemoveColor (ColorTableTree *colptr)
{
if (colptr) {
RemoveColor (colptr -> left);
RemoveColor (colptr -> right);
glReleaseMemory ((char *) colptr);
}
}
GLint FindColorInTree (ColorTableTree *colptr, GLuint color)
{
if (colptr) {
if (color == colptr -> color) return 1;
if (FindColorInTree (colptr -> left, color)) return 1;
if (FindColorInTree (colptr -> right, color)) return 1;
}
return 0;
}
void InsertColorInTree (ColorTableTree *colptr, ColorTableTree *col)
{
if (colptr) {
if (col -> color < colptr -> color) {
if (colptr -> left) InsertColorInTree (colptr -> left, col);
else {
col -> left = colptr -> left;
colptr -> left = col;
}
}
else if (col -> color > colptr -> color) {
if (colptr -> right) InsertColorInTree (colptr -> right, col);
else {
col -> right = colptr -> right;
colptr -> right = col;
}
}
}
}
GLint readHOT (char *filename)
{
if (!in.openread ((char *) filename)) return 0;
Setup ();
if (readHeader ()) {
Cleanup ();
printf ("Problem reading header file!\n");
return 0;
}
if (readVerticesList ()) {
Cleanup ();
printf ("Problem reading vertices list!\n");
return 0;
}
if (readBeadList ()) {
Cleanup ();
printf ("Problem reading data file!\n");
return 0;
}
Cleanup ();
return 1;
}
void Cleanup ()
{
in.closefile();
glReleaseMemory ((char*) colorpatch);
CleanBeadTree (beadRoot);
}
void CleanBeadTree (GLbeadTree *bead)
{
if (bead) {
CleanBeadTree (bead -> next);
CleanBeadTree (bead -> child);
glReleaseMemory ((char *) bead);
}
}
void Setup ()
{
VertexIndex = -1;
colorpatch = 0;
totalcolorpatch = 0;
GlobalErrorFlag = 0;
VertexListPos = BeadListPos = -1;
}
GLint readHeader ()
{
GLint header = findString ("HeaderBead");
if (header == -1) return 1;
in.movefileptr (header);
char str[256];
GLint i, j;
GLfloat f;
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);
break;
case 'Bead': // Bead List
in.read ("%s", str);
BeadListPos = findString (str);
break;
case 'Scal': // Scale
in.read ("%f", &f);
break;
case 'Anim': // Animation
in.read ("%s", str);
break;
case 'Disa': // DisableMask
in.read ("%s", str);
break;
case 'TSet': // Texture Set
in.read ("%d", &j);
break;
case 'Dyna': // Dynamic Vertex
in.read ("%s", str);
break;
case 'Patc': // Patch Color
in.read ("%s", str);
return ReadColorPatch (str);
}
}
return 0;
}
GLint ReadColorPatch (char *str)
{
CStreamIO patch;
if (!patch.openread (str)) {
printf ("Can not open Vertex Color Patch file!\n");
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;
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", &patchptr -> color.r);
patch.read ("%f", &patchptr -> color.g);
patch.read ("%f", &patchptr -> color.b);
patch.read ("%f", &patchptr -> color.a);
InsertColor (&patchptr -> color);
patchptr++;
}
}
patch.closefile ();
if (!colorpatch) {
printf ("Can not allocate memory for Vertex Color Patch!\n");
return 1;
}
return 0;
}
// return: -1 : VerticesList position is not set
GLint readVerticesList ()
{
if (VertexListPos == -1) return -1;
in.movefileptr (VertexListPos);
GLint TotalVertices = countTotalString ("VertexNum");
in.movefileptr (VertexListPos);
char str[256];
GLint i, j, vtxnum;
GLfloat f;
GLcolor vtxcolor;
in.read ("%s", str);
for (i=0; i < TotalVertices; i++) {
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", &f);
in.read ("%f", &f);
in.read ("%f", &f);
in.read ("%s", str);
}
j = GetStringType ((GLbyte *) str);
if (j == 'Norm') { // NormalCoordinate
in.read ("%f", &f);
in.read ("%f", &f);
in.read ("%f", &f);
in.read ("%s", str);
}
j = GetStringType ((GLbyte *) str);
if (j == 'Text') { // TextureCoordinate
in.read ("%f", &f);
in.read ("%f", &f);
in.read ("%s", str);
}
j = GetStringType ((GLbyte *) str);
if (j == 'Colo') { // Color
in.read ("%f", &(vtxcolor.r));
in.read ("%f", &(vtxcolor.g));
in.read ("%f", &(vtxcolor.b));
in.read ("%f", &(vtxcolor.a));
in.read ("%s", str);
InsertColor (&vtxcolor);
}
}
return 0;
}
GLint countTotalString (char *string)
{
GLint i, total;
char str[256];
total = 0;
while (1) {
in.read ("%s", str);
i = *((int *) str);
if (i == 'ZZZZ') break;
if (!strcmp (str, string)) total++;
}
return total;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -