Beispiel #1
0
Branch::Branch(const std::string& name, int level, const double thickness, const double length, 
               double upDist, double upAngle, double zAngle)
  : SceneNode(name), m_boundingBox()
{
  double branchLength = length;
  if (level == 1) {
    branchLength /= 1.5;
  }

  createLeaves(level, thickness, length);

  createGeometryNode(branchLength, thickness, upDist, upAngle, zAngle);
  spawnSubBranches(level, branchLength, thickness);

  createBoundingBox();
}
Beispiel #2
0
Branch::Branch(const std::string& name, int level, const double thickness, const double length)
  : SceneNode(name), m_boundingBox()
{
  createLeaves(level, thickness, length);

  double upDist = 0.0;
  double upAngle = 0.0;
  double zAngle = 0.0;
  if (level != 0) {
    upDist = rand(1.0/3.0, 2.0/3.0, prevLength(length, level));

    upAngle = rand(1.0/3.0, 8.0/9.0, 90.0);
    zAngle = rand(0.0, 1.0, 360.0);
  }

  createGeometryNode(length, thickness, upDist, upAngle, zAngle);
  spawnSubBranches(level, length, thickness);

  createBoundingBox();
}
Beispiel #3
0
bool MeshManager::importObjMesh(const char* filename)
{
	QElapsedTimer timer;
	timer.start();
	QFile objFile(filename);

	if (!objFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
		std::cerr << "could not find file: " << filename;
		return false;
	}

	QTextStream stream(&objFile);

	std::shared_ptr<GenericDataArray<float> > vertices = std::make_shared<GenericDataArray<float> >();
	std::shared_ptr<GenericDataArray<unsigned int> > faces = std::make_shared<GenericDataArray<unsigned int> >();
	std::shared_ptr<GenericDataArray<float> > uniqueNormals = std::make_shared<GenericDataArray<float> >();
	std::vector<int> normalIndices;

	while (!stream.atEnd()) {
		QString linePrefix;
		stream >> linePrefix;

		QString line(stream.readLine());

		if (linePrefix != "#") {
			QStringList elements = line.split(' ', QString::SkipEmptyParts);

			//read vertices
			if (linePrefix == "v") {
				GenericDataArray<float>::value_type vertexPosition;
				vertexPosition[0] = elements.takeFirst().toFloat();
				vertexPosition[1] = elements.takeFirst().toFloat();
				vertexPosition[2] = elements.takeFirst().toFloat();

				vertices->push_back(vertexPosition);
			}

			//read normals
			if (linePrefix == "vn") {
				GenericDataArray<float>::value_type normal;
				normal[0] = elements.takeFirst().toFloat();
				normal[1] = elements.takeFirst().toFloat();
				normal[2] = elements.takeFirst().toFloat();

				uniqueNormals->push_back(normal);
			}

			//read faces
			else if (linePrefix == "f"){
				GenericDataArray<unsigned int>::value_type face;

				int i = 0;
				while (!elements.isEmpty() && i < 3) {
					QStringList faceElements = elements.takeFirst().split('/', QString::SkipEmptyParts);
					if (faceElements.size() == 2) {
						face[i] = faceElements.takeFirst().toInt() - 1;
						normalIndices.push_back(faceElements.takeFirst().toInt() - 1);
					}
					else {
						face[i] = faceElements.takeFirst().toInt() - 1;
					}

					++i;
				}

				faces->push_back(face);
			}
		}
	}

	std::shared_ptr<GenericDataArray<float> > normals; 

	//rearrange normals so that they can be accessed by the same index as vertices
	if (!uniqueNormals->empty()) {
		normals = std::make_shared<GenericDataArray<float> >(vertices->length());
		for (unsigned int i = 0; i < faces->length(); ++i) {
			for (unsigned int j = 0; j < 3; ++j) {
				int vertexIndex = faces->at(i, j);
				int normalIndex = normalIndices[i * 3 + j];
				normals->at(vertexIndex) = uniqueNormals->at(normalIndex);
			}
		}
	}
	else {
		normals = generateNormals(vertices, faces);
	}

	m_geomRoot = std::make_shared<sg::GroupNode>();
	m_geomRoot->addChild(createGeometryNode(vertices, normals, faces));

	std::cout << "Time: " << timer.elapsed() / 1000.0 << std::endl;
	std::cout << "Vertices: " << vertices->length() << std::endl;
	std::cout << "Faces: " << faces->length() << std::endl;

	return true;

}