void buildIndices(dae_reader_t *reader, int geometryID) { geometry_t *geometry = &reader->geometry[geometryID]; vector<draw_range_t> drawRange; getDrawRanges(reader, geometryID, drawRange); geometry->meshes.push_back(drawRange); computeIndices(reader, geometryID); }
CTerrain::CTerrain(IDirect3DDevice9* device, std::string heightmapFileName, int numVertsPerRow, int numVertsPerCol, int cellSpacing, float heightScale) { _device = device; _numVertsPerRow = numVertsPerRow; _numVertsPerCol = numVertsPerCol; _cellSpacing = cellSpacing; _numCellsPerRow = _numVertsPerRow - 1; _numCellsPerCol = _numVertsPerCol - 1; _width = _numCellsPerRow * _cellSpacing; _depth = _numCellsPerCol * _cellSpacing; _numVertices = _numVertsPerRow * _numVertsPerCol; _numTriangles = _numCellsPerRow * _numCellsPerCol * 2; _heightScale = heightScale; // 加载高度图 if (!readRawFile(heightmapFileName)) { ::MessageBox(0, _T("readRawFile - FAILED"), 0, 0); ::PostQuitMessage(0); } // 高度缩放 for (unsigned int i = 0; i < _heightmap.size(); i++) _heightmap[i] *= heightScale; // 计算点点坐标 if (!computeVertices()) { ::MessageBox(0, _T("computeVertices - FAILED"), 0, 0); ::PostQuitMessage(0); } // 计算索引 if (!computeIndices()) { ::MessageBox(0, _T("computeIndices - FAILED"), 0, 0); ::PostQuitMessage(0); } computeNormals(); }
Terrain::Terrain(IDirect3DDevice9* device, std::string heightmapFileName, int numVertsPerRow, int numVertsPerCol, int cellSpacing, float heightScale) { _device = device; _numVertsPerRow = numVertsPerRow; _numVertsPerCol = numVertsPerCol; _cellSpacing = cellSpacing; _numCellsPerRow = _numVertsPerRow - 1; _numCellsPerCol = _numVertsPerCol - 1; _width = _numCellsPerRow * _cellSpacing; _depth = _numCellsPerCol * _cellSpacing; _numVertices = _numVertsPerRow * _numVertsPerCol; _numTriangles = _numCellsPerRow * _numCellsPerCol * 2; _heightScale = heightScale; // load heightmap if( !readRawFile(heightmapFileName) ) { ::MessageBox(0, "readRawFile - FAILED", 0, 0); ::PostQuitMessage(0); } // scale heights for(int i = 0; i < _heightmap.size(); i++) _heightmap[i] *= heightScale; // compute the vertices if( !computeVertices() ) { ::MessageBox(0, "computeVertices - FAILED", 0, 0); ::PostQuitMessage(0); } // compute the indices if( !computeIndices() ) { ::MessageBox(0, "computeIndices - FAILED", 0, 0); ::PostQuitMessage(0); } }
void render::HeightMap::compute( unsigned int rows, unsigned int cols, const float *heights, int stride, const math::matrix3x1<float> span, bool eliminateZeroTriangles, Texture **textures, unsigned int levels, unsigned int M) { vertices.reserve((rows + 1) * (cols + 1)); for (unsigned int r = 0; r <= rows; r++) { for (unsigned int c = 0; c <= cols; c++) { float u = (float)r / (float)rows; float v = (float)c / (float)cols; math::matrix3x1<float> p(u * span.x, 0, v * span.z); if (r < rows && c < cols) { p.y = heights[(r * cols + c) * stride] * span.y; } math::matrix3x1<float> n(0, 1, 0); geometry::Vertex vertex(p, n, u, v); vertices.push_back(vertex); } } for (unsigned int r = 1; r < rows - 1; r++) { for (unsigned int c = 1; c < cols - 1; c++) { geometry::Vertex &vertex = vertices.at(r * (cols + 1) + c); const float dy = 0.01f; math::matrix3x1<float> ne( (heights[(r * cols + c) * stride] - heights[((r + 1) * cols + c) * stride]), dy, 0); math::matrix3x1<float> nw( heights[((r-1) * cols + c) * stride] - heights[(r * cols + c) * stride], dy, 0); math::matrix3x1<float> nn( 0, dy, heights[(r * cols + c) * stride] - heights[(r * cols + c - 1) * stride]); math::matrix3x1<float> ns( 0, dy, heights[(r * cols + c+1) * stride] - heights[(r * cols + c) * stride]); /* math::matrix3x1<float> ne( (heightMap.get(r, c) - heightMap.get(r+1, c)), dy, 0); math::matrix3x1<float> nw( heightMap.get(r-1, c) - heightMap.get(r, c), dy, 0); math::matrix3x1<float> nn( 0, dy, heightMap.get(r, c) - heightMap.get(r, c-1)); math::matrix3x1<float> ns( 0, dy, heightMap.get(r, c+1) - heightMap.get(r, c)); */ math::matrix3x1<float> normal = (ne.unit() + nw.unit() + nn.unit() + ns.unit()).unit(); vertex.nx = normal.x; vertex.ny = normal.y; vertex.nz = normal.z; } } unsigned int N = 1; for (unsigned int level = 0; level < levels; ++level) { meshes.push_back(RigidMesh()); RigidMesh &mesh = meshes.back(); mesh.levelOfDetail = level; if (textures) { for (unsigned int i = 0; i < RigidMesh::Num_textures && textures[i]; ++i) { mesh.textures[i] = 0; } } computeIndices(rows, cols, mesh, N, eliminateZeroTriangles); N *= M; } constructVertexBuffer(); }