diff --git a/components/sceneutil/pathgridutil.cpp b/components/sceneutil/pathgridutil.cpp index 1bf70168d..e7c3cecca 100644 --- a/components/sceneutil/pathgridutil.cpp +++ b/components/sceneutil/pathgridutil.cpp @@ -7,11 +7,12 @@ namespace SceneUtil { - const unsigned short DiamondVertexCount = 24; + const unsigned short DiamondVertexCount = 6; + const unsigned short DiamondIndexCount = 24; const float DiamondHalfHeight = 25.f; const float DiamondHalfWidth = 10.f; - const osg::Vec3f DiamondPoints[6] = + const osg::Vec3f DiamondPoints[DiamondVertexCount] = { osg::Vec3f( 0.f, 0.f, DiamondHalfHeight * 2.f), osg::Vec3f(-DiamondHalfWidth, -DiamondHalfWidth, DiamondHalfHeight), @@ -21,7 +22,7 @@ namespace SceneUtil osg::Vec3f( 0.f, 0.f, 0.f) }; - const unsigned short DiamondIndices[DiamondVertexCount] = + const unsigned short DiamondIndices[DiamondIndexCount] = { 0, 2, 1, 0, 1, 3, @@ -41,24 +42,38 @@ namespace SceneUtil const size_t EdgeCount = pathgrid.mEdges.size(); const unsigned short VertexCount = PointCount * DiamondVertexCount; + const unsigned short ColorCount = 1; + const size_t PointIndexCount = PointCount * DiamondIndexCount; + const size_t EdgeIndexCount = EdgeCount * 2; osg::ref_ptr gridGeometry = new osg::Geometry(); osg::ref_ptr vertices = new osg::Vec3Array(VertexCount); - osg::ref_ptr normals = new osg::Vec3Array(VertexCount); - osg::ref_ptr colors = new osg::Vec4Array(1); + osg::ref_ptr colors = new osg::Vec4Array(ColorCount); osg::ref_ptr pointIndices = - new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, VertexCount); + new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, PointIndexCount); osg::ref_ptr lineIndices = - new osg::DrawElementsUShort(osg::PrimitiveSet::LINES, EdgeCount * 2); + new osg::DrawElementsUShort(osg::PrimitiveSet::LINES, EdgeIndexCount); // Add each point/node - for (unsigned short i = 0; i < PointCount; ++i) + + + for (unsigned short pointIndex = 0; pointIndex < PointCount; ++pointIndex) { - const ESM::Pathgrid::Point& point = pathgrid.mPoints[i]; + const ESM::Pathgrid::Point& point = pathgrid.mPoints[pointIndex]; osg::Vec3f position = osg::Vec3f(point.mX, point.mY, point.mZ); - addPoint(i * DiamondVertexCount, position, vertices, normals, pointIndices); + unsigned short vertexOffset = pointIndex * DiamondVertexCount; + unsigned short indexOffset = pointIndex * DiamondIndexCount; + for (unsigned short i = 0; i < DiamondVertexCount; ++i) + { + (*vertices)[vertexOffset + i] = position + DiamondPoints[i]; + } + + for (unsigned short i = 0; i < DiamondIndexCount; ++i) + { + pointIndices->setElement(indexOffset + i, vertexOffset + DiamondIndices[i]); + } } // Add edges @@ -84,15 +99,15 @@ namespace SceneUtil unsigned short diamondIndex = 0; if (dir.isNaN()) - diamondIndex = 2; + diamondIndex = 1; else if (dir.y() >= 0 && dir.x() > 0) - diamondIndex = 8; + diamondIndex = 4; else if (dir.x() <= 0 && dir.y() > 0) - diamondIndex = 11; - else if (dir.y() <= 0 && dir.x() < 0) diamondIndex = 2; + else if (dir.y() <= 0 && dir.x() < 0) + diamondIndex = 1; else if (dir.x() >= 0 && dir.y() < 0) - diamondIndex = 5; + diamondIndex = 3; unsigned short fromIndex = static_cast(edge->mV0); unsigned short toIndex = static_cast(edge->mV1); @@ -106,10 +121,10 @@ namespace SceneUtil (*colors)[0] = DiamondColor; gridGeometry->setVertexArray(vertices); - gridGeometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX); gridGeometry->setColorArray(colors, osg::Array::BIND_OVERALL); gridGeometry->addPrimitiveSet(pointIndices); gridGeometry->addPrimitiveSet(lineIndices); + gridGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); return gridGeometry; } @@ -122,34 +137,5 @@ namespace SceneUtil PathgridGeometryFactory::PathgridGeometryFactory() { - generateNormals(); - } - - void PathgridGeometryFactory::generateNormals() - { - mGeneratedNormals.resize(DiamondVertexCount); - - for (unsigned short i = 0; i < DiamondVertexCount; i += 3) - { - osg::Vec3f v1 = DiamondPoints[DiamondIndices[i + 1]] - DiamondPoints[DiamondIndices[i]]; - osg::Vec3f v2 = DiamondPoints[DiamondIndices[i + 2]] - DiamondPoints[DiamondIndices[i]]; - - osg::Vec3f normal = v1 ^ v2; - - mGeneratedNormals[i] = normal; - mGeneratedNormals[i + 1] = normal; - mGeneratedNormals[i + 2] = normal; - } - } - - void PathgridGeometryFactory::addPoint(unsigned short offset, const osg::Vec3f& position, osg::Vec3Array* vertices, - osg::Vec3Array* normals, osg::DrawElementsUShort* indices) - { - for (unsigned short i = 0; i < DiamondVertexCount; ++i) - { - (*vertices)[i + offset] = position + DiamondPoints[DiamondIndices[i]]; - (*normals)[i + offset] = mGeneratedNormals[i]; - indices->setElement(i + offset, i + offset); - } } } diff --git a/components/sceneutil/pathgridutil.hpp b/components/sceneutil/pathgridutil.hpp index 07f24fe68..1bb668226 100644 --- a/components/sceneutil/pathgridutil.hpp +++ b/components/sceneutil/pathgridutil.hpp @@ -23,16 +23,9 @@ namespace SceneUtil PathgridGeometryFactory(); - void generateNormals(); - - void addPoint(unsigned short offset, const osg::Vec3f& position, osg::Vec3Array* vertices, - osg::Vec3Array* normals, osg::DrawElementsUShort* indices); - // Not implemented PathgridGeometryFactory(const PathgridGeometryFactory&); PathgridGeometryFactory& operator=(const PathgridGeometryFactory&); - - std::vector mGeneratedNormals; }; }