- fix unit selection

This commit is contained in:
Mark Vejvoda
2013-01-24 15:52:11 +00:00
parent bf7445903d
commit 87b39a3cb5
4 changed files with 248 additions and 331 deletions

View File

@@ -6874,21 +6874,6 @@ void Renderer::selectUsingSelectionBuffer(Selection::UnitContainer &units,
void Renderer::selectUsingColorPicking(Selection::UnitContainer &units,
const Object *&obj, const bool withObjectSelection,
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);
if(units.empty() == true && withObjectSelection == true) {
colorPickObject(obj, posDown,posUp);
}
//glDrawBuffer(GL_BACK);
//glReadBuffer(GL_BACK);
}
void Renderer::colorPickUnits(Selection::UnitContainer &units,
const Vec2i &posDown, const Vec2i &posUp) {
int x1 = posDown.x;
int y1 = posDown.y;
int x2 = posUp.x;
@@ -6913,7 +6898,6 @@ void Renderer::colorPickUnits(Selection::UnitContainer &units,
h= (h * metrics.getScreenH() / metrics.getVirtualH());
PixelBufferWrapper::begin();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
@@ -6923,8 +6907,16 @@ void Renderer::colorPickUnits(Selection::UnitContainer &units,
gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane);
loadGameCameraMatrix();
vector<Unit *> rendererUnits = renderUnitsFast(false, true);
//render units to find which ones should be selected
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
vector<Unit *> rendererUnits = renderUnitsFast(false, true);
//printf("In [%s::%s] Line: %d rendererUnits = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rendererUnits.size());
vector<Object *> rendererObjects;
if(withObjectSelection == true) {
rendererObjects = renderObjectsFast(false,true,true);
}
//pop matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
@@ -6933,7 +6925,6 @@ void Renderer::colorPickUnits(Selection::UnitContainer &units,
// (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4])
//glFlush();
// uncomment this for debugging color picking to see what is colored
//GraphicsInterface::getInstance().getCurrentContext()->swapBuffers();
PixelBufferWrapper::end();
@@ -6942,95 +6933,44 @@ void Renderer::colorPickUnits(Selection::UnitContainer &units,
if(rendererUnits.empty() == false) {
copy(rendererUnits.begin(), rendererUnits.end(), std::inserter(rendererModels, rendererModels.begin()));
}
vector<int> pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, rendererModels);
if(pickedList.empty() == false) {
units.reserve(pickedList.size());
for(unsigned int i = 0; i < pickedList.size(); ++i) {
int index = pickedList[i];
Unit *unit = rendererUnits[index];
if(unit != NULL && unit->isAlive()) {
units.push_back(unit);
}
}
}
}
void Renderer::colorPickObject(const Object *&obj, const Vec2i &posDown,
const Vec2i &posUp) {
int x1 = posDown.x;
int y1 = posDown.y;
int x2 = posUp.x;
int y2 = posUp.y;
int x = min(x1,x2);
int y = min(y1,y2);
int w = max(x1,x2) - min(x1,x2);
int h = max(y1,y2) - min(y1,y2);
if(w < 1) {
w = 1;
}
if(h < 1) {
h = 1;
}
const Metrics &metrics= Metrics::getInstance();
x= (x * metrics.getScreenW() / metrics.getVirtualW());
y= (y * metrics.getScreenH() / metrics.getVirtualH());
w= (w * metrics.getScreenW() / metrics.getVirtualW());
h= (h * metrics.getScreenH() / metrics.getVirtualH());
PixelBufferWrapper::begin();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
//GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()};
//gluPickMatrix(x, y, w, h, view);
gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane);
loadGameCameraMatrix();
vector<Object *> rendererObjects = renderObjectsFast(false,true,true);
//pop matrices
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Added this to ensure all the selection calls are done now
// (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4])
//glFlush();
// uncomment this for debugging color picking to see what is colored
//GraphicsInterface::getInstance().getCurrentContext()->swapBuffers();
PixelBufferWrapper::end();
vector<BaseColorPickEntity *> rendererModels;
if(rendererObjects.empty() == false) {
copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(rendererModels, rendererModels.begin()));
}
vector<int> pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, rendererModels);
if(rendererModels.empty() == false) {
vector<int> pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, rendererModels);
//printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size());
if(pickedList.empty() == false) {
for(unsigned int i = 0; i < pickedList.size(); ++i) {
int index = pickedList[i];
Object *object = rendererObjects[index];
//printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,object);
if(pickedList.empty() == false) {
units.reserve(pickedList.size());
for(unsigned int i = 0; i < pickedList.size(); ++i) {
int index = pickedList[i];
//printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size());
if(object != NULL) {
obj = object;
return;
if(rendererObjects.empty() == false && index < rendererObjects.size()) {
Object *object = rendererObjects[index];
//printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,object);
if(object != NULL) {
obj = object;
if(withObjectSelection == true) {
//printf("In [%s::%s] Line: %d found selected object [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,obj);
return;
}
}
}
else {
index -= rendererObjects.size();
Unit *unit = rendererUnits[index];
if(unit != NULL && unit->isAlive()) {
units.push_back(unit);
}
}
}
}
}
}
// ==================== shadows ====================
void Renderer::renderShadowsToTexture(const int renderFps){
@@ -7496,82 +7436,81 @@ vector<Unit *> Renderer::renderUnitsFast(bool renderingShadows, bool colorPickin
for(int visibleUnitIndex = 0;
visibleUnitIndex < qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) {
Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex];
if(unit != NULL && unit->isAlive() == true) {
if(modelRenderStarted == false) {
modelRenderStarted = true;
if(colorPickingSelection == false) {
//glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT);
glDisable(GL_LIGHTING);
if (renderingShadows == false) {
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_TEXTURE_2D);
}
else {
glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT);
glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GREATER, 0.4f);
if(modelRenderStarted == false) {
modelRenderStarted = true;
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
//set color to the texture alpha
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
//set alpha to the texture alpha
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
}
if(colorPickingSelection == false) {
//glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT);
glDisable(GL_LIGHTING);
if (renderingShadows == false) {
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_TEXTURE_2D);
}
else {
glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT);
glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GREATER, 0.4f);
modelRenderer->begin(false, renderingShadows, false, colorPickingSelection);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
if(colorPickingSelection == false) {
glInitNames();
//set color to the texture alpha
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
//set alpha to the texture alpha
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
}
}
if(colorPickingSelection == false) {
glPushName(visibleUnitIndex);
}
//assertGl();
glMatrixMode(GL_MODELVIEW);
//debuxar modelo
glPushMatrix();
//translate
Vec3f currVec= unit->getCurrVectorFlat();
glTranslatef(currVec.x, currVec.y, currVec.z);
//rotate
glRotatef(unit->getRotation(), 0.f, 1.f, 0.f);
//render
Model *model= unit->getCurrentModelPtr();
//if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) {
// ***MV don't think this is needed below 2013/01/11
//model->updateInterpolationVertices(unit->getAnimProgress(), unit->isAlive() && !unit->isAnimProgressBound());
//}
if(colorPickingSelection == true) {
unit->setUniquePickingColor();
unitsList.push_back(unit);
}
modelRenderer->render(model,rmSelection);
glPopMatrix();
modelRenderer->begin(false, renderingShadows, false, colorPickingSelection);
if(colorPickingSelection == false) {
glPopName();
glInitNames();
}
}
if(colorPickingSelection == false) {
glPushName(visibleUnitIndex);
}
//assertGl();
glMatrixMode(GL_MODELVIEW);
//debuxar modelo
glPushMatrix();
//translate
Vec3f currVec= unit->getCurrVectorFlat();
glTranslatef(currVec.x, currVec.y, currVec.z);
//rotate
glRotatef(unit->getRotation(), 0.f, 1.f, 0.f);
//render
Model *model= unit->getCurrentModelPtr();
//if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) {
// ***MV don't think this is needed below 2013/01/11
//model->updateInterpolationVertices(unit->getAnimProgress(), unit->isAlive() && !unit->isAnimProgressBound());
//}
if(colorPickingSelection == true) {
unit->setUniquePickingColor();
unitsList.push_back(unit);
}
modelRenderer->render(model,rmSelection);
glPopMatrix();
if(colorPickingSelection == false) {
glPopName();
}
}
if(modelRenderStarted == true) {

View File

@@ -540,10 +540,6 @@ public:
void selectUsingSelectionBuffer(Selection::UnitContainer &units,const Object *&obj, const bool withObjectSelection,const Vec2i &posDown, const Vec2i &posUp);
void selectUsingFrustumSelection(Selection::UnitContainer &units,const Object *&obj, const bool withObjectSelection,const Vec2i &posDown, const Vec2i &posUp);
void colorPickUnits(Selection::UnitContainer &units,const Vec2i &posDown, const Vec2i &posUp);
void colorPickObject(const Object *&obj, const Vec2i &posDown,const Vec2i &posUp);
//gl wrap
string getGlInfo();

View File

@@ -252,27 +252,6 @@ public:
virtual ~BaseColorPickEntity() {}
static const int COLOR_COMPONENTS = 4;
class ColorPickStruct {
public:
unsigned char color[COLOR_COMPONENTS];
ColorPickStruct(unsigned char newColor1,unsigned char newColor2,
unsigned char newColor3,unsigned char newColor4) {
color[0] = newColor1;
color[1] = newColor2;
color[2] = newColor3;
color[3] = newColor4;
}
ColorPickStruct() {
color[0] = 0;
color[1] = 0;
color[2] = 0;
color[3] = 0;
}
};
static void init(int bufferSize);
static void beginPicking();
static void endPicking();
@@ -291,10 +270,10 @@ protected:
void recycleUniqueColor();
private:
ColorPickStruct uniqueColorID;
unsigned char uniqueColorID[COLOR_COMPONENTS];
static ColorPickStruct nextColorID;
static vector<ColorPickStruct> nextColorIDReuseList;
static unsigned char nextColorID[COLOR_COMPONENTS];
static vector<vector<unsigned char> > nextColorIDReuseList;
static Mutex mutexNextColorID;
static auto_ptr<PixelBufferWrapper> pbo;

View File

@@ -1435,8 +1435,6 @@ void PixelBufferWrapper::begin() {
if(PixelBufferWrapper::isPBOEnabled == true) {
// set the framebuffer to read
//glReadBuffer(GL_FRONT);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(GL_COLOR_BUFFER_BIT);
}
}
@@ -1460,78 +1458,84 @@ PixelBufferWrapper::~PixelBufferWrapper() {
cleanup();
}
const int colorSpacing = 8;
//unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = {1, 1, 1, 1};
BaseColorPickEntity::ColorPickStruct BaseColorPickEntity::nextColorID = ColorPickStruct(colorSpacing, colorSpacing, colorSpacing, colorSpacing);
vector<BaseColorPickEntity::ColorPickStruct> BaseColorPickEntity::nextColorIDReuseList;
unsigned char BaseColorPickEntity::nextColorID[COLOR_COMPONENTS] = { 1, 1, 1 };
vector<vector<unsigned char> > BaseColorPickEntity::nextColorIDReuseList;
Mutex BaseColorPickEntity::mutexNextColorID;
auto_ptr<PixelBufferWrapper> BaseColorPickEntity::pbo;
void BaseColorPickEntity::recycleUniqueColor() {
MutexSafeWrapper safeMutex(&mutexNextColorID);
nextColorIDReuseList.push_back(uniqueColorID);
vector<unsigned char> reUseColor;
reUseColor.push_back(uniqueColorID[0]);
reUseColor.push_back(uniqueColorID[1]);
reUseColor.push_back(uniqueColorID[2]);
nextColorIDReuseList.push_back(reUseColor);
}
void BaseColorPickEntity::resetUniqueColors() {
MutexSafeWrapper safeMutex(&mutexNextColorID);
nextColorID.color[0] = colorSpacing;
nextColorID.color[1] = colorSpacing;
nextColorID.color[2] = colorSpacing;
BaseColorPickEntity::nextColorID[0] = 1;
BaseColorPickEntity::nextColorID[1] = 1;
BaseColorPickEntity::nextColorID[2] = 1;
nextColorIDReuseList.clear();
}
BaseColorPickEntity::BaseColorPickEntity() {
MutexSafeWrapper safeMutex(&mutexNextColorID);
// Reuse old colors
if(nextColorIDReuseList.empty() == false) {
uniqueColorID.color[0] = nextColorIDReuseList.back().color[0];
uniqueColorID.color[1] = nextColorIDReuseList.back().color[1];
uniqueColorID.color[2] = nextColorIDReuseList.back().color[2];
uniqueColorID[0] = nextColorIDReuseList.back()[0];
uniqueColorID[1] = nextColorIDReuseList.back()[1];
uniqueColorID[2] = nextColorIDReuseList.back()[2];
nextColorIDReuseList.pop_back();
return;
}
uniqueColorID.color[0] = nextColorID.color[0];
uniqueColorID.color[1] = nextColorID.color[1];
uniqueColorID.color[2] = nextColorID.color[2];
//uniqueColorID[3] = nextColorID[3];
if(nextColorID.color[0] + colorSpacing <= 255) {
nextColorID.color[0] += colorSpacing;
}
else {
nextColorID.color[0] = colorSpacing;
if(nextColorID.color[1] + colorSpacing <= 255) {
nextColorID.color[1] += colorSpacing;
}
else {
nextColorID.color[1] = colorSpacing;
if(nextColorID.color[2] + colorSpacing <= 255) {
nextColorID.color[2] += colorSpacing;
}
else {
uniqueColorID[0] = nextColorID[0];
uniqueColorID[1] = nextColorID[1];
uniqueColorID[2] = nextColorID[2];
//uniqueColorID[3] = nextColorID[3];
//printf("Color rolled over on 3rd level!\n");
const int colorSpacing = 8;
nextColorID.color[0] = colorSpacing;
nextColorID.color[1] = colorSpacing;
nextColorID.color[2] = colorSpacing;
if((int)(nextColorID[0] + colorSpacing) <= 255) {
nextColorID[0] += colorSpacing;
}
else {
nextColorID[0] = 1;
if((int)(nextColorID[1] + colorSpacing) <= 255) {
nextColorID[1] += colorSpacing;
}
else {
nextColorID[1] = 1;
if((int)(nextColorID[2] + colorSpacing) <= 255) {
nextColorID[2] += colorSpacing;
}
else {
//printf("Color rolled over on 3rd level!\n");
nextColorID[0] = 1;
nextColorID[1] = 1;
nextColorID[2] = 1;
// nextColorID[2] = 1;
// nextColorID[3]+=colorSpacing;
//
// if(nextColorID[3] > 255) {
// nextColorID[0] = 1;
// nextColorID[1] = 1;
// nextColorID[2] = 1;
// nextColorID[3] = 1;
// }
}
}
}
// nextColorID[2] = 1;
// nextColorID[3]+=colorSpacing;
//
// if(nextColorID[3] > 255) {
// nextColorID[0] = 1;
// nextColorID[1] = 1;
// nextColorID[2] = 1;
// nextColorID[3] = 1;
// }
}
}
}
}
}
void BaseColorPickEntity::init(int bufferSize) {
@@ -1542,7 +1546,7 @@ void BaseColorPickEntity::init(int bufferSize) {
string BaseColorPickEntity::getColorDescription() const {
char szBuf[100]="";
snprintf(szBuf,100,"%d.%d.%d",uniqueColorID.color[0],uniqueColorID.color[1],uniqueColorID.color[2]);
snprintf(szBuf,100,"%d.%d.%d",uniqueColorID[0],uniqueColorID[1],uniqueColorID[2]);
string result = szBuf;
return result;
}
@@ -1560,8 +1564,8 @@ void BaseColorPickEntity::beginPicking() {
glDisable(GL_BLEND);
glDisable(GL_MULTISAMPLE);
glDisable(GL_DITHER);
//glDisable(GL_POLYGON_OFFSET_FILL);
//glDisable(GL_NORMALIZE);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_NORMALIZE);
//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);
@@ -1582,122 +1586,120 @@ void BaseColorPickEntity::endPicking() {
vector<int> BaseColorPickEntity::getPickedList(int x,int y,int w,int h,
const vector<BaseColorPickEntity *> &rendererModels) {
vector<int> pickedModels;
if(rendererModels.empty() == false) {
pickedModels.reserve(rendererModels.size());
pickedModels.reserve(rendererModels.size());
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
//static auto_ptr<unsigned char> cachedPixels;
static auto_ptr<Pixmap2D> cachedPixels;
//static int cachedPixelsW = -1;
//static int cachedPixelsH = -1;
//printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
//static auto_ptr<unsigned char> cachedPixels;
static auto_ptr<Pixmap2D> cachedPixels;
//static int cachedPixelsW = -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);
//const int selectionMillisecondUpdate = 100;
//const int selectionMillisecondUpdate = 0;
static Chrono lastSnapshot(true);
const int selectionMillisecondUpdate = 100;
if(PixelBufferWrapper::getIsPBOEnable() == true) {
// Only update the pixel buffer every x milliseconds or as required
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1) { // ||
//lastSnapshot.getMillis() > selectionMillisecondUpdate) {
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
if(PixelBufferWrapper::getIsPBOEnable() == true) {
// Only update the pixel buffer every x milliseconds or as required
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1 ||
lastSnapshot.getMillis() > selectionMillisecondUpdate) {
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
//lastSnapshot.reset();
//Pixmap2D *pixmapScreenShot = BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS);
cachedPixels.reset(BaseColorPickEntity::pbo->getPixelBufferFor(x,y,w,h, COLOR_COMPONENTS));
lastSnapshot.reset();
//Pixmap2D *pixmapScreenShot = 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()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1;
//cachedPixelsH = h+1;
//cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1;
//cachedPixelsH = h+1;
//delete pixmapScreenShot;
}
//delete pixmapScreenShot;
}
else {
// Only update the pixel buffer every x milliseconds or as required
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1) {// ||
//lastSnapshot.getMillis() > selectionMillisecondUpdate) {
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
}
else {
// Only update the pixel buffer every x milliseconds or as required
if(cachedPixels.get() == NULL || cachedPixels->getW() != w+1 ||cachedPixels->getH() != h+1 ||
lastSnapshot.getMillis() > selectionMillisecondUpdate) {
//printf("Updating selection millis = %ld\n",lastSnapshot.getMillis());
//lastSnapshot.reset();
lastSnapshot.reset();
//Pixmap2D *pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS);
cachedPixels.reset(new Pixmap2D(w+1, h+1, COLOR_COMPONENTS));
//glPixelStorei(GL_PACK_ALIGNMENT, 1);
//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, cachedPixels->getPixels());
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//Pixmap2D *pixmapScreenShot = new Pixmap2D(w+1, h+1, COLOR_COMPONENTS);
cachedPixels.reset(new Pixmap2D(w+1, h+1, COLOR_COMPONENTS));
//glPixelStorei(GL_PACK_ALIGNMENT, 1);
//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, cachedPixels->getPixels());
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1;
//cachedPixelsH = h+1;
//cachedPixels.reset(new unsigned char[(unsigned int)pixmapScreenShot->getPixelByteCount()]);
//memcpy(cachedPixels.get(),pixmapScreenShot->getPixels(),(size_t)pixmapScreenShot->getPixelByteCount());
//cachedPixelsW = w+1;
//cachedPixelsH = h+1;
//delete pixmapScreenShot;
}
//delete pixmapScreenShot;
}
unsigned char *pixelBuffer = cachedPixels->getPixels();
}
unsigned char *pixelBuffer = cachedPixels->getPixels();
// Enable screenshots to debug selection scene
//pixmapScreenShot->save("debug.png");
// Enable screenshots to debug selection scene
//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]
// so we search through our object list looking for the object that was selected
// 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
map<int,bool> modelAlreadyPickedList;
map<unsigned char,map<unsigned char, map<unsigned char,bool> > > colorAlreadyPickedList;
int nEnd = w * h;
for(int x = 0; x < nEnd && pickedModels.size() < rendererModels.size(); ++x) {
int index = x * COLOR_COMPONENTS;
unsigned char *pixel = &pixelBuffer[index];
map<int,bool> modelAlreadyPickedList;
map<unsigned char,map<unsigned char, map<unsigned char,bool> > > colorAlreadyPickedList;
int nEnd = w * h;
for(int x = 0; x < nEnd && pickedModels.size() < rendererModels.size(); ++x) {
int index = x * COLOR_COMPONENTS;
unsigned char *pixel = &pixelBuffer[index];
// Skip duplicate scanned colors
map<unsigned char,map<unsigned char, map<unsigned char,bool> > >::const_iterator iterFind1 = colorAlreadyPickedList.find(pixel[0]);
if(iterFind1 != colorAlreadyPickedList.end()) {
map<unsigned char, map<unsigned char,bool> >::const_iterator iterFind2 = iterFind1->second.find(pixel[1]);
if(iterFind2 != iterFind1->second.end()) {
map<unsigned char,bool>::const_iterator iterFind3 = iterFind2->second.find(pixel[2]);
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()) {
// Skip duplicate scanned colors
map<unsigned char,map<unsigned char, map<unsigned char,bool> > >::const_iterator iterFind1 = colorAlreadyPickedList.find(pixel[0]);
if(iterFind1 != colorAlreadyPickedList.end()) {
map<unsigned char, map<unsigned char,bool> >::const_iterator iterFind2 = iterFind1->second.find(pixel[1]);
if(iterFind2 != iterFind1->second.end()) {
map<unsigned char,bool>::const_iterator iterFind3 = iterFind2->second.find(pixel[2]);
if(iterFind3 != iterFind2->second.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;
for(unsigned int i = 0; i < rendererModels.size(); ++i) {
// Skip models already selected
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__);
return pickedModels;
}
bool BaseColorPickEntity::isUniquePickingColor(unsigned char *pixel) const {
bool result = false;
if( uniqueColorID.color[0] == pixel[0] &&
uniqueColorID.color[1] == pixel[1] &&
uniqueColorID.color[2] == pixel[2]) {
if( uniqueColorID[0] == pixel[0] &&
uniqueColorID[1] == pixel[1] &&
uniqueColorID[2] == pixel[2]) {
//uniqueColorID[3] == pixel[3]) {
result = true;
}
@@ -1707,15 +1709,16 @@ bool BaseColorPickEntity::isUniquePickingColor(unsigned char *pixel) const {
void BaseColorPickEntity::setUniquePickingColor() const {
glColor3ub(uniqueColorID.color[0],
uniqueColorID.color[1],
uniqueColorID.color[2]);
glColor3ub(uniqueColorID[0],
uniqueColorID[1],
uniqueColorID[2]);
/*
glColor3f( uniqueColorID[0] / 255.0f,
uniqueColorID[1] / 255.0f,
uniqueColorID[2] / 255.0f);
//uniqueColorID[3] / 255.0f);
*
*/
}