Generate the graphics mesh to render the height field
| 182 | |
| 183 | // Generate the graphics mesh to render the height field |
| 184 | void HeightField::generateGraphicsMesh() { |
| 185 | |
| 186 | std::vector<unsigned int> indices; |
| 187 | int vertexId = 0; |
| 188 | |
| 189 | for (int i=0; i<NB_POINTS_WIDTH; i++) { |
| 190 | for (int j=0; j<NB_POINTS_LENGTH; j++) { |
| 191 | |
| 192 | float originHeight = -(mMaxHeight - mMinHeight) * 0.5f - mMinHeight; |
| 193 | float height = originHeight + mHeightData[j * NB_POINTS_WIDTH + i]; |
| 194 | openglframework::Vector3 vertex(-(NB_POINTS_WIDTH - 1) * 0.5f + i, height, -(NB_POINTS_LENGTH - 1) * 0.5f + j); |
| 195 | |
| 196 | mVertices.push_back(vertex); |
| 197 | |
| 198 | // Triangle indices |
| 199 | if ((i < NB_POINTS_WIDTH - 1) && (j < NB_POINTS_LENGTH - 1)) { |
| 200 | |
| 201 | unsigned int v1 = vertexId; |
| 202 | unsigned int v2 = vertexId + 1; |
| 203 | unsigned int v3 = vertexId + NB_POINTS_LENGTH; |
| 204 | unsigned int v4 = vertexId + NB_POINTS_LENGTH + 1; |
| 205 | |
| 206 | // First triangle |
| 207 | indices.push_back(v1); |
| 208 | indices.push_back(v2); |
| 209 | indices.push_back(v3); |
| 210 | |
| 211 | // Second triangle |
| 212 | indices.push_back(v2); |
| 213 | indices.push_back(v4); |
| 214 | indices.push_back(v3); |
| 215 | } |
| 216 | |
| 217 | vertexId++; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | mIndices.push_back(indices); |
| 222 | |
| 223 | calculateNormals(); |
| 224 | } |
| 225 | |
| 226 | // Create the Vertex Buffer Objects used to render with OpenGL. |
| 227 | /// We create two VBOs (one for vertices and one for indices) |
nothing calls this directly
no outgoing calls
no test coverage detected