Ejemplo n.º 1
0
void StaticGeometry::DrawBoundingBox(const Aabb &bb)
{
	const vector3f min(bb.min.x, bb.min.y, bb.min.z);
	const vector3f max(bb.max.x, bb.max.y, bb.max.z);
	const vector3f fbl(min.x, min.y, min.z); //front bottom left
	const vector3f fbr(max.x, min.y, min.z); //front bottom right
	const vector3f ftl(min.x, max.y, min.z); //front top left
	const vector3f ftr(max.x, max.y, min.z); //front top right
	const vector3f rtl(min.x, max.y, max.z); //rear top left
	const vector3f rtr(max.x, max.y, max.z); //rear top right
	const vector3f rbl(min.x, min.y, max.z); //rear bottom left
	const vector3f rbr(max.x, min.y, max.z); //rear bottom right

	const Color c(Color::WHITE);

	std::unique_ptr<Graphics::VertexArray> vts(new Graphics::VertexArray(Graphics::ATTRIB_POSITION | Graphics::ATTRIB_DIFFUSE));

	//Front face
	vts->Add(ftr, c); //3
	vts->Add(fbr, c); //1
	vts->Add(fbl, c); //0

	vts->Add(fbl, c); //0
	vts->Add(ftl, c); //2
	vts->Add(ftr, c); //3

	//Rear face
	vts->Add(rbr, c); //7
	vts->Add(rtr, c); //5
	vts->Add(rbl, c); //6

	vts->Add(rbl, c); //6
	vts->Add(rtr, c); //5
	vts->Add(rtl, c); //4

	//Top face
	vts->Add(rtl, c); //4
	vts->Add(rtr, c); //5
	vts->Add(ftr, c); //3

	vts->Add(ftr, c); //3
	vts->Add(ftl, c); //2
	vts->Add(rtl, c); //4

	//bottom face
	vts->Add(fbr, c); //1
	vts->Add(rbr, c); //7
	vts->Add(rbl, c); //6

	vts->Add(rbl, c); //6
	vts->Add(fbl, c); //0
	vts->Add(fbr, c); //1

	//left face
	vts->Add(fbl, c); //0
	vts->Add(rbl, c); //6
	vts->Add(rtl, c); //4

	vts->Add(rtl, c); //4
	vts->Add(ftl, c); //2
	vts->Add(fbl, c); //0

	//right face
	vts->Add(rtr, c); //5
	vts->Add(rbr, c); //7
	vts->Add(fbr, c); //1

	vts->Add(fbr, c); //1
	vts->Add(ftr, c); //3
	vts->Add(rtr, c); //5

	Graphics::Renderer *r = GetRenderer();

	Graphics::RenderStateDesc rsd;
	rsd.cullMode = Graphics::CULL_NONE;

	RefCountedPtr<Graphics::VertexBuffer> vb;
	//create buffer and upload data
	Graphics::VertexBufferDesc vbd;
	vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION;
	vbd.attrib[0].format   = Graphics::ATTRIB_FORMAT_FLOAT3;
	vbd.attrib[1].semantic = Graphics::ATTRIB_DIFFUSE;
	vbd.attrib[1].format   = Graphics::ATTRIB_FORMAT_UBYTE4;
	vbd.numVertices = vts->GetNumVerts();
	vbd.usage = Graphics::BUFFER_USAGE_STATIC;
	vb.Reset( m_renderer->CreateVertexBuffer(vbd) );
	vb->Populate( *vts );

	r->SetWireFrameMode(true);
	r->DrawBuffer(vb.Get(), r->CreateRenderState(rsd), Graphics::vtxColorMaterial);
	r->SetWireFrameMode(false);
}
Ejemplo n.º 2
0
void Shields::ReparentShieldNodes(SceneGraph::Model* model)
{
	assert(s_initialised);

	Graphics::Renderer *renderer = model->GetRenderer();

	using SceneGraph::Node;
	using SceneGraph::Group;
	using SceneGraph::MatrixTransform;
	using SceneGraph::StaticGeometry;

	//This will find all matrix transforms meant for navlights.
	SceneGraph::FindNodeVisitor shieldFinder(SceneGraph::FindNodeVisitor::MATCH_NAME_ENDSWITH, "_shield");
	model->GetRoot()->Accept(shieldFinder);
	const std::vector<Node*> &results = shieldFinder.GetResults();

	//Move shield geometry to same level as the LODs
	for (unsigned int i = 0; i < results.size(); i++) {
		MatrixTransform *mt = dynamic_cast<MatrixTransform*>(results.at(i));
		assert(mt);

		const Uint32 NumChildren = mt->GetNumChildren();
		if (NumChildren>0)
		{
			// Group to contain all of the shields we might find
			Group *shieldGroup = new Group(renderer);
			shieldGroup->SetName(s_shieldGroupName);

			// go through all of this MatrixTransforms children to extract all of the shield meshes
			for (Uint32 iChild = 0; iChild < NumChildren; ++iChild) {
				Node* node = mt->GetChildAt(iChild);
				assert(node);
				if (node)
				{
					RefCountedPtr<StaticGeometry> sg(dynamic_cast<StaticGeometry*>(node));
					assert(sg.Valid());
					sg->SetNodeMask(SceneGraph::NODE_TRANSPARENT);

					// We can early-out if we've already processed this models scenegraph.
					if (Graphics::BLEND_ALPHA == sg->m_blendMode) {
						assert(false);
					}

					// force the blend mode
					sg->m_blendMode = Graphics::BLEND_ALPHA;

					Graphics::RenderStateDesc rsd;
					rsd.blendMode = Graphics::BLEND_ALPHA;
					rsd.depthWrite = false;
					sg->SetRenderState(renderer->CreateRenderState(rsd));

					for (Uint32 iMesh = 0; iMesh < sg->GetNumMeshes(); ++iMesh) {
						StaticGeometry::Mesh &rMesh = sg->GetMeshAt(iMesh);
						rMesh.material = GetGlobalShieldMaterial();
					}

					// find the accumulated transform from the root to our node
					MatrixAccumVisitor mav(mt->GetName());
					model->GetRoot()->Accept(mav);

					// set our nodes transformation to be the accumulated transform
					MatrixTransform *sg_transform_parent = new MatrixTransform(renderer, mav.outMat);
					std::stringstream nodeStream;
					nodeStream << iChild << s_matrixTransformName;
					sg_transform_parent->SetName(nodeStream.str());
					sg_transform_parent->AddChild(sg.Get());

					// dettach node from current location in the scenegraph...
					mt->RemoveChild(node);

					// attach new transform node which parents the our shields mesh to the shield group.
					shieldGroup->AddChild(sg_transform_parent);
				}
			}

			model->GetRoot()->AddChild(shieldGroup);
		}
	}
}