Ignore:
Timestamp:
Aug 13, 2008 1:33:28 PM (16 years ago)
Author:
gah
Message:

nanovis/heightmap update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/HeightMap.cpp

    r1028 r1111  
    11 
    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>
    83#include <stdlib.h>
    9 #include <memory.h>
    10 #include <R2/R2FilePath.h>
    11 #include "RpField1D.h"
    124#include <sys/time.h>
    135#include <sys/types.h>
    146#include <unistd.h>
    157#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"
    1719
    1820bool HeightMap::update_pending = false;
     
    3537    _visible(false),
    3638    _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)
    3841{
    3942    _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");
    4644    _tf = _shader->getNamedParameterFromFP("tf");
    4745}
     
    7371    glPushMatrix();
    7472
     73#ifndef notdef
    7574    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
    7978    glTranslatef(-_centerPoint.x, -_centerPoint.y, -_centerPoint.z);
    8079
    81     if (_contour) {
     80    if (_contour != NULL) {
    8281        glDepthRange (0.001, 1.0);
    8382    }
     
    140139    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    141140   
    142     if (_contour) {
     141    if (_contour != NULL) {
    143142        if (_contourVisible) {
    144143            glDisable(GL_BLEND);
     
    176175    int boundaryHeight = zCount - 1;
    177176    int* ptr = indexBuffer;
    178         int index1, index2, index3, index4;
     177    int index1, index2, index3, index4;
    179178    bool index1Valid, index2Valid, index3Valid, index4Valid;
    180179    index1Valid = index2Valid = index3Valid = index4Valid = true;
     
    257256    reset();
    258257   
     258    heights_ = (float *)heights;
    259259    float min, max;
    260260    min = heights[0].y, max = heights[0].y;
     
    325325
    326326void
    327 HeightMap::setHeight(float startX, float startY, float endX, float endY,
    328                      int xCount, int yCount, float* heights)
    329 {
    330     _vertexCount = xCount * yCount;
    331    
     327HeightMap::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;
    332333    reset();
    333 
     334   
     335    // Get the min/max of the heights. */
    334336    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) {
    338339        if (min > heights[i]) {
    339340            min = heights[i];
     
    342343        }
    343344    }
    344     _scale.x = endX - startX;
    345     _scale.y = max - min;
    346     _scale.z = endY - startY;
    347 
    348345    wAxis.SetRange(min, max);
    349     xAxis.SetRange(startX, endX);
    350346    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
    352357    update_pending = true;
    353358
    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) {
    359364        texcoord[i].set(0, 0, heights[i]);
    360365    }
    361366   
    362     Vector3* heightMap = createHeightVertices(startX, startY, endX, endY, xCount, yCount, heights);
     367    Vector3* heightMap = createHeightVertices(xMin, yMin, xMax, yMax, xNum, yNum, heights);
    363368   
    364369    glGenBuffers(1, &_vertexBufferObjectID);
     
    377382    ContourLineFilter lineFilter;
    378383    //lineFilter.setColorMap(_colorMap);
    379     _contour = lineFilter.create(0.0f, 1.0f, 10, heightMap, xCount, yCount);
     384    _contour = lineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum);
    380385   
    381386#if TOPCONTOUR
    382387    ContourLineFilter topLineFilter;
    383388    topLineFilter.setHeightTop(true);
    384     _topContour = topLineFilter.create(0.0f, 1.0f, 10, heightMap, xCount, yCount);
     389    _topContour = topLineFilter.create(0.0f, 1.0f, 10, heightMap, xNum, yNum);
    385390#endif
    386391
     
    388393    //if (heightMap)
    389394    //{
    390     //  VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, xCount * yCount,
    391     // sizeof(Vector3) * xCount * yCount, heightMap, false);
     395    //  VertexBuffer* vertexBuffer = new VertexBuffer(VertexBuffer::POSITION3, xNum * yNum,
     396    // sizeof(Vector3) * xNum * yNum, heightMap, false);
    392397    if (_indexBuffer) {
    393398        free(_indexBuffer);
    394399    }
    395     this->createIndexBuffer(xCount, yCount, _indexBuffer, _indexCount, heights);
     400    this->createIndexBuffer(xNum, yNum, _indexBuffer, _indexCount, heights);
    396401    //}
    397402    //else
     
    399404    //printf("ERROR - HeightMap::setHeight\n");
    400405    //}
    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
     409Vector3*
     410HeightMap::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);
    406414
    407415    Vector3* dstDataPtr = vertices;
    408416    float* srcDataPtr = height;
    409417   
    410     for (int y = 0; y < yCount; ++y) {
     418    for (int y = 0; y < yNum; ++y) {
    411419        float yCoord;
    412420
    413         yCoord = startY + ((endY - startY) * y) / (yCount - 1);
    414         for (int x = 0; x < xCount; ++x) {
     421        yCoord = yMin + ((yMax - yMin) * y) / (yNum - 1);
     422        for (int x = 0; x < xNum; ++x) {
    415423            float xCoord;
    416424
    417             xCoord = startX + ((endX - startX) * x) / (xCount - 1);
     425            xCoord = xMin + ((xMax - xMin) * x) / (xNum - 1);
    418426            dstDataPtr->set(xCoord, *srcDataPtr, yCoord);
    419427
     
    432440}
    433441
     442// Maps the data coordinates of the surface into the grid's axes.
     443void
     444HeightMap::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.