mirror of
https://github.com/overte-org/overte.git
synced 2025-07-23 11:24:10 +02:00
fix handling of normals. don't crash when extracting a mesh from a polyvox with no 'on' voxels
This commit is contained in:
parent
35c38b8130
commit
b876d22e46
5 changed files with 32 additions and 20 deletions
|
@ -61,6 +61,8 @@ public:
|
||||||
virtual uint8_t getVoxel(int x, int y, int z) override;
|
virtual uint8_t getVoxel(int x, int y, int z) override;
|
||||||
virtual bool setVoxel(int x, int y, int z, uint8_t toValue) override;
|
virtual bool setVoxel(int x, int y, int z, uint8_t toValue) override;
|
||||||
|
|
||||||
|
int getOnCount() const override { return _onCount; }
|
||||||
|
|
||||||
void render(RenderArgs* args) override;
|
void render(RenderArgs* args) override;
|
||||||
virtual bool supportsDetailedRayIntersection() const override { return true; }
|
virtual bool supportsDetailedRayIntersection() const override { return true; }
|
||||||
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
|
virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
|
||||||
|
|
|
@ -930,11 +930,15 @@ bool EntityScriptingInterface::setVoxelsInCuboid(QUuid entityID, const glm::vec3
|
||||||
void EntityScriptingInterface::voxelsToMesh(QUuid entityID, QScriptValue callback) {
|
void EntityScriptingInterface::voxelsToMesh(QUuid entityID, QScriptValue callback) {
|
||||||
PROFILE_RANGE(script_entities, __FUNCTION__);
|
PROFILE_RANGE(script_entities, __FUNCTION__);
|
||||||
|
|
||||||
bool success;
|
bool success { false };
|
||||||
QScriptValue mesh;
|
QScriptValue mesh { false };
|
||||||
|
|
||||||
polyVoxWorker(entityID, [&](PolyVoxEntityItem& polyVoxEntity) mutable {
|
polyVoxWorker(entityID, [&](PolyVoxEntityItem& polyVoxEntity) mutable {
|
||||||
success = polyVoxEntity.getMeshAsScriptValue(callback.engine(), mesh);
|
if (polyVoxEntity.getOnCount() == 0) {
|
||||||
|
success = true;
|
||||||
|
} else {
|
||||||
|
success = polyVoxEntity.getMeshAsScriptValue(callback.engine(), mesh);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,8 @@ class PolyVoxEntityItem : public EntityItem {
|
||||||
virtual void setVoxelData(QByteArray voxelData);
|
virtual void setVoxelData(QByteArray voxelData);
|
||||||
virtual const QByteArray getVoxelData() const;
|
virtual const QByteArray getVoxelData() const;
|
||||||
|
|
||||||
|
virtual int getOnCount() const { return 0; }
|
||||||
|
|
||||||
enum PolyVoxSurfaceStyle {
|
enum PolyVoxSurfaceStyle {
|
||||||
SURFACE_MARCHING_CUBES,
|
SURFACE_MARCHING_CUBES,
|
||||||
SURFACE_CUBIC,
|
SURFACE_CUBIC,
|
||||||
|
|
|
@ -138,15 +138,13 @@ Box Mesh::evalPartsBound(int partStart, int partEnd) const {
|
||||||
model::MeshPointer Mesh::map(std::function<glm::vec3(glm::vec3)> vertexFunc,
|
model::MeshPointer Mesh::map(std::function<glm::vec3(glm::vec3)> vertexFunc,
|
||||||
std::function<glm::vec3(glm::vec3)> normalFunc,
|
std::function<glm::vec3(glm::vec3)> normalFunc,
|
||||||
std::function<uint32_t(uint32_t)> indexFunc) {
|
std::function<uint32_t(uint32_t)> indexFunc) {
|
||||||
int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
|
|
||||||
|
|
||||||
// vertex data
|
// vertex data
|
||||||
gpu::Resource::Size vertexSize = getNumVertices() * sizeof(glm::vec3);
|
|
||||||
unsigned char* resultVertexData = new unsigned char[vertexSize];
|
|
||||||
unsigned char* vertexDataCursor = resultVertexData;
|
|
||||||
|
|
||||||
const gpu::BufferView& vertexBufferView = getVertexBuffer();
|
const gpu::BufferView& vertexBufferView = getVertexBuffer();
|
||||||
gpu::BufferView::Index numVertices = (gpu::BufferView::Index)getNumVertices();
|
gpu::BufferView::Index numVertices = (gpu::BufferView::Index)getNumVertices();
|
||||||
|
gpu::Resource::Size vertexSize = numVertices * sizeof(glm::vec3);
|
||||||
|
unsigned char* resultVertexData = new unsigned char[vertexSize];
|
||||||
|
unsigned char* vertexDataCursor = resultVertexData;
|
||||||
|
|
||||||
for (gpu::BufferView::Index i = 0; i < numVertices; i ++) {
|
for (gpu::BufferView::Index i = 0; i < numVertices; i ++) {
|
||||||
glm::vec3 pos = vertexFunc(vertexBufferView.get<glm::vec3>(i));
|
glm::vec3 pos = vertexFunc(vertexBufferView.get<glm::vec3>(i));
|
||||||
memcpy(vertexDataCursor, &pos, sizeof(pos));
|
memcpy(vertexDataCursor, &pos, sizeof(pos));
|
||||||
|
@ -154,12 +152,13 @@ model::MeshPointer Mesh::map(std::function<glm::vec3(glm::vec3)> vertexFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// normal data
|
// normal data
|
||||||
gpu::Resource::Size normalSize = getNumAttributes() * sizeof(glm::vec3);
|
int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
|
||||||
unsigned char* resultNormalData = new unsigned char[normalSize];
|
const gpu::BufferView& normalsBufferView = getAttributeBuffer(attributeTypeNormal);
|
||||||
|
gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements();
|
||||||
|
gpu::Resource::Size normalSize = numNormals * sizeof(glm::vec3);
|
||||||
|
unsigned char* resultNormalData = new unsigned char[normalSize];
|
||||||
unsigned char* normalDataCursor = resultNormalData;
|
unsigned char* normalDataCursor = resultNormalData;
|
||||||
|
|
||||||
const gpu::BufferView& normalsBufferView = getAttributeBuffer(attributeTypeNormal);
|
|
||||||
gpu::BufferView::Index numNormals = (gpu::BufferView::Index)getNumAttributes();
|
|
||||||
for (gpu::BufferView::Index i = 0; i < numNormals; i ++) {
|
for (gpu::BufferView::Index i = 0; i < numNormals; i ++) {
|
||||||
glm::vec3 normal = normalFunc(normalsBufferView.get<glm::vec3>(i));
|
glm::vec3 normal = normalFunc(normalsBufferView.get<glm::vec3>(i));
|
||||||
memcpy(normalDataCursor, &normal, sizeof(normal));
|
memcpy(normalDataCursor, &normal, sizeof(normal));
|
||||||
|
@ -168,12 +167,12 @@ model::MeshPointer Mesh::map(std::function<glm::vec3(glm::vec3)> vertexFunc,
|
||||||
// TODO -- other attributes
|
// TODO -- other attributes
|
||||||
|
|
||||||
// face data
|
// face data
|
||||||
gpu::Resource::Size indexSize = getNumIndices() * sizeof(uint32_t);
|
const gpu::BufferView& indexBufferView = getIndexBuffer();
|
||||||
unsigned char* resultIndexData = new unsigned char[indexSize];
|
gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)getNumIndices();
|
||||||
|
gpu::Resource::Size indexSize = numIndexes * sizeof(uint32_t);
|
||||||
|
unsigned char* resultIndexData = new unsigned char[indexSize];
|
||||||
unsigned char* indexDataCursor = resultIndexData;
|
unsigned char* indexDataCursor = resultIndexData;
|
||||||
|
|
||||||
const gpu::BufferView& indexBufferView = getIndexBuffer();
|
|
||||||
gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)getNumIndices();
|
|
||||||
for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) {
|
for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) {
|
||||||
uint32_t index = indexFunc(indexBufferView.get<uint32_t>(i));
|
uint32_t index = indexFunc(indexBufferView.get<uint32_t>(i));
|
||||||
memcpy(indexDataCursor, &index, sizeof(index));
|
memcpy(indexDataCursor, &index, sizeof(index));
|
||||||
|
@ -229,7 +228,7 @@ void Mesh::forEach(std::function<void(glm::vec3)> vertexFunc,
|
||||||
|
|
||||||
// normal data
|
// normal data
|
||||||
const gpu::BufferView& normalsBufferView = getAttributeBuffer(attributeTypeNormal);
|
const gpu::BufferView& normalsBufferView = getAttributeBuffer(attributeTypeNormal);
|
||||||
gpu::BufferView::Index numNormals = (gpu::BufferView::Index)getNumAttributes();
|
gpu::BufferView::Index numNormals = (gpu::BufferView::Index) normalsBufferView.getNumElements();
|
||||||
for (gpu::BufferView::Index i = 0; i < numNormals; i ++) {
|
for (gpu::BufferView::Index i = 0; i < numNormals; i ++) {
|
||||||
normalFunc(normalsBufferView.get<glm::vec3>(i));
|
normalFunc(normalsBufferView.get<glm::vec3>(i));
|
||||||
}
|
}
|
||||||
|
@ -237,7 +236,7 @@ void Mesh::forEach(std::function<void(glm::vec3)> vertexFunc,
|
||||||
|
|
||||||
// face data
|
// face data
|
||||||
const gpu::BufferView& indexBufferView = getIndexBuffer();
|
const gpu::BufferView& indexBufferView = getIndexBuffer();
|
||||||
gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)getNumIndices();
|
gpu::BufferView::Index numIndexes = (gpu::BufferView::Index)getNumIndices();
|
||||||
for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) {
|
for (gpu::BufferView::Index i = 0; i < numIndexes; i ++) {
|
||||||
indexFunc(indexBufferView.get<uint32_t>(i));
|
indexFunc(indexBufferView.get<uint32_t>(i));
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,12 @@ QScriptValue ModelScriptingInterface::appendMeshes(MeshProxyList in) {
|
||||||
foreach (const MeshProxy* meshProxy, in) {
|
foreach (const MeshProxy* meshProxy, in) {
|
||||||
MeshPointer mesh = meshProxy->getMeshPointer();
|
MeshPointer mesh = meshProxy->getMeshPointer();
|
||||||
totalVertexCount += mesh->getNumVertices();
|
totalVertexCount += mesh->getNumVertices();
|
||||||
totalAttributeCount += mesh->getNumAttributes();
|
|
||||||
|
int attributeTypeNormal = gpu::Stream::InputSlot::NORMAL; // libraries/gpu/src/gpu/Stream.h
|
||||||
|
const gpu::BufferView& normalsBufferView = mesh->getAttributeBuffer(attributeTypeNormal);
|
||||||
|
gpu::BufferView::Index numNormals = (gpu::BufferView::Index)normalsBufferView.getNumElements();
|
||||||
|
totalAttributeCount += numNormals;
|
||||||
|
|
||||||
totalIndexCount += mesh->getNumIndices();
|
totalIndexCount += mesh->getNumIndices();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue