Changeset 1111 for trunk/packages/vizservers/nanovis/HeightMap.cpp
- Timestamp:
- Aug 13, 2008, 1:33:28 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanovis/HeightMap.cpp
r1028 r1111 1 1 2 #include <GL/glew.h> 3 #include <GL/gl.h> 4 #include "HeightMap.h" 5 #include "ContourLineFilter.h" 6 #include "TypeDefs.h" 7 #include <Texture1D.h> 2 #include <memory.h> 8 3 #include <stdlib.h> 9 #include <memory.h>10 #include <R2/R2FilePath.h>11 #include "RpField1D.h"12 4 #include <sys/time.h> 13 5 #include <sys/types.h> 14 6 #include <unistd.h> 15 7 #include <fcntl.h> 16 #include <RenderContext.h> 8 #include <stdlib.h> 9 #include <GL/glew.h> 10 #include <GL/gl.h> 11 #include "Grid.h" 12 #include "HeightMap.h" 13 #include "ContourLineFilter.h" 14 #include "TypeDefs.h" 15 #include "Texture1D.h" 16 #include "R2/R2FilePath.h" 17 #include "RpField1D.h" 18 #include "RenderContext.h" 17 19 18 20 bool HeightMap::update_pending = false; … … 35 37 _visible(false), 36 38 _scale(1.0f, 1.0f, 1.0f), 37 _centerPoint(0.0f, 0.0f, 0.0f) 39 _centerPoint(0.0f, 0.0f, 0.0f), 40 heights_(NULL) 38 41 { 39 42 _shader = new NvShader(); 40 41 R2string path = R2FilePath::getInstance()->getPath("heightcolor.cg"); 42 if (path.getLength() == 0) { 43 printf("ERROR : file not found %s\n", "heightcolor.cg"); 44 } 45 _shader->loadFragmentProgram(path, "main"); 43 _shader->loadFragmentProgram("heightcolor.cg", "main"); 46 44 _tf = _shader->getNamedParameterFromFP("tf"); 47 45 } … … 73 71 glPushMatrix(); 74 72 73 #ifndef notdef 75 74 if (_scale.x != 0.0) { 76 glScalef(1 / _scale.x, 1 / _scale. x , 1 / _scale.x);77 } 78 75 glScalef(1 / _scale.x, 1 / _scale.y , 1 / _scale.z); 76 } 77 #endif 79 78 glTranslatef(-_centerPoint.x, -_centerPoint.y, -_centerPoint.z); 80 79 81 if (_contour ) {80 if (_contour != NULL) { 82 81 glDepthRange (0.001, 1.0); 83 82 } … … 140 139 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 141 140 142 if (_contour ) {141 if (_contour != NULL) { 143 142 if (_contourVisible) { 144 143 glDisable(GL_BLEND); … … 176 175 int boundaryHeight = zCount - 1; 177 176 int* ptr = indexBuffer; 178 177 int index1, index2, index3, index4; 179 178 bool index1Valid, index2Valid, index3Valid, index4Valid; 180 179 index1Valid = index2Valid = index3Valid = index4Valid = true; … … 257 256 reset(); 258 257 258 heights_ = (float *)heights; 259 259 float min, max; 260 260 min = heights[0].y, max = heights[0].y; … … 325 325 326 326 void 327 HeightMap::setHeight(float startX, float startY, float endX, float endY, 328 int xCount, int yCount, float* heights) 329 { 330 _vertexCount = xCount * yCount; 331 327 HeightMap::setHeight(float xMin, float yMin, float xMax, float yMax, 328 int xNum, int yNum, float* heights) 329 { 330 _vertexCount = xNum * yNum; 331 xNum_ = xNum, yNum_ = yNum; 332 heights_ = heights; 332 333 reset(); 333 334 335 // Get the min/max of the heights. */ 334 336 float min, max; 335 min = heights[0], max = heights[0]; 336 int count = xCount * yCount; 337 for (int i = 0; i < count; ++i) { 337 min = max = heights[0]; 338 for (int i = 0; i < _vertexCount; ++i) { 338 339 if (min > heights[i]) { 339 340 min = heights[i]; … … 342 343 } 343 344 } 344 _scale.x = endX - startX;345 _scale.y = max - min;346 _scale.z = endY - startY;347 348 345 wAxis.SetRange(min, max); 349 xAxis.SetRange(startX, endX);350 346 yAxis.SetRange(min, max); 351 zAxis.SetRange(startY, endY); 347 xAxis.SetRange(xMin, xMax); 348 zAxis.SetRange(yMin, yMax); 349 350 351 min = 0.0, max = 1.0; 352 xMin = yMin = min = 0.0; 353 xMax = yMax = max = 1.0; 354 // Save the scales. 355 _scale.x = _scale.y = _scale.z = 1.0; 356 352 357 update_pending = true; 353 358 354 _centerPoint.set( _scale.x * 0.5 + startX, _scale.y * 0.5 + min,355 _scale.z * 0.5 + startY); 356 357 Vector3* texcoord = new Vector3[ count];358 for (int i = 0; i < count; ++i) {359 _centerPoint.set(0.5, 0.5, 0.5); 360 361 #ifndef notdef 362 Vector3* texcoord = new Vector3[_vertexCount]; 363 for (int i = 0; i < _vertexCount; ++i) { 359 364 texcoord[i].set(0, 0, heights[i]); 360 365 } 361 366 362 Vector3* heightMap = createHeightVertices( startX, startY, endX, endY, xCount, yCount, heights);367 Vector3* heightMap = createHeightVertices(xMin, yMin, xMax, yMax, xNum, yNum, heights); 363 368 364 369 glGenBuffers(1, &_vertexBufferObjectID); … … 377 382 ContourLineFilter lineFilter; 378 383 //lineFilter.setColorMap(_colorMap); 379 _contour = lineFilter.create(0.0f, 1.0f, 10, heightMap, x Count, yCount);384 _contour = lineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum); 380 385 381 386 #if TOPCONTOUR 382 387 ContourLineFilter topLineFilter; 383 388 topLineFilter.setHeightTop(true); 384 _topContour = topLineFilter.create(0.0f, 1.0f, 10, heightMap, x Count, yCount);389 _topContour = topLineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum); 385 390 #endif 386 391 … … 388 393 //if (heightMap) 389 394 //{ 390 // VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, x Count * yCount,391 // sizeof(Vector3) * x Count * yCount, heightMap, false);395 // VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, xNum * yNum, 396 // sizeof(Vector3) * xNum * yNum, heightMap, false); 392 397 if (_indexBuffer) { 393 398 free(_indexBuffer); 394 399 } 395 this->createIndexBuffer(x Count, yCount, _indexBuffer, _indexCount, heights);400 this->createIndexBuffer(xNum, yNum, _indexBuffer, _indexCount, heights); 396 401 //} 397 402 //else … … 399 404 //printf("ERROR - HeightMap::setHeight\n"); 400 405 //} 401 } 402 403 Vector3* HeightMap::createHeightVertices(float startX, float startY, float endX, float endY, int xCount, int yCount, float* height) 404 { 405 Vector3* vertices = (Vector3*) malloc(sizeof(Vector3) * xCount * yCount); 406 #endif 407 } 408 409 Vector3* 410 HeightMap::createHeightVertices(float xMin, float yMin, float xMax, 411 float yMax, int xNum, int yNum, float* height) 412 { 413 Vector3* vertices = (Vector3*) malloc(sizeof(Vector3) * xNum * yNum); 406 414 407 415 Vector3* dstDataPtr = vertices; 408 416 float* srcDataPtr = height; 409 417 410 for (int y = 0; y < y Count; ++y) {418 for (int y = 0; y < yNum; ++y) { 411 419 float yCoord; 412 420 413 yCoord = startY + ((endY - startY) * y) / (yCount- 1);414 for (int x = 0; x < x Count; ++x) {421 yCoord = yMin + ((yMax - yMin) * y) / (yNum - 1); 422 for (int x = 0; x < xNum; ++x) { 415 423 float xCoord; 416 424 417 xCoord = startX + ((endX - startX) * x) / (xCount- 1);425 xCoord = xMin + ((xMax - xMin) * x) / (xNum - 1); 418 426 dstDataPtr->set(xCoord, *srcDataPtr, yCoord); 419 427 … … 432 440 } 433 441 442 // Maps the data coordinates of the surface into the grid's axes. 443 void 444 HeightMap::MapToGrid(Grid *gridPtr) 445 { 446 int count = xNum_ * yNum_; 447 448 reset(); 449 450 // The range of the grid's y-axis 0..1 represents the distance between the 451 // smallest and largest major ticks for all surfaces plotted. Translate 452 // this surface's y-values (heights) into the grid's axis coordinates. 453 454 float yScale = 1.0 / (gridPtr->yAxis.max() - gridPtr->yAxis.min()); 455 float *p, *q, *pend; 456 float *normHeights = new float[count]; 457 for (p = heights_, pend = p + count, q = normHeights; p < pend; p++, q++) { 458 *q = (*p - gridPtr->yAxis.min()) * yScale; 459 } 460 Vector3 *t, *texcoord; 461 texcoord = new Vector3[count]; 462 for (t = texcoord, p = normHeights, pend = p + count; p < pend; p++, t++) { 463 t->set(0, 0, *p); 464 } 465 466 // Normalize the mesh coordinates (x and z min/max) the range of 467 // the major ticks for the x and z grid axes as well. 468 469 float xScale, zScale; 470 float xMin, xMax, zMin, zMax; 471 472 xScale = 1.0 / (gridPtr->xAxis.max() - gridPtr->xAxis.min()); 473 xMin = (xAxis.min() - gridPtr->xAxis.min()) * xScale; 474 xMax = (xAxis.max() - gridPtr->xAxis.min()) * xScale; 475 zScale = 1.0 / (gridPtr->zAxis.max() - gridPtr->zAxis.min()); 476 zMin = (zAxis.min() - gridPtr->zAxis.min()) * zScale; 477 zMax = (zAxis.max() - gridPtr->zAxis.min()) * zScale; 478 479 Vector3* vertices; 480 vertices = createHeightVertices(xMin, zMin, xMax, zMax, xNum_, yNum_, 481 normHeights); 482 483 glGenBuffers(1, &_vertexBufferObjectID); 484 glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID); 485 glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(Vector3), vertices, 486 GL_STATIC_DRAW); 487 glGenBuffers(1, &_textureBufferObjectID); 488 glBindBuffer(GL_ARRAY_BUFFER, _textureBufferObjectID); 489 glBufferData(GL_ARRAY_BUFFER, _vertexCount * sizeof(float) * 3, texcoord, 490 GL_STATIC_DRAW); 491 glBindBuffer(GL_ARRAY_BUFFER, 0); 492 delete [] texcoord; 493 494 ContourLineFilter lineFilter; 495 //lineFilter.setColorMap(_colorMap); 496 _contour = lineFilter.create(0.0f, 1.0f, 10, vertices, xNum_, yNum_); 497 498 #if TOPCONTOUR 499 ContourLineFilter topLineFilter; 500 topLineFilter.setHeightTop(true); 501 _topContour = topLineFilter.create(0.0f, 1.0f, 10, vertices, xNum_, yNum_); 502 #endif 503 if (_indexBuffer) { 504 free(_indexBuffer); 505 } 506 this->createIndexBuffer(xNum_, yNum_, _indexBuffer, _indexCount, 507 normHeights); 508 delete [] normHeights; 509 }
Note: See TracChangeset
for help on using the changeset viewer.