Quantcast
Viewing all articles
Browse latest Browse all 40524

Adding ofMatrix4x4 transformations to a mesh and go back to the original position

Hi, I'm trying to build an L-System in 3d with OF. In order to do this, i generate a mesh, I apply a transformation to it, and i draw the mesh. The problem is that after transforming the mesh a couple of times, i want to go back to the original position. How can I achieve this?
I've tried ofPushMatrix and ofPopMatrix, but they are not working as expected, once i pop the matrix out, the position is not where I left it before.

That's the code that decide which transformation to apply to the mesh:

void Transformer::generate(ofVboMesh& mesh, string _instruction, int depth) {
    instructions = _instruction;
    for (int i = 0; i < instructions.length(); i++) {
        char c = instructions[i];
        if (c == 'F') {
            addBranchIntoMesh(branch, mesh, length, "cube");
            transform.translate(0, 0, length);
            branch.setCurrentTransform(transform);
        }else if( c == 'G') {
            transform.translate(0, 0, length);
            branch.setCurrentTransform(transform);
        }else if (c == '+') {
            transform.rotate(+theta, 0, 1, 0);
            branch.setCurrentTransform(transform);
        }
        else if (c == '-') {
            transform.rotate(-theta, 0, 1, 0);
            branch.setCurrentTransform(transform);
        }
        else if (c == '[') {
            branch.setCurrentTransform(branch.getCurrentTransform());
            ofPushMatrix();
        }
        else if (c == ']') {
            ofPopMatrix();
        }
    }
    currentDepth++;
}

void Transformer::addBranchIntoMesh(Branch branch, ofVboMesh& mesh, float length, string geometry){
    if(geometry == "cube") {
        cube.generate(mesh, currentDepth, length, branch.getCurrentTransform());
    }
}

The Branch class simply store the previous transformation and get the current one

#pragma once
#include "ofMain.h"

class Branch {
public:
    Branch(
           const ofMatrix4x4& currentTransform = ofMatrix4x4()
    );
    ofMatrix4x4 currentTransform;
    ofMatrix4x4 getCurrentTransform() const { return currentTransform; }
    void setCurrentTransform(const ofMatrix4x4& transform) { this->currentTransform = transform; }
};

The cube class, take the general mesh (the mesh that will contain all the cubes), and put into it a cube, a the position specified by the transformation.

void Cube::generate(ofMesh& mesh, int depth, unsigned int length, ofMatrix4x4 transformation){
    ofColor color(255, 0, 0);
    float hue = 254.f;
    vertices = createCube(length);
    // for the sake of brevity i will not include the createCube ;)
    for (unsigned i = 0; i < vertices.size(); ++i){
        mesh.addVertex(vertices[i] * transformation);
        mesh.addNormal(normals[i]);
        mesh.addTexCoord(ofVec2f(0.f, depth));
        mesh.addColor(color);
    }
}

I'm also not sure if ofMatrix4x4 fits my needs, that is basically move a turtle back and forth on the screen and drawing a mesh where is it needed.
Any suggestion is more than appreciated!


Viewing all articles
Browse latest Browse all 40524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>