3 * Copyright (C) 2006-2007 Mathias Froehlich, Tim Moore
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 # include <simgear_config.h>
26 #include "SGOceanTile.hxx"
29 #include <simgear/compiler.h>
32 #include <osg/Geometry>
33 #include <osg/MatrixTransform>
34 #include <osg/StateSet>
36 #include <simgear/bucket/newbucket.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/math/sg_types.hxx>
39 #include <simgear/misc/texcoord.hxx>
40 #include <simgear/scene/material/mat.hxx>
41 #include <simgear/scene/material/matlib.hxx>
42 #include <simgear/scene/util/VectorArrayAdapter.hxx>
44 using namespace simgear;
45 // Ocean tile with curvature and apron to hide cracks. The cracks are
46 // mostly with adjoining coastal tiles that assume a flat ocean
47 // between corners of a tile; they also hide the micro cracks between
48 // adjoining ocean tiles. This is probably over-engineered, but it
49 // serves as a testbed for some things that will come later.
51 // Helper class for building and accessing the mesh. The layout of the
52 // points in the mesh is a little wacky. First is the bottom row of
53 // the points for the apron. Next is the left apron point, the points
54 // in the mesh, and the right apron point, for each of the rows of the
55 // mesh; the points for the top apron come last. This order should
56 // help with things like vertex caching in the OpenGL driver, though
57 // it may be superfluous for such a small mesh.
60 const int lonPoints = 5;
61 const int latPoints = 5;
66 geoPoints(latPoints * lonPoints + 2 * (lonPoints + latPoints)),
67 geod_nodes(latPoints * lonPoints),
68 vl(new osg::Vec3Array(geoPoints)),
69 nl(new osg::Vec3Array(geoPoints)),
70 tl(new osg::Vec2Array(geoPoints)),
71 vlArray(*vl, lonPoints + 2, lonPoints, 1),
72 nlArray(*nl, lonPoints + 2, lonPoints, 1),
73 tlArray(*tl, lonPoints + 2, lonPoints, 1)
77 SGGeod geod[latPoints][lonPoints];
78 SGVec3f normals[latPoints][lonPoints];
79 SGVec3d rel[latPoints][lonPoints];
81 point_list geod_nodes;
86 VectorArrayAdapter<osg::Vec3Array> vlArray;
87 VectorArrayAdapter<osg::Vec3Array> nlArray;
88 VectorArrayAdapter<osg::Vec2Array> tlArray;
90 void calcMesh(const SGVec3d& cartCenter, const SGQuatd& orient,
91 double clon, double clat,
92 double height, double width, double tex_width);
93 void calcApronPt(int latIdx, int lonIdx, int latInner, int lonInner,
94 int destIdx, double tex_width);
95 void calcApronPts(double tex_width);
100 void OceanMesh::calcMesh(const SGVec3d& cartCenter, const SGQuatd& orient,
101 double clon, double clat,
102 double height, double width, double tex_width)
104 // Calculate vertices. By splitting the tile up into 4 quads on a
105 // side we avoid curvature-of-the-earth problems; the error should
106 // be less than .5 meters.
107 double longInc = width * .25;
108 double latInc = height * .25;
109 double startLat = clat - height * .5;
110 double startLon = clon - width * .5;
111 for (int j = 0; j < latPoints; j++) {
112 double lat = startLat + j * latInc;
113 for (int i = 0; i < lonPoints; i++) {
114 geod[j][i] = SGGeod::fromDeg(startLon + i * longInc, lat);
115 SGVec3d cart = SGVec3d::fromGeod(geod[j][i]);
116 rel[j][i] = orient.transform(cart - cartCenter);
117 normals[j][i] = toVec3f(orient.transform(normalize(cart)));
121 // Calculate texture coordinates
122 point_list geod_nodes(latPoints * lonPoints);
123 VectorArrayAdapter<point_list> geodNodesArray(geod_nodes, lonPoints);
124 int_list rectangle(latPoints * lonPoints);
125 VectorArrayAdapter<int_list> rectArray(rectangle, lonPoints);
126 for (int j = 0; j < latPoints; j++) {
127 for (int i = 0; i < lonPoints; i++) {
128 geodNodesArray(j, i) = Point3D(geod[j][i].getLongitudeDeg(),
129 geod[j][i].getLatitudeDeg(),
130 geod[j][i].getElevationM());
131 rectArray(j, i) = j * 5 + i;
134 point_list texs = sgCalcTexCoords( clat, geod_nodes, rectangle,
135 1000.0 / tex_width );
136 VectorArrayAdapter<point_list> texsArray(texs, lonPoints);
138 for (int j = 0; j < latPoints; j++) {
139 for (int i = 0; i < lonPoints; ++i) {
140 vlArray(j, i) = rel[j][i].osg();
141 nlArray(j, i) = normals[j][i].osg();
142 tlArray(j, i) = texsArray(j, i).toSGVec2f().osg();
148 // Apron points. For each point on the edge we'll go 150
149 // metres "down" and 40 metres "out" to create a nice overlap. The
150 // texture should be applied according to this dimension. The
151 // normals of the apron polygons will be the same as the those of
152 // the points on the edge to better disguise the apron.
153 void OceanMesh::calcApronPt(int latIdx, int lonIdx, int latInner, int lonInner,
154 int destIdx, double tex_width)
156 static const float downDist = 150.0f;
157 static const float outDist = 40.0f;
158 // Get vector along edge, in the right direction to make a cross
159 // product with the normal vector that will point out from the
161 osg::Vec3f edgePt = vlArray(latIdx, lonIdx);
163 if (lonIdx == lonInner) { // bottom or top edge
165 edgeVec = vlArray(latIdx, lonIdx - 1) - edgePt;
167 edgeVec = edgePt - vlArray(latIdx, lonIdx + 1);
168 if (latIdx > latInner)
169 edgeVec = -edgeVec; // Top edge
170 } else { // right or left edge
172 edgeVec = edgePt - vlArray(latIdx - 1, lonIdx);
174 edgeVec = vlArray(latIdx + 1, lonIdx) - edgePt;
175 if (lonIdx > lonInner) // right edge
179 osg::Vec3f outVec = nlArray(latIdx, lonIdx) ^ edgeVec;
181 = edgePt - nlArray(latIdx, lonIdx) * downDist + outVec * outDist;
182 (*nl)[destIdx] = nlArray(latIdx, lonIdx);
183 static const float apronDist
184 = sqrtf(downDist * downDist + outDist * outDist);
185 float texDelta = apronDist / tex_width;
186 if (lonIdx == lonInner) {
187 if (latIdx > latInner)
189 = tlArray(latIdx, lonIdx) + osg::Vec2f(0.0f, texDelta);
192 = tlArray(latIdx, lonIdx) - osg::Vec2f(0.0f, texDelta);
194 if (lonIdx > lonInner)
196 = tlArray(latIdx, lonIdx) + osg::Vec2f(texDelta, 0.0f);
199 = tlArray(latIdx, lonIdx) - osg::Vec2f(texDelta, 0.0f);
203 void OceanMesh::calcApronPts(double tex_width)
205 for (int i = 0; i < lonPoints; i++)
206 calcApronPt(0, i, 1, i, i, tex_width);
207 int topApronOffset = latPoints + (2 + lonPoints) * latPoints;
208 for (int i = 0; i < lonPoints; i++)
209 calcApronPt(latPoints - 1, i, latPoints - 2, i,
210 i + topApronOffset, tex_width);
211 for (int i = 0; i < latPoints; i++) {
212 calcApronPt(i, 0, i, 1, lonPoints + i * (lonPoints + 2), tex_width);
213 calcApronPt(i, lonPoints - 1, i, lonPoints - 2,
214 lonPoints + i * (lonPoints + 2) + 1 + lonPoints, tex_width);
220 // Enter the vertices of triangles that fill one row of the
221 // mesh. The vertices are entered in counter-clockwise order.
222 void fillDrawElementsRow(int width, short row0Start, short row1Start,
223 osg::DrawElementsUShort::vector_type::iterator&
226 short row0Idx = row0Start;
227 short row1Idx = row1Start;
228 for (int i = 0; i < width - 1; i++, row0Idx++, row1Idx++) {
229 *elements++ = row0Idx;
230 *elements++ = row0Idx + 1;
231 *elements++ = row1Idx;
232 *elements++ = row1Idx;
233 *elements++ = row0Idx + 1;
234 *elements++ = row1Idx + 1;
238 void fillDrawElementsWithApron(short height, short width,
239 osg::DrawElementsUShort::vector_type::iterator
243 fillDrawElementsRow(width, 0, width + 1, elements);
244 for (short i = 0; i < height - 1; i++)
245 fillDrawElementsRow(width + 2, width + i * (width + 2),
246 width + (i + 1) * (width + 2),
249 short topApronBottom = width + (height - 1) * (width + 2) + 1;
250 fillDrawElementsRow(width, topApronBottom, topApronBottom + width + 1,
255 osg::Node* SGOceanTile(const SGBucket& b, SGMaterialLib *matlib)
257 osg::StateSet *stateSet = 0;
259 double tex_width = 1000.0;
261 // find Ocean material in the properties list
262 SGMaterial *mat = matlib->find( "Ocean" );
264 // set the texture width and height values for this
266 tex_width = mat->get_xsize();
269 stateSet = mat->get_state();
271 SG_LOG( SG_TERRAIN, SG_ALERT, "Ack! unknown use material name = Ocean");
274 // Calculate center point
275 SGVec3d cartCenter = SGVec3d::fromGeod(b.get_center());
276 SGGeod geodPos = SGGeod::fromCart(cartCenter);
277 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos);
279 double clon = b.get_center_lon();
280 double clat = b.get_center_lat();
281 double height = b.get_height();
282 double width = b.get_width();
284 grid.calcMesh(cartCenter, hlOr, clon, clat, height, width, tex_width);
285 grid.calcApronPts(tex_width);
287 osg::Vec4Array* cl = new osg::Vec4Array;
288 cl->push_back(osg::Vec4(1, 1, 1, 1));
290 osg::Geometry* geometry = new osg::Geometry;
291 geometry->setVertexArray(grid.vl);
292 geometry->setNormalArray(grid.nl);
293 geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
294 geometry->setColorArray(cl);
295 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
296 geometry->setTexCoordArray(0, grid.tl);
298 // Allocate the indices for triangles in the mesh and the apron
299 osg::DrawElementsUShort* drawElements
300 = new osg::DrawElementsUShort(GL_TRIANGLES,
301 6 * ((latPoints - 1) * (lonPoints + 1)
302 + 2 * (latPoints - 1)));
303 fillDrawElementsWithApron(latPoints, lonPoints, drawElements->begin());
304 geometry->addPrimitiveSet(drawElements);
305 geometry->setStateSet(stateSet);
307 osg::Geode* geode = new osg::Geode;
308 geode->setName("Ocean tile");
309 geode->addDrawable(geometry);
311 osg::MatrixTransform* transform = new osg::MatrixTransform;
312 transform->setName("Ocean");
313 transform->setMatrix(osg::Matrix::rotate(hlOr.osg())*
314 osg::Matrix::translate(cartCenter.osg()));
315 transform->addChild(geode);