From 01b69865818bfd2731cc6fa12d9fdec8b075dc8b Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Wed, 20 Feb 2013 17:42:47 +0000 Subject: [PATCH] First steps for new terrain texturing. In tileset xml do: --- source/glest_game/world/tileset.cpp | 96 ++++++++++++++----- source/glest_game/world/tileset.h | 5 +- source/glest_game/world/world.cpp | 2 +- source/shared_lib/include/graphics/pixmap.h | 1 + source/shared_lib/sources/graphics/pixmap.cpp | 21 ++++ 5 files changed, 99 insertions(+), 26 deletions(-) diff --git a/source/glest_game/world/tileset.cpp b/source/glest_game/world/tileset.cpp index b9473cf7..5b71f97b 100644 --- a/source/glest_game/world/tileset.cpp +++ b/source/glest_game/world/tileset.cpp @@ -183,7 +183,9 @@ void Tileset::load(const string &dir, Checksum *checksum, Checksum *tilesetCheck //surfaces const XmlNode *surfacesNode= tilesetNode->getChild("surfaces"); + int partsize= 0; for(int i=0; i < surfCount; ++i) { + const XmlNode *surfaceNode; if(surfacesNode->hasChildAtIndex("surface",i)){ surfaceNode= surfacesNode->getChild("surface", i); @@ -193,25 +195,61 @@ void Tileset::load(const string &dir, Checksum *checksum, Checksum *tilesetCheck surfaceNode= surfacesNode->getChild("surface", 2); } - - int childCount= surfaceNode->getChildCount(); - surfPixmaps[i].resize(childCount); - surfProbs[i].resize(childCount); - - for(int j = 0; j < childCount; ++j) { - surfPixmaps[i][j] = NULL; + if(surfaceNode->hasAttribute("partsize")){ + partsize=surfaceNode->getAttribute("partsize",true)->getIntValue(); + } + else{ + partsize=0; } - for(int j = 0; j < childCount; ++j) { - const XmlNode *textureNode= surfaceNode->getChild("texture", j); - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - surfPixmaps[i][j] = new Pixmap2D(); - surfPixmaps[i][j]->init(3); - surfPixmaps[i][j]->load(textureNode->getAttribute("path")->getRestrictedValue(currentPath)); - } - loadedFileList[textureNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(sourceXMLFile,textureNode->getAttribute("path")->getRestrictedValue())); + if(partsize==0){ + int childCount= surfaceNode->getChildCount(); + surfPixmaps[i].resize(childCount); + surfProbs[i].resize(childCount); - surfProbs[i][j]= textureNode->getAttribute("prob")->getFloatValue(); + for(int j = 0; j < childCount; ++j) { + surfPixmaps[i][j] = NULL; + } + + for(int j = 0; j < childCount; ++j) { + const XmlNode *textureNode= surfaceNode->getChild("texture", j); + if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + surfPixmaps[i][j] = new Pixmap2D(); + surfPixmaps[i][j]->init(3); + surfPixmaps[i][j]->load(textureNode->getAttribute("path")->getRestrictedValue(currentPath)); + } + loadedFileList[textureNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(sourceXMLFile,textureNode->getAttribute("path")->getRestrictedValue())); + + surfProbs[i][j]= textureNode->getAttribute("prob")->getFloatValue(); + } + } + else { + // read single big texture and cut it into pieces + const XmlNode *textureNode= surfaceNode->getChild("texture", 0); + Pixmap2D *pixmap=new Pixmap2D(); + pixmap->init(3); + pixmap->load(textureNode->getAttribute("path")->getRestrictedValue(currentPath)); + loadedFileList[textureNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(sourceXMLFile,textureNode->getAttribute("path")->getRestrictedValue())); + int width=pixmap->getW(); + int heith=pixmap->getW(); + assert(width==heith); + assert(width%64==0); + assert(width%partsize==0); + int parts=width/partsize; + int numberOfPieces=parts*parts; + partsArray[i]=parts; + surfPixmaps[i].resize(numberOfPieces); + surfProbs[i].resize(numberOfPieces); + int j=0; + for(int x = 0; x < parts; ++x) { + for(int y = 0; y < parts; ++y) { + surfPixmaps[i][j] = new Pixmap2D(); + surfPixmaps[i][j]->init(partsize,partsize,3); + surfPixmaps[i][j]->copyImagePart(x*partsize,y*partsize,pixmap); + surfProbs[i][j]=-1; + j++; + } + } } } @@ -391,23 +429,33 @@ const Pixmap2D *Tileset::getSurfPixmap(int type, int var) const{ return surfPixmaps[type][var % vars]; } -void Tileset::addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture) { +void Tileset::addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture, int mapX, int mapY) { //center textures if(leftUp == rightUp && leftUp == leftDown && leftUp == rightDown) { //texture variation according to probability float r= random.randRange(0.f, 1.f); int var= 0; float max= 0.f; - for(int i=0; i < surfProbs[leftUp].size(); ++i) { - max+= surfProbs[leftUp][i]; - if(r <= max) { - var= i; - break; - } + const Pixmap2D* pixmap; + if(surfProbs[leftUp][0]<0) + {// big textures use coordinates + int parts=partsArray[leftUp]; + pixmap=getSurfPixmap(leftUp, (mapY%parts)*parts+(mapX%parts)); } - SurfaceInfo si(getSurfPixmap(leftUp, var)); + else{ + for(int i=0; i < surfProbs[leftUp].size(); ++i) { + max+= surfProbs[leftUp][i]; + if(r <= max) { + var= i; + break; + } + } + pixmap=getSurfPixmap(leftUp, var); + } + SurfaceInfo si(pixmap); surfaceAtlas.addSurface(&si); coord= si.getCoord(); + // only for 512px printf("coord.x=%f coord.y=%f mapX=%d mapY=%d result=%d\n",coord.x,coord.y,mapX,mapY,(mapY%8)*8+(mapX%8)); texture= si.getTexture(); } //spatted textures diff --git a/source/glest_game/world/tileset.h b/source/glest_game/world/tileset.h index 9615780a..38b5bd63 100644 --- a/source/glest_game/world/tileset.h +++ b/source/glest_game/world/tileset.h @@ -132,6 +132,9 @@ private: SurfProbs surfProbs[surfCount]; SurfPixmaps surfPixmaps[surfCount]; + int partsArray[surfCount]; + //int partsizes[surfCount]; + RandomGen random; Texture3D *waterTex; bool waterEffects; @@ -181,7 +184,7 @@ public: //surface textures const Pixmap2D *getSurfPixmap(int type, int var) const; - void addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture); + void addSurfTex(int leftUp, int rightUp, int leftDown, int rightDown, Vec2f &coord, const Texture2D *&texture, int mapX, int mapY); //sounds AmbientSounds *getAmbientSounds() {return &ambientSounds;} diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index b4597210..0029056f 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1717,7 +1717,7 @@ void World::initSplattedTextures() { sc10->getSurfaceType(), sc01->getSurfaceType(), sc11->getSurfaceType(), - coord, texture); + coord, texture,j,i); sc00->setSurfTexCoord(coord); sc00->setSurfaceTexture(texture); } diff --git a/source/shared_lib/include/graphics/pixmap.h b/source/shared_lib/include/graphics/pixmap.h index e09e998c..110d2859 100644 --- a/source/shared_lib/include/graphics/pixmap.h +++ b/source/shared_lib/include/graphics/pixmap.h @@ -293,6 +293,7 @@ public: void lerp(float t, const Pixmap2D *pixmap1, const Pixmap2D *pixmap2); void copy(const Pixmap2D *sourcePixmap); void subCopy(int x, int y, const Pixmap2D *sourcePixmap); + void copyImagePart(int x, int y, const Pixmap2D *sourcePixmap); string getPath() const { return path;} uint64 getPixelByteCount() const; diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index 31927760..80701290 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -1286,6 +1286,27 @@ void Pixmap2D::subCopy(int x, int y, const Pixmap2D *sourcePixmap){ delete [] pixel; } +// uses a a part of a bigger source image to fill this image. +void Pixmap2D::copyImagePart(int x, int y, const Pixmap2D *sourcePixmap){ + assert(components==sourcePixmap->getComponents()); + + if(x+w>sourcePixmap->getW() && y+h>sourcePixmap->getH()){ + throw megaglest_runtime_error("Pixmap2D::copyImagePart(), bad dimensions"); + } + + uint8 *pixel= new uint8[components]; + + for(int i=x; igetPixel(i, j, pixel); + setPixel(i-x, j-y, pixel); + } + } + CalculatePixelsCRC(pixels,getPixelByteCount(), crc); + + delete [] pixel; +} + bool Pixmap2D::doDimensionsAgree(const Pixmap2D *pixmap){ return pixmap->getW() == w && pixmap->getH() == h; }