Beispiel #1
0
void Shape::scale(float scalor)
{
	resetBoundingBox();
	for(int i = 0; i < pointList.size(); i++)	{
		pointList[i].x *= scalor;
		pointList[i].y *= scalor;
		updateBoundingBox(pointList[i]);
	}
}
Beispiel #2
0
void Shape::rotate(float rotation)
{
	// Setup variables and create the rotation matrix
	D3DXMATRIX rotationMatrix;
	D3DXVECTOR4 vector;
	vector.z = 0;
	vector.w = 1;
	D3DXMatrixRotationZ(&rotationMatrix, rotation);

	/* Calculate the new position on the origin based on the rotation 
		- Rotating around an axis other than the origin makes  the origins position change
		- Therefor this step is done
		- mRotationAxis is the distance from the origin to the rotation axis
	*/

	// Get the rotation axis (local coordinates)
	vector.x = mRotationAxis.x;
	vector.y = mRotationAxis.y;

	// Do the rotation
	D3DXVec4Transform(&vector,  &vector, &rotationMatrix);

	// Set the new position 
	setOrigin(Vector(vector.x, vector.y) + (getOrigin() - mRotationAxis));

	// Set the new rotation axis offset
	mRotationAxis = Vector(vector.x, vector.y);

	// For proper updating of the bounding box
	resetBoundingBox();

	// Calculate each points new local position when they rotate around the axis (0,0)
	for(int i = 0; i < pointList.size(); i++)	
	{
		vector.x = pointList[i].x;	
		vector.y = pointList[i].y;

		D3DXVec4Transform(&vector,  &vector, &rotationMatrix);

		pointList[i].x = vector.x;
		pointList[i].y = vector.y;

		// Update the bounding box
		updateBoundingBox(pointList[i]);
	}

	// Add to the rotation
	mRotation += rotation;

	// To avoid big numbers
	if(mRotation > 2*3.14)
		mRotation = rotation;
	else if(mRotation < -2*3.14)
		mRotation = rotation;
}
Beispiel #3
0
void ofxLSTurtle::generate(ofVboMesh& mesh, const string _instruction, const int _depth) {
    resetBoundingBox();
    bool branching = false;
    auto instructions = getInstructionsFromString(_instruction);

    for (auto stringInstruction : instructions) {
        auto inst = ofxLSInstruction(stringInstruction);
        auto head = inst.getHead();
        if (head == "F") {
            branching =  true;
        }else if( head == "G") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->boom(inst.getLength(defaultLength));
            nodesContainer.push_back(newJoin);
        }else if (head == "+") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->rollDeg(+inst.getAngle(theta));
            if(randomYRotation){
                newJoin->panDeg(ofRandom(30.00, 330.00));
            }
            nodesContainer.push_back(newJoin);
        }else if (head == "-") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->rollDeg(-inst.getAngle(theta));
            if(randomYRotation){
                newJoin->panDeg(ofRandom(30.00, 330.00));
            }
            nodesContainer.push_back(newJoin);
        }else if (head == "|") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->panDeg(+inst.getAngle(180.00));
            nodesContainer.push_back(newJoin);
        }else if (head == "&") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->tiltDeg(+inst.getAngle(theta));
            nodesContainer.push_back(newJoin);
        }
        else if (head == "^") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->tiltDeg(-inst.getAngle(theta));
            nodesContainer.push_back(newJoin);
        }
        else if (head == "\\") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->panDeg(+inst.getAngle(theta));
            nodesContainer.push_back(newJoin);
        }
        else if (head == "/") {
            shared_ptr<ofNode> newJoin(new ofNode);
            newJoin->setParent(*nodesContainer.back());
            newJoin->panDeg(-inst.getAngle(180.00));
            nodesContainer.push_back(newJoin);
        }
        else if (head == "[") {
            bookmarks.push_back(nodesContainer.back());
        }
        else if (head == "]") {
            nodesContainer.push_back(bookmarks.back());
            bookmarks.pop_back();
        }

        if (branching) {
            float length = inst.getLength(defaultLength);
            auto beginBranch = nodesContainer.back();
            shared_ptr<ofNode> endBranch(new ofNode);
            endBranch->setParent(*nodesContainer.back());
            endBranch->move(ofVec3f(0, length, 0));

            maybeVectorExpandsBoundingBox(endBranch->getGlobalPosition());

            auto widths = getPrevAndCurrentWidth(length);
            auto newBranch = ofxLSBranch(*beginBranch, *endBranch, widths);
            nodesContainer.push_back(endBranch);

            // L-Systems are recursive, to avoid to insert the same mesh multiple
            // times, we check if a branch is not already in
            if (!branchAlreadySaved(newBranch)) {
                geometryBuilder.putIntoMesh(newBranch, mesh, geometry, resolution, length, textureRepeat);
                branchContainer.push_back(newBranch);
            }

            branching = false;
        }
    }

    //TODO, separate the generation of the node skeleton to mesh construction
//    float distance;
//    for(auto b : branchContainer){
//        if(b->getParent() != nullptr){
//            cout << "Start"<< endl;
//            cout << b->getParent()->getGlobalPosition().x;
//            cout << b->getParent()->getGlobalPosition().y;
//            cout << b->getParent()->getGlobalPosition().x;
//            cout << "End" << endl;
//            cout << b->getGlobalPosition().x;
//            cout << b->getGlobalPosition().y;
//            cout << b->getGlobalPosition().x;
//        }else{
//            //root point
//        }
//
//    }

    nodesContainer.clear();
    bookmarks.clear();
    historySizes.clear();
    branchContainer.clear();
}