1
0
forked from 0ad/0ad

Changes to terrain initialisation and lighting.

This was SVN commit r104.
This commit is contained in:
notpete 2003-11-25 20:13:39 +00:00
parent e7637d52c3
commit 59d4bfbae1
2 changed files with 57 additions and 53 deletions

View File

@ -14,10 +14,13 @@
//
//***********************************************************
#include "Terrain.h"
#include "tex.h"
#include "mem.h"
#include "Terrain.h"
#include "LightEnv.h"
#include "SHCoeffs.h"
bool g_HillShading = true;
CVector3D SeasonLight[2];
@ -33,38 +36,36 @@ CTerrain::~CTerrain ()
delete [] m_pVertices;
}
bool CTerrain::Initalize (char *filename)
bool CTerrain::Load(char *filename)
{
SeasonLight[0].Set (3, -1, 3);
SeasonLight[0].Normalize();
SeasonColor[0][0] = 0.8f; SeasonColor[0][1] = 1.0f; SeasonColor[0][2] = 0.8f;
TEX tex;
Handle h = tex_load(filename, &tex);
if(!h)
return false;
SeasonLight[1].Set (2, -1, -3);
SeasonLight[1].Normalize();
SeasonColor[1][0] = 1.0f; SeasonColor[1][1] = 0.9f; SeasonColor[1][2] = 0.9f;
Handle hm = tex.hm;
MEM* mem = (MEM*)h_user_data(hm, H_MEM);
const u8* data = (const u8*)mem->p;
TEX tex;
Handle h = tex_load(filename, &tex);
if(!h)
return false;
Handle hm = tex.hm;
MEM* mem = (MEM*)h_user_data(hm, H_MEM);
const u8* data = (const u8*)mem->p;
return InitFromHeightmap(data);
}
bool CTerrain::InitFromHeightmap(const u8* data)
{
delete[] m_pVertices;
m_pVertices = new STerrainVertex[MAP_SIZE*MAP_SIZE];
if (m_pVertices == NULL)
return false;
int j;
for (j=0; j<MAP_SIZE; j++)
for (int j=0; j<MAP_SIZE; j++)
{
for (int i=0; i<MAP_SIZE; i++)
{
int pos = j*MAP_SIZE + i;
m_pVertices[pos].m_Position.X = ((float)i)*CELL_SIZE;
m_pVertices[pos].m_Position.Y = (*data++)*0.35f;
m_pVertices[pos].m_Position.Y = (*data++)*HEIGHT_SCALE;
m_pVertices[pos].m_Position.Z = ((float)j)*CELL_SIZE;
}
}
@ -80,14 +81,28 @@ const u8* data = (const u8*)mem->p;
}
}
CalcLighting();
CalcNormals();
SetNeighbors();
return true;
}
void CTerrain::CalcLighting ()
void CTerrain::CalcLighting(const CLightEnv& lightEnv)
{
CSHCoeffs coeffs;
coeffs.AddAmbientLight(lightEnv.m_TerrainAmbientColor);
CVector3D dirlight;
lightEnv.GetSunDirection(dirlight);
coeffs.AddDirectionalLight(dirlight,lightEnv.m_SunColor);
for (int k=0;k<MAP_SIZE*MAP_SIZE;++k) {
coeffs.Evaluate(m_pVertices[k].m_Normal,m_pVertices[k].m_Color);
}
}
void CTerrain::CalcNormals()
{
CVector3D left, right, up, down, n[4];
for (int j=0; j<MAP_SIZE; j++)
@ -120,38 +135,23 @@ void CTerrain::CalcLighting ()
n[2] = down.Cross(right);
n[3] = right.Cross(up);
n[0].Normalize();
n[1].Normalize();
n[2].Normalize();
n[3].Normalize();
float n0len=n[0].GetLength();
if (n0len>0.0001f) n[0]*=1.0f/n0len;
float n1len=n[1].GetLength();
if (n1len>0.0001f) n[1]*=1.0f/n1len;
float n2len=n[2].GetLength();
if (n2len>0.0001f) n[2]*=1.0f/n2len;
float n3len=n[3].GetLength();
if (n3len>0.0001f) n[3]*=1.0f/n3len;
CVector3D Normal = n[0] + n[1] + n[2] + n[3];
Normal.Normalize();
float Color1 = Normal.Dot(SeasonLight[0]*-1)/(Normal.GetLength() * SeasonLight[0].GetLength());
Color1 = (Color1+1.0f)/1.4f;
if (Color1>1.0f)
Color1=1.0f;
if (Color1<0.0f)
Color1=0.0f;
float Color2 = Normal.Dot(SeasonLight[1]*-1)/(Normal.GetLength() * SeasonLight[1].GetLength());
Color2 = (Color2+1.0f)/1.4f;
if (Color2>1.0f)
Color2=1.0f;
if (Color2<0.0f)
Color2=0.0f;
m_pVertices[j*MAP_SIZE + i].m_Color[0][0] = Color1*SeasonColor[0][0];
m_pVertices[j*MAP_SIZE + i].m_Color[0][1] = Color1*SeasonColor[0][1];
m_pVertices[j*MAP_SIZE + i].m_Color[0][2] = Color1*SeasonColor[0][2];
m_pVertices[j*MAP_SIZE + i].m_Color[1][0] = Color2*SeasonColor[1][0];
m_pVertices[j*MAP_SIZE + i].m_Color[1][1] = Color2*SeasonColor[1][1];
m_pVertices[j*MAP_SIZE + i].m_Color[1][2] = Color2*SeasonColor[1][2];
float nlen=Normal.GetLength();
if (nlen>0.00001f) Normal*=1.0f/nlen;
m_pVertices[j*MAP_SIZE + i].m_Normal=Normal;
}
}
}

View File

@ -22,6 +22,8 @@
#include "Patch.h"
#include "Vector3D.h"
class CLightEnv;
extern bool g_HillShading;
class CTerrain
@ -30,7 +32,8 @@ class CTerrain
CTerrain ();
~CTerrain ();
bool Initalize (char *filename);
bool Load(char *filename);
bool InitFromHeightmap(const u8* data);
// protected:
//the patches currently loaded
@ -39,7 +42,8 @@ class CTerrain
// protected:
void CalcLighting();
void CalcNormals();
void CalcLighting(const CLightEnv& env);
void SetNeighbors();
};