summaryrefslogtreecommitdiffstats
path: root/src/datavis3d/utils
diff options
context:
space:
mode:
authorTomi Korpipää <tomi.korpipaa@digia.com>2013-04-19 10:05:20 +0300
committerTomi Korpipää <tomi.korpipaa@digia.com>2013-04-19 11:42:57 +0300
commit9c5557a2ba453564ec96165d0c54752b94c21578 (patch)
tree037187841835c1902bb9647d3d81363c7cd94e70 /src/datavis3d/utils
parentf3f8bf7f520dc04e2ae9191f89014304591de9e4 (diff)
Correct selection method works now
Change-Id: If14437dae2a9298bfffc72b5678d401c42c3218f Reviewed-by: Miikka Heikkinen <miikka.heikkinen@digia.com> Reviewed-by: Tomi Korpipää <tomi.korpipaa@digia.com>
Diffstat (limited to 'src/datavis3d/utils')
-rw-r--r--src/datavis3d/utils/texturehelper.cpp91
-rw-r--r--src/datavis3d/utils/texturehelper_p.h9
-rw-r--r--src/datavis3d/utils/utils.cpp14
3 files changed, 103 insertions, 11 deletions
diff --git a/src/datavis3d/utils/texturehelper.cpp b/src/datavis3d/utils/texturehelper.cpp
index 428359a7..1cb245cc 100644
--- a/src/datavis3d/utils/texturehelper.cpp
+++ b/src/datavis3d/utils/texturehelper.cpp
@@ -43,7 +43,7 @@
#include <QImage>
-//#include <QDebug>
+#include <QDebug>
QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE
@@ -101,6 +101,95 @@ GLuint TextureHelper::createCubeMapTexture(const QImage &image, bool useTrilinea
return textureId;
}
+GLuint TextureHelper::createSelectionBuffer(const QSize &size, GLuint &texture,
+ GLuint &depthTexture)
+{
+ GLuint framebuffer;
+
+ // Create frame buffer
+ glGenFramebuffers(1, &framebuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
+
+ // Create texture for the selection buffer
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.width(), size.height(), 0, GL_RGB,
+ GL_UNSIGNED_BYTE, NULL);
+
+ // Create texture object for the depth buffer
+ glGenTextures(1, &depthTexture);
+ glBindTexture(GL_TEXTURE_2D, depthTexture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.width(), size.height(),
+ 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ // Attach texture to color attachment
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
+ // Attach texture to depth attachment
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
+
+ // Verify that the frame buffer is complete
+ GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+ if (status != GL_FRAMEBUFFER_COMPLETE) {
+ qCritical() << "Frame buffer creation failed" << status;
+ return 0;
+ }
+
+ // Restore the default framebuffer
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ return framebuffer;
+}
+
+GLuint TextureHelper::createSelectionTexture(const QSize &size, GLuint &frameBuffer,
+ GLuint &depthBuffer)
+{
+ GLuint textureid;
+
+ // Create texture for the selection buffer
+ glGenTextures(1, &textureid);
+ glBindTexture(GL_TEXTURE_2D, textureid);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size.width(), size.height(), 0, GL_RGBA,
+ GL_UNSIGNED_BYTE, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ // Create render buffer
+ glGenRenderbuffers(1, &depthBuffer);
+ glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, size.width(), size.height());
+ glBindRenderbuffer(GL_RENDERBUFFER, 0);
+
+ // Create frame buffer
+ glGenFramebuffers(1, &frameBuffer);
+ glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
+
+ // Attach texture to color attachment
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureid, 0);
+ // Attach renderbuffer to depth attachment
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
+
+ // Verify that the frame buffer is complete
+ GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+ if (status != GL_FRAMEBUFFER_COMPLETE) {
+ qCritical() << "Frame buffer creation failed" << status;
+ return 0;
+ }
+
+ // Restore the default framebuffer
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ return textureid;
+}
+
+void TextureHelper::deleteTexture(const GLuint *texture)
+{
+ glDeleteTextures(1, texture);
+}
+
QImage TextureHelper::convertToGLFormat(const QImage &srcImage)
{
QImage res(srcImage.size(), QImage::Format_ARGB32);
diff --git a/src/datavis3d/utils/texturehelper_p.h b/src/datavis3d/utils/texturehelper_p.h
index 7fa0816b..eb298988 100644
--- a/src/datavis3d/utils/texturehelper_p.h
+++ b/src/datavis3d/utils/texturehelper_p.h
@@ -58,7 +58,7 @@
QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE
-class TextureHelper: protected QOpenGLFunctions
+class TextureHelper : protected QOpenGLFunctions
{
public:
TextureHelper();
@@ -68,11 +68,18 @@ class TextureHelper: protected QOpenGLFunctions
GLuint create2DTexture(const QImage &image, bool useTrilinearFiltering = false,
bool convert = true);
GLuint createCubeMapTexture(const QImage &image, bool useTrilinearFiltering = false);
+ // Returns selection framebuffer and inserts generated texture id to texture parameters
+ GLuint createSelectionBuffer(const QSize &size, GLuint &texture, GLuint &depthTexture);
+ // Returns selection texture and inserts generated framebuffers to fraembuffer parameters
+ GLuint createSelectionTexture(const QSize &size, GLuint &frameBuffer, GLuint &depthBuffer);
+ void deleteTexture(const GLuint *texture);
private:
QImage convertToGLFormat(const QImage &srcImage);
void convertToGLFormatHelper(QImage &dstImage, const QImage &srcImage, GLenum texture_format);
QRgb qt_gl_convertToGLFormatHelper(QRgb src_pixel, GLenum texture_format);
+
+ friend class Q3DBarsPrivate;
};
QTCOMMERCIALDATAVIS3D_END_NAMESPACE
diff --git a/src/datavis3d/utils/utils.cpp b/src/datavis3d/utils/utils.cpp
index c6a17eae..83c23f04 100644
--- a/src/datavis3d/utils/utils.cpp
+++ b/src/datavis3d/utils/utils.cpp
@@ -203,9 +203,7 @@ QImage Utils::printTextToImage(const QFont &font, const QString &text, const QCo
QVector3D Utils::getSelection(QPoint mousepos, int height)
{
QVector3D selectedColor;
-#ifndef USE_HAX0R_SELECTION
- //glBindFramebuffer(GL_FRAMEBUFFER, d_ptr->m_framebufferSelection);
-#endif
+
// This is the only one that works with ANGLE (ES 2.0)
// Item count will be limited to 256*256*256
GLubyte pixel[4];
@@ -215,21 +213,19 @@ QVector3D Utils::getSelection(QPoint mousepos, int height)
// These work with desktop OpenGL
// They offer a lot higher possible object count and a possibility to use object id's
//GLuint pixel2[3];
- //glReadPixels(d_ptr->m_mousePos.x(), height() - d_ptr->m_mousePos.y(), 1, 1,
+ //glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1,
// GL_RGB, GL_UNSIGNED_INT, (void *)pixel2);
//GLfloat pixel3[3];
- //glReadPixels(d_ptr->m_mousePos.x(), height() - d_ptr->m_mousePos.y(), 1, 1,
+ //glReadPixels(mousepos.x(), height - mousepos.y(), 1, 1,
// GL_RGB, GL_FLOAT, (void *)pixel3);
//qDebug() << "rgba" << pixel[0] << pixel[1] << pixel[2];// << pixel[3];
//qDebug() << "rgba2" << pixel2[0] << pixel2[1] << pixel2[2];
//qDebug() << "rgba3" << pixel3[0] << pixel3[1] << pixel3[2];
selectedColor = QVector3D(pixel[0], pixel[1], pixel[2]);
- //qDebug() << selection;
-#ifndef USE_HAX0R_SELECTION
- //glBindFramebuffer(GL_FRAMEBUFFER, 0);
-#endif
+ //qDebug() << selectedColor;
+
return selectedColor;
}