/* CDTestFramework http://codercorner.com Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #include "stdafx.h" #include "Terrain.h" inline float NxAngle(const Point& v0, const Point& v1) { float cos = v0|v1; // |v0|*|v1|*Cos(Angle) float sin = (v0^v1).Magnitude(); // |v0|*|v1|*Sin(Angle) return ::atan2(sin, cos); } static float computeAngle(const Point* verts, const udword* refs, udword vref) { udword e0=0,e2=0; if(vref==refs[0]) { e0 = 2; e2 = 1; } else if(vref==refs[1]) { e0 = 2; e2 = 0; } else if(vref==refs[2]) { e0 = 0; e2 = 1; } else { assert(0); } Point edge0 = verts[refs[e0]] - verts[vref]; Point edge1 = verts[refs[e2]] - verts[vref]; return NxAngle(edge0, edge1); } static bool buildSmoothNormals( udword nbTris, udword nbVerts, const Point* verts, const udword* dFaces, const uword* wFaces, Point* normals, bool flip) { // Checkings if(!verts || !normals || !nbTris || !nbVerts) return false; // Get correct destination buffers // - if available, write directly to user-provided buffers // - else get some ram and keep track of it Point* FNormals = new Point[nbTris]; if(!FNormals) return false; // Compute face normals udword c = (flip!=0); for(udword i=0;i>=1; if(!currentSize) return; // Compute new heights float v0 = (value * float(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); float v1 = (value * float(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); float v2 = (value * float(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); float v3 = (value * float(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); float v4 = (value * float(rand()-RAND_MAX_OVER_TWO) * ONE_OVER_RAND_MAX); udword x1 = (x0+currentSize) % initSize; udword x2 = (x0+currentSize+currentSize) % initSize; udword y1 = (y0+currentSize) % initSize; udword y2 = (y0+currentSize+currentSize) % initSize; if(!done[x1 + y0*initSize]) field[x1 + y0*initSize].y = v0 + 0.5f * (field[x0 + y0*initSize].y + field[x2 + y0*initSize].y); if(!done[x0 + y1*initSize]) field[x0 + y1*initSize].y = v1 + 0.5f * (field[x0 + y0*initSize].y + field[x0 + y2*initSize].y); if(!done[x2 + y1*initSize]) field[x2 + y1*initSize].y = v2 + 0.5f * (field[x2 + y0*initSize].y + field[x2 + y2*initSize].y); if(!done[x1 + y2*initSize]) field[x1 + y2*initSize].y = v3 + 0.5f * (field[x0 + y2*initSize].y + field[x2 + y2*initSize].y); if(!done[x1 + y1*initSize]) field[x1 + y1*initSize].y = v4 + 0.5f * (field[x0 + y1*initSize].y + field[x2 + y1*initSize].y); done[x1 + y0*initSize] = true; done[x0 + y1*initSize] = true; done[x2 + y1*initSize] = true; done[x1 + y2*initSize] = true; done[x1 + y1*initSize] = true; // Recurse through 4 corners value *= 0.5f; _Compute(done, field, x0, y0, currentSize, value, initSize); _Compute(done, field, x0, y1, currentSize, value, initSize); _Compute(done, field, x1, y0, currentSize, value, initSize); _Compute(done, field, x1, y1, currentSize, value, initSize); } }; // Fractalize srand(42); bool* done = new bool[nbVerts]; memset(done,0,nbVerts*sizeof(bool)); verts[0].y = 10.0f; verts[size-1].y = 10.0f; verts[size*(size-1)].y = 10.0f; verts[nbVerts-1].y = 10.0f; Local::_Compute(done, verts, 0, 0, size, chaos, size); for(udword i=0;iinit(TERRAIN_SIZE, TERRAIN_OFFSET, TERRAIN_WIDTH, TERRAIN_CHAOS); // Build OPCODE model gMeshInterface.SetNbTriangles(gTerrainData->nbFaces); gMeshInterface.SetNbVertices(gTerrainData->nbVerts); gMeshInterface.SetPointers((const IndexedTriangle*)gTerrainData->faces, gTerrainData->verts); OPCODECREATE Create; Create.mIMesh = &gMeshInterface; Create.mSettings.mLimit = 1; Create.mSettings.mRules = SPLIT_SPLATTER_POINTS|SPLIT_GEOM_CENTER; Create.mNoLeaf = true; Create.mQuantized = true; Create.mKeepOriginal = false; Create.mCanRemap = false; gOpcodeModel = new Model; if(!gOpcodeModel->Build(Create)) { } } void RenderTerrain() { if(gTerrainData) renderTerrain(*gTerrainData, true); } void RenderTerrainTriangles(udword nbTriangles, const udword* indices) { if(gTerrainData) renderTerrainTriangles(*gTerrainData, nbTriangles, indices); }