From 59d4bfbae14c29959d6f426ab2a0a482302de11a Mon Sep 17 00:00:00 2001 From: notpete Date: Tue, 25 Nov 2003 20:13:39 +0000 Subject: [PATCH] Changes to terrain initialisation and lighting. This was SVN commit r104. --- source/terrain/Terrain.cpp | 102 ++++++++++++++++++------------------- source/terrain/Terrain.h | 8 ++- 2 files changed, 57 insertions(+), 53 deletions(-) diff --git a/source/terrain/Terrain.cpp b/source/terrain/Terrain.cpp index 3a7bf0487e..e6f884a262 100755 --- a/source/terrain/Terrain.cpp +++ b/source/terrain/Terrain.cpp @@ -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; jp; } } - 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;k0.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; } } } diff --git a/source/terrain/Terrain.h b/source/terrain/Terrain.h index dd8481dc5c..47fa2322d9 100755 --- a/source/terrain/Terrain.h +++ b/source/terrain/Terrain.h @@ -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(); };