diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-03-25 09:31:57 +0200 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-03-25 09:31:57 +0200 |
commit | 8865e64770ac823e78c7adf0a8ae3b15120b489e (patch) | |
tree | 9be017a59b936bda6b1d1eff797ad9a10edafeba | |
parent | 4ea754c9836f2a6bf69766034d9eb529ab9291b9 (diff) |
Moved object loading into a helper class that can be used by others visualization types as well
Changed the look of spectrum demo a bit
-rw-r--r-- | examples/datavis3d/spectrum/spectrumapp/main.cpp | 13 | ||||
-rw-r--r-- | examples/datavis3d/spectrum/spectrumapp/spectrum.h | 2 | ||||
-rw-r--r-- | src/datavis3d/engine/q3dbars.cpp | 164 | ||||
-rw-r--r-- | src/datavis3d/engine/q3dbars_p.h | 20 | ||||
-rw-r--r-- | src/datavis3d/utils/objecthelper.cpp | 164 | ||||
-rw-r--r-- | src/datavis3d/utils/objecthelper_p.h | 83 | ||||
-rw-r--r-- | src/datavis3d/utils/shaderhelper_p.h | 1 | ||||
-rw-r--r-- | src/datavis3d/utils/utils.pri | 8 |
8 files changed, 301 insertions, 154 deletions
diff --git a/examples/datavis3d/spectrum/spectrumapp/main.cpp b/examples/datavis3d/spectrum/spectrumapp/main.cpp index 9366b8f5..7dc94081 100644 --- a/examples/datavis3d/spectrum/spectrumapp/main.cpp +++ b/examples/datavis3d/spectrum/spectrumapp/main.cpp @@ -46,6 +46,8 @@ #include <QAudio> #include <QTimer> +//#define USE_CONES + using namespace QtDataVis3D; class MainApp : public QObject @@ -84,7 +86,8 @@ MainApp::MainApp(Q3DBars *window) , m_lowFreq(SpectrumLowFreq) , m_highFreq(SpectrumHighFreq) { - m_chart->setupSampleSpace(QPoint(SpectrumNumBands, SpectrumNumBands*3)); + m_chart->setupSampleSpace(QPoint(SpectrumNumBands, SpectrumNumBands*2)); +#if USE_CONES // Set bar specifications; make them a bit wider than deep and make them be drawn 75% // inside each other m_chart->setBarSpecs(QPointF(1.0f, 0.75f), QPointF(0.2f, -0.75f)); @@ -93,6 +96,14 @@ MainApp::MainApp(Q3DBars *window) // Adjust zoom manually; automatic zoom level calculation does not work well with negative // spacings (in setBarSpecs) m_chart->setCameraPosition(10.0f, 5.0f, 70); +#else + // Set bar specifications; make them twice as wide as they're deep + m_chart->setBarSpecs(QPointF(1.0f, 0.5f), QPointF(0.0f, 0.0f)); + // Set bar type, flat bars + m_chart->setBarType(Q3DBars::Bars, false); + // Adjust camera position + m_chart->setCameraPosition(10.0f, 7.5f, 75); +#endif // Set color scheme m_chart->setBarColor(QColor(Qt::black), QColor(Qt::red), QColor(Qt::darkYellow)); // Disable selection diff --git a/examples/datavis3d/spectrum/spectrumapp/spectrum.h b/examples/datavis3d/spectrum/spectrumapp/spectrum.h index 776a1289..b51f0dfd 100644 --- a/examples/datavis3d/spectrum/spectrumapp/spectrum.h +++ b/examples/datavis3d/spectrum/spectrumapp/spectrum.h @@ -53,7 +53,7 @@ const int SpectrumLengthSamples = PowerOfTwo<FFTLengthPowerOfTwo>::Result; // Number of bands in the frequency spectrum -const int SpectrumNumBands = 15; +const int SpectrumNumBands = 30; // Lower bound of first band in the spectrum const qreal SpectrumLowFreq = 0.0; // Hz diff --git a/src/datavis3d/engine/q3dbars.cpp b/src/datavis3d/engine/q3dbars.cpp index 87bdc9c1..01a58f8d 100644 --- a/src/datavis3d/engine/q3dbars.cpp +++ b/src/datavis3d/engine/q3dbars.cpp @@ -40,11 +40,10 @@ #include "q3dbars.h" #include "q3dbars_p.h" -#include "meshloader_p.h" -#include "vertexindexer_p.h" #include "camerahelper_p.h" #include "sampledata_p.h" #include "shaderhelper_p.h" +#include "objecthelper_p.h" #include <QMatrix4x4> #include <QOpenGLPaintDevice> @@ -268,15 +267,16 @@ void Q3DBars::drawScene() #ifdef USE_HAX0R_SELECTION // 1st attribute buffer : vertices glEnableVertexAttribArray(d_ptr->m_selectionShader->posAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_barObj->vertexBuf()); glVertexAttribPointer(d_ptr->m_selectionShader->posAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // Index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_elementbuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_barObj->elementBuf()); // Draw the triangles - glDrawElements(GL_TRIANGLES, d_ptr->m_indexCount, GL_UNSIGNED_SHORT, (void*)0); + glDrawElements(GL_TRIANGLES, d_ptr->m_barObj->indexCount() + , GL_UNSIGNED_SHORT, (void*)0); // Free buffers glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -289,16 +289,17 @@ void Q3DBars::drawScene() // 1st attribute buffer : vertices glEnableVertexAttribArray(d_ptr->m_selectionShader->posAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_barObj->vertexBuf()); glVertexAttribPointer(d_ptr->m_selectionShader->posAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // Index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_elementbuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_barObj->elementBuf()); // Draw the triangles GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawElements(GL_TRIANGLES, d_ptr->m_indexCount, GL_UNSIGNED_SHORT, DrawBuffers); + glDrawElements(GL_TRIANGLES, d_ptr->m_barObj->indexCount() + , GL_UNSIGNED_SHORT, DrawBuffers); glDisableVertexAttribArray(d_ptr->m_selectionShader->posAtt()); @@ -355,7 +356,7 @@ void Q3DBars::drawScene() d_ptr->m_backgroundShader->bind(); // Draw background - if (d_ptr->m_background) { + if (d_ptr->m_backgroundObj) { QMatrix4x4 modelMatrix; QMatrix4x4 MVPMatrix; if (zComp != 0) @@ -386,27 +387,28 @@ void Q3DBars::drawScene() // 1st attribute buffer : vertices glEnableVertexAttribArray(d_ptr->m_backgroundShader->posAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_vertexbufferBackground); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->vertexBuf()); glVertexAttribPointer(d_ptr->m_backgroundShader->posAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // 2nd attribute buffer : normals glEnableVertexAttribArray(d_ptr->m_backgroundShader->normalAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_normalbufferBackground); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->normalBuf()); glVertexAttribPointer(d_ptr->m_backgroundShader->normalAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // 3rd attribute buffer : UVs //glEnableVertexAttribArray(d_ptr->m_backgroundShader->uvAtt()); - //glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_uvbufferBackground); + //glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_backgroundObj->uvBuf()); //glVertexAttribPointer(d_ptr->m_backgroundShader->uvAtt() // , 2, GL_FLOAT, GL_FALSE, 0, (void*)0); // Index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_elementbufferBackground); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_backgroundObj->elementBuf()); // Draw the triangles - glDrawElements(GL_TRIANGLES, d_ptr->m_indexCountBackground, GL_UNSIGNED_SHORT, (void*)0); + glDrawElements(GL_TRIANGLES, d_ptr->m_backgroundObj->indexCount() + , GL_UNSIGNED_SHORT, (void*)0); // Free buffers glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -517,27 +519,28 @@ void Q3DBars::drawScene() // 1st attribute buffer : vertices glEnableVertexAttribArray(d_ptr->m_barShader->posAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_barObj->vertexBuf()); glVertexAttribPointer(d_ptr->m_barShader->posAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // 2nd attribute buffer : normals glEnableVertexAttribArray(d_ptr->m_barShader->normalAtt()); - glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_normalbuffer); + glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_barObj->normalBuf()); glVertexAttribPointer(d_ptr->m_barShader->normalAtt() , 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // 3rd attribute buffer : UVs //glEnableVertexAttribArray(d_ptr->m_barShader->m_uvAtt()); - //glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_uvbuffer); + //glBindBuffer(GL_ARRAY_BUFFER, d_ptr->m_barObj->uvBuf()); //glVertexAttribPointer(d_ptr->m_barShader->m_uvAtt() // , 2, GL_FLOAT, GL_FALSE, 0, (void*)0); // Index buffer - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_elementbuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d_ptr->m_barObj->elementBuf()); // Draw the triangles - glDrawElements(GL_TRIANGLES, d_ptr->m_indexCount, GL_UNSIGNED_SHORT, (void*)0); + glDrawElements(GL_TRIANGLES, d_ptr->m_barObj->indexCount() + , GL_UNSIGNED_SHORT, (void*)0); // Free buffers glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -795,10 +798,10 @@ Q3DBarsPrivate::Q3DBarsPrivate(Q3DBars *q) , m_barShader(0) , m_selectionShader(0) , m_backgroundShader(0) + , m_barObj(0) + , m_backgroundObj(0) , m_sampleCount(QPoint(0, 0)) , m_objFile(QStringLiteral(":/defaultMeshes/bar")) - , m_indexCount(0) - , m_indexCountBackground(0) , m_mousePressed(false) , m_mousePos(QPoint(0, 0)) , m_zoomLevel(100) @@ -806,8 +809,6 @@ Q3DBarsPrivate::Q3DBarsPrivate(Q3DBars *q) , m_verticalRotation(15.0f) , m_barThickness(QPointF(0.75f, 0.75f)) , m_barSpacing(m_barThickness * 3.0f) - , m_meshDataLoaded(false) - , m_background(false) , m_dataSet(0) , m_rowWidth(0) , m_columnDepth(0) @@ -835,127 +836,30 @@ Q3DBarsPrivate::~Q3DBarsPrivate() delete m_selectionShader; delete m_backgroundShader; delete m_selectedBar; - - q_ptr->glDeleteBuffers(1, &m_vertexbuffer); - q_ptr->glDeleteBuffers(1, &m_uvbuffer); - q_ptr->glDeleteBuffers(1, &m_normalbuffer); - q_ptr->glDeleteBuffers(1, &m_elementbuffer); + delete m_barObj; + delete m_backgroundObj; #ifndef USE_HAX0R_SELECTION q_ptr->glDeleteFramebuffers(1, &m_framebufferSelection); q_ptr->glDeleteTextures(1, &m_selectionTexture); q_ptr->glDeleteTextures(1, &m_depthTexture); #endif - - q_ptr->glDeleteBuffers(1, &m_vertexbufferBackground); - q_ptr->glDeleteBuffers(1, &m_uvbufferBackground); - q_ptr->glDeleteBuffers(1, &m_normalbufferBackground); - q_ptr->glDeleteBuffers(1, &m_elementbufferBackground); } void Q3DBarsPrivate::loadBarMesh() { - if (m_meshDataLoaded) { - // Delete old data - q_ptr->glDeleteBuffers(1, &m_vertexbuffer); - q_ptr->glDeleteBuffers(1, &m_uvbuffer); - q_ptr->glDeleteBuffers(1, &m_normalbuffer); - q_ptr->glDeleteBuffers(1, &m_elementbuffer); - } - QVector<QVector3D> vertices; - QVector<QVector2D> uvs; - QVector<QVector3D> normals; - bool loadOk = MeshLoader::loadOBJ(m_objFile, vertices, uvs, normals); - if (!loadOk) - qFatal("loading failed"); - - qDebug() << "vertex count" << vertices.size();; - - // Index vertices - QVector<unsigned short> indices; - QVector<QVector3D> indexed_vertices; - QVector<QVector2D> indexed_uvs; - QVector<QVector3D> indexed_normals; - VertexIndexer::indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs - , indexed_normals); - - m_indexCount = indices.size(); - //qDebug() << "index count" << m_indexCount; - - q_ptr->glGenBuffers(1, &m_vertexbuffer); - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_vertexbuffer); - q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(QVector3D) - , &indexed_vertices.at(0) - , GL_STATIC_DRAW); - - q_ptr->glGenBuffers(1, &m_normalbuffer); - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_normalbuffer); - q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(QVector3D) - , &indexed_normals.at(0) - , GL_STATIC_DRAW); - - //q_ptr->glGenBuffers(1, &m_uvbuffer); - //q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_uvbuffer); - //q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(QVector2D), &indexed_uvs.at(0) - // , GL_STATIC_DRAW); - - q_ptr->glGenBuffers(1, &m_elementbuffer); - q_ptr->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbuffer); - q_ptr->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices.at(0) - , GL_STATIC_DRAW); - - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, 0); - q_ptr->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - m_meshDataLoaded = true; + if (m_barObj) + delete m_barObj; + m_barObj = new ObjectHelper(q_ptr, m_objFile); + m_barObj->load(); } void Q3DBarsPrivate::loadBackgroundMesh() { - QVector<QVector3D> vertices; - QVector<QVector2D> uvs; - QVector<QVector3D> normals; - bool loadOk = MeshLoader::loadOBJ(QStringLiteral(":/defaultMeshes/background") - , vertices, uvs, normals); - if (!loadOk) - qFatal("loading failed"); - - // Index vertices - QVector<unsigned short> indices; - QVector<QVector3D> indexed_vertices; - QVector<QVector2D> indexed_uvs; - QVector<QVector3D> indexed_normals; - VertexIndexer::indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs - , indexed_normals); - - m_indexCountBackground = indices.size(); - - q_ptr->glGenBuffers(1, &m_vertexbufferBackground); - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_vertexbufferBackground); - q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(QVector3D) - , &indexed_vertices.at(0) - , GL_STATIC_DRAW); - - q_ptr->glGenBuffers(1, &m_normalbufferBackground); - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_normalbufferBackground); - q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(QVector3D) - , &indexed_normals.at(0) - , GL_STATIC_DRAW); - - //q_ptr->glGenBuffers(1, &m_uvbufferBackground); - //q_ptr->glBindBuffer(GL_ARRAY_BUFFER, m_uvbufferBackground); - //q_ptr->glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(QVector2D), &indexed_uvs.at(0) - // , GL_STATIC_DRAW); - - q_ptr->glGenBuffers(1, &m_elementbufferBackground); - q_ptr->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbufferBackground); - q_ptr->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices.at(0) - , GL_STATIC_DRAW); - - q_ptr->glBindBuffer(GL_ARRAY_BUFFER, 0); - q_ptr->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - m_background = true; + if (m_backgroundObj) + delete m_backgroundObj; + m_backgroundObj = new ObjectHelper(q_ptr, QStringLiteral(":/defaultMeshes/background")); + m_backgroundObj->load(); } void Q3DBarsPrivate::initShaders(const QString &vertexShader, const QString &fragmentShader) diff --git a/src/datavis3d/engine/q3dbars_p.h b/src/datavis3d/engine/q3dbars_p.h index 302d473b..21f83a8a 100644 --- a/src/datavis3d/engine/q3dbars_p.h +++ b/src/datavis3d/engine/q3dbars_p.h @@ -13,6 +13,7 @@ QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE class Q3DBars; class SampleData; class ShaderHelper; +class ObjectHelper; class Q3DBarsPrivate { @@ -37,31 +38,18 @@ public: void calculateSceneScalingFactors(); SelectionType isSelected(int row, int bar, const QVector3D &selection); - GLuint m_vertexbuffer; - GLuint m_uvbuffer; - GLuint m_normalbuffer; - GLuint m_elementbuffer; - GLuint m_framebufferSelection; - GLuint m_vertexbufferBackground; - GLuint m_uvbufferBackground; - GLuint m_normalbufferBackground; - GLuint m_elementbufferBackground; - - GLuint m_selectionTexture; - GLuint m_depthTexture; - Q3DBars *q_ptr; QOpenGLPaintDevice *m_paintDevice; ShaderHelper *m_barShader; ShaderHelper *m_selectionShader; ShaderHelper *m_backgroundShader; + ObjectHelper *m_barObj; + ObjectHelper *m_backgroundObj; QPoint m_sampleCount; QString m_objFile; - int m_indexCount; - int m_indexCountBackground; bool m_mousePressed; QPoint m_mousePos; int m_zoomLevel; @@ -69,8 +57,6 @@ public: float m_verticalRotation; QPointF m_barThickness; QPointF m_barSpacing; - bool m_meshDataLoaded; - bool m_background; QVector< QVector<float> > m_dataSet; float m_rowWidth; float m_columnDepth; diff --git a/src/datavis3d/utils/objecthelper.cpp b/src/datavis3d/utils/objecthelper.cpp new file mode 100644 index 00000000..d2de8ffd --- /dev/null +++ b/src/datavis3d/utils/objecthelper.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "meshloader_p.h" +#include "vertexindexer_p.h" +#include "objecthelper_p.h" + +#include <QDebug> + +QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE + +ObjectHelper::ObjectHelper(QOpenGLFunctions *parent, const QString &objectFile) + : m_caller(parent) + , m_objectFile(objectFile) + , m_vertexbuffer(0) + , m_normalbuffer(0) + , m_uvbuffer(0) + , m_elementbuffer(0) + , m_indexCount(0) + , m_meshDataLoaded(false) +{ +} + +ObjectHelper::~ObjectHelper() +{ + m_caller->glDeleteBuffers(1, &m_vertexbuffer); + m_caller->glDeleteBuffers(1, &m_uvbuffer); + m_caller->glDeleteBuffers(1, &m_normalbuffer); + m_caller->glDeleteBuffers(1, &m_elementbuffer); +} + +void ObjectHelper::setObjectFile(const QString &objectFile) +{ + m_objectFile = objectFile; +} + +void ObjectHelper::load() +{ + if (m_meshDataLoaded) { + // Delete old data + m_caller->glDeleteBuffers(1, &m_vertexbuffer); + m_caller->glDeleteBuffers(1, &m_uvbuffer); + m_caller->glDeleteBuffers(1, &m_normalbuffer); + m_caller->glDeleteBuffers(1, &m_elementbuffer); + } + QVector<QVector3D> vertices; + QVector<QVector2D> uvs; + QVector<QVector3D> normals; + bool loadOk = MeshLoader::loadOBJ(m_objectFile, vertices, uvs, normals); + if (!loadOk) + qFatal("loading failed"); + + qDebug() << "vertex count" << vertices.size();; + + // Index vertices + QVector<unsigned short> indices; + QVector<QVector3D> indexed_vertices; + QVector<QVector2D> indexed_uvs; + QVector<QVector3D> indexed_normals; + VertexIndexer::indexVBO(vertices, uvs, normals, indices, indexed_vertices, indexed_uvs + , indexed_normals); + + m_indexCount = indices.size(); + //qDebug() << "index count" << m_indexCount; + + m_caller->glGenBuffers(1, &m_vertexbuffer); + m_caller->glBindBuffer(GL_ARRAY_BUFFER, m_vertexbuffer); + m_caller->glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(QVector3D) + , &indexed_vertices.at(0) + , GL_STATIC_DRAW); + + m_caller->glGenBuffers(1, &m_normalbuffer); + m_caller->glBindBuffer(GL_ARRAY_BUFFER, m_normalbuffer); + m_caller->glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(QVector3D) + , &indexed_normals.at(0) + , GL_STATIC_DRAW); + + //m_caller->glGenBuffers(1, &m_uvbuffer); + //m_caller->glBindBuffer(GL_ARRAY_BUFFER, m_uvbuffer); + //m_caller->glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(QVector2D), &indexed_uvs.at(0) + // , GL_STATIC_DRAW); + + m_caller->glGenBuffers(1, &m_elementbuffer); + m_caller->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementbuffer); + m_caller->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices.at(0) + , GL_STATIC_DRAW); + + m_caller->glBindBuffer(GL_ARRAY_BUFFER, 0); + m_caller->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + m_meshDataLoaded = true; +} + +GLuint ObjectHelper::vertexBuf() +{ + if (!m_meshDataLoaded) + qFatal("No loaded object"); + return m_vertexbuffer; +} + +GLuint ObjectHelper::normalBuf() +{ + if (!m_meshDataLoaded) + qFatal("No loaded object"); + return m_normalbuffer; +} + +GLuint ObjectHelper::uvBuf() +{ + if (!m_meshDataLoaded) + qFatal("No loaded object"); + return m_uvbuffer; +} + +GLuint ObjectHelper::elementBuf() +{ + if (!m_meshDataLoaded) + qFatal("No loaded object"); + return m_elementbuffer; +} + +GLuint ObjectHelper::indexCount() +{ + return m_indexCount; +} + +QTCOMMERCIALDATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/utils/objecthelper_p.h b/src/datavis3d/utils/objecthelper_p.h new file mode 100644 index 00000000..059b7ab4 --- /dev/null +++ b/src/datavis3d/utils/objecthelper_p.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtXmlPatterns module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef OBJECTHELPER_P_H +#define OBJECTHELPER_P_H + +#include "qdatavis3dglobal.h" +#include <QOpenGLFunctions> + +QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE + +class ObjectHelper +{ + public: + ObjectHelper(QOpenGLFunctions *parent, const QString &objectFile = QString()); + ~ObjectHelper(); + + void setObjectFile(const QString &objectFile); + + void load(); + + GLuint vertexBuf(); + GLuint normalBuf(); + GLuint uvBuf(); + GLuint elementBuf(); + GLuint indexCount(); + + private: + QOpenGLFunctions *m_caller; + + QString m_objectFile; + + GLuint m_vertexbuffer; + GLuint m_uvbuffer; + GLuint m_normalbuffer; + GLuint m_elementbuffer; + + GLuint m_indexCount; + + GLboolean m_meshDataLoaded; +}; + +QTCOMMERCIALDATAVIS3D_END_NAMESPACE + +#endif diff --git a/src/datavis3d/utils/shaderhelper_p.h b/src/datavis3d/utils/shaderhelper_p.h index fc9d1b7c..f937bfbd 100644 --- a/src/datavis3d/utils/shaderhelper_p.h +++ b/src/datavis3d/utils/shaderhelper_p.h @@ -46,7 +46,6 @@ #include <QOpenGLFunctions> class QOpenGLShaderProgram; -//class QObject; QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE diff --git a/src/datavis3d/utils/utils.pri b/src/datavis3d/utils/utils.pri index edacd877..ba2122f2 100644 --- a/src/datavis3d/utils/utils.pri +++ b/src/datavis3d/utils/utils.pri @@ -1,11 +1,11 @@ HEADERS += $$PWD/meshloader_p.h \ $$PWD/vertexindexer_p.h \ $$PWD/camerahelper_p.h \ - $$PWD/shaderhelper_p.h #\ - #$$PWD/objecthelper_p.h + $$PWD/shaderhelper_p.h \ + $$PWD/objecthelper_p.h SOURCES += $$PWD/meshloader.cpp \ $$PWD/vertexindexer.cpp \ $$PWD/camerahelper.cpp \ - $$PWD/shaderhelper.cpp #\ - #$$PWD/objecthelper.cpp + $$PWD/shaderhelper.cpp \ + $$PWD/objecthelper.cpp |