From 50ce6eb1eaadfa426eb76f77df04882fc632f65f Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 12 Oct 2011 05:24:30 +0000 Subject: [PATCH] - updated intro with better timing and multiple models. Updated menu background model as well and about screen to show megapack units. --- source/glest_game/graphics/renderer.cpp | 95 ++++++++++- source/glest_game/graphics/renderer.h | 4 +- source/glest_game/main/intro.cpp | 186 +++++++++++++++++++-- source/glest_game/main/intro.h | 27 ++- source/glest_game/menu/menu_background.cpp | 6 +- 5 files changed, 295 insertions(+), 23 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 8fa5e1246..c08ce3499 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -574,10 +574,18 @@ void Renderer::endMenu() { } //delete resources - modelManager[rsMenu]->end(); - textureManager[rsMenu]->end(); - fontManager[rsMenu]->end(); - particleManager[rsMenu]->end(); + if(modelManager[rsMenu]) { + modelManager[rsMenu]->end(); + } + if(textureManager[rsMenu]) { + textureManager[rsMenu]->end(); + } + if(fontManager[rsMenu]) { + fontManager[rsMenu]->end(); + } + if(particleManager[rsMenu]) { + particleManager[rsMenu]->end(); + } if(this->customlist3dMenu != NULL) { glDeleteLists(*this->customlist3dMenu,1); @@ -4737,7 +4745,7 @@ void Renderer::renderDisplay() { glPopAttrib(); } -void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ +void Renderer::renderMenuBackground(const MenuBackground *menuBackground) { assertGl(); @@ -4767,6 +4775,7 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5f); modelRenderer->begin(true, true, true); + menuBackground->getMainModelPtr()->updateInterpolationData(menuBackground->getAnim(), true); modelRenderer->render(menuBackground->getMainModelPtr()); modelRenderer->end(); glDisable(GL_ALPHA_TEST); @@ -4865,6 +4874,76 @@ void Renderer::renderMenuBackground(const MenuBackground *menuBackground){ assertGl(); } +void Renderer::renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels,const Vec3f characterPosition, float anim) { + + assertGl(); + + const Vec3f &cameraPosition= camera->getConstPosition(); + + glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + + //clear + //Vec4f fogColor= Vec4f(0.4f, 0.4f, 0.4f, 1.f) * fade; + // Show black bacground + Vec4f fogColor= Vec4f(0.f, 0.f, 0.f, 1.f); + glClearColor(fogColor.x, fogColor.y, fogColor.z, 1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glFogfv(GL_FOG_COLOR, fogColor.ptr()); + + //light + Vec4f lightPos= Vec4f(10.f, 10.f, 10.f, 1.f) * fade; + Vec4f diffLight= Vec4f(0.9f, 0.9f, 0.9f, 1.f) * fade; + Vec4f ambLight= Vec4f(0.3f, 0.3f, 0.3f, 1.f) * fade; + Vec4f specLight= Vec4f(0.1f, 0.1f, 0.1f, 1.f) * fade; + + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight.ptr()); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight.ptr()); + glLightfv(GL_LIGHT0, GL_SPECULAR, specLight.ptr()); + + //main model + if(mainModel) { + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.5f); + modelRenderer->begin(true, true, true); + mainModel->updateInterpolationData(anim, true); + modelRenderer->render(mainModel); + modelRenderer->end(); + glDisable(GL_ALPHA_TEST); + } + + //characters + if(characterModels.size() > 0) { + float dist= characterPosition.dist(cameraPosition); + float minDist= 3.f; + if(dist < minDist) { + glAlphaFunc(GL_GREATER, 0.0f); + float alpha= clamp((minDist-dist) / minDist, 0.f, 1.f); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); + modelRenderer->begin(true, true, false); + + for(unsigned int i = 0; i < characterModels.size(); ++i) { + if(characterModels[i]) { + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glTranslatef(i*2.f-4.f, -1.4f, -7.5f); + characterModels[i]->updateInterpolationData(anim, true); + modelRenderer->render(characterModels[i]); + glPopMatrix(); + } + } + modelRenderer->end(); + } + } + + + glPopAttrib(); + + assertGl(); +} + // ==================== computing ==================== bool Renderer::computePosition(const Vec2i &screenPos, Vec2i &worldPos){ @@ -5813,7 +5892,11 @@ void Renderer::init3dListMenu(const MainMenu *mm) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); const Metrics &metrics= Metrics::getInstance(); - const MenuBackground *mb= mm->getConstMenuBackground(); + //const MenuBackground *mb= mm->getConstMenuBackground(); + const MenuBackground *mb = NULL; + if(mm != NULL) { + mb = mm->getConstMenuBackground(); + } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 3a7bdacb6..4053f0fbc 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -456,6 +456,7 @@ public: void renderDisplay(); void renderMenuBackground(const MenuBackground *menuBackground); void renderMapPreview(const MapPreview *map, bool renderAll, int screenX, int screenY,Texture2D **renderToTexture=NULL); + void renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels,const Vec3f characterPosition, float anim); //computing bool computePosition(const Vec2i &screenPos, Vec2i &worldPos); @@ -526,6 +527,8 @@ public: void setCustom3dMenuList(GLuint *customlist3dMenu) { this->customlist3dMenu = customlist3dMenu; } GLuint * getCustom3dMenuList() const { return this->customlist3dMenu; } + void init3dListMenu(const MainMenu *mm); + private: //private misc float computeSunAngle(float time); @@ -547,7 +550,6 @@ private: //gl init void init3dList(); void init2dList(); - void init3dListMenu(const MainMenu *mm); //misc void loadProjectionMatrix(); diff --git a/source/glest_game/main/intro.cpp b/source/glest_game/main/intro.cpp index 592ace85e..2854b97eb 100644 --- a/source/glest_game/main/intro.cpp +++ b/source/glest_game/main/intro.cpp @@ -74,6 +74,12 @@ Intro::Intro(Program *program): mouseY = 0; mouse2d = 0; + Renderer &renderer= Renderer::getInstance(); + //renderer.init3dListMenu(NULL); + renderer.initMenu(NULL); + fade= 0.f; + anim= 0.f; + XmlTree xmlTree; string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); xmlTree.load(data_path + "data/core/menu/menu.xml",Properties::getTagReplacementValues()); @@ -82,6 +88,30 @@ Intro::Intro(Program *program): if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + targetCamera= NULL; + t= 0.f; + + //camera + const XmlNode *cameraNode= introNode->getChild("camera"); + + //position + const XmlNode *positionNode= cameraNode->getChild("start-position"); + startPosition.x= positionNode->getAttribute("x")->getFloatValue(); + startPosition.y= positionNode->getAttribute("y")->getFloatValue(); + startPosition.z= positionNode->getAttribute("z")->getFloatValue(); + camera.setPosition(startPosition); + + //rotation + const XmlNode *rotationNode= cameraNode->getChild("start-rotation"); + Vec3f startRotation; + startRotation.x= rotationNode->getAttribute("x")->getFloatValue(); + startRotation.y= rotationNode->getAttribute("y")->getFloatValue(); + startRotation.z= rotationNode->getAttribute("z")->getFloatValue(); + camera.setOrientation(Quaternion(EulerAngles( + degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); + // intro info const XmlNode *introTimeNode= introNode->getChild("intro-time"); Intro::introTime = introTimeNode->getAttribute("value")->getIntValue(); @@ -96,6 +126,52 @@ Intro::Intro(Program *program): int showIntroPicsTime = showIntroPicturesNode->getAttribute("time")->getIntValue(); bool showIntroPicsRandom = showIntroPicturesNode->getAttribute("random")->getBoolValue(); + const XmlNode *showIntroModelsNode= introNode->getChild("show-intro-models"); + bool showIntroModels = showIntroModelsNode->getAttribute("value")->getBoolValue(); + bool showIntroModelsRandom = showIntroModelsNode->getAttribute("random")->getBoolValue(); + + //load main model + modelIndex = 0; + models.clear(); + if(showIntroModels == true) { + string introPath = data_path + "data/core/menu/main_model/intro*.g3d"; + vector introModels; + findAll(introPath, introModels, false, false); + for(int i = 0; i < introModels.size(); ++i) { + string logo = introModels[i]; + Model *model= renderer.newModel(rsMenu); + if(model) { + model->load(data_path + "data/core/menu/main_model/" + logo); + models.push_back(model); + //printf("model [%s]\n",model->getFileName().c_str()); + } + } + + if(showIntroModelsRandom == true) { + std::vector modelList; + + unsigned int seed = time(NULL); + srand(seed); + int failedLookups=0; + std::map usedIndex; + for(;modelList.size() < models.size();) { + int index = rand() % models.size(); + if(usedIndex.find(index) != usedIndex.end()) { + failedLookups++; + seed = time(NULL) / failedLookups; + srand(seed); + continue; + } + //printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); + modelList.push_back(models[index]); + usedIndex[index]=true; + seed = time(NULL) / modelList.size(); + srand(seed); + } + models = modelList; + } + } + int displayItemNumber = 1; int appear= Intro::appearTime; int disappear= Intro::showTime+Intro::appearTime+Intro::disapearTime; @@ -107,6 +183,8 @@ Intro::Intro(Program *program): texts.push_back(new Text(glestVersionString, Vec2i(w/2+45, h/2-45), disappear *(displayItemNumber++), coreData.getMenuFontNormal(),coreData.getMenuFontNormal3D())); texts.push_back(new Text("www.megaglest.org", Vec2i(w/2, h/2), disappear *(displayItemNumber++), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + modelShowTime = disappear *(displayItemNumber); + if(showIntroPics > 0 && coreData.getMiscTextureList().size() > 0) { const int showMiscTime = showIntroPicsTime; @@ -187,12 +265,12 @@ Intro::~Intro() { deleteValues(texts.begin(),texts.end()); } -void Intro::update(){ +void Intro::update() { timer++; if(timer > introTime * GameConstants::updateFps / 1000){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - program->setState(new MainMenu(program)); + cleanup(); return; } @@ -205,6 +283,86 @@ void Intro::update(){ } mouse2d= (mouse2d+1) % Renderer::maxMouse2dAnim; + + if(targetCamera != NULL) { + t+= ((0.01f+(1.f-t)/10.f)/20.f)*(60.f/GameConstants::updateFps); + + //interpolate position + camera.setPosition(lastCamera.getPosition().lerp(t, targetCamera->getPosition())); + + //interpolate orientation + Quaternion q= lastCamera.getOrientation().lerp(t, targetCamera->getOrientation()); + camera.setOrientation(q); + + if(t>=1.f){ + targetCamera= NULL; + t= 0.f; + } + } + + //fade + if(fade <= 1.f) { + fade += 0.6f / GameConstants::updateFps; + if(fade > 1.f){ + fade = 1.f; + } + } + + //animation + const float minSpeed = 0.015f; + const float maxSpeed = 0.6f; + anim += (maxSpeed / GameConstants::updateFps) / 5 + random.randRange(minSpeed, max(minSpeed + 0.0001f, (maxSpeed / GameConstants::updateFps) / 5.f)); + if(anim > 1.f) { + anim = 0.f; + } +} + +void Intro::renderModelBackground() { + // Black background + glClearColor(0, 0, 0, 1); + + if(models.size() > 0) { + int difTime= 1000 * timer / GameConstants::updateFps - modelShowTime; + int totalModelShowTime = Intro::introTime - modelShowTime; + int individualModelShowTime = totalModelShowTime / models.size(); + + //printf("difTime = %d individualModelShowTime = %d modelIndex = %d\n",difTime,individualModelShowTime,modelIndex); + + //int difTime= 1; + if(difTime > 0) { + if(difTime > ((modelIndex+1) * individualModelShowTime)) { + int oldmodelIndex = modelIndex; + if(modelIndex+1 < models.size()) { + modelIndex++; + + //position + //nextCamera.setPosition(camera.getPosition()); +// nextCamera.setPosition(Vec3f(84,-9,11)); +// +// //rotation +// //Vec3f startRotation(0,12,0); +// Vec3f startRotation(0,-80,0); +// nextCamera.setOrientation(Quaternion(EulerAngles( +// degToRad(startRotation.x), +// degToRad(startRotation.y), +// degToRad(startRotation.z)))); +// +// this->targetCamera = &nextCamera; +// this->lastCamera= camera; +// this->t= 0.f; + + } + //printf("oldmodelIndex = %d, modelIndex = %d\n",oldmodelIndex,modelIndex); + } + Renderer &renderer= Renderer::getInstance(); + vector characterModels; + characterModels.push_back(NULL); + characterModels.push_back(NULL); + characterModels.push_back(models[modelIndex]); + const Vec3f characterPosition = startPosition; + renderer.renderMenuBackground(&camera, fade, NULL, characterModels,characterPosition,anim); + } + } } void Intro::render() { @@ -217,20 +375,16 @@ void Intro::render() { canRender(); incrementFps(); - renderer.reset2d(); renderer.clearBuffers(); + renderer.reset3dMenu(); -// CoreData &coreData= CoreData::getInstance(); -// renderer.renderTextureQuad( -// 1, 1, -// 1, 1, -// coreData.getLogoTexture(), 1.0); + renderer.clearZBuffer(); + renderer.loadCameraMatrix(&camera); -// renderer.renderFPSWhenEnabled(lastFps); + renderModelBackground(); + renderer.renderParticleManager(rsMenu); -// renderer.renderText3D( -// "test 123", coreData.getMenuFontVeryBig3D(), 1.0, -// 1, 1, true); + renderer.reset2d(); for(int i = 0; i < texts.size(); ++i) { Text *text= texts[i]; @@ -295,9 +449,13 @@ void Intro::mouseUpLeft(int x, int y){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - program->setState(new MainMenu(program)); + cleanup(); +} - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); +void Intro::cleanup() { + Renderer::getInstance().endMenu(); + + program->setState(new MainMenu(program)); } void Intro::mouseMove(int x, int y, const MouseState *ms) { diff --git a/source/glest_game/main/intro.h b/source/glest_game/main/intro.h index 51601e18e..8d349b0dc 100644 --- a/source/glest_game/main/intro.h +++ b/source/glest_game/main/intro.h @@ -18,6 +18,10 @@ #include "font.h" #include "vec.h" #include "texture.h" +#include "camera.h" +#include "model.h" +#include "randomgen.h" + #include "leak_dumper.h" using std::vector; @@ -29,6 +33,10 @@ using Shared::Graphics::Font2D; using Shared::Graphics::Font3D; using Shared::Graphics::Texture2D; +using Shared::Graphics::Camera; +using Shared::Graphics::Model; +using Shared::Util::RandomGen; + namespace Glest{ namespace Game{ // ===================================================== @@ -64,7 +72,7 @@ public: /// ProgramState representing the intro // ===================================================== -class Intro: public ProgramState{ +class Intro: public ProgramState { private: static int introTime; static int appearTime; @@ -78,6 +86,23 @@ private: int mouseY; int mouse2d; + //Model *mainModel; + int modelIndex; + vector models; + Camera nextCamera; + Camera camera; + Camera lastCamera; + const Camera *targetCamera; + float t; + RandomGen random; + float anim; + float fade; + Vec3f startPosition; + int modelShowTime; + + void cleanup(); + void renderModelBackground(); + public: Intro(Program *program); virtual ~Intro(); diff --git a/source/glest_game/menu/menu_background.cpp b/source/glest_game/menu/menu_background.cpp index 486af7248..e41e50847 100644 --- a/source/glest_game/menu/menu_background.cpp +++ b/source/glest_game/menu/menu_background.cpp @@ -107,7 +107,11 @@ MenuBackground::MenuBackground(){ //load main model mainModel= renderer.newModel(rsMenu); if(mainModel) { - mainModel->load(data_path + "data/core/menu/main_model/menu_main.g3d"); + //mainModel->load(data_path + "data/core/menu/main_model/menu_main.g3d"); + const XmlNode *mainMenuModelNode= menuNode->getChild("menu-background-model"); + string mainModelFile = mainMenuModelNode->getAttribute("value")->getRestrictedValue(); + + mainModel->load(data_path + mainModelFile); } //models