⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soperfgraph.cpp

📁 海量地形数据漫游系统,对于OPENGL开发人员具有一定的参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    objData.posValid = TRUE;    objData.sizeValid = TRUE;    SbVec2s winSize = vp.getWindowSize();    if (winSize[0] == 0)  winSize[0] = 1;    if (winSize[1] == 0)  winSize[1] = 1;    float pixSizeX = 1.f / winSize[0];    float pixSizeY = 1.f / winSize[1];    float realSizeX = parent->size.getValue()[0] + parent->sizeInPixels.getValue()[0] * pixSizeX;    float realSizeY = parent->size.getValue()[1] + parent->sizeInPixels.getValue()[1] * pixSizeY;        float adX,adY;    switch (parent->horAlignment.getValue()) {    case SoPerfGraph::LEFT:   adX = 0.f; break;    case SoPerfGraph::CENTER: adX = -realSizeX / 2.f; break;    case SoPerfGraph::RIGHT:      default:                  adX = -realSizeX; break;    }    switch (parent->vertAlignment.getValue()) {    case SoPerfGraph::TOP:    adY = -realSizeY; break;    case SoPerfGraph::HALF:   adY = -realSizeY / 2.f; break;    case SoPerfGraph::BOTTOM:    default:                  adY = 0.f; break;    }    float x1 = parent->position.getValue()[0] + (parent->positionInPixels.getValue()[0] * pixSizeX) + adX;    float y1 = parent->position.getValue()[1] + (parent->positionInPixels.getValue()[1] * pixSizeY) + adY;    float x2 = x1 + realSizeX;    float y2 = y1 + realSizeY;    // update coordinates    SbVec3f *c = coordNode->point.startEditing();    c[0].setValue(x1, y1, -5.f);    c[1].setValue(x2, y1, -5.f);    c[2].setValue(x1, y2, -5.f);    c[3].setValue(x2, y2, -5.f);    coordNode->point.finishEditing();    // update text position    //    // please note: The second translation have to "undone" the first one.    //    // another note: Shift by half of pixel was introduced by experiments     // with inconsistencies between rendered box corners and text positions.    // When resizing, text did not stayed 2 pixels from the corner but jumped between two and three.    //    textTranslationMaxNode->translation.setValue(x1+1.5f*pixSizeX,        y2-11.5f*pixSizeY,  -4.f);    textTranslationAvgNode->translation.setValue(x2-3.0f*pixSizeX-x1, y1-(y2-13.0f*pixSizeY), -3.f);     int minimalTexWidth  = int(realSizeX / pixSizeX / parent->dataScale.getValue()[0] + 0.5);    int minimalTexHeight = int(realSizeY / pixSizeY / parent->dataScale.getValue()[1] + 0.5);    texRequestedWidth  = (minimalTexWidth  <= 1) ? 1 : coin_next_power_of_two(minimalTexWidth-1);    texRequestedHeight = (minimalTexHeight <= 1) ? 1 : coin_next_power_of_two(minimalTexHeight-1);    SbVec2s currentTexSize;    int tmp;    textureNode->image.getValue(currentTexSize, tmp);    if (currentTexSize[0] != texRequestedHeight || currentTexSize[1] != texRequestedWidth) {      objData.textureNeedsResize = TRUE;      maxTexCoordX = float(minimalTexWidth)  / texRequestedWidth;      maxTexCoordY = float(minimalTexHeight) / texRequestedHeight;    }  }  if (!(vp == previousViewport) || !objData.vpValid) {    objData.vpValid = TRUE;    float cx,cy;    switch (parent->horScreenOrigin.getValue()) {    case SoPerfGraph::LEFT:   cx = 0.5f; break;    case SoPerfGraph::CENTER: cx = 0.f; break;    case SoPerfGraph::RIGHT:     default:                  cx = -0.5f; break;    }    switch (parent->vertScreenOrigin.getValue()) {    case SoPerfGraph::TOP:    cy = -0.5f; break;    case SoPerfGraph::HALF:   cy = 0.f; break;    case SoPerfGraph::BOTTOM:    default:                  cy = 0.5f; break;    }    camera->position.setValue(cx, cy, 1.f);    previousViewport = vp;  }  if (textureDataRequired && objData.textureNeedsResize) {    objData.textureNeedsResize = FALSE;        SbVec2s currentTexSize;    int tmp;    textureNode->image.getValue(currentTexSize, tmp);    if (currentTexSize[0] != texRequestedHeight || currentTexSize[1] != texRequestedWidth) {      // resize values array      int newNumValues = int(texRequestedWidth * maxTexCoordX + 0.5f) + NUM_VALUES_OVER;      int newIndex = 0;      float *newValues = new float[newNumValues];      double *newTimeStamps = new double[newNumValues];      int delta = newNumValues - numValues;            // fill beginning with zeros      while (newIndex<delta) {        newValues    [newIndex] = 0.f;        newTimeStamps[newIndex] = 0.f;        newIndex++;      }      // move data "after the pointer"      int moveNum = numValues - startIndex;      if (delta < 0)        moveNum += delta; // value is negative => using "+="      if (moveNum > 0) {        memcpy(&newValues    [newIndex], &values    [numValues-moveNum], moveNum*sizeof(float));        memcpy(&newTimeStamps[newIndex], &timeStamps[numValues-moveNum], moveNum*sizeof(double));        newIndex += moveNum;      }      // move data "before the pointer"      if (moveNum < 0)        moveNum += startIndex;      else        moveNum = startIndex;      memcpy(&newValues    [newIndex], &values    [0], moveNum*sizeof(float));      memcpy(&newTimeStamps[newIndex], &timeStamps[0], moveNum*sizeof(double));      newIndex += moveNum;      if (newIndex == newNumValues)        newIndex = 0;      assert(newIndex < newNumValues && "Bad index.");      // assign new values      delete[] values;      delete[] timeStamps;      values = newValues;      timeStamps = newTimeStamps;      startIndex = newIndex;      numValues = newNumValues;      // resize texture      textureNode->image.setValue(SbVec2s(texRequestedHeight, texRequestedWidth), TEXTURE_COMPONENTS, NULL);      // update texture coordinates and texture      redrawTexture();      updateTextureCoordinates(texRequestedWidth);    }  }}void SoPerfGraph::rasterizeColumn(unsigned char *buf, int size, const float v){  int n = int(v / THIS->renderedMax * size);  int x;  for(x=0; x<n && x<size; x++) {    *(buf+0) = 0;    *(buf+1) = 128;    *(buf+2) = 255;    *(buf+3) = 255;    buf += TEXTURE_COMPONENTS;  }  for(; x<size; x++) {    *(buf+0) = 0;    *(buf+1) = 0;    *(buf+2) = 128;    *(buf+3) = 128;    buf += TEXTURE_COMPONENTS;  }}void SoPerfGraphP::redrawTexture(){  // start texture editing  SbVec2s texSize;  int components;  unsigned char *img = textureNode->image.startEditing(texSize, components);  if (img) {#ifndef NDEBUG    unsigned char *base = img;#endif    int c = int(maxTexCoordX * texSize[1] +0.5f);    assert(c <= texSize[1]);    texIndex = c;    if (texIndex == texSize[1])      texIndex = 0;      // edit texture    int i = startIndex - c;    if (i<0)  i+=numValues;    while (--c >= 0) {      parent->rasterizeColumn(img, texSize[0], values[i]);      img += texSize[0]*TEXTURE_COMPONENTS;      if (++i==numValues)  i=0;    }    assert(i==startIndex && "Final index is not equal startIndex.");    assert(img-base <= texSize[0]*texSize[1]*TEXTURE_COMPONENTS);  }  textureNode->image.finishEditing();}void SoPerfGraphP::updateTextureCoordinates(int texSizeY){  SbVec2f *tc = texCoordNode->point.startEditing();  float tcRight = float(texIndex) / texSizeY;  float tcLeft = tcRight - maxTexCoordX;  tc[0][1] = tcLeft;  tc[2][1] = tcLeft;  tc[1][1] = tcRight;  tc[3][1] = tcRight;  texCoordNode->point.finishEditing();}float SoPerfGraphP::roundUp(float value){  // handle negative values  float sign = (value>=0.f) ? +1.f : -1.f;  value *= sign;  // handle zero and inf  if (value == 0.f)    return 1.f;  if (value / 10.f == value)    return value;  // compute exponent  float e = 1.;  if (value > 1.f)    while (value > 10.f) {      value /= 10.f;      e *= 10.f;    }  else    while (value < 1.f) {      value *= 10.f;      e /= 10.f;    }  // truncate number  int i = int(value);  if (float(i) != value)    i++;    // round up  int r;  if (i>10) r=20; else  if (i> 6) r=10; else  if (i> 4) r= 6; else  if (i> 2) r= 4; else  { assert(i>=1); r=2; }  // return value  return sign*r*e;}void SoPerfGraph::GLRender(SoGLRenderAction *action){  THIS->updateSceneIfNecessary(action->getViewportRegion(), TRUE);  SoPerfGraph::doAction(action);}void SoPerfGraph::getBoundingBox(SoGetBoundingBoxAction *action){  THIS->updateSceneIfNecessary(action->getViewportRegion(), FALSE);  SoPerfGraph::doAction((SoAction*)action);}void SoPerfGraph::pick(SoPickAction *action){#if 0 // Enabling picking can cause some weird behaviour,      // therefore it is disabled for SoPerfGraph.      //      // The reason is that the scene graph contains camera and      // there is known limitation in Inventor design, that      // cameras does not have elements, that will be able to restore      // original camera state after traversing of SoPerfGraph is done.      // During the rendering, everything seems fine since modelview stack is used,      // but during the raypick action, orthographic projection of SoPerfGraph      // stays for the rest of the scene (e.g. camera state is not restored when      // traversal is leaving SoPerfGraph separator).  THIS->updateSceneIfNecessary(action->getViewportRegion(), FALSE);  SoPerfGraph::doAction((SoAction*)action);#endif}void SoPerfGraph::doAction(SoAction *action){  THIS->children.traverse(action);}void SoPerfGraph::initClass(){  SO_NODE_INIT_CLASS(SoPerfGraph, SoNode, "Node");}void SoPerfGraph::cleanup(){  SoPerfGraph::atexit_cleanup();}void SoPerfGraph::callback(SoCallbackAction *action){  SoPerfGraph::doAction((SoAction*)action);}void SoPerfGraph::getMatrix(SoGetMatrixAction *action){  SoPerfGraph::doAction((SoAction*)action);}void SoPerfGraph::handleEvent(SoHandleEventAction *action){  SoPerfGraph::doAction((SoAction*)action);}void SoPerfGraph::getPrimitiveCount(SoGetPrimitiveCountAction *action){  SoPerfGraph::doAction((SoAction*)action);}void SoPerfGraph::audioRender(SoAudioRenderAction *action){  SoPerfGraph::doAction((SoAction*)action);}SoChildList* SoPerfGraph::getChildren() const{  return &THIS->children;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -