Revert "Remove redundant allocations for NIF meshes"

This reverts commit a7c5beb7c5.

Conflicts:
	components/nif/data.cpp
	components/nifbullet/bulletnifloader.cpp
	components/nifosg/nifloader.cpp
pull/1213/head
scrawl 8 years ago
parent 6a37909ee7
commit aa8459b5c7

@ -1,9 +1,6 @@
#include "data.hpp"
#include "node.hpp"
#include <osg/Array>
#include <osg/PrimitiveSet>
namespace Nif
{
void NiSkinInstance::read(NIFStream *nif)
@ -40,18 +37,15 @@ void ShapeData::read(NIFStream *nif)
{
int verts = nif->getUShort();
vertices = new osg::Vec3Array;
if(nif->getInt())
nif->getVector3s(vertices, verts);
normals = new osg::Vec3Array(osg::Array::BIND_PER_VERTEX);
if(nif->getInt())
nif->getVector3s(normals, verts);
center = nif->getVector3();
radius = nif->getFloat();
colors = new osg::Vec4Array(osg::Array::BIND_PER_VERTEX);
if(nif->getInt())
nif->getVector4s(colors, verts);
@ -65,13 +59,11 @@ void ShapeData::read(NIFStream *nif)
uvlist.resize(uvs);
for(int i = 0;i < uvs;i++)
{
osg::Vec2Array* list = uvlist[i] = new osg::Vec2Array(osg::Array::BIND_PER_VERTEX);
nif->getVector2s(list, verts);
nif->getVector2s(uvlist[i], verts);
// flip the texture coordinates to convert them to the OpenGL convention of bottom-left image origin
for (unsigned int uv=0; uv<list->size(); ++uv)
for (unsigned int uv=0; uv<uvlist[i].size(); ++uv)
{
(*list)[uv] = osg::Vec2((*list)[uv].x(), 1.f - (*list)[uv].y());
uvlist[i][uv] = osg::Vec2f(uvlist[i][uv].x(), 1.f - uvlist[i][uv].y());
}
}
}
@ -86,7 +78,6 @@ void NiTriShapeData::read(NIFStream *nif)
// We have three times as many vertices as triangles, so this
// is always equal to tris*3.
int cnt = nif->getInt();
triangles = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES);
nif->getUShorts(triangles, cnt);
// Read the match list, which lists the vertices that are equal to
@ -113,9 +104,8 @@ void NiAutoNormalParticlesData::read(NIFStream *nif)
if(nif->getInt())
{
int numVerts = vertices->size();
// Particle sizes
nif->getFloats(sizes, numVerts);
nif->getFloats(sizes, vertices.size());
}
}
@ -125,9 +115,8 @@ void NiRotatingParticlesData::read(NIFStream *nif)
if(nif->getInt())
{
int numVerts = vertices->size();
// Rotation quaternions.
nif->getQuaternions(rotations, numVerts);
nif->getQuaternions(rotations, vertices.size());
}
}
@ -246,7 +235,6 @@ void NiMorphData::read(NIFStream *nif)
{
mMorphs[i].mKeyFrames.reset(new FloatKeyMap);
mMorphs[i].mKeyFrames->read(nif, true);
mMorphs[i].mVertices = new osg::Vec3Array;
nif->getVector3s(mMorphs[i].mVertices, vertCount);
}
}

