Hello,
I am trying to customize the ofDrawGrid();
void ofDrawGrid(float stepSize, size_t numberOfSteps, bool labels, bool x, bool y, bool z);
same method but replacing ofBitmapFont
labels with custom fonts.
I want to get something like that,
but labels are not shown when enable the ofTrueTypeFont
:
I can’t figure out how to deal with camera->worldToScreen()
or something else that I am missing…
Anybody has made that before to share a snippet or any thoughts?
That’s the small function I am using to draw a text into the camera:
void ofxDrawString(string s, float x, float y, float z, ofTrueTypeFont* font, ofCamera* camera) {
if (font == nullptr) return;
if (camera == nullptr) return;
glm::vec3 posScreen = camera->worldToScreen(glm::vec3(x, y, z));
// tryed too:
//ofRectangle r = ofGetCurrentViewport();
//glm::vec3 posScreen = camera->worldToScreen(glm::vec3(x, y, z), r);
float _x = posScreen.x;
float _y = posScreen.y;
font->drawString(s, _x, _y);
}
And here ofxDrawGrid()
and ofxDrawGridPlane()
. Uses the above ofxDrawString()
:
// Draws a grid of the three planes with labels
void ofxDrawGrid(float stepSize, size_t numberOfSteps, bool labels, bool x, bool y, bool z, ofTrueTypeFont* font, ofCamera* camera)
{
//ofPushStyle();
ofColor c;
ofColor prevColor = ofGetStyle().color;
if (x) {
c.setHsb(0.0f, 0.0f, 125.0f);
ofSetColor(c);
if (font == nullptr || camera == nullptr) ofDrawGridPlane(stepSize, numberOfSteps, labels); // bitmap
else ofxDrawGridPlane(stepSize, numberOfSteps, labels, font, camera); // custom
}
if (y) {
c.setHsb(0.0f, 0.0f, 125.0f);
ofSetColor(c);
glm::mat4 m = glm::rotate(glm::mat4(1.0), glm::half_pi<float>(), glm::vec3(0, 0, -1));
ofPushMatrix();
ofMultMatrix(m);
if (font == nullptr || camera == nullptr) ofDrawGridPlane(stepSize, numberOfSteps, labels); // bitmap
else ofxDrawGridPlane(stepSize, numberOfSteps, labels, font, camera); // custom
ofPopMatrix();
}
if (z) {
c.setHsb(0.0f, 0.0f, 125.0f);
ofSetColor(c);
glm::mat4 m = glm::rotate(glm::mat4(1.0), glm::half_pi<float>(), glm::vec3(0, 1, 0));
ofPushMatrix();
ofMultMatrix(m);
if (font == nullptr || camera == nullptr) ofDrawGridPlane(stepSize, numberOfSteps, labels); // bitmap
else ofxDrawGridPlane(stepSize, numberOfSteps, labels, font, camera); // custom
ofPopMatrix();
}
if (labels) { // bitmap
if (font == nullptr || camera == nullptr) {
ofDrawBitmapMode mode = ofGetStyle().drawBitmapMode;
ofSetColor(255, 255, 255);
float labelPos = stepSize * (numberOfSteps + 0.5);
ofSetDrawBitmapMode(OF_BITMAPMODE_MODEL_BILLBOARD);
ofDrawBitmapString("x", labelPos, 0, 0);
ofDrawBitmapString("y", 0, labelPos, 0);
ofDrawBitmapString("z", 0, 0, labelPos);
ofSetDrawBitmapMode(mode);
}
else { // custom
float labelPos = stepSize * (numberOfSteps + 0.5);
ofxDrawString("x", labelPos, 0, 0, font, camera);
ofxDrawString("y", 0, labelPos, 0, font, camera);
ofxDrawString("z", 0, 0, labelPos, font, camera);
}
}
ofSetColor(prevColor);
//ofPopStyle();
}
// Allows passing a font and the camera to customize style instead of using bitmap font
void ofxDrawGridPlane(float stepSize, size_t numberOfSteps, bool labels, ofTrueTypeFont* font, ofCamera* camera)
{
auto renderer = ofGetCurrentRenderer();
float scale = stepSize * numberOfSteps;
float lineWidth = renderer->getStyle().lineWidth;
// Draw all the lines
for (int iDimension = 0; iDimension < 2; iDimension++)
{
for (size_t i = 0; i <= numberOfSteps; i++)
{
float yz = i * stepSize;
if (i == numberOfSteps || i == 0)
renderer->setLineWidth(2); // central axis or cap line
else if (i % 2 == 0) {
renderer->setLineWidth(1.5); // major
}
else {
renderer->setLineWidth(1); // minor
}
if (iDimension == 0) {
renderer->drawLine(0, yz, -scale, 0, yz, scale);
if (yz != 0) renderer->drawLine(0, -yz, -scale, 0, -yz, scale);
}
else {
renderer->drawLine(0, -scale, yz, 0, scale, yz);
if (yz != 0) renderer->drawLine(0, -scale, -yz, 0, scale, -yz);
}
}
}
renderer->setLineWidth(lineWidth);
// Draw all the labels
if (labels) {
//draw numbers on axes
if (font == nullptr || camera == nullptr) // bitmap
{
ofColor prevColor = renderer->getStyle().color;
ofDrawBitmapMode mode = renderer->getStyle().drawBitmapMode;
renderer->setColor(255, 255, 255);
renderer->setBitmapTextMode(OF_BITMAPMODE_MODEL_BILLBOARD);
renderer->drawString(ofToString(0), 0, 0, 0);
for (float i = 1; i <= numberOfSteps; i++)
{
float yz = i * stepSize;
renderer->drawString(ofToString(yz), 0, yz, 0);
renderer->drawString(ofToString(-yz), 0, -yz, 0);
renderer->drawString(ofToString(yz), 0, 0, yz);
renderer->drawString(ofToString(-yz), 0, 0, -yz);
}
renderer->setColor(prevColor);
renderer->setBitmapTextMode(mode);
}
else // custom
{
ofPushStyle();
ofxDrawString(ofToString(0), 0, 0, 0, font, camera);
for (float i = 1; i <= numberOfSteps; i++)
{
float yz = i * stepSize;
ofxDrawString(ofToString(yz), 0, yz, 0, font, camera);
ofxDrawString(ofToString(-yz), 0, -yz, 0, font, camera);
ofxDrawString(ofToString(yz), 0, 0, yz, font, camera);
ofxDrawString(ofToString(-yz), 0, 0, -yz, font, camera);
}
ofPopStyle();
}
}
}
Then in ofApp:
//.h
ofTrueTypeFont font;
ofEasyCam camera;
//--
//.cpp
//draw()
camera.begin();
//ofScale(100.f);//this will break it all?
// A. trying to draw planes with labels using ofTrueTypeFont's
ofxDrawGrid(0.5f, 10, true, false, true, false, &font, &camera);
// B. uses bitmap fonts if camera or font are nullptr's.
// it works fine like in the above top screenshot
//ofxDrawGrid(0.5f, 10, true, false, true, false, nullptr, nullptr);
camera.begin();
Regards