Privacy
An open-source, flexible 3D physical simulation framework
VertexBufferTerrain.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, 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 Langosz
20  */
21 
22 /* The prototypes are only declared in GL/glext.h if this is defined */
23 #ifndef GL_GLEXT_PROTOTYPES
24 #define GL_GLEXT_PROTOTYPES 1 //for glGenBuffers, glBindBuffer,
25  //glDeleteBuffers
26 #endif
27 
28 
29 // On MS Windows MultiResHeightMapRenderer.h needs to be included before
30 // any OpenGL stuff!
32 
33 #ifdef HAVE_OSG_VERSION_H
34  #include <osg/Version>
35 #else
36  #include <osg/Export>
37 #endif
38 
39 #include "VertexBufferTerrain.h"
40 
41 #ifdef WIN32
42 #include <windows.h>
43 #endif
44 
45 //#define DEBUG_TIME
46 
47 #ifdef DEBUG_TIME
48 #include <mars/utils/misc.h>
49 #endif
50 
51 namespace osg_terrain {
52 
53  VertexBufferTerrain::VertexBufferTerrain(int width, int height, double scaleZ,
54  int resolution, int depth) {
55 
56  setSupportsDisplayList(false);
57  setUseDisplayList(false);
58 
59  int steps = resolution;
60  mrhmr = new MultiResHeightMapRenderer(steps, steps, width, height,
61  scaleZ, 1.0/width, 1.0/height, depth,
62  "heightmap.png");
63 
64  width = height = scale = 1.0;
65  mrhmr->setDrawWireframe(true);
66  mrhmr->setDrawSolid(false);
67  }
68  /*
69  VertexBufferTerrain::VertexBufferTerrain(const interfaces::terrainStruct *ts) {
70 
71  setSupportsDisplayList(false);
72 
73  mrhmr = new MultiResHeightMapRenderer(ts->width, ts->height,
74  ts->targetWidth, ts->targetHeight,
75  1.0, 1.0, 1.0, ts->texScaleX,
76  ts->texScaleY);
77  double maxHeight = 0.0;
78  double offset;
79 
80  for(int i=0; i<ts->height; ++i)
81  for(int j=0; j<ts->width; ++j) {
82  if(i==0 || j==0 || i==ts->height-1 || j==ts->width-1) offset = -0.1;
83  else offset = 0.0;
84  mrhmr->setHeight(j, i, offset+ts->scale*ts->pixelData[i*ts->width+j]);
85  if(ts->pixelData[i*ts->width+j]*ts->scale > maxHeight) {
86  maxHeight = ts->pixelData[i*ts->width+j]*ts->scale;
87  }
88  }
89 
90  width = ts->targetWidth;
91  height = ts->targetHeight;
92  scale = maxHeight;
93  }
94  */
96  delete mrhmr;
97  }
98 
99  void VertexBufferTerrain::drawImplementation(osg::RenderInfo& renderInfo) const{
100 #ifdef DEBUG_TIME
101  long drawTime = utils::getTime();
102 #endif
103  osg::State& state = *renderInfo.getState();
104  state.disableAllVertexArrays();
105 #if (OPENSCENEGRAPH_MAJOR_VERSION < 3 || ( OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION < 5) || ( OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION == 5 && OPENSCENEGRAPH_PATCH_VERSION < 9))
106  osg::ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers();
107 #elif (OPENSCENEGRAPH_MAJOR_VERSION > 3 || (OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION > 5) || (OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION == 5 && OPENSCENEGRAPH_PATCH_VERSION >= 9))
108  osg::AttributeDispatchers& arrayDispatchers = state.getAttributeDispatchers();
109 #else
110 #error Unknown OSG Version
111 #endif
112 
113  arrayDispatchers.reset();
114  //arrayDispatchers.dispatch(osg::Geometry::BIND_OVERALL,0);
115 
116  state.lazyDisablingOfVertexAttributes();
117  state.applyDisablingOfVertexAttributes();
118 
119  mrhmr->render();
120 #ifdef DEBUG_TIME
121  drawTime = utils::getTimeDiff(drawTime);
122  if(drawTime > 1)
123  fprintf(stderr, "MultiResHeightMapRenderer: drawTime: %ld\n", drawTime);
124 #endif
125  }
126 
127  void VertexBufferTerrain::collideSphere(double xPos, double yPos,
128  double zPos, double radius) {
129 
130  //mrhmr->collideSphere(xPos, yPos, zPos, radius);
131  }
132 
133 #if (OPENSCENEGRAPH_MAJOR_VERSION < 3 || ( OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION < 4))
134  osg::BoundingBox VertexBufferTerrain::computeBound() const {
135  return osg::BoundingBox(0.0, 0.0, 0.0, width, height, scale);
136  }
137 #elif (OPENSCENEGRAPH_MAJOR_VERSION > 3 || (OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION >= 4))
138  osg::BoundingSphere VertexBufferTerrain::computeBound() const {
139  return osg::BoundingSphere(osg::Vec3(width*0.5, height*0.5, scale*0.5), sqrt(width*width+height*height+scale*scale));
140  }
141 #else
142 #error Unknown OSG Version
143 #endif
144 
146  if(val) {
147  mrhmr->setDrawSolid(false);
148  mrhmr->setDrawWireframe(true);
149  }
150  else {
151  mrhmr->setDrawSolid(true);
152  mrhmr->setDrawWireframe(false);
153  }
154  }
155 
156  void VertexBufferTerrain::setCameraPosition(double x, double y) {
157  if(mrhmr) mrhmr->setCameraPosition(x, y);
158  }
159 
160 } // end of namespace osg_terrain
MultiResHeightMapRenderer * mrhmr
virtual void drawImplementation(osg::RenderInfo &renderInfo) const
void setCameraPosition(double x, double y)
long long getTime()
Definition: misc.h:71
VertexBufferTerrain(int width, int height, double scaleZ, int resolution, int depth)
void collideSphere(double xPos, double yPos, double zPos, double radius)
osg::BoundingBox computeBound() const
long long getTimeDiff(long long start)
returns the time difference between now and a given reference.
Definition: misc.h:88