/**
 * ---|> [State]
 */
State::stateResult_t StrangeExampleRenderer::doEnableState(FrameContext & context,Node * node, const RenderParam & rp){

	if (rp.getFlag(NO_GEOMETRY) || rp.getFlag(SKIP_RENDERER))
		return State::STATE_SKIPPED;

	/// Collect all nodes in the frustum
	const auto nodes = collectNodesInFrustum<GeometryNode>(node, context.getCamera()->getFrustum());

	if(nodes.size() == 1 && nodes.front() == node) {
		WARN("This renderer must not be attached to GeometryNodes.");
		return State::STATE_SKIPPED;
	}

	/// Render all nodes with a little extra strangeness
	Geometry::Matrix4x4 m;
	float f=0;
	for(const auto & geoNode : nodes) {
		float f2=f+Util::Timer::now();
		m.setIdentity();
		m.scale(1.0+sin( f2)/40.0);
		m.rotate_deg( sin( f2/0.7)/2.0,0,1,0);
		context.getRenderingContext().pushMatrix_modelToCamera();
		context.getRenderingContext().multMatrix_modelToCamera(m);
		context.displayNode(geoNode,rp);
		context.getRenderingContext().popMatrix_modelToCamera();
		f+=137.0;

	}

	return State::STATE_SKIP_RENDERING;
}
void drawGrid(RenderingContext & rc, float scale) {
	static Util::Reference<Mesh> mesh;
	if (mesh.isNull()) {
		VertexDescription vertexDescription;
		vertexDescription.appendPosition3D();
		mesh = new Mesh(vertexDescription, 4 * 101, 4 * 101);
		mesh->setDrawMode(Mesh::DRAW_LINES);
		
		MeshVertexData & vd = mesh->openVertexData();
		float * vertices = reinterpret_cast<float *> (vd.data());
		MeshIndexData & id = mesh->openIndexData();
		uint32_t * indices = id.data();
		uint32_t nextIndex = 0;
		const float step = 1.0f / 100.0f;
		for (uint_fast8_t line = 0; line < 101; ++line) {
			const float pos = -0.5f + static_cast<float> (line) * step;

			*vertices++ = -0.5f;
			*vertices++ = 0.0f;
			*vertices++ = pos;

			*vertices++ = 0.5f;
			*vertices++ = 0.0f;
			*vertices++ = pos;

			*indices++ = nextIndex++;
			*indices++ = nextIndex++;

			*vertices++ = pos;
			*vertices++ = 0.0f;
			*vertices++ = -0.5f;

			*vertices++ = pos;
			*vertices++ = 0.0f;
			*vertices++ = 0.5f;

			*indices++ = nextIndex++;
			*indices++ = nextIndex++;

		}
		vd.updateBoundingBox();
		vd.markAsChanged();
		id.updateIndexRange();
		id.markAsChanged();
	}

	Geometry::Matrix4x4 matrix;
	matrix.scale(scale);
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(matrix);
	rc.displayMesh(mesh.get());
	rc.popMatrix_modelToCamera();
}
Exemple #3
0
void drawBox(RenderingContext & rc, const Geometry::Box & box) {
	static Util::Reference<Mesh> mesh;
	if (mesh.isNull()) {
		VertexDescription vertexDescription;
		vertexDescription.appendPosition3D();
		vertexDescription.appendNormalFloat();
		const Geometry::Box unitBox(Geometry::Vec3(-0.5f, -0.5f, -0.5f), Geometry::Vec3(0.5f, 0.5f, 0.5f));
		mesh = MeshUtils::MeshBuilder::createBox(vertexDescription, unitBox);
	}

	Geometry::Matrix4x4 matrix;
	matrix.translate(box.getCenter());
	matrix.scale(box.getExtentX(), box.getExtentY(), box.getExtentZ());
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(matrix);
	rc.displayMesh(mesh.get());
	rc.popMatrix_modelToCamera();
}
Exemple #4
0
void drawRect(RenderingContext & rc, const Geometry::Rect & rect) {
	static Util::Reference<Mesh> mesh;
	if (mesh.isNull()) {
		VertexDescription vertexDescription;
		vertexDescription.appendPosition2D();
		mesh = new Mesh(vertexDescription, 4, 6);
		mesh->setDrawMode(Mesh::DRAW_TRIANGLES);

		MeshVertexData & vd = mesh->openVertexData();
		float * vertices = reinterpret_cast<float *> (vd.data());
		*vertices++ = 0.0f; // Bottom left
		*vertices++ = 0.0f;
		*vertices++ = 1.0f; // Bottom right
		*vertices++ = 0.0f;
		*vertices++ = 1.0f; // Top right
		*vertices++ = 1.0f;
		*vertices++ = 0.0f; // Top left
		*vertices++ = 1.0f;
		vd.updateBoundingBox();
		vd.markAsChanged();

		MeshIndexData & id = mesh->openIndexData();
		uint32_t * indices = id.data();
		indices[0] = 0;
		indices[1] = 2;
		indices[2] = 1;
		indices[3] = 0;
		indices[4] = 3;
		indices[5] = 2;
		id.updateIndexRange();
		id.markAsChanged();
	}

	Geometry::Matrix4x4 matrix;
	matrix.translate(rect.getX(), rect.getY(), 0.0f);
	matrix.scale(rect.getWidth(), rect.getHeight(), 1.0f);
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(matrix);
	rc.displayMesh(mesh.get());
	rc.popMatrix_modelToCamera();
}
Exemple #5
0
void BoxTest::testSetters() {
	const Geometry::Box b1(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f);
	Geometry::Box b2;

	b2.setMinX(-1.0f);
	b2.setMaxX(1.0f);
	b2.setMinY(-2.0f);
	b2.setMaxY(2.0f);
	b2.setMinZ(-3.0f);
	b2.setMaxZ(3.0f);
	CPPUNIT_ASSERT(b2 == b1);

	b2.setMin(Geometry::dimension_t::X, -1.0f);
	b2.setMax(Geometry::dimension_t::X, 1.0f);
	b2.setMin(Geometry::dimension_t::Y, -2.0f);
	b2.setMax(Geometry::dimension_t::Y, 2.0f);
	b2.setMin(Geometry::dimension_t::Z, -3.0f);
	b2.setMax(Geometry::dimension_t::Z, 3.0f);
	CPPUNIT_ASSERT(b2 == b1);

	b2.set(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f);
	CPPUNIT_ASSERT(b2 == b1);

	b2 = Geometry::Box();
	CPPUNIT_ASSERT(b2 == Geometry::Box());

	b2 = b1;
	CPPUNIT_ASSERT(b2 == b1);

	b2.set(1.0f, 2.0f, 3.0f);
	b2.include(-1.0, -2.0f, -3.0f);
	CPPUNIT_ASSERT(b2 == b1);

	b2.set(1.0f, 2.0f, 3.0f);
	b2.include(Geometry::Vec3(-1.0, -2.0f, -3.0f));
	CPPUNIT_ASSERT(b2 == b1);

	b2 = Geometry::Box();
	b2.include(b1);
	CPPUNIT_ASSERT(b2 == b1);

	b2 = Geometry::Box();
	b2.setCenter(Geometry::Vec3(0.0f, 0.0f, 0.0f));
	b2.include(-1.0, -2.0f, -3.0f);
	b2.include(1.0, 2.0f, 3.0f);
	CPPUNIT_ASSERT(b2 == b1);

	b2 = Geometry::Box();
	b2.setCenter(Geometry::Vec3(0.5f, 0.5f, 0.5f));
	b2.setExtentX(1.0f);
	b2.setExtentY(1.0f);
	b2.setExtentZ(1.0f);
	CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f));

	b2 = Geometry::Box();
	b2.setCenter(Geometry::Vec3(0.5f, 0.5f, 0.5f));
	b2.setExtent(1.0f);
	CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f));

	b2.resizeAbs(2.0f);
	CPPUNIT_ASSERT(b2 == Geometry::Box(-2.0f, 3.0f, -2.0f, 3.0f, -2.0f, 3.0f));

	b2.resizeRel(1.5f);
	CPPUNIT_ASSERT(b2 == Geometry::Box(-3.25f, 4.25f, -3.25f, 4.25f, -3.25f, 4.25f));

	b2.translate(Geometry::Vec3(3.25f, -4.25f, 0.0f));
	CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 7.5f, -7.5f, 0.0f, -3.25f, 4.25f));

	Geometry::Matrix4x4 mat;

	mat.setIdentity();
	b2 = Geometry::Helper::getTransformedBox(b2, mat);
	CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 7.5f, -7.5f, 0.0f, -3.25f, 4.25f));

	mat.scale(2.0f);
	mat.translate(-3.25f, 4.25f, 0.0f);
	b2 = Geometry::Helper::getTransformedBox(b2, mat);
	CPPUNIT_ASSERT(b2 == Geometry::Box(-6.5f, 8.5f, -6.5f, 8.5f, -6.5f, 8.5f));
}
void drawCoordSys(RenderingContext & rc, float scale) {
	static Util::Reference<Mesh> arrow;
	static Util::Reference<Mesh> sphere;
	static Util::Reference<Mesh> charX;
	static Util::Reference<Mesh> charY;
	static Util::Reference<Mesh> charZ;
	const float radius = 0.025f;
	if (arrow.isNull()) {
		std::deque<Mesh *> meshes;
		std::deque<Geometry::Matrix4x4> transformations;

		Geometry::Matrix4x4 transform;

		meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(radius, radius, 0.7f, 16));
		transformations.push_back(transform);

		meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(radius, 2.0f * radius, 0.01f, 16));
		transform.translate(0.7f, 0.0f, 0.0f);
		transformations.push_back(transform);

		meshes.push_back(MeshUtils::MeshBuilder::createCone(2.0f * radius, 0.29f, 16));
		transform.translate(0.01f, 0.0f, 0.0f);
		transformations.push_back(transform);

		arrow = MeshUtils::combineMeshes(meshes, transformations);
		MeshUtils::optimizeIndices(arrow.get());

		while (!meshes.empty()) {
			delete meshes.back();
			meshes.pop_back();
		}
	}
	if (sphere.isNull()) {
		Util::Reference<Mesh> icosahedron = MeshUtils::PlatonicSolids::createIcosahedron();
		sphere = MeshUtils::PlatonicSolids::createEdgeSubdivisionSphere(icosahedron.get(), 2);
		Geometry::Matrix4x4 transform;
		transform.scale(1.1f * radius);
		MeshUtils::transform(sphere.get()->openVertexData(), transform);
	}
	if(charX.isNull()) {
		std::deque<Mesh *> meshes;
		std::deque<Geometry::Matrix4x4> transformations;

		VertexDescription vertexDescription;
		vertexDescription.appendPosition3D();
		vertexDescription.appendNormalFloat();

		const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.2f, 0.05f);
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(1.2f, 0.0f, 0.0f);
			transform.rotate_deg(30.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(1.2f, 0.0f, 0.0f);
			transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		charX = MeshUtils::combineMeshes(meshes, transformations);
		MeshUtils::optimizeIndices(charX.get());

		while(!meshes.empty()) {
			delete meshes.back();
			meshes.pop_back();
		}
	}
	if(charY.isNull()) {
		std::deque<Mesh *> meshes;
		std::deque<Geometry::Matrix4x4> transformations;

		VertexDescription vertexDescription;
		vertexDescription.appendPosition3D();
		vertexDescription.appendNormalFloat();

		const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.1f, 0.05f);
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(0.025f, 0.045f, 0.0f);
			transform.rotate_deg(30.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(-0.025f, 0.045f, 0.0f);
			transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(0.0f, -0.045f, 0.0f);
			transformations.push_back(transform);
		}
		charY = MeshUtils::combineMeshes(meshes, transformations);
		Geometry::Matrix4x4 transform;
		transform.translate(1.2f, 0.0f, 0.0f);
		transform.rotate_deg(90.0f, 0.0f, 0.0f, -1.0f);
		MeshUtils::transform(charY->openVertexData(), transform);
		MeshUtils::optimizeIndices(charY.get());

		while(!meshes.empty()) {
			delete meshes.back();
			meshes.pop_back();
		}
	}
	if(charZ.isNull()) {
		std::deque<Mesh *> meshes;
		std::deque<Geometry::Matrix4x4> transformations;

		VertexDescription vertexDescription;
		vertexDescription.appendPosition3D();
		vertexDescription.appendNormalFloat();

		const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.1f, 0.05f);
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(1.2f, 0.075f, 0.0f);
			transform.rotate_deg(90.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(1.2f, 0.0f, 0.0f);
			transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f);
			transform.scale(1.0f, 1.6f, 1.0f);
			transformations.push_back(transform);
		}
		{
			meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box));
			Geometry::Matrix4x4 transform;
			transform.translate(1.2f, -0.075f, 0.0f);
			transform.rotate_deg(-90.0f, 0.0f, 0.0f, -1.0f);
			transformations.push_back(transform);
		}
		charZ = MeshUtils::combineMeshes(meshes, transformations);
		MeshUtils::optimizeIndices(charZ.get());

		while(!meshes.empty()) {
			delete meshes.back();
			meshes.pop_back();
		}
	}
	// Origin
	rc.pushAndSetColorMaterial(Util::ColorLibrary::WHITE);
	rc.displayMesh(sphere.get());
	rc.popMaterial();

	// X axis
	Geometry::Matrix4x4 transform;
	transform.scale(scale, 1.0f, 1.0f);
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(transform);
	rc.pushAndSetColorMaterial(Util::ColorLibrary::RED);
	rc.displayMesh(arrow.get());
	rc.displayMesh(charX.get());
	rc.popMaterial();
	rc.popMatrix_modelToCamera();
	// Y axis
	transform.setIdentity();
	transform.scale(1.0f, scale, 1.0f);
	transform.rotate_deg(90.0f, 0.0f, 0.0f, 1.0f);
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(transform);
	rc.pushAndSetColorMaterial(Util::ColorLibrary::GREEN);
	rc.displayMesh(arrow.get());
	rc.displayMesh(charY.get());
	rc.popMaterial();
	rc.popMatrix_modelToCamera();
	// Z axis
	transform.setIdentity();
	transform.scale(1.0f, 1.0f, scale);
	transform.rotate_deg(90.0f, 0.0f, -1.0f, 0.0f);
	rc.pushMatrix_modelToCamera();
	rc.multMatrix_modelToCamera(transform);
	rc.pushAndSetColorMaterial(Util::ColorLibrary::BLUE);
	rc.displayMesh(arrow.get());
	rc.displayMesh(charZ.get());
	rc.popMaterial();
	rc.popMatrix_modelToCamera();
}