#include "render/Grid.h" #include #include #include #include #include #include #include namespace mr { namespace { osg::Geometry* lineGeometry(osg::Vec3Array* verts, osg::Vec4Array* colors, float width) { auto* geom = new osg::Geometry; geom->setVertexArray(verts); geom->setColorArray(colors, osg::Array::BIND_PER_VERTEX); geom->addPrimitiveSet(new osg::DrawArrays(GL_LINES, 0, static_cast(verts->size()))); geom->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(width)); return geom; } void addLine(osg::Vec3Array* v, osg::Vec4Array* c, const osg::Vec3& a, const osg::Vec3& b, const osg::Vec4& color) { v->push_back(a); v->push_back(b); c->push_back(color); c->push_back(color); } osgText::Text* label(const char* text, const osg::Vec3& pos, const osg::Vec4& color) { auto* t = new osgText::Text; t->setText(text); t->setPosition(pos); t->setColor(color); t->setCharacterSizeMode(osgText::Text::SCREEN_COORDS); t->setCharacterSize(18.f); t->setAxisAlignment(osgText::Text::SCREEN); t->setAlignment(osgText::Text::CENTER_CENTER); return t; } } // namespace osg::Node* createGrid(float halfExtent, float spacing) { // World is Z-up: MMD X = world X, MMD Y = world Z, MMD +Z (away from the model's front) = world +Y. const float h = halfExtent, z = 0.01f; // lifted slightly to avoid fighting with stage floors const int n = static_cast(halfExtent / spacing); const osg::Vec4 minor(0.42f, 0.45f, 0.50f, 0.45f), major(0.55f, 0.58f, 0.64f, 0.7f); auto* gv = new osg::Vec3Array; auto* gc = new osg::Vec4Array; for (int i = -n; i <= n; ++i) { if (i == 0) continue; const float c = static_cast(i) * spacing; const osg::Vec4& color = i % 2 == 0 ? major : minor; addLine(gv, gc, {-h, c, z}, {h, c, z}, color); addLine(gv, gc, {c, -h, z}, {c, h, z}, color); } const osg::Vec4 red(0.95f, 0.30f, 0.30f, 1.f), green(0.40f, 0.90f, 0.35f, 1.f), blue(0.35f, 0.55f, 1.f, 1.f); const float up = spacing * 3.f; auto* av = new osg::Vec3Array; auto* ac = new osg::Vec4Array; addLine(av, ac, {-h, 0, z}, {h, 0, z}, red); addLine(av, ac, {0, -h, z}, {0, h, z}, blue); addLine(av, ac, {0, 0, z}, {0, 0, up}, green); auto* geode = new osg::Geode; osg::Geometry* gridLines = lineGeometry(gv, gc, 1.f); osg::Geometry* axisLines = lineGeometry(av, ac, 2.f); geode->addDrawable(gridLines); geode->addDrawable(axisLines); geode->addDrawable(label("X", {h + spacing * 0.4f, 0, z}, red)); geode->addDrawable(label("Y", {0, 0, up + spacing * 0.3f}, green)); geode->addDrawable(label("Z", {0, h + spacing * 0.4f, z}, blue)); osg::StateSet* ss = geode->getOrCreateStateSet(); ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF); ss->setMode(GL_BLEND, osg::StateAttribute::ON); ss->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); // No depth writes: thin lines would otherwise show up as occluders in ambient occlusion. ss->setAttributeAndModes(new osg::Depth(osg::Depth::LESS, 0.0, 1.0, false)); // Explicit vertex-colour program so no model shader state can leak onto the lines. auto* program = new osg::Program; program->addShader(new osg::Shader(osg::Shader::VERTEX, "#version 120\nvoid main() { gl_FrontColor = gl_Color;" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }\n")); program->addShader(new osg::Shader(osg::Shader::FRAGMENT, "#version 120\nvoid main() { gl_FragColor = gl_Color; }\n")); for (osg::Geometry* lines : {gridLines, axisLines}) lines->getOrCreateStateSet()->setAttributeAndModes(program); geode->setNodeMask(kGridNodeMask); return geode; } } // namespace mr