From: <mk...@us...> - 2003-02-15 20:56:24
|
Update of /cvsroot/csp/THIRDPARTYLIBS/demeter In directory sc8-pr-cvs1:/tmp/cvs-serv32395 Modified Files: Terrain.cpp TerrainTextureFactory.cpp TerrainTextureFactory.h Log Message: big speedup in terrain loading by computing less accurate normals. turned on bounds checking to prevent some segfaults. Index: Terrain.cpp =================================================================== RCS file: /cvsroot/csp/THIRDPARTYLIBS/demeter/Terrain.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Terrain.cpp 2 Feb 2003 20:59:22 -0000 1.2 --- Terrain.cpp 15 Feb 2003 20:56:18 -0000 1.3 *************** *** 56,59 **** --- 56,60 ---- //#endif + #define _PROTECT_ACCESS_ #include "Terrain.h" *************** *** 914,917 **** --- 915,922 ---- DimensionPowerOf2(imageWidth,imageHeight,elevWidth,elevHeight); pImageData = new float[elevWidth * elevHeight]; + if (pImageData == 0) { + cout << "TERRAIN: out of memory allocating new terrain elevation map\n"; + exit(1); + } if (imageWidth != elevWidth || imageHeight != elevHeight) { *************** *** 965,969 **** m_HeightVertices = elevHeight + 1; m_NumberOfVertices = m_WidthVertices * m_HeightVertices; ! m_pVertices = new Vector[m_WidthVertices * m_HeightVertices]; int i,j; float x,y; --- 970,974 ---- m_HeightVertices = elevHeight + 1; m_NumberOfVertices = m_WidthVertices * m_HeightVertices; ! m_pVertices = new Vector[m_WidthVertices * m_HeightVertices]; int i,j; float x,y; *************** *** 1007,1010 **** --- 1012,1016 ---- { m_pNormals = new Vector[m_NumberOfVertices]; + assert(m_pNormals); const float delta = (M_PI * 2.0f) / 8.0f; for (i = 0; i < m_NumberOfVertices; i++) *************** *** 1016,1022 **** vertexX = indexX * m_VertexSpacing; vertexY = indexY * m_VertexSpacing; Vector avgNormal; avgNormal.x = avgNormal.y = avgNormal.z = 0.0f; ! for (float theta = -0.5f * delta; theta < (M_PI * 2.0f); theta++) { Vector v; --- 1022,1041 ---- vertexX = indexX * m_VertexSpacing; vertexY = indexY * m_VertexSpacing; + /* + // This average over 8 nearby normals is extremely slow compared + // to all the other terrain loading code. First, the offset vector + // calculation is ineffecient, since v is recomputed 8 times at + // every vertex to get exactly the same set of vectors each time. + // Next, GetNormal() is slow, so doing such an average is costly. + // For now I've stripped out this computation, and substituted a + // single normal evaluation at a slight (fixed) offset from each + // vertex. This seems to produe comparable results. If better + // normal matching needs to be done at the boundaries of terrain + // tiles, this should be done in a separate computation that only + // operates on vertices near the edges of the terrain tiles. + // -MR Vector avgNormal; avgNormal.x = avgNormal.y = avgNormal.z = 0.0f; ! for (float theta = -0.5f * delta; theta < (M_PI * 2.0f); theta+=delta) { Vector v; *************** *** 1027,1031 **** v.x += vertexX; v.y += vertexY; ! float nx,ny,nz; GetNormal(v.x,v.y,nx,ny,nz); avgNormal.x += nx; --- 1046,1050 ---- v.x += vertexX; v.y += vertexY; ! float nx, ny, nz; GetNormal(v.x,v.y,nx,ny,nz); avgNormal.x += nx; *************** *** 1036,1039 **** --- 1055,1064 ---- m_pNormals[i].y = avgNormal.y / 8.0f; m_pNormals[i].z = avgNormal.z / 8.0f; + */ + float nx, ny, nz; + GetNormal(vertexX+0.2,vertexY+0.8,nx,ny,nz); + m_pNormals[i].x = nx; + m_pNormals[i].y = ny; + m_pNormals[i].z = nz; } } *************** *** 2959,2964 **** v2.z = p3.z - p1.z; ! v1.Normalize(); ! v2.Normalize(); // Find surface normal based on cross product. normal.x = v1.y * v2.z - v2.y * v1.z; --- 2984,2990 ---- v2.z = p3.z - p1.z; ! // why? ! //v1.Normalize(); ! //v2.Normalize(); // Find surface normal based on cross product. normal.x = v1.y * v2.z - v2.y * v1.z; Index: TerrainTextureFactory.cpp =================================================================== RCS file: /cvsroot/csp/THIRDPARTYLIBS/demeter/TerrainTextureFactory.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TerrainTextureFactory.cpp 8 Feb 2003 15:56:45 -0000 1.2 --- TerrainTextureFactory.cpp 15 Feb 2003 20:56:19 -0000 1.3 *************** *** 132,135 **** --- 132,136 ---- m_BaseTextures[1] = pTextureImage; + //LoadImage("Snow1.tga", width, height, &pTextureImage, false); LoadImage("Grass1.bmp", width, height, &pTextureImage, false); m_BaseTextures[2] = pTextureImage; Index: TerrainTextureFactory.h =================================================================== RCS file: /cvsroot/csp/THIRDPARTYLIBS/demeter/TerrainTextureFactory.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** TerrainTextureFactory.h 2 Feb 2003 20:59:23 -0000 1.3 --- TerrainTextureFactory.h 15 Feb 2003 20:56:19 -0000 1.4 *************** *** 14,21 **** #if __GNUC__ >= 3 #include <ext/hash_map> #else #include <hash_map> #endif - using std::hash_map; #endif #endif --- 14,22 ---- #if __GNUC__ >= 3 #include <ext/hash_map> + using __gnu_cxx::hash_map; #else #include <hash_map> + using std::hash_map; #endif #endif #endif |