@ -28,8 +28,6 @@
#include "niftypes.hpp" // Transformation
#include <osg/Array>
namespace Nif
{
@ -37,10 +35,9 @@ namespace Nif
class ShapeData : public Record
{
public:
osg::ref_ptr<osg::Vec3Array> vertices, normals;
osg::ref_ptr<osg::Vec4Array> colors;
std::vector< osg::ref_ptr<osg::Vec2Array> > uvlist;
std::vector<osg::Vec3f> vertices, normals;
std::vector<osg::Vec4f> colors;
std::vector< std::vector<osg::Vec2f> > uvlist;
osg::Vec3f center;
float radius;
@ -51,7 +48,7 @@ class NiTriShapeData : public ShapeData
{
public:
// Triangles, three vertex indices per triangle
osg::ref_ptr<osg::DrawElementsUShort> triangles;
std::vector<unsigned short> triangles;
void read(NIFStream *nif);
};
@ -190,7 +187,7 @@ struct NiMorphData : public Record
{
struct MorphData {
FloatKeyMapPtr mKeyFrames;
osg::ref_ptr<osg::Vec3Array> mVertices;
std::vector<osg::Vec3f> mVertices;
};
std::vector<MorphData> mMorphs;

@ -103,11 +103,11 @@ std::string NIFStream::getVersionString()
return result;
}
void NIFStream::getUShorts(osg::VectorGLushort* vec, size_t size)
void NIFStream::getUShorts(std::vector<unsigned short> &vec, size_t size)
{
vec->reserve(size);
for(size_t i = 0;i < size;i++)
vec->push_back(getUShort());
vec.resize(size);
for(size_t i = 0;i < vec.size();i++)
vec[i] = getUShort();
}
void NIFStream::getFloats(std::vector<float> &vec, size_t size)
{
@ -115,23 +115,23 @@ void NIFStream::getFloats(std::vector<float> &vec, size_t size)
for(size_t i = 0;i < vec.size();i++)
vec[i] = getFloat();
}
void NIFStream::getVector2s(osg::Vec2Array* vec, size_t size)
void NIFStream::getVector2s(std::vector<osg::Vec2f> &vec, size_t size)
{
vec->reserve(size);
for(size_t i = 0;i < size;i++)
vec->push_back(getVector2());
vec.resize(size);
for(size_t i = 0;i < vec.size();i++)
vec[i] = getVector2();
}
void NIFStream::getVector3s(osg::Vec3Array* vec, size_t size)
void NIFStream::getVector3s(std::vector<osg::Vec3f> &vec, size_t size)
{
vec->reserve(size);
for(size_t i = 0;i < size;i++)
vec->push_back(getVector3());
vec.resize(size);
for(size_t i = 0;i < vec.size();i++)
vec[i] = getVector3();
}
void NIFStream::getVector4s(osg::Vec4Array* vec, size_t size)
void NIFStream::getVector4s(std::vector<osg::Vec4f> &vec, size_t size)
{
vec->reserve(size);
for(size_t i = 0;i < size;i++)
vec->push_back(getVector4());
vec.resize(size);
for(size_t i = 0;i < vec.size();i++)
vec[i] = getVector4();
}
void NIFStream::getQuaternions(std::vector<osg::Quat> &quat, size_t size)
{

@ -12,12 +12,9 @@
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <osg/Quat>
#include <osg/Array>
#include <osg/PrimitiveSet>
#include "niftypes.hpp"
namespace Nif
{
@ -62,11 +59,11 @@ public:
///This is special since the version string doesn't start with a number, and ends with "\n"
std::string getVersionString();
void getUShorts(osg::VectorGLushort* vec, size_t size);
void getUShorts(std::vector<unsigned short> &vec, size_t size);
void getFloats(std::vector<float> &vec, size_t size);
void getVector2s(osg::Vec2Array* vec, size_t size);
void getVector3s(osg::Vec3Array* vec, size_t size);
void getVector4s(osg::Vec4Array* vec, size_t size);
void getVector2s(std::vector<osg::Vec2f> &vec, size_t size);
void getVector3s(std::vector<osg::Vec3f> &vec, size_t size);
void getVector4s(std::vector<osg::Vec4f> &vec, size_t size);
void getQuaternions(std::vector<osg::Quat> &quat, size_t size);
};

@ -268,7 +268,7 @@ void BulletNifLoader::handleNiTriShape(const Nif::NiTriShape *shape, int flags,
if (shape->data.empty())
return;
if (shape->data->triangles->empty())
if (shape->data->triangles.empty())
return;
if (isAnimated)
@ -280,14 +280,13 @@ void BulletNifLoader::handleNiTriShape(const Nif::NiTriShape *shape, int flags,
const Nif::NiTriShapeData *data = shape->data.getPtr();
childMesh->preallocateVertices(data->vertices->size());
childMesh->preallocateIndices(data->triangles->size());
childMesh->preallocateVertices(data->vertices.size());
childMesh->preallocateIndices(data->triangles.size());
const osg::Vec3Array& vertices = *data->vertices;
const osg::DrawElementsUShort& triangles = *data->triangles;
const std::vector<osg::Vec3f> &vertices = data->vertices;
const std::vector<unsigned short> &triangles = data->triangles;
size_t numtris = data->triangles->size();
for(size_t i = 0;i < numtris;i+=3)
for(size_t i = 0;i < data->triangles.size();i+=3)
{
osg::Vec3f b1 = vertices[triangles[i+0]];
osg::Vec3f b2 = vertices[triangles[i+1]];
@ -321,13 +320,13 @@ void BulletNifLoader::handleNiTriShape(const Nif::NiTriShape *shape, int flags,
// Static shape, just transform all vertices into position
const Nif::NiTriShapeData *data = shape->data.getPtr();
const osg::Vec3Array& vertices = *data->vertices;
const osg::DrawElementsUShort& triangles = *data->triangles;
const std::vector<osg::Vec3f> &vertices = data->vertices;
const std::vector<unsigned short> &triangles = data->triangles;
mStaticMesh->preallocateVertices(data->vertices->size());
mStaticMesh->preallocateIndices(data->triangles->size());
mStaticMesh->preallocateVertices(data->vertices.size());
mStaticMesh->preallocateIndices(data->triangles.size());
size_t numtris = data->triangles->size();
size_t numtris = data->triangles.size();
for(size_t i = 0;i < numtris;i+=3)
{
osg::Vec3f b1 = vertices[triangles[i+0]]*transform;

@ -912,12 +912,12 @@ namespace NifOsg
// Note this position and velocity is not correct for a particle system with absolute reference frame,
// which can not be done in this loader since we are not attached to the scene yet. Will be fixed up post-load in the SceneManager.
created->setVelocity(particle.velocity);
const osg::Vec3f& position = particledata->vertices->at(particle.vertex);
const osg::Vec3f& position = particledata->vertices.at(particle.vertex);
created->setPosition(position);
osg::Vec4f partcolor (1.f,1.f,1.f,1.f);
if (particle.vertex < int(particledata->colors->size()))
partcolor = particledata->colors->at(particle.vertex);
if (particle.vertex < int(particledata->colors.size()))
partcolor = particledata->colors.at(particle.vertex);
float size = particledata->sizes.at(particle.vertex) * partctrl->size;
@ -1076,10 +1076,11 @@ namespace NifOsg
{
const Nif::NiTriShapeData* data = triShape->data.getPtr();
geometry->setVertexArray(data->vertices);
if (!data->normals->empty())
geometry->setNormalArray(data->normals);
{
geometry->setVertexArray(new osg::Vec3Array(data->vertices.size(), &data->vertices[0]));
if (!data->normals.empty())
geometry->setNormalArray(new osg::Vec3Array(data->normals.size(), &data->normals[0]), osg::Array::BIND_PER_VERTEX);
}
int textureStage = 0;
for (std::vector<int>::const_iterator it = boundTextures.begin(); it != boundTextures.end(); ++it,++textureStage)
@ -1089,18 +1090,19 @@ namespace NifOsg
{
std::cerr << "Warning: out of bounds UV set " << uvSet << " on TriShape \"" << triShape->name << "\" in " << mFilename << std::endl;
if (!data->uvlist.empty())
geometry->setTexCoordArray(textureStage, data->uvlist[0]);
geometry->setTexCoordArray(textureStage, new osg::Vec2Array(data->uvlist[0].size(), &data->uvlist[0][0]), osg::Array::BIND_PER_VERTEX);
continue;
}
geometry->setTexCoordArray(textureStage, data->uvlist[uvSet]);
geometry->setTexCoordArray(textureStage, new osg::Vec2Array(data->uvlist[uvSet].size(), &data->uvlist[uvSet][0]), osg::Array::BIND_PER_VERTEX);
}
if (!data->colors->empty())
geometry->setColorArray(data->colors);
if (!data->colors.empty())
geometry->setColorArray(new osg::Vec4Array(data->colors.size(), &data->colors[0]), osg::Array::BIND_PER_VERTEX);
if (!data->triangles->empty())
geometry->addPrimitiveSet(data->triangles);
geometry->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,
data->triangles.size(),
(unsigned short*)&data->triangles[0]));
// osg::Material properties are handled here for two reasons:
// - if there are no vertex colors, we need to disable colorMode.
@ -1108,7 +1110,7 @@ namespace NifOsg
// above the actual renderable would be tedious.
std::vector<const Nif::Property*> drawableProps;
collectDrawableProperties(triShape, drawableProps);
applyDrawableProperties(parentNode, drawableProps, composite, !data->colors->empty(), animflags, false);
applyDrawableProperties(parentNode, drawableProps, composite, !data->colors.empty(), animflags, false);
}
void handleTriShape(const Nif::NiTriShape* triShape, osg::Group* parentNode, SceneUtil::CompositeStateSetUpdater* composite, const std::vector<int>& boundTextures, int animflags)
@ -1175,13 +1177,13 @@ namespace NifOsg
for (unsigned int i = 1; i < morphs.size(); ++i)
{
osg::ref_ptr<osg::Geometry> morphTarget = new osg::Geometry;
morphTarget->setVertexArray(morphs[i].mVertices);
morphTarget->setVertexArray(new osg::Vec3Array(morphs[i].mVertices.size(), &morphs[i].mVertices[0]));
morphGeom->addMorphTarget(morphTarget, 0.f);
}
// build the bounding box containing all possible morph combinations
std::vector<osg::BoundingBox> vertBounds(morphs[0].mVertices->size());
std::vector<osg::BoundingBox> vertBounds(morphs[0].mVertices.size());
// Since we don't know what combinations of morphs are being applied we need to keep track of a bounding box for each vertex.
// The minimum/maximum of the box is the minimum/maximum offset the vertex can have from its starting position.
@ -1192,19 +1194,19 @@ namespace NifOsg
for (unsigned int i = 1; i < morphs.size(); ++i)
{
for (unsigned int j=0; j<morphs[i].mVertices->size() && vertBounds.size(); ++j)
for (unsigned int j=0; j<morphs[i].mVertices.size() && vertBounds.size(); ++j)
{
osg::BoundingBox& bounds = vertBounds[j];
bounds.expandBy(bounds._max + (*morphs[i].mVertices)[j]);
bounds.expandBy(bounds._min + (*morphs[i].mVertices)[j]);
bounds.expandBy(bounds._max + morphs[i].mVertices[j]);
bounds.expandBy(bounds._min + morphs[i].mVertices[j]);
}
}
osg::BoundingBox box;
for (unsigned int i=0; i<vertBounds.size(); ++i)
{
vertBounds[i]._max += (*morphs[0].mVertices)[i];
vertBounds[i]._min += (*morphs[0].mVertices)[i];
vertBounds[i]._max += morphs[0].mVertices[i];
vertBounds[i]._min += morphs[0].mVertices[i];
box.expandBy(vertBounds[i]);
}

Loading…
Cancel
Save