Exemple #1
0
bool Obj::read(const std::string& filename) {
    int pos = filename.find_last_of('.');
    name = filename.substr(0, pos);

    std::string err = STTriangleMesh::LoadObj(stMeshes, filename);
    if (err.length() > 0) {
        std::cout << "Load obj error: " << err;
        return false;
    }
    STPoint3 cmSt = STTriangleMesh::GetMassCenter(stMeshes);
    centerOfMass = glm::vec3(cmSt.x, cmSt.y, cmSt.z);
    
    if (!readWorldMatrix()) {
        // no default matrix file was found; use bbcenter-to-origin translation matrix as default
        std::cout << name << " has no mat file, using default" << std::endl;
        centerAtOrigin();
        defaultWorldMat = worldMat;
    } else {
        std::cout << name << " mat file found" << std::endl;
    }
    return true;
}
void Lightning::generate(unsigned length)
{
	if (fracType == 1) {
		generate_ball();
		return;
	}
	double rotX;
	double rotY;
	double rotZ;
	float scale;
	int branchingTest;
	unsigned totalBolts = 0;
	Point minPoint, maxPoint;
	Point tmpPoint;
	Vector translate;
	LightningNode *tmp;
	LightningNode *prev;
	if (head != NULL || length == 0) {
		refresh();
	}
	if (length == 0) {
		return;
	}
	head = new LightningNode;
	tmp = head;
	prev = head;
	for (unsigned i = 0; i < length; i++) {
		tmp->depth = maxFracDepth;
		//Random rotation of the bolt
		srand(time(NULL)*i);
		rotX = ((double)(rand() % 60) - 30.0) * PI / 180.0;
		srand(time(NULL)*(i+i));
		rotY = ((double)(rand() % 360)) * PI / 180.0;
		srand(time(NULL)*(i * i));
		rotZ = ((double)(rand() % 60) - 30.0) * PI / 180.0;
		tmp->transform = rotY_mat(rotY) * (rotZ_mat(rotZ) * rotX_mat(rotX));
		
		//Random scaling of the bolt
		scale = 1.0;//((double)(rand() % 3) / 10.0) + 0.8;
		tmp->transform = scale_mat(Vector(scale, scale, scale)) * tmp->transform;

		//Inheriting previous matrices
		if (tmp == head) {
			minPoint = tmp->transform * headPoint;
			maxPoint = minPoint;
			tmpPoint = tmp->transform * tailPoint;
			if (tmpPoint[0] > maxPoint[0]) {
				maxPoint[0] = tmpPoint[0];
			} else if (tmpPoint[0] < minPoint[0]) {
				minPoint[0] = tmpPoint[0];
			} 
			if (tmpPoint[1] > maxPoint[1]) {
				maxPoint[1] = tmpPoint[1];
			} else if (tmpPoint[1] < minPoint[1]) {
				minPoint[1] = tmpPoint[1];
			} 
			if (tmpPoint[2] > maxPoint[2]) {
				maxPoint[2] = tmpPoint[2];
			} else if (tmpPoint[2] < minPoint[2]) {
				minPoint[2] = tmpPoint[2];
			}
		} else {
			tmp->transform = prev->transform * tmp->transform;
			tmpPoint = tmp->transform * headPoint;
			translate = (prev->transform * tailPoint) - (tmpPoint);
			tmp->transform = trans_mat(translate) * tmp->transform;
			if (tmpPoint[0] > maxPoint[0]) {
				maxPoint[0] = tmpPoint[0];
			} else if (tmpPoint[0] < minPoint[0]) {
				minPoint[0] = tmpPoint[0];
			} 
			if (tmpPoint[1] > maxPoint[1]) {
				maxPoint[1] = tmpPoint[1];
			} else if (tmpPoint[1] < minPoint[1]) {
				minPoint[1] = tmpPoint[1];
			} 
			if (tmpPoint[2] > maxPoint[2]) {
				maxPoint[2] = tmpPoint[2];
			} else if (tmpPoint[2] < minPoint[2]) {
				minPoint[2] = tmpPoint[2];
			}
			tmpPoint = tmp->transform * tailPoint;
			if (tmpPoint[0] > maxPoint[0]) {
				maxPoint[0] = tmpPoint[0];
			} else if (tmpPoint[0] < minPoint[0]) {
				minPoint[0] = tmpPoint[0];
			} 
			if (tmpPoint[1] > maxPoint[1]) {
				maxPoint[1] = tmpPoint[1];
			} else if (tmpPoint[1] < minPoint[1]) {
				minPoint[1] = tmpPoint[1];
			}
			if (tmpPoint[2] > maxPoint[2]) {
				maxPoint[2] = tmpPoint[2];
			} else if (tmpPoint[2] < minPoint[2]) {
				minPoint[2] = tmpPoint[2];
			}
		}

		//Give it branches
		srand((time(NULL) * 24678 * i) + (i *2));
		branchingTest = (rand()) % 200;
		if (branchingTest >= THRESHOLD3) {
			tmp->numBranches = 3;
			tmp->branches = new LightningNode*[3];
			tmp->branches[0] = NULL;
			tmp->branches[1] = NULL;
			tmp->branches[2] = NULL;
			branchesPerLine += 3;
		} else if (branchingTest >= THRESHOLD2) {
			tmp->numBranches = 2;
			tmp->branches = new LightningNode*[2];
			tmp->branches[0] = NULL;
			tmp->branches[1] = NULL;
			branchesPerLine += 2;
		} else if (branchingTest >= THRESHOLD1) {
			tmp->numBranches = 1;
			tmp->branches = new LightningNode*[1];
			tmp->branches[0] = NULL;
			branchesPerLine++;
		} else {
			tmp->numBranches = 0;
			tmp->branches = NULL;
		}

		if (i < (length - 1)) {
			tmp->next = new LightningNode;
			prev = tmp;
			tmp = tmp->next;
			tmp->next = NULL;
		}
	}
	std::cout<<"Total bolts rendered: ";
	for (int i = 0; i <= maxFracDepth; i++) {
		totalBolts += length * pow(branchesPerLine, i);
	}
	std::cout<<totalBolts<<std::endl;
	centerAtOrigin(maxPoint, minPoint);
	//TODO: Fractal compatibility
	tmp = head;
	while (tmp != NULL) {
		fractalize(tmp);
		tmp = tmp->next;
	}
}