Bad side: The projects at times take a toll on you.
Our first project was to read a .skel file which contains the coordinates of a certain figure and to render that figure.
You can find the complete question here.
Well, to make things a little simple for some of you guys who might consider taking this course I have placed a sample code snippet below. (P.S: Do not try re-using the same code as it will not work ;) )
void Generate_Tree()
{
char *string213;
int length = 0;
char temp[500], temp1[500], temp2[500];
int j=0, counter =0, counter_temp = 0;
ifstream pointer;
fflush(stdin);
pointer.open("dragon.skel");
if(pointer == NULL)
{
cout<<"File not there\n";
cout<<"Press any key to exit: ";
getch();
exit(1);
}
root->Left = NULL;
root->Right = NULL;
root->Center = NULL;
node = root;
do
{
pointer.getline(temp, MaxLine);
//cout<<
length = strlen(temp);
//cout<<
if(temp[length-2] == '{')
{
// cout<<"After entering the loop: "<
Tree_Node *child = new Tree_Node;
child->nodeNum = number+1;
child->pose[0] = 0;
child->pose[1] = 0;
child->pose[2] = 0;
memset(&child->rotateX,0,sizeof(double)*2);
memset(&child->rotateY,0,sizeof(double)*2);
memset(&child->rotateZ,0,sizeof(double)*2);
if(node->Left == NULL)
{
// cout << "left created"<
node->Left = child;
}
else if(node->Center == NULL)
{
//cout << "center created"<
node->Center = child;
}
else
{
//cout << "right created"<
node->Right = child;
}
child->Left = NULL;
child->Right = NULL;
child->Center = NULL;
child->Prev = node;
node = child;
counter++;
}
else if(temp[length-2] == '}')
{
// cout << "go back " <
node = node->Prev;
counter--;
}
else if(temp[length-2] == ' ')
{
continue;
}
else
{
j = 0;
for(int r = 0;r
{
if(temp[r] != 9)
{
temp2[j] = temp[r];
j++;
}
}
temp2[j] = '\0';
{
string213 = strtok(temp2," ");
//cout<<<"\n";
strcpy(temp1,string213);
if(strcmp(temp1,"offset") == 0)
{
node->offset[0] = atof(strtok(NULL," "));
node->offset[1] = atof(strtok(NULL," "));
node->offset[2] = atof(strtok(NULL," "));
cout<offset[0]<offset[1]<offset[2]<<"\n";
}
else if(strcmp(string213,"boxmin") == 0)
{
node->boxmin[0] = atof(strtok(NULL," "));
node->boxmin[1] = atof(strtok(NULL," "));
node->boxmin[2] = atof(strtok(NULL," "));
//cout<boxmin[0]<<" "<boxmin[1]<<" "<boxmin[2]<<"\n";
}
else if(strcmp(string213,"boxmax") == 0)
{
node->boxmax[0] = atof(strtok(NULL," "));
node->boxmax[1] = atof(strtok(NULL," "));
node->boxmax[2] = atof(strtok(NULL," "));
//cout<boxmax[0]<boxmax[1]<boxmax[2]<<"\n";
}
else if(strcmp(string213,"rotxlimit") == 0)
{
node->rotateX[0] = atof(strtok(NULL," "));
node->rotateX[1] = atof(strtok(NULL," "));
//cout<rotateX[0]<rotateX[1]<<"\n";
}
else if(strcmp(string213,"rotylimit") == 0)
{
node->rotateY[0] = atof(strtok(NULL," "));
node->rotateY[1] = atof(strtok(NULL," "));
}
else if(strcmp(string213,"rotzlimit") == 0)
{
node->rotateZ[0] = atof(strtok(NULL," "));
node->rotateZ[1] = atof(strtok(NULL," "));
}
else if(strcmp(string213,"pose") == 0)
{
node->pose[0] = atof(strtok(NULL," "));
node->pose[1] = atof(strtok(NULL," "));
node->pose[2] = atof(strtok(NULL," "));
}
}
}
//cout<<
}while(counter != 0);
pointer.close();
}
void Read_Tree()
{
int tempNO=0;
int Copy_Flag = 0;
int Visited = 0;
/*root->Left->FlagL = 0;
root->Left->FlagC = 0;
root->Left->FlagR = 0;*/
node = root->Left;
//cout<<"Enter Read Tree"<<"\n";
Queue[front] = node;
rear++;
do
{
node=Queue[front];
//cout<offset[0]<<" "<offset[1]<<" "<offset[2]<
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glTranslatef(node->offset[0], node->offset[1], node->offset[2]);
if(node->pose[0] <>rotateX[0])
node->pose[0] = node->rotateX[0];
if(node->pose[0] > node->rotateX[1])
node->pose[0] = node->rotateX[1];
if(node->pose[1] <>rotateY[0])
node->pose[1] = node->rotateY[0];
if(node->pose[1] > node->rotateY[1])
node->pose[1] = node->rotateY[1];
if(node->pose[2] <>rotateZ[0])
node->pose[2] = node->rotateZ[0];
if(node->pose[2] > node->rotateZ[1])
node->pose[2] = node->rotateZ[1];
//cout<pose[0]<<" "<pose[1]<<" "<pose[2]<
glRotatef(57.3 * node->pose[0], 1.0, 0.0, 0.0);
glRotatef(57.3 * node->pose[1], 0.0, 1.0, 0.0);
glRotatef(57.3 * node->pose[2], 0.0, 0.0, 1.0);
glGetDoublev(GL_MODELVIEW_MATRIX,node->local);
/*cout<<"Current Local";
for(int d = 0;d<=15;d++)
{
cout<local[d]<<" ";
}*/
//cout<
if(node->Prev == root)
{
//cout<<"I am root \n";
glMatrixMode(GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX,node->world);
}
else
{
//cout<<"I am NOT root \n";
getWorldMatrix(node->Prev->world, node->local, node->world);
/*cout<<"Current World";
for(int d = 1;d<=16;d++)
{
cout<world[d]<<" ";
}*/
}
if(node->Left != NULL)
{
//cout<<"IF condition1"<
tempNO = node->Left->nodeNum;
Insert_Queue(node->Left);
}
if(node->Center != NULL)
{
//cout<<"IF condition2"<
tempNO = node->Center->nodeNum;
Insert_Queue(node->Center);
}
if(node->Right != NULL)
{
//cout<<"IF condition3"<
tempNO = node->Right->nodeNum;
Insert_Queue(node->Right);
}
front++;
//cout<<"Front: "<<<">
}while(front != rear);
}
As you can see I was dumb enough not to use the generic creation of a tree which made my work hell. Anyways, good luck!!
Will be happy to answer queries.