ofTrueTypeFont text is not opaque

#1

Hello,

I’m building a wordcloud in a 3D space but the text doesn’t seem to be opaque when it’s in front of other text.

struct Word {
    string text;
    float val;
    int pos_x;
    int pos_y;
    int pos_z;
} ;

std::vector <Word> words;
ofTrueTypeFont font;
ofEasyCam cam;

void ofApp::setup(){
    
    font.load("font.ttf", 100);
    
    for(int i = 0; i < 7500; i++){
        Word word;
        word.text = "test";
        word.val = ofRandom(-1.0,1.0);
        word.pos_x = ofRandom(-2000,2000);
        word.pos_y = ofRandom(-2000,20000);
        word.pos_z = ofRandom(-6000,2000);

        words.push_back(word);
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    
}

//--------------------------------------------------------------
void ofApp::draw(){
    
    ofBackground(0);
    
    cam.begin();
    
    for (Word & word : words) {
        if(word.val >= 0.0)
            ofSetColor(255, 0, 0, 255);
        else
            ofSetColor(0, 255, 0, 255);
        
        ofPushMatrix();
        ofTranslate(word.pos_x,word.pos_y,word.pos_z);
        font.drawString(word.text, 0, 0);
        ofPopMatrix();
    }
    cam.end();
}

Thanks,
Joan

#2

You will have to use:
ofEnableDepthTest();

The text is being drawn in order that the values are read from your words vector but not sorted for z values (so you see ones that are further away being drawn on top of the ones that are closer). If you draw them in order of z, starting from furthest away this would not happen, but ofEnableDepthTest(); will do this for you.

You can set it once, or call it just before you draw your text, and then turn it off after with:

ofDisableDepthTest();

#3

Thanks fresla! It kind of works but I get sometimes black rectangles around the letters

#4

To draw text you really need to sort it by depth cause it has transparency and you can’t use depth test with transparency

1 Like
#5

That solved it! Thanks Arturo and fresla!