]> git.cworth.org Git - apitrace/blobdiff - gui/vertexdatainterpreter.cpp
Add code to interpret and display binary vertex data.
[apitrace] / gui / vertexdatainterpreter.cpp
diff --git a/gui/vertexdatainterpreter.cpp b/gui/vertexdatainterpreter.cpp
new file mode 100644 (file)
index 0000000..e00466b
--- /dev/null
@@ -0,0 +1,204 @@
+#include "vertexdatainterpreter.h"
+
+#include <QListWidget>
+#include <QStringList>
+
+#include <QDebug>
+
+#include <GL/gl.h>
+
+static int
+sizeForType(int glType)
+{
+    switch(glType) {
+    case GL_FLOAT:
+        return sizeof(GLfloat);
+    case GL_UNSIGNED_BYTE:
+        return sizeof(GLubyte);
+    case GL_BYTE:
+        return sizeof(GLbyte);
+    case GL_SHORT:
+        return sizeof(GLshort);
+    case GL_UNSIGNED_SHORT:
+        return sizeof(GLushort);
+    case GL_INT:
+        return sizeof(GLint);
+    case GL_UNSIGNED_INT:
+        return sizeof(GLuint);
+    default:
+        return sizeof(GLint);
+    }
+}
+
+template <typename T>
+static QStringList
+convertData(const QByteArray &dataArray,
+            int type,
+            int stride,
+            int numComponents)
+{
+    QStringList strings;
+
+    const char *data = dataArray.constData();
+    int typeSize = sizeForType(type);
+    int sizePerAttribute = typeSize;
+
+    if (stride)
+        sizePerAttribute = stride;
+
+    int numElements = dataArray.size() / sizePerAttribute;
+
+#if 0
+    qDebug() << "numElements = "<<numElements;
+    qDebug() << "sizePerAttribute = "<<sizePerAttribute;
+    qDebug() << "stride = "<<stride;
+    qDebug() << "numComponents = "<<numComponents;
+#endif
+
+    if (numElements * sizePerAttribute > dataArray.size()) {
+        qDebug()<<"Vertex data too large for the given binary data";
+        return strings;
+    }
+    if ((numElements % numComponents) != 0) {
+        qDebug()<<"Bad stride for the given vertex data";
+        return strings;
+    }
+
+    for (int i = 0; i < numElements; i += numComponents) {
+        QString vectorString = QString::fromLatin1("%1) [").arg(i / numComponents);
+        for (int j = 0; j < numComponents; ++j) {
+            int idx = i + j;
+            const T *elementPtr =
+                (const T*)(data + idx * sizePerAttribute);
+            T elem = *elementPtr;
+            vectorString += QString::number(elem);
+            if ((j + 1) < numComponents)
+                vectorString += QLatin1String(", ");
+        }
+        vectorString += "]";
+        strings += vectorString;
+    }
+
+    return strings;
+}
+
+
+VertexDataInterpreter::VertexDataInterpreter(QObject *parent)
+    : QObject(parent),
+      m_listWidget(0),
+      m_type(GL_FLOAT),
+      m_stride(16),
+      m_components(4)
+{
+}
+
+void VertexDataInterpreter::setData(const QByteArray &data)
+{
+    m_data = data;
+    if (m_listWidget)
+        m_listWidget->clear();
+}
+
+QByteArray VertexDataInterpreter::data() const
+{
+    return m_data;
+}
+
+void VertexDataInterpreter::setType(int type)
+{
+    m_type = type;
+}
+
+int VertexDataInterpreter::type() const
+{
+    return m_type;
+}
+
+void VertexDataInterpreter::setStride(int stride)
+{
+    m_stride = stride;
+}
+
+int VertexDataInterpreter::stride() const
+{
+    return m_stride;
+}
+
+void VertexDataInterpreter::setComponents(int num)
+{
+    m_components = num;
+}
+
+int VertexDataInterpreter::components() const
+{
+    return m_components;
+}
+
+void VertexDataInterpreter::setListWidget(QListWidget *listWidget)
+{
+    m_listWidget = listWidget;
+}
+
+void VertexDataInterpreter::interpretData()
+{
+    if (!m_listWidget)
+        return;
+
+    m_listWidget->clear();
+
+    if (m_data.isEmpty() || !m_components)
+        return;
+
+    QStringList lst;
+    switch(m_type) {
+    case GL_FLOAT:
+        lst = convertData<float>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_UNSIGNED_BYTE:
+        lst = convertData<quint8>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_BYTE:
+        lst = convertData<qint8>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_SHORT:
+        lst = convertData<qint16>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_UNSIGNED_SHORT:
+        lst = convertData<quint16>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_INT:
+        lst = convertData<unsigned int>(m_data, m_type, m_stride, m_components);
+        break;
+    case GL_UNSIGNED_INT:
+        lst = convertData<int>(m_data, m_type, m_stride, m_components);
+        break;
+    default:
+        qDebug()<<"unkown gltype = "<<m_type;
+    }
+    //qDebug()<<"list is "<<lst;
+    m_listWidget->addItems(lst);
+}
+
+
+void VertexDataInterpreter::setTypeFromString(const QString &str)
+{
+    if (str == QLatin1String("GL_FLOAT")) {
+        setType(GL_FLOAT);
+    } else if (str == QLatin1String("GL_INT")) {
+        setType(GL_INT);
+    } else if (str == QLatin1String("GL_UNSIGNED_INT")) {
+        setType(GL_UNSIGNED_INT);
+    } else if (str == QLatin1String("GL_SHORT")) {
+        setType(GL_SHORT);
+    } else if (str == QLatin1String("GL_UNSIGNED_SHORT")) {
+        setType(GL_UNSIGNED_SHORT);
+    } else if (str == QLatin1String("GL_BYTE")) {
+        setType(GL_BYTE);
+    } else if (str == QLatin1String("GL_UNSIGNED_BYTE")) {
+        setType(GL_UNSIGNED_BYTE);
+    } else {
+        qDebug()<<"unknown vertex data type";
+    }
+}
+
+#include "vertexdatainterpreter.moc"