From 7146a20976a04579bbb3dee00c45500b8ebd3817 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 22 Jan 2013 04:30:06 +0000 Subject: [PATCH] - attempt to fix / improve performance of color picking --- source/glest_game/graphics/renderer.cpp | 172 +++++++++++++++++-- source/glest_game/graphics/renderer.h | 4 + source/glest_game/gui/gui.cpp | 1 + source/shared_lib/sources/graphics/model.cpp | 10 +- 4 files changed, 168 insertions(+), 19 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index b75654b96..fbcecbf26 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -6846,6 +6846,13 @@ void Renderer::selectUsingSelectionBuffer(Selection::UnitContainer &units, void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp) { + + colorPickUnits(units,posDown, posUp); + if(units.empty() == true && withObjectSelection == true) { + colorPickObject(obj, posDown,posUp); + } + +/* int x1 = posDown.x; int y1 = posDown.y; int x2 = posUp.x; @@ -6895,8 +6902,9 @@ void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, // Added this to ensure all the selection calls are done now // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) - //glFlush(); + glFlush(); + // uncomment this for debugging color picking to see what is colored //GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); PixelBufferWrapper::end(); @@ -6909,26 +6917,10 @@ void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(rendererModels, rendererModels.begin())); } -/* - for(unsigned int i = 0; i < rendererObjects.size(); ++i) { - Object *object = rendererObjects[i]; - rendererModels.push_back(object); - //printf("In [%s::%s] Line: %d rendered object i = %d [%s] [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,object->getUniquePickName().c_str(),object->getColorDescription().c_str()); - //printf("In [%s::%s] Line: %d\ni = %d [%d - %s] ptr[%p] color[%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,unit->getId(),unit->getType()->getName().c_str(),unit->getCurrentModelPtr(),unit->getColorDescription().c_str()); - } - - //printf("In [%s::%s] Line: %d\nLooking for picks inside [%d,%d,%d,%d] posdown [%s] posUp [%s]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,x,y,w,h,posDown.getString().c_str(),posUp.getString().c_str()); - for(unsigned int i = 0; i < rendererUnits.size(); ++i) { - Unit *unit = rendererUnits[i]; - rendererModels.push_back(unit); - //printf("In [%s::%s] Line: %d rendered unit i = %d [%s] [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,unit->getUniquePickName().c_str(),unit->getColorDescription().c_str()); - //printf("In [%s::%s] Line: %d\ni = %d [%d - %s] ptr[%p] color[%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,unit->getId(),unit->getType()->getName().c_str(),unit->getCurrentModelPtr(),unit->getColorDescription().c_str()); - } -*/ - vector 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()); + //glPopAttrib(); if(pickedList.empty() == false) { units.reserve(pickedList.size()); for(unsigned int i = 0; i < pickedList.size(); ++i) { @@ -6956,8 +6948,152 @@ void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, } } } +*/ } +void Renderer::colorPickUnits(Selection::UnitContainer &units, + 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 rendererUnits = renderUnitsFast(false, 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 rendererModels; + if(rendererUnits.empty() == false) { + copy(rendererUnits.begin(), rendererUnits.end(), std::inserter(rendererModels, rendererModels.begin())); + } + + vector 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 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 rendererModels; + if(rendererObjects.empty() == false) { + copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(rendererModels, rendererModels.begin())); + } + + vector pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, rendererModels); + + 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(object != NULL) { + obj = object; + return; + } + } + } +} + + // ==================== shadows ==================== void Renderer::renderShadowsToTexture(const int renderFps){ diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index c8d21aa06..8f7afff2c 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -540,6 +540,10 @@ 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(); diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 0fbede600..a9d4d701c 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -1020,6 +1020,7 @@ void Gui::computeSelected(bool doubleClick, bool force){ const Object *selectedResourceObject=NULL; selectedResourceObjectPos=Vec2i(-1,-1); if(selectionQuad.isEnabled() && selectionQuad.getPosUp().dist(selectionQuad.getPosDown())getMapPos()); diff --git a/source/shared_lib/sources/graphics/model.cpp b/source/shared_lib/sources/graphics/model.cpp index 3adffe79b..848a98dc6 100644 --- a/source/shared_lib/sources/graphics/model.cpp +++ b/source/shared_lib/sources/graphics/model.cpp @@ -1568,7 +1568,8 @@ vector BaseColorPickEntity::getPickedList(int x,int y,int w,int h, //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); static Chrono lastSnapshot(true); - const int selectionMillisecondUpdate = 100; + //const int selectionMillisecondUpdate = 100; + const int selectionMillisecondUpdate = 0; if(PixelBufferWrapper::getIsPBOEnable() == true) { // Only update the pixel buffer every x milliseconds or as required @@ -1679,10 +1680,17 @@ bool BaseColorPickEntity::isUniquePickingColor(unsigned char *pixel) const { } void BaseColorPickEntity::setUniquePickingColor() const { + + glColor3ub(uniqueColorID[0], + uniqueColorID[1], + uniqueColorID[2]); +/* glColor3f( uniqueColorID[0] / 255.0f, uniqueColorID[1] / 255.0f, uniqueColorID[2] / 255.0f); //uniqueColorID[3] / 255.0f); + + */ }