Support larger custom meshes.

Vertex index was limited to unsigned short, meaning even slightly
complex meshes couldn't be used. Changed to unsigned int.

Also removed unused vertex indexer methods.

Change-Id: Iebe62bd3a501dc79ee2857cca28ac0d05bd4a55e
Reviewed-by: Tomi Korpipää <tomi.korpipaa@digia.com>
This commit is contained in:
Miikka Heikkinen 2014-11-18 09:49:02 +02:00
parent d22799efdb
commit 11ed44c13c
14 changed files with 23 additions and 126 deletions

1
dist/changes-1.2.0 vendored
View File

@ -64,6 +64,7 @@ General:
- Fixed a crash related to selection render buffer reuse.
- Fixed the flipped Z-coordinate for absolutely positioned custom items.
- Fixed shadows when viewing the graph directly from above or below.
- Fixed problems using large custom meshes where vertex index count overflowed unsigned short.
New examples
------------

View File

@ -1124,7 +1124,7 @@ void Bars3DRenderer::drawScene(GLuint defaultFboHandle)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, barObj->elementBuf());
// Draw the triangles
glDrawElements(GL_TRIANGLES, barObj->indexCount(), GL_UNSIGNED_SHORT,
glDrawElements(GL_TRIANGLES, barObj->indexCount(), GL_UNSIGNED_INT,
(void *)0);
// Free buffers

View File

@ -142,7 +142,7 @@ void Drawer::drawObject(ShaderHelper *shader, AbstractObjectHelper *object, GLui
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->elementBuf());
// Draw the triangles
glDrawElements(GL_TRIANGLES, object->indexCount(), object->indicesType(), (void*)0);
glDrawElements(GL_TRIANGLES, object->indexCount(), GL_UNSIGNED_INT, (void*)0);
// Free buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -177,7 +177,7 @@ void Drawer::drawSelectionObject(ShaderHelper *shader, AbstractObjectHelper *obj
glBindBuffer(GL_ARRAY_BUFFER, object->vertexBuf());
glVertexAttribPointer(shader->posAtt(), 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->elementBuf());
glDrawElements(GL_TRIANGLES, object->indexCount(), GL_UNSIGNED_SHORT, (void *)0);
glDrawElements(GL_TRIANGLES, object->indexCount(), GL_UNSIGNED_INT, (void *)0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(shader->posAtt());
@ -194,7 +194,7 @@ void Drawer::drawSurfaceGrid(ShaderHelper *shader, SurfaceObject *object)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gridElementBuf());
// Draw the lines
glDrawElements(GL_LINES, object->gridIndexCount(), object->indicesType(), (void*)0);
glDrawElements(GL_LINES, object->gridIndexCount(), GL_UNSIGNED_INT, (void*)0);
// Free buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -641,8 +641,8 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dotObj->elementBuf());
// Draw the triangles
glDrawElements(GL_TRIANGLES, dotObj->indexCount(), GL_UNSIGNED_SHORT,
(void *)0);
glDrawElements(GL_TRIANGLES, dotObj->indexCount(),
GL_UNSIGNED_INT, (void *)0);
// Free buffers
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
@ -662,7 +662,7 @@ void Scatter3DRenderer::drawScene(const GLuint defaultFboHandle)
// Draw the triangles
glDrawElements(GL_TRIANGLES, object->indexCount(),
object->indicesType(), (void *)0);
GL_UNSIGNED_INT, (void *)0);
// Free buffers
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

View File

@ -1216,8 +1216,7 @@ void Surface3DRenderer::drawScene(GLuint defaultFboHandle)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->elementBuf());
// Draw the triangles
glDrawElements(GL_TRIANGLES, object->indexCount(),
object->indicesType(), (void *)0);
glDrawElements(GL_TRIANGLES, object->indexCount(), GL_UNSIGNED_INT, (void *)0);
}
}

View File

@ -74,9 +74,4 @@ GLuint AbstractObjectHelper::indexCount()
return m_indexCount;
}
GLuint AbstractObjectHelper::indicesType()
{
return m_indicesType;
}
QT_END_NAMESPACE_DATAVISUALIZATION

View File

@ -45,7 +45,6 @@ public:
virtual GLuint uvBuf();
GLuint elementBuf();
GLuint indexCount();
GLuint indicesType();
public:
GLuint m_vertexbuffer;
@ -55,8 +54,6 @@ public:
GLuint m_indexCount;
GLboolean m_meshDataLoaded;
GLuint m_indicesType;
};
QT_END_NAMESPACE_DATAVISUALIZATION

