blob: 7e3616e35b517f079270a9dcc3b33e3ac28151d2 [file] [log] [blame]
Zack Rusin8e7a4ff2011-04-07 01:15:48 -04001#include "vertexdatainterpreter.h"
2
3#include <QListWidget>
4#include <QStringList>
5
6#include <QDebug>
7
8#include <GL/gl.h>
9
Zack Rusinb9341152011-04-07 19:09:28 -040010#include <qmath.h>
11
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040012static int
13sizeForType(int glType)
14{
15 switch(glType) {
16 case GL_FLOAT:
17 return sizeof(GLfloat);
18 case GL_UNSIGNED_BYTE:
19 return sizeof(GLubyte);
20 case GL_BYTE:
21 return sizeof(GLbyte);
22 case GL_SHORT:
23 return sizeof(GLshort);
24 case GL_UNSIGNED_SHORT:
25 return sizeof(GLushort);
26 case GL_INT:
27 return sizeof(GLint);
28 case GL_UNSIGNED_INT:
29 return sizeof(GLuint);
Zack Rusinb9341152011-04-07 19:09:28 -040030 case GL_DOUBLE:
31 return sizeof(GLdouble);
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040032 default:
33 return sizeof(GLint);
34 }
35}
36
37template <typename T>
38static QStringList
39convertData(const QByteArray &dataArray,
40 int type,
41 int stride,
42 int numComponents)
43{
44 QStringList strings;
45
46 const char *data = dataArray.constData();
47 int typeSize = sizeForType(type);
Zack Rusinb9341152011-04-07 19:09:28 -040048 int elementSize = numComponents * typeSize;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040049
Zack Rusinb9341152011-04-07 19:09:28 -040050 if (!stride)
51 stride = elementSize;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040052
Zack Rusinb9341152011-04-07 19:09:28 -040053 int numElements = dataArray.size() / stride;
54
55 if ((numElements % numComponents) != 0) {
56 int temp = qFloor(dataArray.size() / (float)stride);
57 int fullElemSize = temp * stride;
58 if (fullElemSize + numComponents * typeSize <= dataArray.size()){
59 /* num full elements plus the part of the buffer in which we fit */
60 numElements = temp + 1;
61 } else {
62 numElements = temp;
63 }
64 }
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040065
66#if 0
67 qDebug() << "numElements = "<<numElements;
Zack Rusinb9341152011-04-07 19:09:28 -040068 qDebug() << "elementSize = "<<elementSize;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040069 qDebug() << "stride = "<<stride;
70 qDebug() << "numComponents = "<<numComponents;
Zack Rusinb9341152011-04-07 19:09:28 -040071 qDebug() << "typeSize = "<<typeSize;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040072#endif
73
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040074
Zack Rusinb9341152011-04-07 19:09:28 -040075 for (int i = 0; i < numElements; ++i) {
76 QString vectorString = QString::fromLatin1("%1) [").arg(i);
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040077 for (int j = 0; j < numComponents; ++j) {
Zack Rusinb9341152011-04-07 19:09:28 -040078 int offset = i*stride + j*typeSize;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040079 const T *elementPtr =
Zack Rusinb9341152011-04-07 19:09:28 -040080 (const T*)(data + offset);
Zack Rusin8e7a4ff2011-04-07 01:15:48 -040081 T elem = *elementPtr;
82 vectorString += QString::number(elem);
83 if ((j + 1) < numComponents)
84 vectorString += QLatin1String(", ");
85 }
86 vectorString += "]";
87 strings += vectorString;
88 }
89
90 return strings;
91}
92
93
94VertexDataInterpreter::VertexDataInterpreter(QObject *parent)
95 : QObject(parent),
96 m_listWidget(0),
97 m_type(GL_FLOAT),
98 m_stride(16),
99 m_components(4)
100{
101}
102
103void VertexDataInterpreter::setData(const QByteArray &data)
104{
105 m_data = data;
106 if (m_listWidget)
107 m_listWidget->clear();
108}
109
110QByteArray VertexDataInterpreter::data() const
111{
112 return m_data;
113}
114
115void VertexDataInterpreter::setType(int type)
116{
117 m_type = type;
118}
119
120int VertexDataInterpreter::type() const
121{
122 return m_type;
123}
124
125void VertexDataInterpreter::setStride(int stride)
126{
127 m_stride = stride;
128}
129
130int VertexDataInterpreter::stride() const
131{
132 return m_stride;
133}
134
135void VertexDataInterpreter::setComponents(int num)
136{
137 m_components = num;
138}
139
140int VertexDataInterpreter::components() const
141{
142 return m_components;
143}
144
145void VertexDataInterpreter::setListWidget(QListWidget *listWidget)
146{
147 m_listWidget = listWidget;
148}
149
150void VertexDataInterpreter::interpretData()
151{
152 if (!m_listWidget)
153 return;
154
155 m_listWidget->clear();
156
157 if (m_data.isEmpty() || !m_components)
158 return;
159
160 QStringList lst;
161 switch(m_type) {
162 case GL_FLOAT:
163 lst = convertData<float>(m_data, m_type, m_stride, m_components);
164 break;
165 case GL_UNSIGNED_BYTE:
166 lst = convertData<quint8>(m_data, m_type, m_stride, m_components);
167 break;
168 case GL_BYTE:
169 lst = convertData<qint8>(m_data, m_type, m_stride, m_components);
170 break;
171 case GL_SHORT:
172 lst = convertData<qint16>(m_data, m_type, m_stride, m_components);
173 break;
174 case GL_UNSIGNED_SHORT:
175 lst = convertData<quint16>(m_data, m_type, m_stride, m_components);
176 break;
177 case GL_INT:
178 lst = convertData<unsigned int>(m_data, m_type, m_stride, m_components);
179 break;
180 case GL_UNSIGNED_INT:
181 lst = convertData<int>(m_data, m_type, m_stride, m_components);
182 break;
Zack Rusinb9341152011-04-07 19:09:28 -0400183 case GL_DOUBLE:
184 lst = convertData<double>(m_data, m_type, m_stride, m_components);
185 break;
Zack Rusin8e7a4ff2011-04-07 01:15:48 -0400186 default:
187 qDebug()<<"unkown gltype = "<<m_type;
188 }
189 //qDebug()<<"list is "<<lst;
190 m_listWidget->addItems(lst);
191}
192
193
194void VertexDataInterpreter::setTypeFromString(const QString &str)
195{
196 if (str == QLatin1String("GL_FLOAT")) {
197 setType(GL_FLOAT);
198 } else if (str == QLatin1String("GL_INT")) {
199 setType(GL_INT);
200 } else if (str == QLatin1String("GL_UNSIGNED_INT")) {
201 setType(GL_UNSIGNED_INT);
202 } else if (str == QLatin1String("GL_SHORT")) {
203 setType(GL_SHORT);
204 } else if (str == QLatin1String("GL_UNSIGNED_SHORT")) {
205 setType(GL_UNSIGNED_SHORT);
206 } else if (str == QLatin1String("GL_BYTE")) {
207 setType(GL_BYTE);
208 } else if (str == QLatin1String("GL_UNSIGNED_BYTE")) {
209 setType(GL_UNSIGNED_BYTE);
Zack Rusinb9341152011-04-07 19:09:28 -0400210 } else if (str == QLatin1String("GL_DOUBLE")) {
211 setType(GL_DOUBLE);
Zack Rusin8e7a4ff2011-04-07 01:15:48 -0400212 } else {
213 qDebug()<<"unknown vertex data type";
214 }
215}
216
217#include "vertexdatainterpreter.moc"