[927] | 1 | |
---|
[1111] | 2 | #include <memory.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <sys/time.h> |
---|
| 5 | #include <sys/types.h> |
---|
| 6 | #include <unistd.h> |
---|
| 7 | #include <fcntl.h> |
---|
| 8 | #include <stdlib.h> |
---|
[776] | 9 | #include <GL/glew.h> |
---|
| 10 | #include <GL/gl.h> |
---|
[1111] | 11 | #include "Grid.h" |
---|
[776] | 12 | #include "HeightMap.h" |
---|
| 13 | #include "ContourLineFilter.h" |
---|
| 14 | #include "TypeDefs.h" |
---|
[1111] | 15 | #include "Texture1D.h" |
---|
| 16 | #include "R2/R2FilePath.h" |
---|
[820] | 17 | #include "RpField1D.h" |
---|
[1111] | 18 | #include "RenderContext.h" |
---|
[776] | 19 | |
---|
[932] | 20 | bool HeightMap::update_pending = false; |
---|
| 21 | double HeightMap::valueMin = 0.0; |
---|
| 22 | double HeightMap::valueMax = 1.0; |
---|
| 23 | |
---|
[1028] | 24 | #define TOPCONTOUR 0 |
---|
[1188] | 25 | //#define TOPCONTOUR 1 |
---|
[862] | 26 | HeightMap::HeightMap() : |
---|
[927] | 27 | _vertexBufferObjectID(0), |
---|
| 28 | _textureBufferObjectID(0), |
---|
| 29 | _vertexCount(0), |
---|
[862] | 30 | _contour(0), |
---|
[1000] | 31 | _topContour(0), |
---|
[1493] | 32 | _tfPtr(0), |
---|
[1546] | 33 | _opacity(0.5f), |
---|
[862] | 34 | _indexBuffer(0), |
---|
| 35 | _indexCount(0), |
---|
[927] | 36 | _contourColor(1.0f, 0.0f, 0.0f), |
---|
[1000] | 37 | _contourVisible(false), |
---|
| 38 | _topContourVisible(true), |
---|
[927] | 39 | _visible(false), |
---|
[862] | 40 | _scale(1.0f, 1.0f, 1.0f), |
---|
[1111] | 41 | _centerPoint(0.0f, 0.0f, 0.0f), |
---|
| 42 | heights_(NULL) |
---|
[776] | 43 | { |
---|
[862] | 44 | _shader = new NvShader(); |
---|
[1111] | 45 | _shader->loadFragmentProgram("heightcolor.cg", "main"); |
---|
[1546] | 46 | _tfParam = _shader->getNamedParameterFromFP("tf"); |
---|
| 47 | _opacityParam = _shader->getNamedParameterFromFP("opacity"); |
---|
[776] | 48 | } |
---|
| 49 | |
---|
| 50 | HeightMap::~HeightMap() |
---|
| 51 | { |
---|
[862] | 52 | reset(); |
---|
[776] | 53 | |
---|
[862] | 54 | if (_shader) { |
---|
[927] | 55 | delete _shader; |
---|
[862] | 56 | } |
---|
[776] | 57 | |
---|
| 58 | // TMP |
---|
[1493] | 59 | //if (_tfPtr) delete _tfPtr; |
---|
[776] | 60 | } |
---|
| 61 | |
---|
[838] | 62 | void HeightMap::render(graphics::RenderContext* renderContext) |
---|
[776] | 63 | { |
---|
[862] | 64 | if (renderContext->getCullMode() == graphics::RenderContext::NO_CULL) { |
---|
[838] | 65 | glDisable(GL_CULL_FACE); |
---|
[862] | 66 | } else { |
---|
[838] | 67 | glEnable(GL_CULL_FACE); |
---|
| 68 | glCullFace((GLuint) renderContext->getCullMode()); |
---|
| 69 | } |
---|
| 70 | glPolygonMode(GL_FRONT_AND_BACK, (GLuint) renderContext->getPolygonMode()); |
---|
| 71 | glShadeModel((GLuint) renderContext->getShadingModel()); |
---|
| 72 | |
---|
[776] | 73 | glPushMatrix(); |
---|
[837] | 74 | |
---|
[1111] | 75 | #ifndef notdef |
---|
[932] | 76 | if (_scale.x != 0.0) { |
---|
[1111] | 77 | glScalef(1 / _scale.x, 1 / _scale.y , 1 / _scale.z); |
---|
[837] | 78 | } |
---|
[1111] | 79 | #endif |
---|
[862] | 80 | glTranslatef(-_centerPoint.x, -_centerPoint.y, -_centerPoint.z); |
---|
[837] | 81 | |
---|
[1111] | 82 | if (_contour != NULL) { |
---|
[927] | 83 | glDepthRange (0.001, 1.0); |
---|
[862] | 84 | } |
---|
[927] | 85 | |
---|
[862] | 86 | glEnable(GL_DEPTH_TEST); |
---|
[776] | 87 | |
---|
[862] | 88 | if (_vertexBufferObjectID) { |
---|
[927] | 89 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
| 90 | glShadeModel(GL_SMOOTH); |
---|
| 91 | glEnable(GL_BLEND); |
---|
| 92 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
| 93 | glDisableClientState(GL_COLOR_ARRAY); |
---|
| 94 | glDisableClientState(GL_INDEX_ARRAY); |
---|
| 95 | glDisableClientState(GL_NORMAL_ARRAY); |
---|
| 96 | |
---|
[1493] | 97 | if (_tfPtr) { |
---|
[1000] | 98 | // PUT vertex program here |
---|
| 99 | // |
---|
| 100 | // |
---|
| 101 | |
---|
[776] | 102 | cgGLBindProgram(_shader->getFP()); |
---|
| 103 | cgGLEnableProfile(CG_PROFILE_FP30); |
---|
[927] | 104 | |
---|
[1493] | 105 | cgGLSetTextureParameter(_tfParam, _tfPtr->id()); |
---|
| 106 | cgGLEnableTextureParameter(_tfParam); |
---|
[1546] | 107 | cgGLSetParameter1f(_opacityParam, _opacity); |
---|
[927] | 108 | |
---|
| 109 | glEnable(GL_TEXTURE_1D); |
---|
[1493] | 110 | _tfPtr->getTexture()->activate(); |
---|
[927] | 111 | |
---|
| 112 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); |
---|
| 116 | glVertexPointer(3, GL_FLOAT, 12, 0); |
---|
| 117 | |
---|
| 118 | glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); |
---|
| 119 | ::glTexCoordPointer(3, GL_FLOAT, 12, 0); |
---|
| 120 | |
---|
[1000] | 121 | #define _TRIANGLES_ |
---|
| 122 | #ifdef _TRIANGLES_ |
---|
[927] | 123 | glDrawElements(GL_TRIANGLES, _indexCount, GL_UNSIGNED_INT, |
---|
| 124 | _indexBuffer); |
---|
[1000] | 125 | #else |
---|
| 126 | glDrawElements(GL_QUADS, _indexCount, GL_UNSIGNED_INT, |
---|
| 127 | _indexBuffer); |
---|
| 128 | #endif |
---|
| 129 | |
---|
[927] | 130 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
| 131 | |
---|
| 132 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
[1493] | 133 | if (_tfPtr != NULL) { |
---|
| 134 | _tfPtr->getTexture()->deactivate(); |
---|
[927] | 135 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 136 | |
---|
[776] | 137 | cgGLDisableProfile(CG_PROFILE_FP30); |
---|
[927] | 138 | } |
---|
[862] | 139 | } |
---|
| 140 | |
---|
| 141 | glShadeModel(GL_FLAT); |
---|
[838] | 142 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
---|
[862] | 143 | |
---|
[1111] | 144 | if (_contour != NULL) { |
---|
[1000] | 145 | if (_contourVisible) { |
---|
| 146 | glDisable(GL_BLEND); |
---|
| 147 | glDisable(GL_TEXTURE_2D); |
---|
[1546] | 148 | glColor4f(_contourColor.x, _contourColor.y, _contourColor.z, |
---|
| 149 | _opacity /*1.0f*/); |
---|
[1000] | 150 | glDepthRange (0.0, 0.999); |
---|
| 151 | _contour->render(); |
---|
| 152 | glDepthRange (0.0, 1.0); |
---|
| 153 | } |
---|
| 154 | |
---|
[1028] | 155 | #if TOPCONTOUR |
---|
[1000] | 156 | if (_topContourVisible) { |
---|
| 157 | glDisable(GL_BLEND); |
---|
| 158 | glDisable(GL_TEXTURE_2D); |
---|
[1546] | 159 | glColor4f(_contourColor.x, _contourColor.y, _contourColor.z, |
---|
| 160 | _opacity /*1.0f*/); |
---|
[1000] | 161 | //glDepthRange (0.0, 0.999); |
---|
| 162 | _topContour->render(); |
---|
| 163 | //glDepthRange (0.0, 1.0); |
---|
| 164 | } |
---|
| 165 | #endif |
---|
[862] | 166 | } |
---|
[776] | 167 | glPopMatrix(); |
---|
| 168 | } |
---|
| 169 | |
---|
[932] | 170 | void |
---|
[1112] | 171 | HeightMap::createIndexBuffer(int xCount, int zCount, float* heights) |
---|
[776] | 172 | { |
---|
[1112] | 173 | if (_indexBuffer != NULL) { |
---|
| 174 | delete [] _indexBuffer; |
---|
| 175 | } |
---|
| 176 | _indexCount = (xCount - 1) * (zCount - 1) * 6; |
---|
| 177 | _indexBuffer = new int[_indexCount]; |
---|
[776] | 178 | |
---|
| 179 | int i, j; |
---|
| 180 | int boundaryWidth = xCount - 1; |
---|
| 181 | int boundaryHeight = zCount - 1; |
---|
[1112] | 182 | int* ptr = _indexBuffer; |
---|
[1111] | 183 | int index1, index2, index3, index4; |
---|
[820] | 184 | bool index1Valid, index2Valid, index3Valid, index4Valid; |
---|
| 185 | index1Valid = index2Valid = index3Valid = index4Valid = true; |
---|
| 186 | |
---|
[862] | 187 | if (heights) { |
---|
[820] | 188 | int ic = 0; |
---|
[862] | 189 | for (i = 0; i < boundaryHeight; ++i) { |
---|
| 190 | for (j = 0; j < boundaryWidth; ++j) { |
---|
[820] | 191 | index1 = i * xCount +j; |
---|
| 192 | if (isnan(heights[index1])) index1Valid = false; |
---|
| 193 | index2 = (i + 1) * xCount + j; |
---|
| 194 | if (isnan(heights[index2])) index2Valid = false; |
---|
| 195 | index3 = (i + 1) * xCount + j + 1; |
---|
| 196 | if (isnan(heights[index3])) index3Valid = false; |
---|
| 197 | index4 = i * xCount + j + 1; |
---|
| 198 | if (isnan(heights[index4])) index4Valid = false; |
---|
| 199 | |
---|
[1000] | 200 | #ifdef _TRIANGLES_ |
---|
[862] | 201 | if (index1Valid && index2Valid && index3Valid) { |
---|
[820] | 202 | *ptr = index1; ++ptr; |
---|
| 203 | *ptr = index2; ++ptr; |
---|
| 204 | *ptr = index3; ++ptr; |
---|
| 205 | ++ic; |
---|
| 206 | } |
---|
[862] | 207 | if (index1Valid && index3Valid && index4Valid) { |
---|
[820] | 208 | *ptr = index1; ++ptr; |
---|
| 209 | *ptr = index3; ++ptr; |
---|
| 210 | *ptr = index4; ++ptr; |
---|
| 211 | ++ic; |
---|
| 212 | } |
---|
[1000] | 213 | #else |
---|
| 214 | if (index1Valid && index2Valid && index3Valid && index4Valid) { |
---|
| 215 | *ptr = index1; ++ptr; |
---|
| 216 | *ptr = index2; ++ptr; |
---|
| 217 | *ptr = index3; ++ptr; |
---|
| 218 | *ptr = index4; ++ptr; |
---|
| 219 | ++ic; |
---|
| 220 | } |
---|
| 221 | #endif |
---|
[820] | 222 | } |
---|
[776] | 223 | } |
---|
[862] | 224 | } else { |
---|
| 225 | for (i = 0; i < boundaryHeight; ++i) { |
---|
| 226 | for (j = 0; j < boundaryWidth; ++j) { |
---|
[820] | 227 | *ptr = i * xCount + j; ++ptr; |
---|
| 228 | *ptr = (i + 1) * xCount + j; ++ptr; |
---|
| 229 | *ptr = (i + 1) * xCount + j + 1; ++ptr; |
---|
[927] | 230 | *ptr = i * xCount + j; ++ptr; |
---|
| 231 | *ptr = (i + 1) * xCount + j + 1; ++ptr; |
---|
[820] | 232 | *ptr = i * xCount + j + 1; ++ptr; |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
[776] | 236 | } |
---|
| 237 | |
---|
[932] | 238 | void |
---|
| 239 | HeightMap::reset() |
---|
[776] | 240 | { |
---|
[862] | 241 | if (_vertexBufferObjectID) { |
---|
[927] | 242 | glDeleteBuffers(1, &_vertexBufferObjectID); |
---|
[1112] | 243 | _vertexBufferObjectID = 0; |
---|
[862] | 244 | } |
---|
[1112] | 245 | if (_textureBufferObjectID) { |
---|
| 246 | glDeleteBuffers(1, &_textureBufferObjectID); |
---|
| 247 | _textureBufferObjectID = 0; |
---|
[862] | 248 | } |
---|
[1112] | 249 | if (_contour != NULL) { |
---|
[927] | 250 | delete _contour; |
---|
[1112] | 251 | _contour = NULL; |
---|
[862] | 252 | } |
---|
[776] | 253 | } |
---|
| 254 | |
---|
[932] | 255 | void |
---|
| 256 | HeightMap::setHeight(int xCount, int yCount, Vector3* heights) |
---|
[776] | 257 | { |
---|
[862] | 258 | _vertexCount = xCount * yCount; |
---|
| 259 | reset(); |
---|
| 260 | |
---|
[1111] | 261 | heights_ = (float *)heights; |
---|
[929] | 262 | float min, max; |
---|
| 263 | min = heights[0].y, max = heights[0].y; |
---|
| 264 | |
---|
[862] | 265 | int count = xCount * yCount; |
---|
| 266 | for (int i = 0; i < count; ++i) { |
---|
[929] | 267 | if (min > heights[i].y) { |
---|
| 268 | min = heights[i].y; |
---|
[927] | 269 | } |
---|
[929] | 270 | if (max < heights[i].y) { |
---|
| 271 | max = heights[i].y; |
---|
[927] | 272 | } |
---|
[862] | 273 | } |
---|
[776] | 274 | |
---|
[837] | 275 | _scale.x = 1.0f; |
---|
[929] | 276 | _scale.z = max - min; |
---|
[837] | 277 | _scale.y = 1.0f; |
---|
| 278 | |
---|
[932] | 279 | xAxis.SetRange(0.0, 1.0); |
---|
| 280 | yAxis.SetRange(0.0, 1.0); |
---|
| 281 | zAxis.SetRange(0.0, 1.0); |
---|
| 282 | wAxis.SetRange(min, max); |
---|
| 283 | update_pending = true; |
---|
| 284 | |
---|
[929] | 285 | _centerPoint.set(_scale.x * 0.5, _scale.z * 0.5 + min, _scale.y * 0.5); |
---|
| 286 | |
---|
[927] | 287 | Vector3* texcoord = new Vector3[count]; |
---|
[862] | 288 | for (int i = 0; i < count; ++i) { |
---|
[927] | 289 | texcoord[i].set(0, 0, heights[i].y); |
---|
[862] | 290 | } |
---|
| 291 | |
---|
| 292 | glGenBuffers(1, &_vertexBufferObjectID); |
---|
[776] | 293 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); |
---|
[927] | 294 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof( Vector3 ), heights, |
---|
| 295 | GL_STATIC_DRAW); |
---|
[862] | 296 | glGenBuffers(1, &_textureBufferObjectID); |
---|
[776] | 297 | glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); |
---|
[927] | 298 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(float) * 3, texcoord, |
---|
| 299 | GL_STATIC_DRAW); |
---|
[862] | 300 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
[776] | 301 | |
---|
[927] | 302 | delete [] texcoord; |
---|
[862] | 303 | |
---|
| 304 | |
---|
| 305 | ContourLineFilter lineFilter; |
---|
| 306 | _contour = lineFilter.create(0.0f, 1.0f, 10, heights, xCount, yCount); |
---|
[776] | 307 | |
---|
[1028] | 308 | #if TOPCONTOUR |
---|
[1000] | 309 | ContourLineFilter topLineFilter; |
---|
| 310 | topLineFilter.setHeightTop(true); |
---|
| 311 | _topContour = topLineFilter.create(0.0f, 1.0f, 10, heights, xCount, yCount); |
---|
| 312 | #endif |
---|
| 313 | |
---|
[862] | 314 | //if (heightMap) |
---|
| 315 | //{ |
---|
[927] | 316 | // VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, xCount * yCount, sizeof(Vector3) * xCount * yCount, heightMap, false); |
---|
[1112] | 317 | this->createIndexBuffer(xCount, yCount, 0); |
---|
[862] | 318 | //} |
---|
| 319 | //else |
---|
| 320 | //{ |
---|
| 321 | //printf("ERROR - HeightMap::setHeight\n"); |
---|
| 322 | //} |
---|
[776] | 323 | } |
---|
| 324 | |
---|
[862] | 325 | void |
---|
[1111] | 326 | HeightMap::setHeight(float xMin, float yMin, float xMax, float yMax, |
---|
| 327 | int xNum, int yNum, float* heights) |
---|
[776] | 328 | { |
---|
[1111] | 329 | _vertexCount = xNum * yNum; |
---|
| 330 | xNum_ = xNum, yNum_ = yNum; |
---|
| 331 | heights_ = heights; |
---|
| 332 | reset(); |
---|
[862] | 333 | |
---|
[1111] | 334 | // Get the min/max of the heights. */ |
---|
[929] | 335 | float min, max; |
---|
[1111] | 336 | min = max = heights[0]; |
---|
| 337 | for (int i = 0; i < _vertexCount; ++i) { |
---|
[929] | 338 | if (min > heights[i]) { |
---|
| 339 | min = heights[i]; |
---|
| 340 | } else if (max < heights[i]) { |
---|
| 341 | max = heights[i]; |
---|
[927] | 342 | } |
---|
[862] | 343 | } |
---|
[1161] | 344 | #ifdef notdef |
---|
| 345 | if (retainScale_) { |
---|
| 346 | // Check the units of each axis. If they are the same, we want to |
---|
| 347 | // retain the surface's aspect ratio when transforming coordinates to |
---|
| 348 | // the grid. Use the range of the longest axis when the units are the |
---|
| 349 | // same. |
---|
| 350 | if (xAxis.units() != NULL) && (xAxis.units() == yAxis.units()) { |
---|
| 351 | } |
---|
| 352 | if (yAxis.units() != NULL) && (yAxis.units() == zAxis.units()) { |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | #endif |
---|
| 356 | |
---|
[932] | 357 | wAxis.SetRange(min, max); |
---|
| 358 | yAxis.SetRange(min, max); |
---|
[1111] | 359 | xAxis.SetRange(xMin, xMax); |
---|
| 360 | zAxis.SetRange(yMin, yMax); |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | min = 0.0, max = 1.0; |
---|
| 364 | xMin = yMin = min = 0.0; |
---|
| 365 | xMax = yMax = max = 1.0; |
---|
| 366 | // Save the scales. |
---|
| 367 | _scale.x = _scale.y = _scale.z = 1.0; |
---|
| 368 | |
---|
[932] | 369 | update_pending = true; |
---|
[929] | 370 | |
---|
[1111] | 371 | _centerPoint.set(0.5, 0.5, 0.5); |
---|
[862] | 372 | |
---|
[1111] | 373 | #ifndef notdef |
---|
| 374 | Vector3* texcoord = new Vector3[_vertexCount]; |
---|
| 375 | for (int i = 0; i < _vertexCount; ++i) { |
---|
[927] | 376 | texcoord[i].set(0, 0, heights[i]); |
---|
[862] | 377 | } |
---|
| 378 | |
---|
[1111] | 379 | Vector3* heightMap = createHeightVertices(xMin, yMin, xMax, yMax, xNum, yNum, heights); |
---|
[862] | 380 | |
---|
| 381 | glGenBuffers(1, &_vertexBufferObjectID); |
---|
[776] | 382 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); |
---|
[927] | 383 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof( Vector3 ), heightMap, |
---|
| 384 | GL_STATIC_DRAW); |
---|
[862] | 385 | glGenBuffers(1, &_textureBufferObjectID); |
---|
[776] | 386 | glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); |
---|
[927] | 387 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(float) * 3, texcoord, |
---|
| 388 | GL_STATIC_DRAW); |
---|
[862] | 389 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
[776] | 390 | |
---|
[927] | 391 | delete [] texcoord; |
---|
[862] | 392 | |
---|
| 393 | |
---|
| 394 | ContourLineFilter lineFilter; |
---|
[1493] | 395 | //lineFilter.transferFunction(_tfPtr); |
---|
[1111] | 396 | _contour = lineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum); |
---|
[862] | 397 | |
---|
[1028] | 398 | #if TOPCONTOUR |
---|
[1000] | 399 | ContourLineFilter topLineFilter; |
---|
| 400 | topLineFilter.setHeightTop(true); |
---|
[1111] | 401 | _topContour = topLineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum); |
---|
[1000] | 402 | #endif |
---|
| 403 | |
---|
| 404 | |
---|
[862] | 405 | //if (heightMap) |
---|
| 406 | //{ |
---|
[1111] | 407 | // VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, xNum * yNum, |
---|
| 408 | // sizeof(Vector3) * xNum * yNum, heightMap, false); |
---|
[1112] | 409 | if (_indexBuffer != NULL) { |
---|
[927] | 410 | free(_indexBuffer); |
---|
[1112] | 411 | _indexBuffer = NULL; |
---|
[862] | 412 | } |
---|
[1112] | 413 | this->createIndexBuffer(xNum, yNum, heights); |
---|
[862] | 414 | //} |
---|
| 415 | //else |
---|
| 416 | //{ |
---|
| 417 | //printf("ERROR - HeightMap::setHeight\n"); |
---|
| 418 | //} |
---|
[1111] | 419 | #endif |
---|
[776] | 420 | } |
---|
| 421 | |
---|
[1111] | 422 | Vector3* |
---|
| 423 | HeightMap::createHeightVertices(float xMin, float yMin, float xMax, |
---|
| 424 | float yMax, int xNum, int yNum, float* height) |
---|
[776] | 425 | { |
---|
[1111] | 426 | Vector3* vertices = (Vector3*) malloc(sizeof(Vector3) * xNum * yNum); |
---|
[776] | 427 | |
---|
[862] | 428 | Vector3* dstDataPtr = vertices; |
---|
| 429 | float* srcDataPtr = height; |
---|
| 430 | |
---|
[1111] | 431 | for (int y = 0; y < yNum; ++y) { |
---|
[927] | 432 | float yCoord; |
---|
[776] | 433 | |
---|
[1111] | 434 | yCoord = yMin + ((yMax - yMin) * y) / (yNum - 1); |
---|
| 435 | for (int x = 0; x < xNum; ++x) { |
---|
[927] | 436 | float xCoord; |
---|
[776] | 437 | |
---|
[1111] | 438 | xCoord = xMin + ((xMax - xMin) * x) / (xNum - 1); |
---|
[927] | 439 | dstDataPtr->set(xCoord, *srcDataPtr, yCoord); |
---|
[862] | 440 | |
---|
[927] | 441 | ++dstDataPtr; |
---|
| 442 | ++srcDataPtr; |
---|
| 443 | } |
---|
[862] | 444 | } |
---|
| 445 | return vertices; |
---|
[776] | 446 | } |
---|
| 447 | |
---|
[1111] | 448 | // Maps the data coordinates of the surface into the grid's axes. |
---|
| 449 | void |
---|
| 450 | HeightMap::MapToGrid(Grid *gridPtr) |
---|
| 451 | { |
---|
| 452 | int count = xNum_ * yNum_; |
---|
| 453 | |
---|
| 454 | reset(); |
---|
| 455 | |
---|
| 456 | // The range of the grid's y-axis 0..1 represents the distance between the |
---|
[1161] | 457 | // smallest and largest major ticks for all surfaces plotted. Translate |
---|
[1111] | 458 | // this surface's y-values (heights) into the grid's axis coordinates. |
---|
| 459 | |
---|
| 460 | float yScale = 1.0 / (gridPtr->yAxis.max() - gridPtr->yAxis.min()); |
---|
| 461 | float *p, *q, *pend; |
---|
| 462 | float *normHeights = new float[count]; |
---|
| 463 | for (p = heights_, pend = p + count, q = normHeights; p < pend; p++, q++) { |
---|
| 464 | *q = (*p - gridPtr->yAxis.min()) * yScale; |
---|
| 465 | } |
---|
| 466 | Vector3 *t, *texcoord; |
---|
| 467 | texcoord = new Vector3[count]; |
---|
| 468 | for (t = texcoord, p = normHeights, pend = p + count; p < pend; p++, t++) { |
---|
| 469 | t->set(0, 0, *p); |
---|
| 470 | } |
---|
| 471 | |
---|
[1161] | 472 | // Normalize the mesh coordinates (x and z min/max) the range of the major |
---|
| 473 | // ticks for the x and z grid axes as well. |
---|
[1111] | 474 | |
---|
| 475 | float xScale, zScale; |
---|
| 476 | float xMin, xMax, zMin, zMax; |
---|
| 477 | |
---|
| 478 | xScale = 1.0 / (gridPtr->xAxis.max() - gridPtr->xAxis.min()); |
---|
| 479 | xMin = (xAxis.min() - gridPtr->xAxis.min()) * xScale; |
---|
| 480 | xMax = (xAxis.max() - gridPtr->xAxis.min()) * xScale; |
---|
| 481 | zScale = 1.0 / (gridPtr->zAxis.max() - gridPtr->zAxis.min()); |
---|
| 482 | zMin = (zAxis.min() - gridPtr->zAxis.min()) * zScale; |
---|
| 483 | zMax = (zAxis.max() - gridPtr->zAxis.min()) * zScale; |
---|
| 484 | |
---|
| 485 | Vector3* vertices; |
---|
| 486 | vertices = createHeightVertices(xMin, zMin, xMax, zMax, xNum_, yNum_, |
---|
| 487 | normHeights); |
---|
| 488 | |
---|
| 489 | glGenBuffers(1, &_vertexBufferObjectID); |
---|
| 490 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); |
---|
| 491 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(Vector3), vertices, |
---|
| 492 | GL_STATIC_DRAW); |
---|
| 493 | glGenBuffers(1, &_textureBufferObjectID); |
---|
| 494 | glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); |
---|
| 495 | glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(float) * 3, texcoord, |
---|
| 496 | GL_STATIC_DRAW); |
---|
| 497 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
| 498 | delete [] texcoord; |
---|
| 499 | |
---|
| 500 | ContourLineFilter lineFilter; |
---|
[1493] | 501 | //lineFilter.transferFunction(_tfPtr); |
---|
[1111] | 502 | _contour = lineFilter.create(0.0f, 1.0f, 10, vertices, xNum_, yNum_); |
---|
| 503 | |
---|
| 504 | #if TOPCONTOUR |
---|
| 505 | ContourLineFilter topLineFilter; |
---|
| 506 | topLineFilter.setHeightTop(true); |
---|
| 507 | _topContour = topLineFilter.create(0.0f, 1.0f, 10, vertices, xNum_, yNum_); |
---|
| 508 | #endif |
---|
[1112] | 509 | this->createIndexBuffer(xNum_, yNum_, normHeights); |
---|
[1111] | 510 | delete [] normHeights; |
---|
| 511 | } |
---|
[1161] | 512 | |
---|
[1188] | 513 | void HeightMap::render_topview(graphics::RenderContext* renderContext, int render_width, int render_height) |
---|
| 514 | { |
---|
| 515 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 516 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
| 517 | glViewport(0, 0, render_width, render_height); |
---|
| 518 | glMatrixMode(GL_PROJECTION); |
---|
| 519 | glPushMatrix(); |
---|
| 520 | glLoadIdentity(); |
---|
| 521 | //gluOrtho2D(0, render_width, 0, render_height); |
---|
| 522 | glOrtho(-.5, .5, -.5, .5, -50, 50); |
---|
| 523 | glMatrixMode(GL_MODELVIEW); |
---|
| 524 | glPushMatrix(); |
---|
| 525 | glLoadIdentity(); |
---|
| 526 | |
---|
| 527 | glTranslatef(0.0, 0.0, -10.0); |
---|
| 528 | |
---|
| 529 | // put camera rotation and traslation |
---|
| 530 | //glScalef(1 / _scale.x, 1 / _scale.y , 1 / _scale.z); |
---|
| 531 | |
---|
| 532 | if (renderContext->getCullMode() == graphics::RenderContext::NO_CULL) { |
---|
| 533 | glDisable(GL_CULL_FACE); |
---|
| 534 | } else { |
---|
| 535 | glEnable(GL_CULL_FACE); |
---|
| 536 | glCullFace((GLuint) renderContext->getCullMode()); |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | glPolygonMode(GL_FRONT_AND_BACK, (GLuint) renderContext->getPolygonMode()); |
---|
| 540 | glShadeModel((GLuint) renderContext->getShadingModel()); |
---|
| 541 | |
---|
| 542 | glPushMatrix(); |
---|
| 543 | |
---|
| 544 | //glTranslatef(-_centerPoint.x, -_centerPoint.y, -_centerPoint.z); |
---|
| 545 | |
---|
| 546 | //glScalef(0.01, 0.01, 0.01f); |
---|
| 547 | glRotatef(90.0f, 1.0f, 0.0f, 0.0f); |
---|
| 548 | glTranslatef(-_centerPoint.x, -_centerPoint.y, -_centerPoint.z); |
---|
| 549 | if (_contour != NULL) { |
---|
| 550 | glDepthRange (0.001, 1.0); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | glEnable(GL_DEPTH_TEST); |
---|
| 554 | |
---|
| 555 | glEnable(GL_BLEND); |
---|
| 556 | glEnable(GL_TEXTURE_2D); |
---|
| 557 | if (_vertexBufferObjectID) |
---|
| 558 | { |
---|
| 559 | printf("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\n"); |
---|
| 560 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
| 561 | glShadeModel(GL_SMOOTH); |
---|
| 562 | glEnable(GL_BLEND); |
---|
| 563 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
| 564 | glDisableClientState(GL_COLOR_ARRAY); |
---|
| 565 | glDisableClientState(GL_INDEX_ARRAY); |
---|
| 566 | glDisableClientState(GL_NORMAL_ARRAY); |
---|
| 567 | |
---|
[1493] | 568 | if (_tfPtr != NULL) { |
---|
[1188] | 569 | cgGLBindProgram(_shader->getFP()); |
---|
| 570 | cgGLEnableProfile(CG_PROFILE_FP30); |
---|
| 571 | |
---|
[1493] | 572 | cgGLSetTextureParameter(_tfParam, _tfPtr->id()); |
---|
| 573 | cgGLEnableTextureParameter(_tfParam); |
---|
[1188] | 574 | |
---|
| 575 | glEnable(GL_TEXTURE_1D); |
---|
[1493] | 576 | _tfPtr->getTexture()->activate(); |
---|
[1188] | 577 | |
---|
| 578 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 579 | } |
---|
| 580 | else { |
---|
| 581 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); |
---|
| 585 | glVertexPointer(3, GL_FLOAT, 12, 0); |
---|
| 586 | |
---|
| 587 | glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); |
---|
| 588 | ::glTexCoordPointer(3, GL_FLOAT, 12, 0); |
---|
| 589 | |
---|
| 590 | #define _TRIANGLES_ |
---|
| 591 | #ifdef _TRIANGLES_ |
---|
| 592 | glDrawElements(GL_TRIANGLES, _indexCount, GL_UNSIGNED_INT, |
---|
| 593 | _indexBuffer); |
---|
| 594 | #else |
---|
| 595 | glDrawElements(GL_QUADS, _indexCount, GL_UNSIGNED_INT, |
---|
| 596 | _indexBuffer); |
---|
| 597 | #endif |
---|
| 598 | |
---|
| 599 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
| 600 | |
---|
| 601 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
[1493] | 602 | if (_tfPtr != NULL) { |
---|
| 603 | _tfPtr->getTexture()->deactivate(); |
---|
[1188] | 604 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 605 | |
---|
| 606 | cgGLDisableProfile(CG_PROFILE_FP30); |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | glShadeModel(GL_FLAT); |
---|
| 611 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
---|
| 612 | |
---|
| 613 | if (_contour != NULL) { |
---|
| 614 | if (_contourVisible) { |
---|
| 615 | glDisable(GL_BLEND); |
---|
| 616 | glDisable(GL_TEXTURE_2D); |
---|
| 617 | glColor4f(_contourColor.x, _contourColor.y, _contourColor.z, 1.0f); |
---|
| 618 | glDepthRange (0.0, 0.999); |
---|
| 619 | _contour->render(); |
---|
| 620 | glDepthRange (0.0, 1.0); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | #if TOPCONTOUR |
---|
| 624 | if (_topContourVisible) { |
---|
| 625 | glDisable(GL_BLEND); |
---|
| 626 | glDisable(GL_TEXTURE_2D); |
---|
| 627 | glColor4f(_contourColor.x, _contourColor.y, _contourColor.z, 1.0f); |
---|
| 628 | //glDepthRange (0.0, 0.999); |
---|
| 629 | _topContour->render(); |
---|
| 630 | //glDepthRange (0.0, 1.0); |
---|
| 631 | } |
---|
| 632 | #endif |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | glPopMatrix(); |
---|
| 636 | glPopMatrix(); |
---|
| 637 | |
---|
| 638 | glMatrixMode(GL_PROJECTION); |
---|
| 639 | glPushMatrix(); |
---|
| 640 | glMatrixMode(GL_MODELVIEW); |
---|
| 641 | |
---|
| 642 | glPopAttrib(); |
---|
| 643 | } |
---|
| 644 | |
---|