Example #1
0
void drawRecurse(void (*callbackFunction)(ObjectAddress), SceneGraphNode* node,bool preTransform){
#ifndef XSICONVERT

	FloatVector3d pos,rot,scale;

	if(node->object.type==OBJECT){

		if(node->object.object->envelopes.size()>0){
			pos=node->object.object->basePos;
			rot=node->object.object->baseRot;
			scale=node->object.object->baseScale;
		}else{
			pos=node->object.object->pos;
			rot=node->object.object->rot;
			scale=node->object.object->scale;
		}

	}else if(node->object.type==NULL3D){
		pos=node->object.null->pos;
		rot=node->object.null->rot;
		scale=node->object.null->scale;
	}else if(node->object.type==IK_ROOT){
		pos=node->object.ikRoot->pos;
		rot=node->object.ikRoot->rot;
		scale=node->object.ikRoot->scale;
	}else if(node->object.type==IK_JOINT){
		pos=node->object.ikJoint->pos;
		rot=node->object.ikJoint->rot;
		scale=node->object.ikJoint->scale;
	}else if(node->object.type==IK_EFFECTOR){
		pos=FloatVector3d();
		rot=FloatVector3d();
		scale=FloatVector3d(1,1,1);
	}else if(node->object.type==LIGHT){
		pos=node->object.light->pos;
		rot=node->object.light->rot;
		scale=node->object.light->scale;
	}else{

	}

	if(node->draw && !preTransform){

		if(node->object.type==OBJECT){
			if(node->object.object->envelopeVerticesCount>1){
			
				for(int i=0; i<node->object.object->envelopeVerticesCount; i++){
					node->object.object->drawCurrentEnvelope=i;
					callbackFunction(node->object);
				}
			}else{
				callbackFunction(node->object);
			}
		}else{
			callbackFunction(node->object);
		}
	}

#ifndef SOFTWARE_TRANSFORMS

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	glTranslatef(pos.x,pos.y,pos.z);
	glRotatef(rot.z,0.0f,0.0f,1.0f);
	glRotatef(rot.y,0.0f,1.0f,0.0f);
	glRotatef(rot.x,1.0f,0.0f,0.0f);
	glScalef(scale.x,scale.y,scale.z);

	if(node->object.type==IK_EFFECTOR){
		IkRoot* r=node->object.ikEffector->root;

		if(r->sceneGraphNode!=node->parent){
			//we only really care where the root is
			//so if the root isn't our parent, we need to trace to the root
			//argh


			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();

			glLoadIdentity();
			level->camera->transform();
			sceneGraph.modelviewTransform(&sceneGraph.root,r->sceneGraphNode);


		}

		for(int i=0; i<r->joints.size(); i++){

			IkJoint* j=r->joints[i];

			glRotatef(j->rot.z,0.0f,0.0f,1.0f);
			glRotatef(j->rot.y,0.0f,1.0f,0.0f);
			glRotatef(j->rot.x,1.0f,0.0f,0.0f);
			glScalef(j->scale.x,j->scale.y,j->scale.z);

			glTranslatef(j->length,0,0);
			

		}
	}

#endif

	if(node->draw && preTransform){

		if(node->object.type==OBJECT){
			if(node->object.object->envelopeVerticesCount>1){
			
				for(int i=0; i<node->object.object->envelopeVerticesCount; i++){
					node->object.object->drawCurrentEnvelope=i;
					callbackFunction(node->object);
				}
			}else{
				callbackFunction(node->object);
			}
		}else{
			callbackFunction(node->object);
		}
	}

	Array<SortItem> sortids;

	FloatVector3d p;
	
	for(int i=0; i<node->children.size(); i++){
		if(node->children[i].object.type==OBJECT){
			p=node->children[i].object.object->box.center()-level->camera->pos;
			sortids.pushBack(SortItem(i,math.dotProduct(p,p)));
		}else{
			sortids.pushBack(SortItem(i,0));
		}
	}

	Array<int> res=sort.sort(sortids,true);

	for(int i=0; i<res.size(); i++){
		drawRecurse(callbackFunction,&node->children[res[i]],preTransform);
	}
	
	if(node->object.type==IK_EFFECTOR){
		IkRoot* r=node->object.ikEffector->root;

		if(r->sceneGraphNode!=node->parent){
			glPopMatrix();
		}
	}

#ifndef SOFTWARE_TRANSFORMS
	glPopMatrix(); 
#endif
#endif
}