Merge from development branch heightmap to main.
[scorched3d.git] / src / client / landscape / GraphicalLandscapeMap.cpp
blob2ff5904bcb84cafc9cd087ac549c63dae189631d
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2003
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D 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 General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <landscape/GraphicalLandscapeMap.h>
22 #include <graph/OptionsDisplay.h>
23 #include <GLEXT/GLStateExtension.h>
24 #include <GLEXT/GLVertexBufferObject.h>
26 GraphicalLandscapeMap::GraphicalLandscapeMap() :
27 heightData_(0), bufferObject_(0),
28 width_(0), height_(0), bufferSizeBytes_(0)
32 GraphicalLandscapeMap::~GraphicalLandscapeMap()
34 delete [] heightData_;
37 void GraphicalLandscapeMap::create(const int width, const int height)
39 width_ = width;
40 height_ = height;
42 delete [] heightData_;
43 heightData_ = new HeightData[(width_ + 1) * (height_ + 1)];
44 int patchVolume = (width_ + 1) * (height_ + 1);
45 bufferSizeBytes_ = patchVolume * sizeof(GraphicalLandscapeMap::HeightData);
47 if (GLStateExtension::hasVBO())
49 if (!bufferObject_ || bufferObject_->get_map_size() != bufferSizeBytes_)
51 delete bufferObject_;
52 bufferObject_ = new GLVertexBufferObject();
53 bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);
57 reset();
60 void GraphicalLandscapeMap::reset()
62 HeightData *current = heightData_;
64 int texTileX = width_ / 16;
65 int texTileY = height_ / 16;
67 for (int y=0; y<=height_; y++)
69 for (int x=0; x<=width_; x++)
71 current->floatPosition[0] = float(x);
72 current->floatPosition[1] = float(y);
73 current->floatPosition[2] = 0.0f;
75 current->floatNormal[0] = 0.0f;
76 current->floatNormal[1] = 0.0f;
77 current->floatNormal[2] = 1.0f;
79 current->texCoord1x = float(x) / float(width_);
80 current->texCoord1y = float(y) / float(height_);
82 current->texCoord2x = float(x) / float(width_) * float(texTileX);
83 current->texCoord2y = float(y) / float(height_) * float(texTileY);
85 current++;
90 void GraphicalLandscapeMap::setHeight(int w, int h, float height)
92 DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
94 HeightData &data = heightData_[(width_+1) * h + w];
95 data.floatPosition[2] = height;
98 void GraphicalLandscapeMap::setNormal(int w, int h, Vector &normal)
100 DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
102 HeightData &data = heightData_[(width_+1) * h + w];
103 data.floatNormal = normal;
106 void GraphicalLandscapeMap::updateWholeBuffer()
108 // Generate the VBO if any
109 if (!GLStateExtension::hasVBO()) return;
111 bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);