Fixed the position of text so it is drawn ontop of the quads.

This commit is contained in:
Robert Osfield 2004-07-19 18:56:42 +00:00
parent 25e0f27ce5
commit 221c75fb6e
2 changed files with 69 additions and 64 deletions

View File

@ -3,93 +3,95 @@
#include <osgText/Text>
Frame::Frame()
: osg::Geode(),
bgcolor_(0.5f, 0.5f, 0.5f, 1.0f),
rect_(0, 0, 100, 100),
caption_("Frame")
: osg::Geode(),
bgcolor_(0.5f, 0.5f, 0.5f, 1.0f),
rect_(0, 0, 100, 100),
caption_("Frame")
{
}
Frame::Frame(const Frame &copy, const osg::CopyOp &copyop)
: osg::Geode(copy, copyop),
bgcolor_(copy.bgcolor_),
rect_(copy.rect_),
caption_(copy.caption_)
: osg::Geode(copy, copyop),
bgcolor_(copy.bgcolor_),
rect_(copy.rect_),
caption_(copy.caption_)
{
}
void Frame::rebuild()
{
removeDrawable(0, getNumDrawables());
addDrawable(build_quad(rect_, bgcolor_));
addDrawable(build_quad(Rect(rect_.x0 + 4, rect_.y1 - 24, rect_.x1 - 4, rect_.y1 - 4), osg::Vec4(0, 0, 0, bgcolor_.w()), false, 0.1f));
float zPos = -0.1f;
osg::ref_ptr<osgText::Text> caption_text = new osgText::Text;
caption_text->setText(caption_);
caption_text->setColor(osg::Vec4(1, 1, 1, 1));
caption_text->setAlignment(osgText::Text::CENTER_CENTER);
caption_text->setFont("fonts/arial.ttf");
caption_text->setCharacterSize(16);
caption_text->setFontResolution(16, 16);
caption_text->setPosition(osg::Vec3((rect_.x0 + rect_.x1) / 2, rect_.y1 - 15, 0.2f));
addDrawable(caption_text.get());
removeDrawable(0, getNumDrawables());
addDrawable(build_quad(rect_, bgcolor_));
addDrawable(build_quad(Rect(rect_.x0 + 4, rect_.y1 - 24, rect_.x1 - 4, rect_.y1 - 4), osg::Vec4(0, 0, 0, bgcolor_.w()), false, zPos));
rebuild_client_area(Rect(rect_.x0 + 4, rect_.y0 + 4, rect_.x1 - 4, rect_.y1 - 28));
osg::ref_ptr<osgText::Text> caption_text = new osgText::Text;
caption_text->setText(caption_);
caption_text->setColor(osg::Vec4(1, 1, 1, 1));
caption_text->setAlignment(osgText::Text::CENTER_CENTER);
caption_text->setFont("fonts/arial.ttf");
caption_text->setCharacterSize(16);
caption_text->setFontResolution(16, 16);
caption_text->setPosition(osg::Vec3((rect_.x0 + rect_.x1) / 2, rect_.y1 - 15, zPos*2.0f));
addDrawable(caption_text.get());
rebuild_client_area(Rect(rect_.x0 + 4, rect_.y0 + 4, rect_.x1 - 4, rect_.y1 - 28));
}
osg::Geometry *Frame::build_quad(const Rect &rect, const osg::Vec4 &color, bool shadow, float z)
{
const float shadow_space = 8;
const float shadow_size = 10;
const float shadow_space = 8;
const float shadow_size = 10;
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vx = new osg::Vec3Array;
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vx = new osg::Vec3Array;
vx->push_back(osg::Vec3(rect.x0, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y1, z));
vx->push_back(osg::Vec3(rect.x0, rect.y1, z));
vx->push_back(osg::Vec3(rect.x0, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y1, z));
vx->push_back(osg::Vec3(rect.x0, rect.y1, z));
if (shadow) {
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0, z));
if (shadow) {
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y1-shadow_space, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z));
}
vx->push_back(osg::Vec3(rect.x1, rect.y1-shadow_space, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z));
}
geo->setVertexArray(vx.get());
geo->setVertexArray(vx.get());
osg::ref_ptr<osg::Vec4Array> clr = new osg::Vec4Array;
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
osg::ref_ptr<osg::Vec4Array> clr = new osg::Vec4Array;
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
if (shadow) {
if (shadow) {
float alpha = color.w() * 0.5f;
const osg::Vec3 black(0, 0, 0);
float alpha = color.w() * 0.5f;
const osg::Vec3 black(0, 0, 0);
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
}
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
}
geo->setColorArray(clr.get());
geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geo->setColorArray(clr.get());
geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geo->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, shadow? 12: 4));
geo->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, shadow? 12: 4));
return geo.take();
return geo.take();
}

View File

@ -153,6 +153,9 @@ public:
protected:
void rebuild_client_area(const Rect &client_rect)
{
float zPos = -0.1; // note from Robert, was 0.1f, but now must be -0.1f to keep text visible??#!? due
// to some other change in the OSG not tracked down yet...
osg::ref_ptr<osgText::Font> arial = osgText::readFontFile("fonts/arial.ttf");
osg::ref_ptr<osgText::Text> hints = new osgText::Text;
@ -161,7 +164,7 @@ protected:
hints->setAlignment(osgText::Text::CENTER_BOTTOM);
hints->setCharacterSize(13);
hints->setFontResolution(13, 13);
hints->setPosition(osg::Vec3((client_rect.x0+client_rect.x1)/2, client_rect.y0 + 4, 0.1f));
hints->setPosition(osg::Vec3((client_rect.x0+client_rect.x1)/2, client_rect.y0 + 4, zPos));
hints->setText("<RETURN> show/hide this panel <LEFT> previous effect <RIGHT> next effect <DEL> enable/disable effects 'x' save to file 'r' rotate/stop");
addDrawable(hints.get());
@ -193,7 +196,7 @@ protected:
ename->setAlignment(osgText::Text::CENTER_TOP);
ename->setCharacterSize(32);
ename->setFontResolution(32, 32);
ename->setPosition(osg::Vec3((client_rect.x0 + client_rect.x1) / 2, client_rect.y1 - 22, 0.1f));
ename->setPosition(osg::Vec3((client_rect.x0 + client_rect.x1) / 2, client_rect.y1 - 22, zPos));
ename->setText(effect_name);
addDrawable(ename.get());
@ -204,7 +207,7 @@ protected:
edesc->setAlignment(osgText::Text::LEFT_TOP);
edesc->setCharacterSize(16);
edesc->setFontResolution(16, 16);
edesc->setPosition(osg::Vec3(client_rect.x0 + 8, client_rect.y1 - 60, 0.1f));
edesc->setPosition(osg::Vec3(client_rect.x0 + 8, client_rect.y1 - 60, zPos));
edesc->setText(effect_description);
addDrawable(edesc.get());
}