]> git.cworth.org Git - apitrace/blobdiff - gui/vertexdatainterpreter.cpp
Show the call for which the surface we're displaying.
[apitrace] / gui / vertexdatainterpreter.cpp
index e00466b18bea56ab15c2ac764328a3f76fad402c..c5cdd45d19b8434338eb9344fb3aa5e91c28e46a 100644 (file)
@@ -7,6 +7,8 @@
 
 #include <GL/gl.h>
 
+#include <qmath.h>
+
 static int
 sizeForType(int glType)
 {
@@ -25,6 +27,8 @@ sizeForType(int glType)
         return sizeof(GLint);
     case GL_UNSIGNED_INT:
         return sizeof(GLuint);
+    case GL_DOUBLE:
+        return sizeof(GLdouble);
     default:
         return sizeof(GLint);
     }
@@ -35,41 +39,47 @@ static QStringList
 convertData(const QByteArray &dataArray,
             int type,
             int stride,
-            int numComponents)
+            int numComponents,
+            int startingOffset)
 {
     QStringList strings;
 
-    const char *data = dataArray.constData();
+    int dataSize = dataArray.size() - startingOffset;
+    const char *data = dataArray.constData() + startingOffset;
     int typeSize = sizeForType(type);
-    int sizePerAttribute = typeSize;
+    int elementSize = numComponents * typeSize;
+
+    if (!stride)
+        stride = elementSize;
 
-    if (stride)
-        sizePerAttribute = stride;
+    int numElements = dataSize / stride;
 
-    int numElements = dataArray.size() / sizePerAttribute;
+    if ((numElements % numComponents) != 0) {
+        int temp = qFloor(dataSize / (float)stride);
+        int fullElemSize = temp * stride;
+        if (fullElemSize + numComponents * typeSize <= dataSize) {
+            /* num full elements plus the part of the buffer in which we fit */
+            numElements = temp + 1;
+        } else {
+            numElements = temp;
+        }
+    }
 
 #if 0
     qDebug() << "numElements = "<<numElements;
-    qDebug() << "sizePerAttribute = "<<sizePerAttribute;
+    qDebug() << "elementSize = "<<elementSize;
     qDebug() << "stride = "<<stride;
     qDebug() << "numComponents = "<<numComponents;
+    qDebug() << "typeSize = "<<typeSize;
 #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 i = 0; i < numElements; ++i) {
+        QString vectorString = QString::fromLatin1("%1) [").arg(i);
         for (int j = 0; j < numComponents; ++j) {
-            int idx = i + j;
+            int offset = i*stride + j*typeSize;
             const T *elementPtr =
-                (const T*)(data + idx * sizePerAttribute);
+                (const T*)(data + offset);
             T elem = *elementPtr;
             vectorString += QString::number(elem);
             if ((j + 1) < numComponents)
@@ -88,7 +98,8 @@ VertexDataInterpreter::VertexDataInterpreter(QObject *parent)
       m_listWidget(0),
       m_type(GL_FLOAT),
       m_stride(16),
-      m_components(4)
+      m_components(4),
+      m_startingOffset(0)
 {
 }
 
@@ -152,25 +163,36 @@ void VertexDataInterpreter::interpretData()
     QStringList lst;
     switch(m_type) {
     case GL_FLOAT:
-        lst = convertData<float>(m_data, m_type, m_stride, m_components);
+        lst = convertData<float>(m_data, m_type, m_stride, m_components,
+                                 m_startingOffset);
         break;
     case GL_UNSIGNED_BYTE:
-        lst = convertData<quint8>(m_data, m_type, m_stride, m_components);
+        lst = convertData<quint8>(m_data, m_type, m_stride, m_components,
+                                  m_startingOffset);
         break;
     case GL_BYTE:
-        lst = convertData<qint8>(m_data, m_type, m_stride, m_components);
+        lst = convertData<qint8>(m_data, m_type, m_stride, m_components,
+                                 m_startingOffset);
         break;
     case GL_SHORT:
-        lst = convertData<qint16>(m_data, m_type, m_stride, m_components);
+        lst = convertData<qint16>(m_data, m_type, m_stride, m_components,
+                                  m_startingOffset);
         break;
     case GL_UNSIGNED_SHORT:
-        lst = convertData<quint16>(m_data, m_type, m_stride, m_components);
+        lst = convertData<quint16>(m_data, m_type, m_stride, m_components,
+                                   m_startingOffset);
         break;
     case GL_INT:
-        lst = convertData<unsigned int>(m_data, m_type, m_stride, m_components);
+        lst = convertData<unsigned int>(m_data, m_type, m_stride, m_components,
+                                        m_startingOffset);
         break;
     case GL_UNSIGNED_INT:
-        lst = convertData<int>(m_data, m_type, m_stride, m_components);
+        lst = convertData<int>(m_data, m_type, m_stride, m_components,
+                               m_startingOffset);
+        break;
+    case GL_DOUBLE:
+        lst = convertData<double>(m_data, m_type, m_stride, m_components,
+                                  m_startingOffset);
         break;
     default:
         qDebug()<<"unkown gltype = "<<m_type;
@@ -196,9 +218,21 @@ void VertexDataInterpreter::setTypeFromString(const QString &str)
         setType(GL_BYTE);
     } else if (str == QLatin1String("GL_UNSIGNED_BYTE")) {
         setType(GL_UNSIGNED_BYTE);
+    } else if (str == QLatin1String("GL_DOUBLE")) {
+        setType(GL_DOUBLE);
     } else {
         qDebug()<<"unknown vertex data type";
     }
 }
 
+int VertexDataInterpreter::startingOffset() const
+{
+    return m_startingOffset;
+}
+
+void VertexDataInterpreter::setStartingOffset(int offset)
+{
+    m_startingOffset = offset;
+}
+
 #include "vertexdatainterpreter.moc"