Example #1
0
void MonoTexCube::drawAll()
{
    loadMaterial();
    loadModel();
    draw();
    unloadModel();
    unloadMaterial();
}
	/*! Renders the scenegraph
	  This method is responsible for sorting by material and rendering game objects in material priority order
	  \param root	The root of the scenegraph to be rendered
	  */
	void Renderer::render(Transform *root){
		Vector3 cameraPos;
		float distance;
		GameObject *object;
		std::vector<Material*>::iterator mit;

		// Single common camera for all rendering
		if (camera)
			cameraPos = camera->gameObject->getTransform()->getWorldPosition();
		else
			cameraPos = Vector3(0,0,0);

		buildRenderQueue(root);

		for (int i=0; i<PRIORITY_LEVELS; i++) {

			// temp list of objects requiring sorted draw order (if any)
			std::priority_queue<GameObject*, std::vector<GameObject*>, GameObjectCameraDistanceCompare> sorted;

			for (mit = materials[i].begin(); mit!=materials[i].end(); mit++){
				
				std::queue<GameObject*> &q = (*mit)->getQueue();	// objects to be drawn

				if (!(*mit)->getSortedDraw()) {
					// objects for normal unsorted materials are drawn immediately
					loadMaterial(*mit);
					while (!q.empty()) {
						object = q.front();
						if (object->isVisible()) {
							draw(object);
						}
						q.pop();
					}
					unloadMaterial(*mit);
				}
				else {
					// objects to be sorted and drawn later
					while (!q.empty()) {
						object = q.front();
						if (object->isVisible()) {
							// Note using squared distance as we only care about relative distance
							distance = cameraPos.squaredDistance(object->getTransform()->getWorldPosition());
							object->setDistanceToCamera(distance);
							sorted.push(object);
						}
						q.pop();
					}
				}
			}

			// Draw sorted objects (if any)
			Material *loaded = NULL;			// current loaded material
			while (!sorted.empty()) {
				object = sorted.top();
				if (loaded != object->getMaterial()) {
					// only load material if changed
					loaded = object->getMaterial();
					loadMaterial(loaded);
				}
				draw(object);
				sorted.pop();
			}
		}
	}