2011-04-06 16:11:08 +00:00
|
|
|
#include "loadland.hpp"
|
|
|
|
|
|
|
|
namespace ESM
|
|
|
|
{
|
2012-04-01 19:29:49 +00:00
|
|
|
|
|
|
|
Land::Land()
|
|
|
|
: flags(0)
|
|
|
|
, X(0)
|
|
|
|
, Y(0)
|
|
|
|
, mEsm(NULL)
|
|
|
|
, hasData(false)
|
|
|
|
, dataLoaded(false)
|
2012-04-04 19:05:19 +00:00
|
|
|
, landData(NULL)
|
2012-04-01 19:29:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:05:19 +00:00
|
|
|
Land::~Land()
|
|
|
|
{
|
|
|
|
delete landData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
void Land::load(ESMReader &esm)
|
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm = &esm;
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
// Get the grid location
|
|
|
|
esm.getSubNameIs("INTV");
|
|
|
|
esm.getSubHeaderIs(8);
|
|
|
|
esm.getT<int>(X);
|
|
|
|
esm.getT<int>(Y);
|
|
|
|
|
|
|
|
esm.getHNT(flags, "DATA");
|
|
|
|
|
|
|
|
// Store the file position
|
|
|
|
context = esm.getContext();
|
|
|
|
|
|
|
|
hasData = false;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
// Skip these here. Load the actual data when the cell is loaded.
|
2012-01-21 16:59:08 +00:00
|
|
|
if (esm.isNextSub("VNML"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(12675);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
if (esm.isNextSub("VHGT"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(4232);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
if (esm.isNextSub("WNAM"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(81);
|
|
|
|
}
|
|
|
|
if (esm.isNextSub("VCLR"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(12675);
|
|
|
|
}
|
|
|
|
if (esm.isNextSub("VTEX"))
|
|
|
|
{
|
|
|
|
esm.skipHSubSize(512);
|
|
|
|
cnt++;
|
|
|
|
}
|
2011-04-06 16:11:08 +00:00
|
|
|
|
|
|
|
// We need all three of VNML, VHGT and VTEX in order to use the
|
|
|
|
// landscape.
|
|
|
|
hasData = (cnt == 3);
|
2012-01-21 16:59:08 +00:00
|
|
|
|
|
|
|
dataLoaded = false;
|
2012-04-04 19:05:19 +00:00
|
|
|
landData = NULL;
|
2011-04-06 16:11:08 +00:00
|
|
|
}
|
2012-01-21 16:59:08 +00:00
|
|
|
|
2012-03-27 08:20:22 +00:00
|
|
|
void Land::loadData()
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
|
|
|
if (dataLoaded)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:05:19 +00:00
|
|
|
landData = new LandData;
|
|
|
|
|
2012-01-21 16:59:08 +00:00
|
|
|
if (hasData)
|
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->restoreContext(context);
|
2012-01-21 16:59:08 +00:00
|
|
|
|
|
|
|
//esm.getHNExact(landData->normals, sizeof(VNML), "VNML");
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("VNML"))
|
2012-01-21 16:59:08 +00:00
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->skipHSubSize(12675);
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VHGT rawHeights;
|
|
|
|
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->getHNExact(&rawHeights, sizeof(VHGT), "VHGT");
|
2012-01-21 16:59:08 +00:00
|
|
|
int currentHeightOffset = rawHeights.heightOffset;
|
|
|
|
for (int y = 0; y < LAND_SIZE; y++)
|
|
|
|
{
|
|
|
|
currentHeightOffset += rawHeights.heightData[y * LAND_SIZE];
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->heights[y * LAND_SIZE] = currentHeightOffset * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
|
|
|
|
int tempOffset = currentHeightOffset;
|
|
|
|
for (int x = 1; x < LAND_SIZE; x++)
|
|
|
|
{
|
|
|
|
tempOffset += rawHeights.heightData[y * LAND_SIZE + x];
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->heights[x + y * LAND_SIZE] = tempOffset * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-21 17:59:12 +00:00
|
|
|
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("WNAM"))
|
2012-01-21 17:59:12 +00:00
|
|
|
{
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->skipHSubSize(81);
|
2012-01-21 17:59:12 +00:00
|
|
|
}
|
2012-03-27 08:20:22 +00:00
|
|
|
if (mEsm->isNextSub("VCLR"))
|
2012-01-21 17:59:12 +00:00
|
|
|
{
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->usingColours = true;
|
|
|
|
mEsm->getHExact(&landData->colours, 3*LAND_NUM_VERTS);
|
2012-02-29 23:05:22 +00:00
|
|
|
}else{
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->usingColours = false;
|
2012-01-21 17:59:12 +00:00
|
|
|
}
|
|
|
|
//TODO fix magic numbers
|
|
|
|
uint16_t vtex[512];
|
2012-03-27 08:20:22 +00:00
|
|
|
mEsm->getHNExact(&vtex, 512, "VTEX");
|
2012-01-21 17:59:12 +00:00
|
|
|
|
|
|
|
int readPos = 0; //bit ugly, but it works
|
|
|
|
for ( int y1 = 0; y1 < 4; y1++ )
|
|
|
|
for ( int x1 = 0; x1 < 4; x1++ )
|
|
|
|
for ( int y2 = 0; y2 < 4; y2++)
|
|
|
|
for ( int x2 = 0; x2 < 4; x2++ )
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->textures[(y1*4+y2)*16+(x1*4+x2)] = vtex[readPos++];
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->usingColours = false;
|
|
|
|
memset(&landData->textures, 0, 512 * sizeof(uint16_t));
|
2012-01-21 16:59:08 +00:00
|
|
|
for (int i = 0; i < LAND_NUM_VERTS; i++)
|
|
|
|
{
|
2012-04-04 19:05:19 +00:00
|
|
|
landData->heights[i] = -256.0f * HEIGHT_SCALE;
|
2012-01-21 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataLoaded = true;
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:05:19 +00:00
|
|
|
void Land::unloadData()
|
|
|
|
{
|
|
|
|
if (dataLoaded)
|
|
|
|
{
|
|
|
|
delete landData;
|
|
|
|
landData = NULL;
|
|
|
|
dataLoaded = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 16:11:08 +00:00
|
|
|
}
|