Privacy
An open-source, flexible 3D physical simulation framework
VertexBufferTerrain.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Created by Malte Roemmermann
20  */
21 
22 #ifdef USE_VERTEX_BUFFER
23 
24 /* The prototypes are only declared in GL/glext.h if this is defined */
25 #ifndef GL_GLEXT_PROTOTYPES
26 #define GL_GLEXT_PROTOTYPES 1 //for glGenBuffers, glBindBuffer,
27  //glDeleteBuffers
28 #endif
29 
30 #include "VertexBufferTerrain.h"
31 
32 #ifdef WIN32
33 #include <windows.h>
34 #endif
35 
36 //#define DEBUG_TIME
37 
38 #ifdef DEBUG_TIME
39 #include <mars/utils/misc.h>
40 #endif
41 
42 namespace mars {
43  namespace graphics {
44 
46 
47  setSupportsDisplayList(false);
48  setUseDisplayList(false);
49 
50  mrhmr = new MultiResHeightMapRenderer(1, 1, 1, 1, 1.0, 1.0, 1, 1.0, 1.0);
51  width = height = scale = 1.0;
52  }
53 
54  VertexBufferTerrain::VertexBufferTerrain(const interfaces::terrainStruct *ts) {
55 
56  setSupportsDisplayList(false);
57 
58  mrhmr = new MultiResHeightMapRenderer(ts->width, ts->height,
59  ts->targetWidth, ts->targetHeight,
60  1.0, 1.0, 1.0, ts->texScaleX,
61  ts->texScaleY);
62  double maxHeight = 0.0;
63  double offset;
64 
65  for(int i=0; i<ts->height; ++i)
66  for(int j=0; j<ts->width; ++j) {
67  if(i==0 || j==0 || i==ts->height-1 || j==ts->width-1) offset = -0.1;
68  else offset = 0.0;
69  mrhmr->setHeight(j, i, offset+ts->scale*ts->pixelData[i*ts->width+j]);
70  if(ts->pixelData[i*ts->width+j]*ts->scale > maxHeight) {
71  maxHeight = ts->pixelData[i*ts->width+j]*ts->scale;
72  }
73  }
74 
75  width = ts->targetWidth;
76  height = ts->targetHeight;
77  scale = maxHeight;
78  }
79 
81  delete mrhmr;
82  }
83 
84  void VertexBufferTerrain::drawImplementation(osg::RenderInfo& renderInfo) const{
85 #ifdef DEBUG_TIME
86  long drawTime = utils::getTime();
87 #endif
88  osg::State& state = *renderInfo.getState();
89  state.disableAllVertexArrays();
90  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
91 
92  arrayDispatchers.reset();
93  //arrayDispatchers.dispatch(osg::Geometry::BIND_OVERALL,0);
94 
95  state.lazyDisablingOfVertexAttributes();
96  state.applyDisablingOfVertexAttributes();
97 
98  mrhmr->render();
99 #ifdef DEBUG_TIME
100  drawTime = utils::getTimeDiff(drawTime);
101  if(drawTime > 1)
102  fprintf(stderr, "MultiResHeightMapRenderer: drawTime: %ld\n", drawTime);
103 #endif
104  }
105 
106  void VertexBufferTerrain::collideSphere(double xPos, double yPos,
107  double zPos, double radius) {
108 
109  mrhmr->collideSphere(xPos, yPos, zPos, radius);
110  }
111 
112  osg::BoundingBox VertexBufferTerrain::computeBound() const {
113  return osg::BoundingBox(0.0, 0.0, 0.0, width, height, scale);
114  }
115 
116  void VertexBufferTerrain::setSelected(bool val) {
117  if(val) {
118  mrhmr->setDrawSolid(false);
119  mrhmr->setDrawWireframe(true);
120  }
121  else {
122  mrhmr->setDrawSolid(true);
123  mrhmr->setDrawWireframe(false);
124  }
125  }
126 
127  } // end of namespace graphics
128 } // end of namespace mars
129 
130 #endif /* USE_VERTEX_BUFFER */
virtual void drawImplementation(osg::RenderInfo &renderInfo) const
void collideSphere(double xPos, double yPos, double zPos, double radius)
void setDrawSolid(bool drawSolid)
virtual osg::BoundingBox computeBound() const
long long getTime()
Definition: misc.h:71
Copyright 2012, DFKI GmbH Robotics Innovation Center.
void collideSphere(double xPos, double yPos, double zPos, double radius)
MultiResHeightMapRenderer * mrhmr
void setDrawWireframe(bool drawWireframe)
void setHeight(unsigned int gridX, unsigned int gridY, double height)
long long getTimeDiff(long long start)
returns the time difference between now and a given reference.
Definition: misc.h:88