View File

@ -25,7 +25,6 @@ QT_BEGIN_NAMESPACE_DATAVISUALIZATION
ObjectHelper::ObjectHelper(const QString &objectFile)
: m_objectFile(objectFile)
{
m_indicesType = GL_UNSIGNED_SHORT;
load();
}
@ -158,7 +157,7 @@ void ObjectHelper::load()
glGenBuffers(1, &m_elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(unsigned short),
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLuint),
&m_indices.at(0), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -48,7 +48,7 @@ public:
static void releaseObjectHelper(const Abstract3DRenderer *cacheId, ObjectHelper *&obj);
inline const QString &objectFile() { return m_objectFile; }
inline const QVector<unsigned short> &indices() const { return m_indices; }
inline const QVector<GLuint> &indices() const { return m_indices; }
inline const QVector<QVector3D> &indexedvertices() const { return m_indexedVertices; }
inline const QVector<QVector2D> &indexedUVs() const { return m_indexedUVs; }
inline const QVector<QVector3D> &indexedNormals() const { return m_indexedNormals; }
@ -59,7 +59,7 @@ private:
void load();
QString m_objectFile;
QVector<unsigned short> m_indices;
QVector<GLuint> m_indices;
QVector<QVector3D> m_indexedVertices;
QVector<QVector2D> m_indexedUVs;
QVector<QVector3D> m_indexedNormals;

View File

@ -29,7 +29,6 @@ const GLfloat itemScaler = 3.0f;
ScatterObjectBufferHelper::ScatterObjectBufferHelper()
: m_scaleY(0.0f)
{
m_indicesType = GL_UNSIGNED_INT;
}
ScatterObjectBufferHelper::~ScatterObjectBufferHelper()
@ -64,7 +63,7 @@ void ScatterObjectBufferHelper::fullLoad(ScatterSeriesRenderCache *cache, qreal
}
// Index vertices
const QVector<unsigned short> indices = dotObj->indices();
const QVector<GLuint> indices = dotObj->indices();
const QVector<QVector3D> indexed_vertices = dotObj->indexedvertices();
const QVector<QVector2D> indexed_uvs = dotObj->indexedUVs();
const QVector<QVector3D> indexed_normals = dotObj->indexedNormals();

View File

@ -27,7 +27,6 @@ ScatterPointBufferHelper::ScatterPointBufferHelper()
: m_pointbuffer(0),
m_oldRemoveIndex(-1)
{
m_indicesType = GL_UNSIGNED_INT;
}
ScatterPointBufferHelper::~ScatterPointBufferHelper()

View File

@ -36,7 +36,6 @@ SurfaceObject::SurfaceObject(Surface3DRenderer *renderer)
m_dataDimension(0),
m_oldDataDimension(-1)
{
m_indicesType = GL_UNSIGNED_INT;
glGenBuffers(1, &m_vertexbuffer);
glGenBuffers(1, &m_normalbuffer);
glGenBuffers(1, &m_uvbuffer);

View File

@ -24,45 +24,11 @@ QT_BEGIN_NAMESPACE_DATAVISUALIZATION
int unique_vertices = 0;
// Returns true if v1 can be considered equal to v2
bool VertexIndexer::is_near(float v1, float v2)
{
return qAbs(v1 - v2) < 0.01f;
}
// Searches through all already exported vertices for a similar one.
// Similar = same position + same UVs + same normal
bool VertexIndexer::getSimilarVertexIndex(const QVector3D &in_vertex,
const QVector2D &in_uv,
const QVector3D &in_normal,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals,
unsigned short &result)
{
// Linear search
for (int i = 0; i < out_vertices.size(); i++) {
if (is_near(in_vertex.x() , out_vertices[i].x())
&& is_near(in_vertex.y() , out_vertices[i].y())
&& is_near(in_vertex.z() , out_vertices[i].z())
&& is_near(in_uv.x() , out_uvs [i].x())
&& is_near(in_uv.y() , out_uvs [i].y())
&& is_near(in_normal.x() , out_normals [i].x())
&& is_near(in_normal.y() , out_normals [i].y())
&& is_near(in_normal.z() , out_normals [i].z())) {
result = i;
return true;
}
}
// No other vertex could be used instead
return false;
}
bool VertexIndexer::getSimilarVertexIndex_fast(const PackedVertex &packed,
QMap<PackedVertex, unsigned short> &VertexToOutIndex,
unsigned short &result)
QMap<PackedVertex, GLuint> &VertexToOutIndex,
GLuint &result)
{
QMap<PackedVertex, unsigned short>::iterator it = VertexToOutIndex.find(packed);
QMap<PackedVertex, GLuint>::iterator it = VertexToOutIndex.find(packed);
if (it == VertexToOutIndex.end()) {
return false;
} else {
@ -74,20 +40,20 @@ bool VertexIndexer::getSimilarVertexIndex_fast(const PackedVertex &packed,
void VertexIndexer::indexVBO(const QVector<QVector3D> &in_vertices,
const QVector<QVector2D> &in_uvs,
const QVector<QVector3D> &in_normals,
QVector<unsigned short> &out_indices,
QVector<GLuint> &out_indices,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals)
{
unique_vertices = 0;
QMap<PackedVertex, unsigned short> VertexToOutIndex;
QMap<PackedVertex, GLuint> VertexToOutIndex;
// For each input vertex
for (int i = 0; i < in_vertices.size(); i++) {
PackedVertex packed = {in_vertices[i], in_uvs[i], in_normals[i]};
// Try to find a similar vertex in out_XXXX
unsigned short index;
GLuint index;
bool found = getSimilarVertexIndex_fast(packed, VertexToOutIndex, index);
if (found) {
@ -97,48 +63,11 @@ void VertexIndexer::indexVBO(const QVector<QVector3D> &in_vertices,
out_vertices.append(in_vertices[i]);
out_uvs.append(in_uvs[i]);
out_normals.append(in_normals[i]);
unsigned short newindex = (unsigned short)out_vertices.size() - 1;
GLuint newindex = (GLuint)out_vertices.size() - 1;
out_indices.append(newindex);
VertexToOutIndex[packed] = newindex;
}
}
}
void VertexIndexer::indexVBO_TBN(const QVector<QVector3D> &in_vertices,
const QVector<QVector2D> &in_uvs,
const QVector<QVector3D> &in_normals,
const QVector<QVector3D> &in_tangents,
const QVector<QVector3D> &in_bitangents,
QVector<unsigned short> &out_indices,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals,
QVector<QVector3D> &out_tangents,
QVector<QVector3D> &out_bitangents)
{
unique_vertices = 0;
// For each input vertex
for (int i = 0; i < in_vertices.size(); i++) {
// Try to find a similar vertex in out_XXXX
unsigned short index;
bool found = getSimilarVertexIndex(in_vertices[i], in_uvs[i], in_normals[i],
out_vertices, out_uvs, out_normals, index);
if (found) {
out_indices.append(index);
// Average the tangents and the bitangents
out_tangents[index] += in_tangents[i];
out_bitangents[index] += in_bitangents[i];
} else {
unique_vertices++;
out_vertices.append(in_vertices[i]);
out_uvs.append(in_uvs[i]);
out_normals.append(in_normals[i]);
out_tangents.append(in_tangents[i]);
out_bitangents.append(in_bitangents[i]);
out_indices.append((unsigned short)out_vertices.size() - 1);
}
}
}
QT_END_NAMESPACE_DATAVISUALIZATION

View File

@ -51,35 +51,15 @@ public:
static void indexVBO(const QVector<QVector3D> &in_vertices,
const QVector<QVector2D> &in_uvs,
const QVector<QVector3D> &in_normals,
QVector<unsigned short> &out_indices,
QVector<GLuint> &out_indices,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals);
static void indexVBO_TBN(const QVector<QVector3D> &in_vertices,
const QVector<QVector2D> &in_uvs,
const QVector<QVector3D> &in_normals,
const QVector<QVector3D> &in_tangents,
const QVector<QVector3D> &in_bitangents,
QVector<unsigned short> &out_indices,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals,
QVector<QVector3D> &out_tangents,
QVector<QVector3D> &out_bitangents);
private:
static bool is_near(float v1, float v2);
static bool getSimilarVertexIndex(const QVector3D &in_vertex,
const QVector2D &in_uv,
const QVector3D &in_normal,
QVector<QVector3D> &out_vertices,
QVector<QVector2D> &out_uvs,
QVector<QVector3D> &out_normals,
unsigned short &result);
static bool getSimilarVertexIndex_fast(const PackedVertex &packed,
QMap<PackedVertex, unsigned short> &VertexToOutIndex,
unsigned short &result);
QMap<PackedVertex, GLuint> &VertexToOutIndex,
GLuint &result);
};
QT_END_NAMESPACE_DATAVISUALIZATION