From 0783b23e1234c71655dad25d8ff7715b30d2221d Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 10 Sep 2010 08:51:32 +0000 Subject: [PATCH] - bugfixes for rendering (new ini setting toggles new quad cache and is called: UseQuadCache=true) --- source/glest_game/game/game.cpp | 2 +- source/glest_game/graphics/renderer.cpp | 1423 +++++++++--------- source/glest_game/graphics/renderer.h | 107 +- source/glest_game/gui/gui.cpp | 11 +- source/glest_game/type_instances/faction.cpp | 8 +- source/glest_game/type_instances/faction.h | 2 +- source/glest_game/world/map.cpp | 3 +- source/glest_game/world/map.h | 5 +- source/glest_game/world/surface_atlas.cpp | 6 +- source/glest_game/world/world.cpp | 2 +- 10 files changed, 736 insertions(+), 833 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 7c360f65..084476fe 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -1549,7 +1549,7 @@ void Game::render2d(){ int visibleObjectCount = 0; Map *map= world.getMap(); int thisTeamIndex= world.getThisTeamIndex(); - PosQuadIterator pqi(map, visibleQuad, Map::cellScale); + PosQuadIterator pqi(visibleQuad, Map::cellScale); while(pqi.next()){ const Vec2i &pos= pqi.getPos(); bool isPosVisible = map->isInside(pos.x, pos.y); diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 074c7e16..6dc3fd44 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -40,19 +40,6 @@ namespace Glest { namespace Game{ bool MeshCallbackTeamColor::noTeamColors = false; -bool RenderEntity::operator<(const RenderEntity &rhs) const { - if(this->type == retUnit || this->type == retUnitFast) { - return(this->unit->getLastRenderFrame() < rhs.unit->getLastRenderFrame()); - } - else { - return(this->o->getLastRenderFrame() < rhs.o->getLastRenderFrame()); - } -} - -bool RenderEntity::operator()(const RenderEntity &lhs,const RenderEntity &rhs) const { - return lhs < lhs; -} - void MeshCallbackTeamColor::execute(const Mesh *mesh){ //team color @@ -155,7 +142,8 @@ const int MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD = 25; // ==================== constructor and destructor ==================== -Renderer::Renderer(){ +Renderer::Renderer() { + this->useQuadCache = true; this->allowRenderUnitTitles = false; this->menu = NULL; this->game = NULL; @@ -168,7 +156,7 @@ Renderer::Renderer(){ shadowsOffDueToMinRender=false; //resources - for(int i=0; iuseQuadCache = config.getBool("UseQuadCache","true"); + this->no2DMouseRendering = config.getBool("No2DMouseRendering","false"); + this->maxConsoleLines= config.getInt("ConsoleMaxLines"); gi.setFactory(fr.getGraphicsFactory(config.getString("FactoryGraphics"))); GraphicsFactory *graphicsFactory= GraphicsInterface::getInstance().getFactory(); @@ -190,7 +179,7 @@ Renderer::Renderer(){ particleRenderer= graphicsFactory->newParticleRenderer(); //resources - for(int i=0; inewModelManager(); textureManager[i]= graphicsFactory->newTextureManager(); modelManager[i]->setTextureManager(textureManager[i]); @@ -940,7 +929,7 @@ void Renderer::renderSelectionQuad(){ Vec2i down= sq->getPosDown(); Vec2i up= sq->getPosUp(); - if(gui->isSelecting()){ + if(gui->isSelecting()) { glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT); glColor3f(0,1,0); glBegin(GL_LINE_LOOP); @@ -1303,107 +1292,139 @@ void Renderer::renderSurface(const int renderFps, const int worldFrameCount) { glActiveTexture(baseTexUnit); + if(useQuadCache == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleScaledCellList.size() > 0) { + for(int visibleIndex = 0; + visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleIndex = 0; visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); + SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); + SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); + SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); - SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); - SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); + if(tc00 == NULL) { + throw runtime_error("tc00 == NULL"); + } + if(tc10 == NULL) { + throw runtime_error("tc10 == NULL"); + } + if(tc01 == NULL) { + throw runtime_error("tc01 == NULL"); + } + if(tc11 == NULL) { + throw runtime_error("tc11 == NULL"); + } - triangleCount+= 2; - pointCount+= 4; + triangleCount+= 2; + pointCount+= 4; - //set texture - currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); - if(currTex != lastTex) { - lastTex = currTex; - glBindTexture(GL_TEXTURE_2D, lastTex); - } + //set texture + if(tc00->getSurfaceTexture() == NULL) { + throw runtime_error("tc00->getSurfaceTexture() == NULL"); + } + currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); + if(currTex != lastTex) { + lastTex = currTex; + glBindTexture(GL_TEXTURE_2D, lastTex); + } - const Vec2f &surfCoord= tc00->getSurfTexCoord(); + const Vec2f &surfCoord= tc00->getSurfTexCoord(); - glBegin(GL_TRIANGLE_STRIP); + glBegin(GL_TRIANGLE_STRIP); - //draw quad using immediate mode - glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); - glNormal3fv(tc01->getNormal().ptr()); - glVertex3fv(tc01->getVertex().ptr()); + //draw quad using immediate mode + glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); + glNormal3fv(tc01->getNormal().ptr()); + glVertex3fv(tc01->getVertex().ptr()); - glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); - glNormal3fv(tc00->getNormal().ptr()); - glVertex3fv(tc00->getVertex().ptr()); + glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); + glNormal3fv(tc00->getNormal().ptr()); + glVertex3fv(tc00->getVertex().ptr()); - glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); - glNormal3fv(tc11->getNormal().ptr()); - glVertex3fv(tc11->getVertex().ptr()); + glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); + glNormal3fv(tc11->getNormal().ptr()); + glVertex3fv(tc11->getVertex().ptr()); - glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); - glNormal3fv(tc10->getNormal().ptr()); - glVertex3fv(tc10->getVertex().ptr()); + glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); + glNormal3fv(tc10->getNormal().ptr()); + glVertex3fv(tc10->getVertex().ptr()); - glEnd(); - } - -/* - Quad2i scaledQuad= visibleQuad/Map::cellScale; - - PosQuadIterator pqi(map, scaledQuad); - while(pqi.next()){ - const Vec2i &pos= pqi.getPos(); - if(mapBounds.isInside(pos)){ - - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); - SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); - SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); - - triangleCount+= 2; - pointCount+= 4; - - //set texture - currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); - if(currTex!=lastTex){ - lastTex=currTex; - glBindTexture(GL_TEXTURE_2D, lastTex); + glEnd(); } - - const Vec2f &surfCoord= tc00->getSurfTexCoord(); - - glBegin(GL_TRIANGLE_STRIP); - - //draw quad using immediate mode - glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); - glNormal3fv(tc01->getNormal().ptr()); - glVertex3fv(tc01->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); - glNormal3fv(tc00->getNormal().ptr()); - glVertex3fv(tc00->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); - glNormal3fv(tc11->getNormal().ptr()); - glVertex3fv(tc11->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); - glNormal3fv(tc10->getNormal().ptr()); - glVertex3fv(tc10->getVertex().ptr()); - - glEnd(); } } - //glEnd(); -*/ + else { + Quad2i scaledQuad= visibleQuad/Map::cellScale; + + PosQuadIterator pqi(scaledQuad); + while(pqi.next()) { + const Vec2i &pos= pqi.getPos(); + if(mapBounds.isInside(pos)){ + + SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); + SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); + SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); + SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); + + if(tc00 == NULL) { + throw runtime_error("tc00 == NULL"); + } + if(tc10 == NULL) { + throw runtime_error("tc10 == NULL"); + } + if(tc01 == NULL) { + throw runtime_error("tc01 == NULL"); + } + if(tc11 == NULL) { + throw runtime_error("tc11 == NULL"); + } + + triangleCount+= 2; + pointCount+= 4; + + //set texture + currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); + if(currTex!=lastTex){ + lastTex=currTex; + glBindTexture(GL_TEXTURE_2D, lastTex); + } + + const Vec2f &surfCoord= tc00->getSurfTexCoord(); + + glBegin(GL_TRIANGLE_STRIP); + + //draw quad using immediate mode + glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); + glNormal3fv(tc01->getNormal().ptr()); + glVertex3fv(tc01->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); + glNormal3fv(tc00->getNormal().ptr()); + glVertex3fv(tc00->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); + glNormal3fv(tc11->getNormal().ptr()); + glVertex3fv(tc11->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); + glNormal3fv(tc10->getNormal().ptr()); + glVertex3fv(tc10->getVertex().ptr()); + + glEnd(); + } + } + //glEnd(); + } //Restore static_cast(modelRenderer)->setDuplicateTexCoords(false); @@ -1429,149 +1450,150 @@ void Renderer::renderObjects(const int renderFps, const int worldFrameCount) { Vec3f baseFogColor; - int thisTeamIndex= world->getThisTeamIndex(); bool modelRenderStarted = false; - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleIndex = 0; visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { - Object *o = qCache.visibleObjectList[visibleIndex]; + if(useQuadCache == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + for(int visibleIndex = 0; + visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { + Object *o = qCache.visibleObjectList[visibleIndex]; - const Model *objModel= o->getModel(); - const Vec3f &v= o->getConstPos(); + const Model *objModel= o->getModel(); + const Vec3f &v= o->getConstPos(); - if(modelRenderStarted == false) { - modelRenderStarted = true; + if(modelRenderStarted == false) { + modelRenderStarted = true; - fowTex= world->getMinimap()->getFowTexture(); - baseFogColor= world->getTileset()->getFogColor()*computeLightColor(world->getTimeFlow()->getTime()); + fowTex= world->getMinimap()->getFowTexture(); + baseFogColor= world->getTileset()->getFogColor() * computeLightColor(world->getTimeFlow()->getTime()); - glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); - if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD && - shadows == sShadowMapping) { - glActiveTexture(shadowTexUnit); - glEnable(GL_TEXTURE_2D); + if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD && + shadows == sShadowMapping) { + glActiveTexture(shadowTexUnit); + glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - static_cast(modelRenderer)->setDuplicateTexCoords(true); - enableProjectiveTexturing(); - } - - glActiveTexture(baseTexUnit); - - glEnable(GL_COLOR_MATERIAL); - glAlphaFunc(GL_GREATER, 0.5f); - - modelRenderer->begin(true, true, false); - } - //ambient and diffuse color is taken from cell color - - float fowFactor= fowTex->getPixmap()->getPixelf(o->getMapPos().x / Map::cellScale, o->getMapPos().y / Map::cellScale); - Vec4f color= Vec4f(Vec3f(fowFactor), 1.f); - glColor4fv(color.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color * ambFactor).ptr()); - glFogfv(GL_FOG_COLOR, (baseFogColor * fowFactor).ptr()); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glTranslatef(v.x, v.y, v.z); - glRotatef(o->getRotation(), 0.f, 1.f, 0.f); - - objModel->updateInterpolationData(0.f, true); - modelRenderer->render(objModel); - - triangleCount+= objModel->getTriangleCount(); - pointCount+= objModel->getVertexCount(); - - glPopMatrix(); - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); - -/* - int thisTeamIndex= world->getThisTeamIndex(); - bool modelRenderStarted = false; - - PosQuadIterator pqi(map, visibleQuad, Map::cellScale); - while(pqi.next()){ - const Vec2i &pos= pqi.getPos(); - if(map->isInside(pos)){ - SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(pos)); - Object *o= sc->getObject(); - if(sc->isExplored(thisTeamIndex) && o!=NULL){ - const Model *objModel= sc->getObject()->getModel(); - const Vec3f &v= o->getConstPos(); - - if(modelRenderStarted == false) { - modelRenderStarted = true; - - fowTex= world->getMinimap()->getFowTexture(); - baseFogColor= world->getTileset()->getFogColor()*computeLightColor(world->getTimeFlow()->getTime()); - - glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); - - if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD && - shadows == sShadowMapping) { - glActiveTexture(shadowTexUnit); - glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - - static_cast(modelRenderer)->setDuplicateTexCoords(true); - enableProjectiveTexturing(); - } - - glActiveTexture(baseTexUnit); - - glEnable(GL_COLOR_MATERIAL); - glAlphaFunc(GL_GREATER, 0.5f); - - modelRenderer->begin(true, true, false); + static_cast(modelRenderer)->setDuplicateTexCoords(true); + enableProjectiveTexturing(); } - //ambient and diffuse color is taken from cell color - float fowFactor= fowTex->getPixmap()->getPixelf(pos.x / Map::cellScale, pos.y / Map::cellScale); - Vec4f color= Vec4f(Vec3f(fowFactor), 1.f); - glColor4fv(color.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color*ambFactor).ptr()); - glFogfv(GL_FOG_COLOR, (baseFogColor*fowFactor).ptr()); + glActiveTexture(baseTexUnit); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glTranslatef(v.x, v.y, v.z); - glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + glEnable(GL_COLOR_MATERIAL); + glAlphaFunc(GL_GREATER, 0.5f); - objModel->updateInterpolationData(0.f, true); - modelRenderer->render(objModel); + modelRenderer->begin(true, true, false); + } + //ambient and diffuse color is taken from cell color - triangleCount+= objModel->getTriangleCount(); - pointCount+= objModel->getVertexCount(); + float fowFactor= fowTex->getPixmap()->getPixelf(o->getMapPos().x / Map::cellScale, o->getMapPos().y / Map::cellScale); + Vec4f color= Vec4f(Vec3f(fowFactor), 1.f); + glColor4fv(color.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color * ambFactor).ptr()); + glFogfv(GL_FOG_COLOR, (baseFogColor * fowFactor).ptr()); - glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(v.x, v.y, v.z); + glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + + objModel->updateInterpolationData(0.f, true); + modelRenderer->render(objModel); + + triangleCount+= objModel->getTriangleCount(); + pointCount+= objModel->getVertexCount(); + + glPopMatrix(); + } + + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } + + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + } + else { + int thisTeamIndex= world->getThisTeamIndex(); + bool modelRenderStarted = false; + + PosQuadIterator pqi(visibleQuad, Map::cellScale); + while(pqi.next()) { + const Vec2i &pos= pqi.getPos(); + if(map->isInside(pos)){ + SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(pos)); + Object *o= sc->getObject(); + if(sc->isExplored(thisTeamIndex) && o!=NULL){ + const Model *objModel= sc->getObject()->getModel(); + const Vec3f &v= o->getConstPos(); + + if(modelRenderStarted == false) { + modelRenderStarted = true; + + fowTex= world->getMinimap()->getFowTexture(); + baseFogColor= world->getTileset()->getFogColor()*computeLightColor(world->getTimeFlow()->getTime()); + + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); + + if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD && + shadows == sShadowMapping) { + glActiveTexture(shadowTexUnit); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + + static_cast(modelRenderer)->setDuplicateTexCoords(true); + enableProjectiveTexturing(); + } + + glActiveTexture(baseTexUnit); + + glEnable(GL_COLOR_MATERIAL); + glAlphaFunc(GL_GREATER, 0.5f); + + modelRenderer->begin(true, true, false); + } + //ambient and diffuse color is taken from cell color + + float fowFactor= fowTex->getPixmap()->getPixelf(pos.x / Map::cellScale, pos.y / Map::cellScale); + Vec4f color= Vec4f(Vec3f(fowFactor), 1.f); + glColor4fv(color.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color*ambFactor).ptr()); + glFogfv(GL_FOG_COLOR, (baseFogColor*fowFactor).ptr()); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(v.x, v.y, v.z); + glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + + objModel->updateInterpolationData(0.f, true); + modelRenderer->render(objModel); + + triangleCount+= objModel->getTriangleCount(); + pointCount+= objModel->getVertexCount(); + + glPopMatrix(); + } } } - } - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); -*/ + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + } assertGl(); } -void Renderer::renderWater(){ +void Renderer::renderWater() { bool closed= false; const World *world= game->getWorld(); @@ -1590,6 +1612,9 @@ void Renderer::renderWater(){ glEnable(GL_BLEND); if(textures3D){ Texture3D *waterTex= world->getTileset()->getWaterTex(); + if(waterTex == NULL) { + throw runtime_error("waterTex == NULL"); + } glEnable(GL_TEXTURE_3D); glBindTexture(GL_TEXTURE_3D, static_cast(waterTex)->getHandle()); } @@ -1707,93 +1732,15 @@ void Renderer::renderUnits(const int renderFps, const int worldFrameCount) { visibleFrameUnitList.clear(); bool modelRenderStarted = false; - int lastFactionIndex = -1; - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleUnitIndex = 0; visibleUnitIndex < qCache.visibleUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleUnitList[visibleUnitIndex]; - meshCallbackTeamColor.setTeamTexture(unit->getFaction()->getTexture()); + if(useQuadCache == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleQuadUnitList.size() > 0) { + for(int visibleUnitIndex = 0; + visibleUnitIndex < qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(modelRenderStarted == false) { - modelRenderStarted = true; - - glPushAttrib(GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); - glEnable(GL_COLOR_MATERIAL); - - if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD) { - if(shadows == sShadowMapping) { - glActiveTexture(shadowTexUnit); - glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - - static_cast(modelRenderer)->setDuplicateTexCoords(true); - enableProjectiveTexturing(); - } - } - glActiveTexture(baseTexUnit); - - modelRenderer->begin(true, true, true, &meshCallbackTeamColor); - } - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - //translate - Vec3f currVec= unit->getCurrVectorFlat(); - glTranslatef(currVec.x, currVec.y, currVec.z); - - //rotate - glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); - glRotatef(unit->getVerticalRotation(), 1.f, 0.f, 0.f); - - //dead alpha - float alpha= 1.0f; - const SkillType *st= unit->getCurrSkill(); - if(st->getClass() == scDie && static_cast(st)->getFade()) { - alpha= 1.0f-unit->getAnimProgress(); - glDisable(GL_COLOR_MATERIAL); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); - } - else{ - glEnable(GL_COLOR_MATERIAL); - } - - //render - const Model *model= unit->getCurrentModel(); - model->updateInterpolationData(unit->getAnimProgress(), unit->isAlive()); - - modelRenderer->render(model); - triangleCount+= model->getTriangleCount(); - pointCount+= model->getVertexCount(); - - glPopMatrix(); - unit->setVisible(true); - unit->setScreenPos(computeScreenPosition(unit->getCurrVectorFlat())); - visibleFrameUnitList.push_back(unit); - - if(allowRenderUnitTitles == true) { - // Add to the pending render unit title list - renderUnitTitleList.push_back(std::pair(unit,computeScreenPosition(unit->getCurrVectorFlat())) ); - } - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); - -/* - bool modelRenderStarted = false; - - for(int i=0; igetFactionCount(); ++i){ - meshCallbackTeamColor.setTeamTexture(world->getFaction(i)->getTexture()); - for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ - unit= world->getFaction(i)->getUnit(j); - if(world->toRenderUnit(unit, visibleQuad)) { + meshCallbackTeamColor.setTeamTexture(unit->getFaction()->getTexture()); if(modelRenderStarted == false) { modelRenderStarted = true; @@ -1802,7 +1749,7 @@ void Renderer::renderUnits(const int renderFps, const int worldFrameCount) { glEnable(GL_COLOR_MATERIAL); if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD) { - if(shadows==sShadowMapping){ + if(shadows == sShadowMapping) { glActiveTexture(shadowTexUnit); glEnable(GL_TEXTURE_2D); @@ -1831,12 +1778,12 @@ void Renderer::renderUnits(const int renderFps, const int worldFrameCount) { //dead alpha float alpha= 1.0f; const SkillType *st= unit->getCurrSkill(); - if(st->getClass()==scDie && static_cast(st)->getFade()){ + if(st->getClass() == scDie && static_cast(st)->getFade()) { alpha= 1.0f-unit->getAnimProgress(); glDisable(GL_COLOR_MATERIAL); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); } - else{ + else { glEnable(GL_COLOR_MATERIAL); } @@ -1858,99 +1805,107 @@ void Renderer::renderUnits(const int renderFps, const int worldFrameCount) { renderUnitTitleList.push_back(std::pair(unit,computeScreenPosition(unit->getCurrVectorFlat())) ); } } - else - { - unit->setVisible(false); + + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); } } - } - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); -*/ + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + } + else { + bool modelRenderStarted = false; + for(int i=0; igetFactionCount(); ++i){ + meshCallbackTeamColor.setTeamTexture(world->getFaction(i)->getTexture()); + for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ + unit= world->getFaction(i)->getUnit(j); + if(world->toRenderUnit(unit, visibleQuad)) { + + if(modelRenderStarted == false) { + modelRenderStarted = true; + + glPushAttrib(GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); + glEnable(GL_COLOR_MATERIAL); + + if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD) { + if(shadows==sShadowMapping){ + glActiveTexture(shadowTexUnit); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + + static_cast(modelRenderer)->setDuplicateTexCoords(true); + enableProjectiveTexturing(); + } + } + glActiveTexture(baseTexUnit); + + modelRenderer->begin(true, true, true, &meshCallbackTeamColor); + } + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + //translate + Vec3f currVec= unit->getCurrVectorFlat(); + glTranslatef(currVec.x, currVec.y, currVec.z); + + //rotate + glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); + glRotatef(unit->getVerticalRotation(), 1.f, 0.f, 0.f); + + //dead alpha + float alpha= 1.0f; + const SkillType *st= unit->getCurrSkill(); + if(st->getClass()==scDie && static_cast(st)->getFade()){ + alpha= 1.0f-unit->getAnimProgress(); + glDisable(GL_COLOR_MATERIAL); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); + } + else{ + glEnable(GL_COLOR_MATERIAL); + } + + //render + const Model *model= unit->getCurrentModel(); + model->updateInterpolationData(unit->getAnimProgress(), unit->isAlive()); + + modelRenderer->render(model); + triangleCount+= model->getTriangleCount(); + pointCount+= model->getVertexCount(); + + glPopMatrix(); + unit->setVisible(true); + unit->setScreenPos(computeScreenPosition(unit->getCurrVectorFlat())); + visibleFrameUnitList.push_back(unit); + + if(allowRenderUnitTitles == true) { + // Add to the pending render unit title list + renderUnitTitleList.push_back(std::pair(unit,computeScreenPosition(unit->getCurrVectorFlat())) ); + } + } + else + { + unit->setVisible(false); + } + } + } + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } + + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + } //assert assertGl(); } -void Renderer::renderUnitList(std::vector &vctEntity,MeshCallbackTeamColor *meshCallbackTeamColor,const int renderFps, const int worldFrameCount) { - for(int idx=0; idx < vctEntity.size(); ++idx) { - RenderEntity &entity = vctEntity[idx]; - prepareUnitForRender(entity); - renderUnit(entity,meshCallbackTeamColor,renderFps, worldFrameCount); - } -} - -void Renderer::renderUnit(RenderEntity &entity,MeshCallbackTeamColor *meshCallbackTeamColor,const int renderFps, const int worldFrameCount) { - Unit *unit = entity.unit; - if(unit != NULL) { - renderUnit(unit,meshCallbackTeamColor, entity.teamTexture, worldFrameCount); - entity.setState(resRendered); - } -} - -void Renderer::renderUnit(Unit *unit,MeshCallbackTeamColor *meshCallbackTeamColor, const Texture2D *teamTexture, const int worldFrameCount) { - if(unit != NULL) { - if(meshCallbackTeamColor != NULL) { - meshCallbackTeamColor->setTeamTexture(teamTexture); - } - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - //translate - Vec3f currVec= unit->getCurrVectorFlat(); - glTranslatef(currVec.x, currVec.y, currVec.z); - - //rotate - glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); - glRotatef(unit->getVerticalRotation(), 1.f, 0.f, 0.f); - - //dead alpha - float alpha= 1.0f; - const SkillType *st= unit->getCurrSkill(); - if(st->getClass()==scDie && static_cast(st)->getFade()){ - alpha= 1.0f-unit->getAnimProgress(); - glDisable(GL_COLOR_MATERIAL); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); - } - else{ - glEnable(GL_COLOR_MATERIAL); - } - - //render - const Model *model= unit->getCurrentModel(); - //model->updateInterpolationData(unit->getAnimProgress(), unit->isAlive()); - - modelRenderer->render(model); - unit->setLastRenderFrame(worldFrameCount); - triangleCount+= model->getTriangleCount(); - pointCount+= model->getVertexCount(); - - glPopMatrix(); - unit->setVisible(true); - - if(allowRenderUnitTitles == true) { - // Add to the pending render unit title list - renderUnitTitleList.push_back(std::pair(unit,computeScreenPosition(unit->getCurrVectorFlat())) ); - } - } -} - - -void Renderer::prepareUnitForRender(RenderEntity &entity) { - if(entity.unit != NULL) { - const Model *model= entity.unit->getCurrentModel(); - model->updateInterpolationData(entity.unit->getAnimProgress(), entity.unit->isAlive()); - } - entity.setState(resInterpolated); -} - void Renderer::renderSelectionEffects(){ const World *world= game->getWorld(); @@ -1985,7 +1940,7 @@ void Renderer::renderSelectionEffects(){ glColor4f(0, unit->getHpRatio(), 0, 0.3f); } } - else if ( world->getThisTeamIndex() == unit->getTeam()){ + else if ( world->getThisTeamIndex() == unit->getTeam()) { glColor4f(unit->getHpRatio(), unit->getHpRatio(), 0, 0.3f); } else{ @@ -1994,24 +1949,24 @@ void Renderer::renderSelectionEffects(){ renderSelectionCircle(currVec, unit->getType()->getSize(), selectionCircleRadius); //magic circle - if(world->getThisFactionIndex()==unit->getFactionIndex() && unit->getType()->getMaxEp()>0){ + if(world->getThisFactionIndex() == unit->getFactionIndex() && unit->getType()->getMaxEp() > 0) { glColor4f(unit->getEpRatio()/2.f, unit->getEpRatio(), unit->getEpRatio(), 0.5f); renderSelectionCircle(currVec, unit->getType()->getSize(), magicCircleRadius); } } //target arrow - if(selection->getCount()==1){ + if(selection->getCount() == 1) { const Unit *unit= selection->getUnit(0); //comand arrow - if(focusArrows && unit->anyCommand()){ + if(focusArrows && unit->anyCommand()) { const CommandType *ct= unit->getCurrCommand()->getCommandType(); - if(ct->getClicks()!=cOne){ + if(ct->getClicks() != cOne){ //arrow color Vec3f arrowColor; - switch(ct->getClass()){ + switch(ct->getClass()) { case ccMove: arrowColor= Vec3f(0.f, 1.f, 0.f); break; @@ -2026,10 +1981,10 @@ void Renderer::renderSelectionEffects(){ //arrow target Vec3f arrowTarget; Command *c= unit->getCurrCommand(); - if(c->getUnit()!=NULL){ + if(c->getUnit() != NULL) { arrowTarget= c->getUnit()->getCurrVectorFlat(); } - else{ + else { Vec2i pos= c->getPos(); arrowTarget= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); } @@ -2039,7 +1994,7 @@ void Renderer::renderSelectionEffects(){ } //meeting point arrow - if(unit->getType()->getMeetingPoint()){ + if(unit->getType()->getMeetingPoint()) { Vec2i pos= unit->getMeetingPos(); Vec3f arrowTarget= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); renderArrow(unit->getCurrVectorFlat(), arrowTarget, Vec3f(0.f, 0.f, 1.f), 0.3f); @@ -2048,13 +2003,13 @@ void Renderer::renderSelectionEffects(){ } //render selection hightlights - for(int i=0; igetFactionCount(); ++i){ - for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ + for(int i=0; i < world->getFactionCount(); ++i) { + for(int j=0; j < world->getFaction(i)->getUnitCount(); ++j) { const Unit *unit= world->getFaction(i)->getUnit(j); - if(unit->isHighlighted()){ + if(unit->isHighlighted()) { float highlight= unit->getHightlight(); - if(game->getWorld()->getThisFactionIndex()==unit->getFactionIndex()){ + if(game->getWorld()->getThisFactionIndex() == unit->getFactionIndex()) { glColor4f(0.f, 1.f, 0.f, highlight); } else{ @@ -2189,46 +2144,48 @@ void Renderer::renderMinimap(){ glDisable(GL_TEXTURE_2D); //draw units + if(useQuadCache == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleUnitList.size() > 0) { + for(int visibleIndex = 0; + visibleIndex < qCache.visibleUnitList.size(); ++visibleIndex) { + Unit *unit = qCache.visibleUnitList[visibleIndex]; - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleUnitList.size() > 0) { - glBegin(GL_QUADS); - - for(int visibleUnitIndex = 0; visibleUnitIndex < qCache.visibleUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleUnitList[visibleUnitIndex]; - - Vec2i pos= unit->getPos() / Map::cellScale; - int size= unit->getType()->getSize(); - Vec3f color= unit->getFaction()->getTexture()->getPixmap()->getPixel3f(0, 0); - glColor3fv(color.ptr()); - glVertex2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); - glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - (pos.y*zoom.y)); - glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - ((pos.y+size)*zoom.y)); - glVertex2f(mx + pos.x*zoom.x, my + mh - ((pos.y+size)*zoom.y)); - } - - glEnd(); - } - -/* - glBegin(GL_QUADS); - for(int i=0; igetFactionCount(); ++i){ - for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ - Unit *unit= world->getFaction(i)->getUnit(j); - if(world->toRenderUnit(unit)){ - Vec2i pos= unit->getPos()/Map::cellScale; + Vec2i pos= unit->getPos() / Map::cellScale; int size= unit->getType()->getSize(); - Vec3f color= world->getFaction(i)->getTexture()->getPixmap()->getPixel3f(0, 0); + Vec3f color= unit->getFaction()->getTexture()->getPixmap()->getPixel3f(0, 0); glColor3fv(color.ptr()); + + glBegin(GL_QUADS); + glVertex2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - (pos.y*zoom.y)); glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - ((pos.y+size)*zoom.y)); glVertex2f(mx + pos.x*zoom.x, my + mh - ((pos.y+size)*zoom.y)); + + glEnd(); } } } - glEnd(); -*/ + else { + glBegin(GL_QUADS); + for(int i=0; igetFactionCount(); ++i){ + for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ + Unit *unit= world->getFaction(i)->getUnit(j); + if(world->toRenderUnit(unit)){ + Vec2i pos= unit->getPos()/Map::cellScale; + int size= unit->getType()->getSize(); + Vec3f color= world->getFaction(i)->getTexture()->getPixmap()->getPixel3f(0, 0); + glColor3fv(color.ptr()); + glVertex2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); + glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - (pos.y*zoom.y)); + glVertex2f(mx + (pos.x+1)*zoom.x+size, my + mh - ((pos.y+size)*zoom.y)); + glVertex2f(mx + pos.x*zoom.x, my + mh - ((pos.y+size)*zoom.y)); + } + } + } + glEnd(); + } //draw camera float wRatio= static_cast(metrics.getMinimapW()) / world->getMap()->getW(); @@ -2241,31 +2198,35 @@ void Renderer::renderMinimap(){ glEnable(GL_BLEND); + int x1 = 0; + int y1 = 0; +#ifdef USE_STREFLOP + x1 = mx + x + static_cast(20*streflop::sin(ang-pi/5)); + y1 = my + mh - (y-static_cast(20*streflop::cos(ang-pi/5))); +#else + x1 = mx + x + static_cast(20*sin(ang-pi/5)); + y1 = my + mh - (y-static_cast(20*cos(ang-pi/5))); +#endif + + int x2 = 0; + int y2 = 0; +#ifdef USE_STREFLOP + x2 = mx + x + static_cast(20*streflop::sin(ang+pi/5)); + y2 = my + mh - (y-static_cast(20*streflop::cos(ang+pi/5))); +#else + x2 = mx + x + static_cast(20*sin(ang+pi/5)); + y2 = my + mh - (y-static_cast(20*cos(ang+pi/5))); +#endif + glColor4f(1.f, 1.f, 1.f, 1.f); glBegin(GL_TRIANGLES); glVertex2i(mx+x, my+mh-y); + glColor4f(1.f, 1.f, 1.f, 0.0f); + glVertex2i(x1,y1); + glColor4f(1.f, 1.f, 1.f, 0.0f); + glVertex2i(x2,y2); - glColor4f(1.f, 1.f, 1.f, 0.0f); -#ifdef USE_STREFLOP - glVertex2i( - mx + x + static_cast(20*streflop::sin(ang-pi/5)), - my + mh - (y-static_cast(20*streflop::cos(ang-pi/5)))); -#else - glVertex2i( - mx + x + static_cast(20*sin(ang-pi/5)), - my + mh - (y-static_cast(20*cos(ang-pi/5)))); -#endif - glColor4f(1.f, 1.f, 1.f, 0.0f); -#ifdef USE_STREFLOP - glVertex2i( - mx + x + static_cast(20*streflop::sin(ang+pi/5)), - my + mh - (y-static_cast(20*streflop::cos(ang+pi/5)))); -#else - glVertex2i( - mx + x + static_cast(20*sin(ang+pi/5)), - my + mh - (y-static_cast(20*cos(ang+pi/5)))); -#endif glEnd(); glPopAttrib(); @@ -2279,7 +2240,7 @@ void Renderer::renderDisplay(){ const Display *display= game->getGui()->getDisplay(); glPushAttrib(GL_ENABLE_BIT); - + //infoString renderTextShadow( display->getInfoText().c_str(), @@ -2404,14 +2365,14 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ //characters float dist= menuBackground->getAboutPosition().dist(cameraPosition); float minDist= 3.f; - if(distbegin(true, true, false); - for(int i=0; igetWater()){ + if(menuBackground->getWater()) { //water surface const int waterTesselation= 10; @@ -2438,20 +2399,20 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.f, 1.f, 1.f, 1.f).ptr()); GLuint waterHandle= static_cast(menuBackground->getWaterTexture())->getHandle(); glBindTexture(GL_TEXTURE_2D, waterHandle); - for(int i=1; igetRain()){ + if(menuBackground->getRain()) { const float maxRaindropAlpha= 0.5f; glEnable(GL_BLEND); @@ -2462,7 +2423,7 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ //splashes CoreData &coreData= CoreData::getInstance(); glBindTexture(GL_TEXTURE_2D, static_cast(coreData.getWaterSplashTexture())->getHandle()); - for(int i=0; igetRaindropPos(i); float scale= menuBackground->getRaindropState(i); @@ -2568,7 +2529,8 @@ Vec3f Renderer::computeScreenPosition(const Vec3f &worldPos) { return screenPos; } -void Renderer::computeSelected(Selection::UnitContainer &units, const Vec2i &posDown, const Vec2i &posUp){ +void Renderer::computeSelected( Selection::UnitContainer &units, + const Vec2i &posDown, const Vec2i &posUp) { //declarations GLuint selectBuffer[Gui::maxSelBuff]; @@ -2593,28 +2555,40 @@ void Renderer::computeSelected(Selection::UnitContainer &units, const Vec2i &pos gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); loadGameCameraMatrix(); - //render units + //render units to find which ones should be selected renderUnitsFast(); //pop matrices glMatrixMode(GL_PROJECTION); glPopMatrix(); - //select units - const World *world= game->getWorld(); + //select units by checking the selected buffer int selCount= glRenderMode(GL_RENDER); - for(int i=1; i <= selCount; ++i) { - int factionIndex= selectBuffer[i*5-2]; - int unitIndex= selectBuffer[i*5-1]; + if(selCount > 0) { + const World *world= game->getWorld(); + for(int i=1; i <= selCount; ++i) { + int factionIndex= selectBuffer[i*5-2]; + //int unitIndex= selectBuffer[i*5-1]; + int unitId= selectBuffer[i*5-1]; - if( factionIndex >= 0 && factionIndex < world->getFactionCount() && - unitIndex >= 0 && unitIndex < world->getFaction(factionIndex)->getUnitCount()) { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] factionIndex = %d, unitId = %d, selCount = %d\n",__FILE__,__FUNCTION__,__LINE__,factionIndex,unitId,selCount); - Unit *unit= world->getFaction(factionIndex)->getUnit(unitIndex); - if(unit->isAlive()) { - units.push_back(unit); + //if( factionIndex >= 0 && factionIndex < world->getFactionCount() && + // unitIndex >= 0 && unitIndex < world->getFaction(factionIndex)->getUnitCount()) { + // Unit *unit= world->getFaction(factionIndex)->getUnit(unitIndex); + if( factionIndex >= 0 && factionIndex < world->getFactionCount() && + unitId >= 0) { + + const Faction *faction= world->getFaction(factionIndex); + if(faction != NULL) { + Unit *unit = faction->findUnit(unitId); + if(unit != NULL && unit->isAlive()) { + units.push_back(unit); + } + } } } + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] units.size() = %d, selCount = %d\n",__FILE__,__FUNCTION__,__LINE__,units.size(),selCount); } } @@ -2622,12 +2596,11 @@ void Renderer::computeSelected(Selection::UnitContainer &units, const Vec2i &pos void Renderer::renderShadowsToTexture(const int renderFps){ if(lastRenderFps >= MIN_FPS_NORMAL_RENDERING && renderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD && - (shadows==sProjected || shadows==sShadowMapping)) { + (shadows == sProjected || shadows == sShadowMapping)) { shadowMapFrame= (shadowMapFrame + 1) % (shadowFrameSkip + 1); - if(shadowMapFrame==0){ - + if(shadowMapFrame == 0){ assertGl(); glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT | GL_POLYGON_BIT); @@ -2701,7 +2674,7 @@ void Renderer::renderShadowsToTexture(const int renderFps){ glTranslatef(-nearestLightPos.x, -nearestLightPos.y-2, -nearestLightPos.z); } - if(shadows==sShadowMapping){ + if(shadows == sShadowMapping) { glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.0f, 0.001f); } @@ -2896,14 +2869,14 @@ void Renderer::saveScreen(const string &path){ // ==================== PRIVATE ==================== -float Renderer::computeSunAngle(float time){ +float Renderer::computeSunAngle(float time) { float dayTime= TimeFlow::dusk-TimeFlow::dawn; float fTime= (time-TimeFlow::dawn)/dayTime; return clamp(fTime*pi, pi/8.f, 7.f*pi/8.f); } -float Renderer::computeMoonAngle(float time){ +float Renderer::computeMoonAngle(float time) { float nightTime= 24-(TimeFlow::dusk-TimeFlow::dawn); if(timegetWorld()->getTileset(); Vec3f color; @@ -2942,26 +2915,26 @@ Vec3f Renderer::computeLightColor(float time){ const float nightStart= TimeFlow::dusk; const float nightEnd= TimeFlow::dawn-transition; - if(time>dayStart && timedayStart && timegetSunLightColor(); } - else if(time>nightStart || timenightStart || timegetMoonLightColor(); } - else if(time>=dayEnd && time<=nightStart){ + else if(time>=dayEnd && time<=nightStart) { color= tileset->getSunLightColor().lerp((time-dayEnd)/transition, tileset->getMoonLightColor()); } - else if(time>=nightEnd && time<=dayStart){ + else if(time>=nightEnd && time<=dayStart) { color= tileset->getMoonLightColor().lerp((time-nightEnd)/transition, tileset->getSunLightColor()); } - else{ + else { assert(false); color= tileset->getSunLightColor(); } return color; } -Vec4f Renderer::computeWaterColor(float waterLevel, float cellHeight){ +Vec4f Renderer::computeWaterColor(float waterLevel, float cellHeight) { const float waterFactor= 1.5f; return Vec4f(1.f, 1.f, 1.f, clamp((waterLevel-cellHeight)*waterFactor, 0.f, 1.f)); } @@ -2969,7 +2942,7 @@ Vec4f Renderer::computeWaterColor(float waterLevel, float cellHeight){ // ==================== fast render ==================== //render units for selection purposes -void Renderer::renderUnitsFast(){ +void Renderer::renderUnitsFast() { assert(game != NULL); const World *world= game->getWorld(); assert(world != NULL); @@ -2979,71 +2952,14 @@ void Renderer::renderUnitsFast(){ bool modelRenderStarted = false; bool modelRenderFactionStarted = false; - int lastFactionIndex = -1; - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleUnitIndex = 0; visibleUnitIndex < qCache.visibleUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleUnitList[visibleUnitIndex]; + if(useQuadCache == true) { + int lastFactionIndex = -1; + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleQuadUnitList.size() > 0) { + for(int visibleUnitIndex = 0; + visibleUnitIndex < qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(lastFactionIndex != unit->getFactionIndex()) { - modelRenderFactionStarted = false; - } - if(modelRenderStarted == false) { - modelRenderStarted = true; - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_TEXTURE_2D); - glDisable(GL_LIGHTING); - - modelRenderer->begin(false, false, false); - glInitNames(); - } - - if(modelRenderFactionStarted == false) { - modelRenderFactionStarted = true; - glPushName(unit->getFactionIndex()); - } - glPushName(visibleUnitIndex); - - 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 - const Model *model= unit->getCurrentModel(); - model->updateInterpolationVertices(unit->getAnimProgress(), unit->isAlive()); - modelRenderer->render(model); - - glPopMatrix(); - glPopName(); - - if((lastFactionIndex != -1 && lastFactionIndex != unit->getFactionIndex()) || - (visibleUnitIndex == qCache.visibleUnitList.size() - 1)) { - glPopName(); - } - - lastFactionIndex = unit->getFactionIndex(); - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - -/* - bool modelRenderStarted = false; - bool modelRenderFactionStarted = false; - for(int i=0; igetFactionCount(); ++i){ - modelRenderFactionStarted = false; - for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ - Unit *unit= world->getFaction(i)->getUnit(j); - if(world->toRenderUnit(unit, visibleQuad)) { if(modelRenderStarted == false) { modelRenderStarted = true; glPushAttrib(GL_ENABLE_BIT); @@ -3053,11 +2969,21 @@ void Renderer::renderUnitsFast(){ modelRenderer->begin(false, false, false); glInitNames(); } - if(modelRenderFactionStarted == false) { + + if(lastFactionIndex != unit->getFactionIndex()) { + if(lastFactionIndex != -1) { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] calling glPopName() for lastFactionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,lastFactionIndex); + glPopName(); + } + modelRenderFactionStarted = true; - glPushName(i); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] calling glPushName() for lastFactionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unit->getFactionIndex()); + // Used for selection lookup in computeSelected + glPushName(unit->getFactionIndex()); } - glPushName(j); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] calling glPushName() for unit->getId() = %d\n",__FILE__,__FUNCTION__,__LINE__,unit->getId()); + // Used for selection lookup in computeSelected + glPushName(unit->getId()); glMatrixMode(GL_MODELVIEW); @@ -3077,32 +3003,80 @@ void Renderer::renderUnitsFast(){ modelRenderer->render(model); glPopMatrix(); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] calling glPopName() for unit->getId() = %d\n",__FILE__,__FUNCTION__,__LINE__,unit->getId()); + glPopName(); + + lastFactionIndex = unit->getFactionIndex(); + } + + if(modelRenderFactionStarted == true) { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] calling glPopName() for lastFactionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,lastFactionIndex); + glPopName(); + } + + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } + } + } + else { + bool modelRenderStarted = false; + bool modelRenderFactionStarted = false; + for(int i=0; igetFactionCount(); ++i){ + modelRenderFactionStarted = false; + for(int j=0; jgetFaction(i)->getUnitCount(); ++j){ + Unit *unit= world->getFaction(i)->getUnit(j); + if(world->toRenderUnit(unit, visibleQuad)) { + if(modelRenderStarted == false) { + modelRenderStarted = true; + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + + modelRenderer->begin(false, false, false); + glInitNames(); + } + if(modelRenderFactionStarted == false) { + modelRenderFactionStarted = true; + glPushName(i); + } + glPushName(j); + + 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 + const Model *model= unit->getCurrentModel(); + model->updateInterpolationVertices(unit->getAnimProgress(), unit->isAlive()); + modelRenderer->render(model); + + glPopMatrix(); + glPopName(); + } + } + if(modelRenderFactionStarted == true) { glPopName(); } } - if(modelRenderFactionStarted == true) { - glPopName(); + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); } } - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } -*/ assertGl(); } -void Renderer::prepareUnitFastForRender(RenderEntity &entity) { - if(entity.unit != NULL) { - const Model *model= entity.unit->getCurrentModel(); - if(model != NULL) { - model->updateInterpolationVertices(entity.unit->getAnimProgress(), entity.unit->isAlive()); - } - } - entity.setState(resInterpolated); -} - //render objects for selection purposes void Renderer::renderObjectsFast() { const World *world= game->getWorld(); @@ -3110,70 +3084,16 @@ void Renderer::renderObjectsFast() { assertGl(); - int thisTeamIndex= world->getThisTeamIndex(); bool modelRenderStarted = false; - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleIndex = 0; visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { - Object *o = qCache.visibleObjectList[visibleIndex]; + if(useQuadCache == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleObjectList.size() > 0) { + for(int visibleIndex = 0; + visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { + Object *o = qCache.visibleObjectList[visibleIndex]; - const Model *objModel= o->getModel(); - const Vec3f &v= o->getConstPos(); - - if(modelRenderStarted == false) { - modelRenderStarted = true; - glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); - glDisable(GL_LIGHTING); - - glAlphaFunc(GL_GREATER, 0.5f); - - 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); - - modelRenderer->begin(false, true, false); - } - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glTranslatef(v.x, v.y, v.z); - glRotatef(o->getRotation(), 0.f, 1.f, 0.f); - - modelRenderer->render(objModel); - - glPopMatrix(); - - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - -/* - int thisTeamIndex= world->getThisTeamIndex(); - bool modelRenderStarted = false; - - PosQuadIterator pqi(map, visibleQuad, Map::cellScale); - while(pqi.next()){ - const Vec2i &pos= pqi.getPos(); - if(map->isInside(pos)){ - const Vec2i &mapPos = Map::toSurfCoords(pos); - SurfaceCell *sc= map->getSurfaceCell(mapPos); - Object *o= sc->getObject(); - bool isExplored = (sc->isExplored(thisTeamIndex) && o!=NULL); - //bool isVisible = (sc->isVisible(thisTeamIndex) && o!=NULL); - bool isVisible = true; - - if(isExplored == true && isVisible == true) { - const Model *objModel= sc->getObject()->getModel(); + const Model *objModel= o->getModel(); const Vec3f &v= o->getConstPos(); if(modelRenderStarted == false) { @@ -3181,7 +3101,7 @@ void Renderer::renderObjectsFast() { glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); glDisable(GL_LIGHTING); - glAlphaFunc(GL_GREATER, 0.5f); + glAlphaFunc(GL_GREATER, 0.5f); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); @@ -3206,46 +3126,85 @@ void Renderer::renderObjectsFast() { glPopMatrix(); -// vctEntity.push_back(RenderEntity(o,mapPos)); + } + + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); } } } + else { + int thisTeamIndex= world->getThisTeamIndex(); + bool modelRenderStarted = false; -// modelRenderer->begin(false, true, false); -// renderObjectFastList(vctEntity); - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); + PosQuadIterator pqi(visibleQuad, Map::cellScale); + while(pqi.next()) { + const Vec2i &pos= pqi.getPos(); + if(map->isInside(pos)){ + const Vec2i &mapPos = Map::toSurfCoords(pos); + SurfaceCell *sc= map->getSurfaceCell(mapPos); + Object *o= sc->getObject(); + bool isExplored = (sc->isExplored(thisTeamIndex) && o!=NULL); + //bool isVisible = (sc->isVisible(thisTeamIndex) && o!=NULL); + bool isVisible = true; + + if(isExplored == true && isVisible == true) { + const Model *objModel= sc->getObject()->getModel(); + const Vec3f &v= o->getConstPos(); + + if(modelRenderStarted == false) { + modelRenderStarted = true; + glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); + glDisable(GL_LIGHTING); + + glAlphaFunc(GL_GREATER, 0.5f); + + 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); + + modelRenderer->begin(false, true, false); + } + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(v.x, v.y, v.z); + glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + + modelRenderer->render(objModel); + + glPopMatrix(); + + // vctEntity.push_back(RenderEntity(o,mapPos)); + } + } + } + + // modelRenderer->begin(false, true, false); + // renderObjectFastList(vctEntity); + if(modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } } -*/ assertGl(); } -void Renderer::renderObjectFastList(std::vector &vctEntity) { - for(int idx=0; idx < vctEntity.size(); ++idx) { - RenderEntity &entity = vctEntity[idx]; - - const Model *objModel= entity.o->getModel(); - Vec3f v= entity.o->getPos(); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glTranslatef(v.x, v.y, v.z); - glRotatef(entity.o->getRotation(), 0.f, 1.f, 0.f); - - modelRenderer->render(objModel); - - glPopMatrix(); - } -} - // ==================== gl caps ==================== -void Renderer::checkGlCaps(){ +void Renderer::checkGlCaps() { //opengl 1.3 - if(!isGlVersionSupported(1, 3, 0)){ + if(!isGlVersionSupported(1, 3, 0)) { string message; message += "Your system supports OpenGL version \""; @@ -3262,24 +3221,24 @@ void Renderer::checkGlCaps(){ } } -void Renderer::checkGlOptionalCaps(){ +void Renderer::checkGlOptionalCaps() { //shadows - if(shadows==sProjected || shadows==sShadowMapping){ - if(getGlMaxTextureUnits()<3){ + if(shadows == sProjected || shadows == sShadowMapping) { + if(getGlMaxTextureUnits() < 3) { throw runtime_error("Your system doesn't support 3 texture units, required for shadows"); } } //shadow mapping - if(shadows==sShadowMapping){ + if(shadows == sShadowMapping) { checkExtension("GL_ARB_shadow", "Shadow Mapping"); checkExtension("GL_ARB_shadow_ambient", "Shadow Mapping"); } } -void Renderer::checkExtension(const string &extension, const string &msg){ - if(!isGlExtensionSupported(extension.c_str())){ +void Renderer::checkExtension(const string &extension, const string &msg) { + if(!isGlExtensionSupported(extension.c_str())) { string str= "OpenGL extension not supported: " + extension + ", required for " + msg; throw runtime_error(str); } @@ -3287,7 +3246,7 @@ void Renderer::checkExtension(const string &extension, const string &msg){ // ==================== init 3d lists ==================== -void Renderer::init3dList(){ +void Renderer::init3dList() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -3373,15 +3332,15 @@ void Renderer::init3dList(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(tileset != NULL && tileset->getFog()){ + if(tileset != NULL && tileset->getFog()) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); glEnable(GL_FOG); - if(tileset->getFogMode()==fmExp){ + if(tileset->getFogMode()==fmExp) { glFogi(GL_FOG_MODE, GL_EXP); } - else{ + else { glFogi(GL_FOG_MODE, GL_EXP2); } @@ -3400,7 +3359,7 @@ void Renderer::init3dList(){ } -void Renderer::init2dList(){ +void Renderer::init2dList() { const Metrics &metrics= Metrics::getInstance(); @@ -3442,7 +3401,7 @@ void Renderer::init2dList(){ assertGl(); } -void Renderer::init3dListMenu(const MainMenu *mm){ +void Renderer::init3dListMenu(const MainMenu *mm) { assertGl(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -3525,7 +3484,7 @@ void Renderer::init3dListMenu(const MainMenu *mm){ // ==================== misc ==================== -void Renderer::loadProjectionMatrix(){ +void Renderer::loadProjectionMatrix() { GLdouble clipping; const Metrics &metrics= Metrics::getInstance(); @@ -3540,7 +3499,7 @@ void Renderer::loadProjectionMatrix(){ assertGl(); } -void Renderer::enableProjectiveTexturing(){ +void Renderer::enableProjectiveTexturing() { glTexGenfv(GL_S, GL_EYE_PLANE, &shadowMapMatrix[0]); glTexGenfv(GL_T, GL_EYE_PLANE, &shadowMapMatrix[4]); glTexGenfv(GL_R, GL_EYE_PLANE, &shadowMapMatrix[8]); @@ -3553,7 +3512,7 @@ void Renderer::enableProjectiveTexturing(){ // ==================== private aux drawing ==================== -void Renderer::renderSelectionCircle(Vec3f v, int size, float radius){ +void Renderer::renderSelectionCircle(Vec3f v, int size, float radius) { GLUquadricObj *disc; glMatrixMode(GL_MODELVIEW); @@ -3569,7 +3528,8 @@ void Renderer::renderSelectionCircle(Vec3f v, int size, float radius){ glPopMatrix(); } -void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &color, float width){ +void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, + const Vec3f &color, float width) { const int tesselation= 3; const float arrowEndSize= 0.4f; const float maxlen= 25; @@ -3578,7 +3538,7 @@ void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &co Vec3f dir= Vec3f(pos2-pos1); float len= dir.length(); - if(len>maxlen){ + if(len>maxlen) { return; } float alphaFactor= clamp((maxlen-len)/blendDelay, 0.f, 1.f); @@ -3592,18 +3552,19 @@ void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &co Vec3f pos1Right= pos1 - normal*(width+0.05f); //arrow body - glBegin(GL_TRIANGLE_STRIP); - for(int i=0; i<=tesselation; ++i){ + for(int i=0; i<=tesselation; ++i) { float t= static_cast(i)/tesselation; Vec3f a= pos1Left.lerp(t, pos2Left); Vec3f b= pos1Right.lerp(t, pos2Right); Vec4f c= Vec4f(color, t*0.25f*alphaFactor); glColor4fv(c.ptr()); + + glBegin(GL_TRIANGLE_STRIP); glVertex3fv(a.ptr()); glVertex3fv(b.ptr()); + glEnd(); } - glEnd(); //arrow end glBegin(GL_TRIANGLES); @@ -3613,7 +3574,7 @@ void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &co glEnd(); } -void Renderer::renderProgressBar(int size, int x, int y, Font2D *font){ +void Renderer::renderProgressBar(int size, int x, int y, Font2D *font) { //bar glBegin(GL_QUADS); @@ -3645,7 +3606,7 @@ void Renderer::renderProgressBar(int size, int x, int y, Font2D *font){ } -void Renderer::renderTile(const Vec2i &pos){ +void Renderer::renderTile(const Vec2i &pos) { const Map *map= game->getWorld()->getMap(); Vec2i scaledPos= pos * Map::cellScale; @@ -3655,8 +3616,8 @@ void Renderer::renderTile(const Vec2i &pos){ glTranslatef(-0.5f, 0.f, -0.5f); glInitNames(); - for(int i=0; igetCell(renderPos.x, renderPos.y)->getHeight(); + float h2 = map->getCell(renderPos.x, renderPos.y+1)->getHeight(); + float h3 = map->getCell(renderPos.x+1, renderPos.y)->getHeight(); + float h4 = map->getCell(renderPos.x+1, renderPos.y+1)->getHeight(); + glBegin(GL_TRIANGLE_STRIP); glVertex3f( static_cast(renderPos.x), - map->getCell(renderPos.x, renderPos.y)->getHeight(), + h1, static_cast(renderPos.y)); glVertex3f( static_cast(renderPos.x), - map->getCell(renderPos.x, renderPos.y+1)->getHeight(), + h2, static_cast(renderPos.y+1)); glVertex3f( static_cast(renderPos.x+1), - map->getCell(renderPos.x+1, renderPos.y)->getHeight(), + h3, static_cast(renderPos.y)); glVertex3f( static_cast(renderPos.x+1), - map->getCell(renderPos.x+1, renderPos.y+1)->getHeight(), + h4, static_cast(renderPos.y+1)); glEnd(); @@ -3692,7 +3658,7 @@ void Renderer::renderTile(const Vec2i &pos){ glPopMatrix(); } -void Renderer::renderQuad(int x, int y, int w, int h, const Texture2D *texture){ +void Renderer::renderQuad(int x, int y, int w, int h, const Texture2D *texture) { glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); glBegin(GL_TRIANGLE_STRIP); glTexCoord2i(0, 1); @@ -3783,40 +3749,53 @@ void Renderer::renderUnitTitles(Font2D *font, Vec3f color) { } } -VisibleQuadContainerCache & Renderer::getQuadCache(bool updateOnDirtyFrame) { +VisibleQuadContainerCache & Renderer::getQuadCache( bool updateOnDirtyFrame, + bool forceNew) { + //forceNew = true; if(game != NULL && game->getWorld() != NULL) { const World *world= game->getWorld(); - if(updateOnDirtyFrame == true && + + if(forceNew == true || + (updateOnDirtyFrame == true && (world->getFrameCount() != quadCache.cacheFrame || - visibleQuad != quadCache.lastVisibleQuad)) { + visibleQuad != quadCache.lastVisibleQuad))) { // Dump cached info - if(visibleQuad != quadCache.lastVisibleQuad) { - quadCache.clearCacheData(); - } - else { - quadCache.clearVolatileCacheData(); - } + //if(forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { + //quadCache.clearCacheData(); + //} + //else { + quadCache.clearVolatileCacheData(); + //} // Unit calculations for(int i = 0; i < world->getFactionCount(); ++i) { - for(int j = 0; j < world->getFaction(i)->getUnitCount(); ++j) { - Unit *unit= world->getFaction(i)->getUnit(j); + const Faction *faction = world->getFaction(i); + for(int j = 0; j < faction->getUnitCount(); ++j) { + Unit *unit= faction->getUnit(j); if(world->toRenderUnit(unit, visibleQuad)) { - quadCache.visibleUnitList.push_back(unit); + quadCache.visibleQuadUnitList.push_back(unit); } else { unit->setVisible(false); - quadCache.inVisibleUnitList.push_back(unit); + // Currently don't need this list + //quadCache.inVisibleUnitList.push_back(unit); + } + + if(world->toRenderUnit(unit)) { + quadCache.visibleUnitList.push_back(unit); } } } - if(visibleQuad != quadCache.lastVisibleQuad) { + if(forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { // Object calculations const Map *map= world->getMap(); - PosQuadIterator pqi(map, visibleQuad, Map::cellScale); - while(pqi.next()){ + + quadCache.clearNonVolatileCacheData(); + + PosQuadIterator pqi(visibleQuad, Map::cellScale); + while(pqi.next()) { const Vec2i &pos= pqi.getPos(); if(map->isInside(pos)) { const Vec2i &mapPos = Map::toSurfCoords(pos); @@ -3835,18 +3814,18 @@ VisibleQuadContainerCache & Renderer::getQuadCache(bool updateOnDirtyFrame) { } } + const Rect2i mapBounds(0, 0, map->getSurfaceW()-1, map->getSurfaceH()-1); Quad2i scaledQuad = visibleQuad / Map::cellScale; - PosQuadIterator pqis(map, scaledQuad); + PosQuadIterator pqis(scaledQuad); while(pqis.next()) { const Vec2i &pos= pqis.getPos(); - if(map->isInside(pos)) { + if(mapBounds.isInside(pos)) { quadCache.visibleScaledCellList.push_back(pos); } } - - quadCache.lastVisibleQuad = visibleQuad; } quadCache.cacheFrame = world->getFrameCount(); + quadCache.lastVisibleQuad = visibleQuad; } } diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 0bcbf3f5..f414fe8c 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -74,75 +74,6 @@ class Object; /// OpenGL renderer, uses the shared library // =========================================================== -enum RenderEntityState { - resNone, - resInterpolated, - resRendered -}; - -enum RenderEntityType { - retObject, - retUnit, - retUnitFast -}; - -class RenderEntity { -protected: - RenderEntityState state; - - void CopyAll(const RenderEntity &obj) { - this->type = obj.type; - this->state = obj.state; - this->o = obj.o; - this->mapPos = obj.mapPos; - this->unit = obj.unit; - this->teamTexture = obj.teamTexture;; - } - -public: - - static bool forceRenderWithIterpolation; - - RenderEntity() { - this->type = retObject; - this->o = NULL; - this->unit = NULL; - this->teamTexture = NULL; - setState(resNone); - } - RenderEntity(RenderEntityType type,Object *o, Vec2i mapPos, Unit *unit,const Texture2D *teamTexture=NULL) { - this->type = type; - this->o = o; - this->mapPos = mapPos; - this->unit = unit; - this->teamTexture = teamTexture; - setState(resNone); - } - RenderEntity(const RenderEntity &obj) { - CopyAll(obj); - } - RenderEntity & operator=(const RenderEntity &obj) { - CopyAll(obj); - return *this; - } - bool operator<(const RenderEntity &rhs) const; - bool operator()(const RenderEntity &lhs,const RenderEntity &rhs) const; - - RenderEntityType type; - Object *o; - Vec2i mapPos; - Unit *unit; - const Texture2D *teamTexture; - - RenderEntityState getState() { - return this->state; - } - void setState(RenderEntityState value) { - this->state = value; - } -}; - - class VisibleQuadContainerCache { protected: @@ -150,7 +81,8 @@ protected: cacheFrame = obj.cacheFrame; visibleObjectList = obj.visibleObjectList; visibleUnitList = obj.visibleUnitList; - inVisibleUnitList = obj.inVisibleUnitList; + visibleQuadUnitList = obj.visibleQuadUnitList; + //inVisibleUnitList = obj.inVisibleUnitList; //visibleCellList = obj.visibleCellList; visibleScaledCellList = obj.visibleScaledCellList; lastVisibleQuad = obj.lastVisibleQuad; @@ -169,26 +101,27 @@ public: CopyAll(obj); return *this; } - //bool operator<(const RenderEntity &rhs) const; - //bool operator()(const RenderEntity &lhs,const RenderEntity &rhs) const; void clearCacheData() { clearVolatileCacheData(); - - visibleObjectList.clear(); - //visibleCellList.clear(); - visibleScaledCellList.clear(); + clearNonVolatileCacheData(); } void clearVolatileCacheData() { visibleUnitList.clear(); - inVisibleUnitList.clear(); + visibleQuadUnitList.clear(); + //inVisibleUnitList.clear(); + } + void clearNonVolatileCacheData() { + visibleObjectList.clear(); + visibleScaledCellList.clear(); } int cacheFrame; Quad2i lastVisibleQuad; std::vector visibleObjectList; + std::vector visibleQuadUnitList; std::vector visibleUnitList; - std::vector inVisibleUnitList; + //std::vector inVisibleUnitList; //std::vector visibleCellList; std::vector visibleScaledCellList; }; @@ -237,7 +170,7 @@ public: static const float maxLightDist; public: - enum Shadows{ + enum Shadows { sDisabled, sProjected, sShadowMapping, @@ -302,6 +235,7 @@ private: int lastRenderFps; bool shadowsOffDueToMinRender; + bool useQuadCache; private: Renderer(); ~Renderer(); @@ -381,17 +315,9 @@ public: //complex rendering void renderSurface(const int renderFps, const int worldFrameCount); void renderObjects(const int renderFps, const int worldFrameCount); - void renderObject(RenderEntity &entity,const Vec3f &baseFogColor,const int renderFps, const int worldFrameCount); - void renderObject(Object *o,const Vec2i &mapPos,const Vec3f &baseFogColor,const int worldFrameCount); - void prepareObjectForRender(RenderEntity &entity); - void renderObjectList(std::vector &vctEntity,const Vec3f &baseFogColor,const int renderFps, const int worldFrameCount); void renderWater(); void renderUnits(const int renderFps, const int worldFrameCount); - void prepareUnitForRender(RenderEntity &entity); - void renderUnitList(std::vector &vctEntity,MeshCallbackTeamColor *meshCallbackTeamColor,const int renderFps, const int worldFrameCount); - void renderUnit(RenderEntity &entity,MeshCallbackTeamColor *meshCallbackTeamColor,const int renderFps, const int worldFrameCount); - void renderUnit(Unit *unit,MeshCallbackTeamColor *meshCallbackTeamColor, const Texture2D *teamTexture, const int worldFrameCount); void renderSelectionEffects(); void renderWaterEffects(); @@ -442,7 +368,7 @@ public: void setLastRenderFps(int value) { lastRenderFps = value;} int getLastRenderFps() const { return lastRenderFps;} - VisibleQuadContainerCache & getQuadCache(bool updateOnDirtyFrame=true); + VisibleQuadContainerCache & getQuadCache(bool updateOnDirtyFrame=true,bool forceNew=false); private: //private misc @@ -456,12 +382,7 @@ private: //selection render void renderObjectsFast(); - void renderObjectFastList(std::vector &vctEntity); void renderUnitsFast(); - void renderUnitFastList(std::vector &vctEntity); - void renderUnitFast(RenderEntity &entity); - void renderUnitFast(Unit *unit, int x, int y); - void prepareUnitFastForRender(RenderEntity &entity); //gl requirements void checkGlCaps(); diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index bbfac89e..50687e2e 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -864,18 +864,19 @@ bool Gui::isSharedCommandClass(CommandClass commandClass){ void Gui::computeSelected(bool doubleClick){ Selection::UnitContainer units; - Renderer::getInstance().computeSelected(units, selectionQuad.getPosDown(), selectionQuad.getPosUp()); + Renderer::getInstance().computeSelected(units, selectionQuad.getPosDown(), + selectionQuad.getPosUp()); selectingBuilding= false; activeCommandType= NULL; //select all units of the same type if double click - if(doubleClick && units.size()==1){ + if(doubleClick && units.size() == 1) { const Unit *refUnit= units.front(); int factionIndex= refUnit->getFactionIndex(); - for(int i=0; igetFaction(factionIndex)->getUnitCount(); ++i){ + for(int i=0; i< world->getFaction(factionIndex)->getUnitCount(); ++i) { Unit *unit= world->getFaction(factionIndex)->getUnit(i); - if(unit->getPos().dist(refUnit->getPos())getType()==refUnit->getType()) + if(unit->getPos().dist(refUnit->getPos()) < doubleClickSelectionRadius && + unit->getType() == refUnit->getType()) { units.push_back(unit); } diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 66db44a2..8208663b 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -449,21 +449,21 @@ void Faction::setResourceBalance(const ResourceType *rt, int balance){ assert(false); } -Unit *Faction::findUnit(int id){ +Unit *Faction::findUnit(int id) const { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] id = %d\n",__FILE__,__FUNCTION__, __LINE__,id); - UnitMap::iterator it= unitMap.find(id); + UnitMap::const_iterator itFound = unitMap.find(id); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - if(it==unitMap.end()){ + if(itFound == unitMap.end()) { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); return NULL; } //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] it->second = %p\n",__FILE__,__FUNCTION__, __LINE__,it->second); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] it->second->id = %d\n",__FILE__,__FUNCTION__, __LINE__,it->second->getId()); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] it->second = %s\n",__FILE__,__FUNCTION__, __LINE__,it->second->toString().c_str()); - return it->second; + return itFound->second; } void Faction::addUnit(Unit *unit){ diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index a5f1ab85..f974134d 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -130,7 +130,7 @@ public: bool isAlly(const Faction *faction); //other - Unit *findUnit(int id); + Unit *findUnit(int id) const; void addUnit(Unit *unit); void removeUnit(Unit *unit); void addStore(const UnitType *unitType); diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index abaf061c..d01a24a5 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -906,8 +906,7 @@ const Vec2i &PosCircularIterator::getPos(){ // class PosQuadIterator // ===================================================== -PosQuadIterator::PosQuadIterator(const Map *map, const Quad2i &quad, int step){ - this->map= map; +PosQuadIterator::PosQuadIterator(const Quad2i &quad, int step){ this->quad= quad; this->boundingRect= quad.computeBoundingRect(); this->step= step; diff --git a/source/glest_game/world/map.h b/source/glest_game/world/map.h index d34e3e4e..486f30ec 100755 --- a/source/glest_game/world/map.h +++ b/source/glest_game/world/map.h @@ -271,15 +271,14 @@ public: // class PosQuadIterator // =============================== -class PosQuadIterator{ +class PosQuadIterator { private: Quad2i quad; Rect2i boundingRect; - const Map *map; Vec2i pos; int step; public: - PosQuadIterator(const Map *map, const Quad2i &quad, int step=1); + PosQuadIterator(const Quad2i &quad, int step=1); bool next(); void skipX(); const Vec2i &getPos(); diff --git a/source/glest_game/world/surface_atlas.cpp b/source/glest_game/world/surface_atlas.cpp index 3fd3050b..15541d1f 100644 --- a/source/glest_game/world/surface_atlas.cpp +++ b/source/glest_game/world/surface_atlas.cpp @@ -29,12 +29,15 @@ namespace Glest{ namespace Game{ // class PixmapInfo // ===================================================== -SurfaceInfo::SurfaceInfo(const Pixmap2D *lu, const Pixmap2D *ru, const Pixmap2D *ld, const Pixmap2D *rd) { +SurfaceInfo::SurfaceInfo(const Pixmap2D *lu, const Pixmap2D *ru, const Pixmap2D *ld, + const Pixmap2D *rd) { this->leftDown= ld; this->leftUp= lu; this->rightDown= rd; this->rightUp= ru; center= NULL; + texture=NULL; + } SurfaceInfo::SurfaceInfo(const Pixmap2D *center) { @@ -43,6 +46,7 @@ SurfaceInfo::SurfaceInfo(const Pixmap2D *center) { this->rightDown= NULL; this->rightUp= NULL; this->center= center; + texture=NULL; } bool SurfaceInfo::operator==(const SurfaceInfo &si) const { diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 22bd4046..dfdf52dd 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -831,7 +831,7 @@ void World::initSplattedTextures(){ for(int i=0; i