From c64bb48a674b62c6bc4dba8e5e132046ed7c3130 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20M=C3=B6bius?= <moebius@cs.rwth-aachen.de>
Date: Thu, 30 Apr 2009 14:08:02 +0000
Subject: [PATCH] Dennis: Add alpha to colorpicking ( Up to 4.294.967.295
 Elements )

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@5910 383ad7c9-94d9-4d36-a494-682f7c89f535
---
 widgets/glWidget/QtBaseViewer.cc        |  2 ++
 widgets/glWidget/QtBaseViewerPicking.cc | 40 ++++++++++++++-----------
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/widgets/glWidget/QtBaseViewer.cc b/widgets/glWidget/QtBaseViewer.cc
index ff561dd5..d3ea7243 100644
--- a/widgets/glWidget/QtBaseViewer.cc
+++ b/widgets/glWidget/QtBaseViewer.cc
@@ -511,6 +511,7 @@ void glViewer::drawScene_mono()
       makeCurrent();
 
       glDisable(GL_LIGHTING);
+      glDisable(GL_BLEND);
       glClear(GL_DEPTH_BUFFER_BIT);
       glInitNames();
       glPushName((GLuint) 0);
@@ -521,6 +522,7 @@ void glViewer::drawScene_mono()
       ACG::SceneGraph::traverse(sceneGraphRoot_, action);
 
       glEnable(GL_LIGHTING);
+      glEnable(GL_BLEND);
     }
   }
 
diff --git a/widgets/glWidget/QtBaseViewerPicking.cc b/widgets/glWidget/QtBaseViewerPicking.cc
index ee2bb4ea..b71e2b00 100644
--- a/widgets/glWidget/QtBaseViewerPicking.cc
+++ b/widgets/glWidget/QtBaseViewerPicking.cc
@@ -99,7 +99,7 @@ int glViewer::pickColor( ACG::SceneGraph::PickTarget _pickTarget,
                 b = scene()->height () - scenePos().y() - h,
                 x = _mousePos.x(),
                 y = scene()->height () - _mousePos.y();
-  GLubyte       pixels[9][3];
+  GLubyte       pixels[9][4];
   GLfloat       depths[9];
   int           hit = -1;
   unsigned char order[9] = { 4, 7, 1, 3, 5, 0, 2, 6, 8 };
@@ -144,12 +144,12 @@ int glViewer::pickColor( ACG::SceneGraph::PickTarget _pickTarget,
   glPixelStorei(GL_PACK_ALIGNMENT, 1);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
-  glReadPixels (x - 1, y - 1, 3, 3, GL_RGB, GL_UNSIGNED_BYTE, pixels);
+  glReadPixels (x - 1, y - 1, 3, 3, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
   glReadPixels (x - 1, y - 1, 3, 3, GL_DEPTH_COMPONENT, GL_FLOAT, depths);
 
   for (int i = 0; i < 9; i++)
   {
-    if (hit < 0 && (pixels[order[i]][2] != 0 || pixels[order[i]][1] != 0 || pixels[order[i]][0] != 0))
+    if (hit < 0 && (pixels[order[i]][2] != 0 || pixels[order[i]][1] != 0 || pixels[order[i]][0] != 0 || pixels[order[i]][3] != 0))
     {
       hit = order[i];
       break;
@@ -160,12 +160,13 @@ int glViewer::pickColor( ACG::SceneGraph::PickTarget _pickTarget,
     return 0;
 
 
-  ACG::Vec3uc rgb;
-  rgb[0] = pixels[hit][0];
-  rgb[1] = pixels[hit][1];
-  rgb[2] = pixels[hit][2];
+  ACG::Vec4uc rgba;
+  rgba[0] = pixels[hit][0];
+  rgba[1] = pixels[hit][1];
+  rgba[2] = pixels[hit][2];
+  rgba[3] = pixels[hit][3];
 
-  std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgb);
+  std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
 
   // something wrong with the color stack ?
   if (rv.size () < 2)
@@ -215,6 +216,7 @@ bool glViewer::pickGL( ACG::SceneGraph::PickTarget _pickTarget,
   glMatrixMode(GL_MODELVIEW);
   glLoadMatrixd(modelview.get_raw_data());
   glDisable(GL_LIGHTING);
+  glDisable(GL_BLEND);
   glClear(GL_DEPTH_BUFFER_BIT);
   properties_.glState().pick_init (false);
 
@@ -229,6 +231,7 @@ bool glViewer::pickGL( ACG::SceneGraph::PickTarget _pickTarget,
   glMatrixMode( GL_MODELVIEW );
   glLoadMatrixd(modelview.get_raw_data());
   glEnable(GL_LIGHTING);
+  glEnable(GL_BLEND);
 
   // process hit record
   if ( hits > 0 )
@@ -309,6 +312,7 @@ bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarg
   glMatrixMode(GL_MODELVIEW);
   glLoadMatrixd(modelview.get_raw_data());
   glDisable(GL_LIGHTING);
+  glDisable(GL_BLEND);
   glEnable(GL_DEPTH_TEST);
   glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
   properties_.glState().pick_init (true);
@@ -323,19 +327,20 @@ bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarg
   glMatrixMode( GL_MODELVIEW );
   glLoadMatrixd(modelview.get_raw_data());
   glEnable(GL_LIGHTING);
+  glEnable(GL_BLEND);
 
   properties_.glState().set_clear_color (clear_color);
 
   if (properties_.glState().pick_error ())
     return false;
 
-  buffer = new GLubyte[3 * rect.width() * rect.height()];
+  buffer = new GLubyte[4 * rect.width() * rect.height()];
 
   glPixelStorei(GL_PACK_ALIGNMENT, 1);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
   glReadPixels (rect.x(), scene()->height () - rect.bottom() , rect.width(),
-                rect.height(), GL_RGB, GL_UNSIGNED_BYTE, buffer);
+                rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);
 
   QSet<QPair<unsigned int, unsigned int> > found;
 
@@ -344,15 +349,16 @@ bool glViewer::pick_region( ACG::SceneGraph::PickTarget                _pickTarg
     {
       if (_region.contains (QPoint (rect.x() + x, rect.y() + y)))
       {
-        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 3;
-        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0)
+        int bPos = (((rect.height () - (y + 1)) * rect.width ()) + x) * 4;
+        if (buffer[bPos + 2] != 0 || buffer[bPos + 1] != 0 || buffer[bPos] != 0 || buffer[bPos + 3] != 0)
         {
-          ACG::Vec3uc rgb;
-          rgb[0] = buffer[bPos];
-          rgb[1] = buffer[bPos + 1];
-          rgb[2] = buffer[bPos + 2];
+          ACG::Vec4uc rgba;
+          rgba[0] = buffer[bPos];
+          rgba[1] = buffer[bPos + 1];
+          rgba[2] = buffer[bPos + 2];
+          rgba[3] = buffer[bPos + 3];
 
-          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgb);
+          std::vector<unsigned int> rv = properties_.glState().pick_color_to_stack (rgba);
           if (rv.size () < 2)
             continue;
 
-- 
GitLab