- attempt to speed up color picking

This commit is contained in:
Mark Vejvoda 2013-01-23 23:20:54 +00:00
parent 86eac7451b
commit d7d83f7c5e
3 changed files with 104 additions and 96 deletions

View File

@ -6875,10 +6875,16 @@ void Renderer::selectUsingColorPicking(Selection::UnitContainer &units,
const Object *&obj, const bool withObjectSelection, const Object *&obj, const bool withObjectSelection,
const Vec2i &posDown, const Vec2i &posUp) { const Vec2i &posDown, const Vec2i &posUp) {
//glReadBuffer(GL_FRONT);
//glDrawBuffer(GL_FRONT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
colorPickUnits(units,posDown, posUp); colorPickUnits(units,posDown, posUp);
if(units.empty() == true && withObjectSelection == true) { if(units.empty() == true && withObjectSelection == true) {
colorPickObject(obj, posDown,posUp); colorPickObject(obj, posDown,posUp);
} }
//glDrawBuffer(GL_BACK);
//glReadBuffer(GL_BACK);
} }
void Renderer::colorPickUnits(Selection::UnitContainer &units, void Renderer::colorPickUnits(Selection::UnitContainer &units,

View File

@ -251,7 +251,7 @@ public:
BaseColorPickEntity(); BaseColorPickEntity();
virtual ~BaseColorPickEntity() {} virtual ~BaseColorPickEntity() {}
static const int COLOR_COMPONENTS = 3; static const int COLOR_COMPONENTS = 4;
struct ColorPickStruct { struct ColorPickStruct {
unsigned char color[COLOR_COMPONENTS]; unsigned char color[COLOR_COMPONENTS];

View File

@ -1435,6 +1435,8 @@ void PixelBufferWrapper::begin() {
if(PixelBufferWrapper::isPBOEnabled == true) { if(PixelBufferWrapper::isPBOEnabled == true) {
// set the framebuffer to read // set the framebuffer to read
//glReadBuffer(GL_FRONT); //glReadBuffer(GL_FRONT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT);
} }
} }
@ -1458,8 +1460,9 @@ PixelBufferWrapper::~PixelBufferWrapper() {
cleanup(); cleanup();
} }
const int colorSpacing = 8;
//unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = {1, 1, 1, 1}; //unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = {1, 1, 1, 1};
BaseColorPickEntity::ColorPickStruct BaseColorPickEntity::nextColorID = { 1, 1, 1 }; BaseColorPickEntity::ColorPickStruct BaseColorPickEntity::nextColorID = { colorSpacing, colorSpacing, colorSpacing };
vector<BaseColorPickEntity::ColorPickStruct> BaseColorPickEntity::nextColorIDReuseList; vector<BaseColorPickEntity::ColorPickStruct> BaseColorPickEntity::nextColorIDReuseList;
Mutex BaseColorPickEntity::mutexNextColorID; Mutex BaseColorPickEntity::mutexNextColorID;
auto_ptr<PixelBufferWrapper> BaseColorPickEntity::pbo; auto_ptr<PixelBufferWrapper> BaseColorPickEntity::pbo;
@ -1471,9 +1474,9 @@ void BaseColorPickEntity::recycleUniqueColor() {
void BaseColorPickEntity::resetUniqueColors() { void BaseColorPickEntity::resetUniqueColors() {
MutexSafeWrapper safeMutex(&mutexNextColorID); MutexSafeWrapper safeMutex(&mutexNextColorID);
nextColorID.color[0] = 1; nextColorID.color[0] = colorSpacing;
nextColorID.color[1] = 1; nextColorID.color[1] = colorSpacing;
nextColorID.color[2] = 1; nextColorID.color[2] = colorSpacing;
nextColorIDReuseList.clear(); nextColorIDReuseList.clear();
} }
@ -1495,18 +1498,16 @@ BaseColorPickEntity::BaseColorPickEntity() {
uniqueColorID.color[2] = nextColorID.color[2]; uniqueColorID.color[2] = nextColorID.color[2];
//uniqueColorID[3] = nextColorID[3]; //uniqueColorID[3] = nextColorID[3];
const int colorSpacing = 8;
if(nextColorID.color[0] + colorSpacing <= 255) { if(nextColorID.color[0] + colorSpacing <= 255) {
nextColorID.color[0] += colorSpacing; nextColorID.color[0] += colorSpacing;
} }
else { else {
nextColorID.color[0] = 1; nextColorID.color[0] = colorSpacing;
if(nextColorID.color[1] + colorSpacing <= 255) { if(nextColorID.color[1] + colorSpacing <= 255) {
nextColorID.color[1] += colorSpacing; nextColorID.color[1] += colorSpacing;
} }
else { else {
nextColorID.color[1] = 1; nextColorID.color[1] = colorSpacing;
if(nextColorID.color[2] + colorSpacing <= 255) { if(nextColorID.color[2] + colorSpacing <= 255) {
nextColorID.color[2] += colorSpacing; nextColorID.color[2] += colorSpacing;
} }
@ -1514,9 +1515,9 @@ BaseColorPickEntity::BaseColorPickEntity() {
//printf("Color rolled over on 3rd level!\n"); //printf("Color rolled over on 3rd level!\n");
nextColorID.color[0] = 1; nextColorID.color[0] = colorSpacing;
nextColorID.color[1] = 1; nextColorID.color[1] = colorSpacing;
nextColorID.color[2] = 1; nextColorID.color[2] = colorSpacing;
// nextColorID[2] = 1; // nextColorID[2] = 1;
@ -1559,8 +1560,8 @@ void BaseColorPickEntity::beginPicking() {
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDisable(GL_MULTISAMPLE); glDisable(GL_MULTISAMPLE);
glDisable(GL_DITHER); glDisable(GL_DITHER);
glDisable(GL_POLYGON_OFFSET_FILL); //glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_NORMALIZE); //glDisable(GL_NORMALIZE);
//glPushAttrib(GL_TEXTURE_2D | GL_LIGHTING | GL_BLEND | GL_MULTISAMPLE | GL_DITHER); //glPushAttrib(GL_TEXTURE_2D | GL_LIGHTING | GL_BLEND | GL_MULTISAMPLE | GL_DITHER);
//glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT | GL_NORMALIZE | GL_BLEND | GL_POLYGON_OFFSET_FILL); //glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT | GL_NORMALIZE | GL_BLEND | GL_POLYGON_OFFSET_FILL);
@ -1581,112 +1582,113 @@ void BaseColorPickEntity::endPicking() {
vector<int> BaseColorPickEntity::getPickedList(int x,int y,int w,int h, vector<int> BaseColorPickEntity::getPickedList(int x,int y,int w,int h,
const vector<BaseColorPickEntity *> &rendererModels) { const vector<BaseColorPickEntity *> &rendererModels) {
vector<int> pickedModels; vector<int> pickedModels;
pickedModels.reserve(rendererModels.size()); if(rendererModels.empty() == false) {
pickedModels.reserve(rendererModels.size());
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
//static auto_ptr<unsigned char> cachedPixels; //static auto_ptr<unsigned char> cachedPixels;
static auto_ptr<Pixmap2D> cachedPixels; static auto_ptr<Pixmap2D> cachedPixels;
//static int cachedPixelsW = -1; //static int cachedPixelsW = -1;
//static int cachedPixelsH = -1; //static int cachedPixelsH = -1;
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
static Chrono lastSnapshot(true); //static Chrono lastSnapshot(true);
//const int selectionMillisecondUpdate = 100; //const int selectionMillisecondUpdate = 100;
const int selectionMillisecondUpdate = 0; //const int selectionMillisecondUpdate = 0;
if(PixelBufferWrapper::getIsPBOEnable() == true) { if(PixelBufferWrapper::getIsPBOEnable() == true) {
// Only update the pixel buffer every x milliseconds or as required // Only update the pixel buffer every x milliseconds or as required
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1 || if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1) { // ||
lastSnapshot.getMillis() > selectionMillisecondUpdate) { //lastSnapshot.getMillis() > selectionMillisecondUpdate) {
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis()); //printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
lastSnapshot.reset(); //lastSnapshot.reset();
//Pixmap2D *pixmapScreenShot = BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS); //Pixmap2D *pixmapScreenShot = BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS);
cachedPixels.reset(BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS)); cachedPixels.reset(BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS));
//cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]); //cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount()); //memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1; //cachedPixelsW = w+1;
//cachedPixelsH = h+1; //cachedPixelsH = h+1;
//delete pixmapScreenShot; //delete pixmapScreenShot;
}
} }
} else {
else { // Only update the pixel buffer every x milliseconds or as required
// Only update the pixel buffer every x milliseconds or as required if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1) {// ||
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1 || //lastSnapshot.getMillis() > selectionMillisecondUpdate) {
lastSnapshot.getMillis() > selectionMillisecondUpdate) { //printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
lastSnapshot.reset(); //lastSnapshot.reset();
//Pixmap2D *pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS); //Pixmap2D *pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS);
cachedPixels.reset(new Pixmap2D(w+1, h+1, COLOR_COMPONENTS)); cachedPixels.reset(new Pixmap2D(w+1, h+1, COLOR_COMPONENTS));
//glPixelStorei(GL_PACK_ALIGNMENT, 1); //glPixelStorei(GL_PACK_ALIGNMENT, 1);
//glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); //glReadPixels(x, y, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels());
//glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); //glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels());
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cachedPixels->getPixels()); glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cachedPixels->getPixels());
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]); //cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount()); //memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1; //cachedPixelsW = w+1;
//cachedPixelsH = h+1; //cachedPixelsH = h+1;
//delete pixmapScreenShot; //delete pixmapScreenShot;
}
} }
} unsigned char *pixelBuffer = cachedPixels->getPixels();
unsigned char *pixelBuffer = cachedPixels->getPixels();
// Enable screenshots to debug selection scene // Enable screenshots to debug selection scene
//pixmapScreenShot->save("debug.png"); //pixmapScreenShot->save("debug.png");
//printf("In [%s::%s] Line: %d x,y,w,h [%d,%d,%d,%d] pixels = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,x,y,w,h,pixmapScreenShot->getPixelByteCount()); //printf("In [%s::%s] Line: %d x,y,w,h [%d,%d,%d,%d] pixels = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,x,y,w,h,pixmapScreenShot->getPixelByteCount());
// now our picked screen pixel color is stored in pixel[3] // now our picked screen pixel color is stored in pixel[3]
// so we search through our object list looking for the object that was selected // so we search through our object list looking for the object that was selected
map<int,bool> modelAlreadyPickedList; map<int,bool> modelAlreadyPickedList;
map<unsigned char,map<unsigned char, map<unsigned char,bool> > > colorAlreadyPickedList; map<unsigned char,map<unsigned char, map<unsigned char,bool> > > colorAlreadyPickedList;
int nEnd = w * h; int nEnd = w * h;
for(int x = 0; x < nEnd && pickedModels.size() < rendererModels.size(); ++x) { for(int x = 0; x < nEnd && pickedModels.size() < rendererModels.size(); ++x) {
int index = x * COLOR_COMPONENTS; int index = x * COLOR_COMPONENTS;
unsigned char *pixel = &pixelBuffer[index]; unsigned char *pixel = &pixelBuffer[index];
// Skip duplicate scanned colors // Skip duplicate scanned colors
map<unsigned char,map<unsigned char, map<unsigned char,bool> > >::const_iterator iterFind1 = colorAlreadyPickedList.find(pixel[0]); map<unsigned char,map<unsigned char, map<unsigned char,bool> > >::const_iterator iterFind1 = colorAlreadyPickedList.find(pixel[0]);
if(iterFind1 != colorAlreadyPickedList.end()) { if(iterFind1 != colorAlreadyPickedList.end()) {
map<unsigned char, map<unsigned char,bool> >::const_iterator iterFind2 = iterFind1->second.find(pixel[1]); map<unsigned char, map<unsigned char,bool> >::const_iterator iterFind2 = iterFind1->second.find(pixel[1]);
if(iterFind2 != iterFind1->second.end()) { if(iterFind2 != iterFind1->second.end()) {
map<unsigned char,bool>::const_iterator iterFind3 = iterFind2->second.find(pixel[2]); map<unsigned char,bool>::const_iterator iterFind3 = iterFind2->second.find(pixel[2]);
if(iterFind3 != iterFind2->second.end()) { if(iterFind3 != iterFind2->second.end()) {
continue;
}
}
}
for(unsigned int i = 0; i < rendererModels.size(); ++i) {
// Skip models already selected
if(modelAlreadyPickedList.find(i) != modelAlreadyPickedList.end()) {
continue; continue;
} }
const BaseColorPickEntity *model = rendererModels[i];
if( model != NULL && model->isUniquePickingColor(pixel) == true) {
//printf("Found match pixel [%d.%d.%d] for model [%s] ptr [%p][%s]\n",pixel[0],pixel[1],pixel[2],model->getColorDescription().c_str(), model,model->getUniquePickName().c_str());
pickedModels.push_back(i);
modelAlreadyPickedList[i]=true;
colorAlreadyPickedList[pixel[0]][pixel[1]][pixel[2]]=true;
break;
}
} }
} }
for(unsigned int i = 0; i < rendererModels.size(); ++i) { //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
// Skip models already selected //delete pixmapScreenShot;
if(modelAlreadyPickedList.find(i) != modelAlreadyPickedList.end()) {
continue;
}
const BaseColorPickEntity *model = rendererModels[i];
if( model != NULL && model->isUniquePickingColor(pixel) == true) {
//printf("Found match pixel [%d.%d.%d] for model [%s] ptr [%p][%s]\n",pixel[0],pixel[1],pixel[2],model->getColorDescription().c_str(), model,model->getUniquePickName().c_str());
pickedModels.push_back(i);
modelAlreadyPickedList[i]=true;
colorAlreadyPickedList[pixel[0]][pixel[1]][pixel[2]]=true;
break;
}
}
} }
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
//delete pixmapScreenShot;
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
return pickedModels; return pickedModels;
} }