00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <landscape/GraphicalLandscapeMap.h>
00022 #include <graph/OptionsDisplay.h>
00023 #include <GLEXT/GLStateExtension.h>
00024 #include <GLEXT/GLVertexBufferObject.h>
00025
00026 GraphicalLandscapeMap::GraphicalLandscapeMap() :
00027 heightData_(0), bufferObject_(0),
00028 width_(0), height_(0), bufferSizeBytes_(0)
00029 {
00030 }
00031
00032 GraphicalLandscapeMap::~GraphicalLandscapeMap()
00033 {
00034 delete [] heightData_;
00035 }
00036
00037 void GraphicalLandscapeMap::create(const int width, const int height)
00038 {
00039 width_ = width;
00040 height_ = height;
00041
00042 delete [] heightData_;
00043 heightData_ = new HeightData[(width_ + 1) * (height_ + 1)];
00044 int patchVolume = (width_ + 1) * (height_ + 1);
00045 bufferSizeBytes_ = patchVolume * sizeof(GraphicalLandscapeMap::HeightData);
00046
00047 if (GLStateExtension::hasVBO())
00048 {
00049 if (!bufferObject_ || bufferObject_->get_map_size() != bufferSizeBytes_)
00050 {
00051 delete bufferObject_;
00052 bufferObject_ = new GLVertexBufferObject();
00053 bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);
00054 }
00055 }
00056
00057 reset();
00058 }
00059
00060 void GraphicalLandscapeMap::reset()
00061 {
00062 HeightData *current = heightData_;
00063
00064 int texTileX = width_ / 16;
00065 int texTileY = height_ / 16;
00066
00067 for (int y=0; y<=height_; y++)
00068 {
00069 for (int x=0; x<=width_; x++)
00070 {
00071 current->floatPosition[0] = float(x);
00072 current->floatPosition[1] = float(y);
00073 current->floatPosition[2] = 0.0f;
00074
00075 current->floatNormal[0] = 0.0f;
00076 current->floatNormal[1] = 0.0f;
00077 current->floatNormal[2] = 1.0f;
00078
00079 current->texCoord1x = float(x) / float(width_);
00080 current->texCoord1y = float(y) / float(height_);
00081
00082 current->texCoord2x = float(x) / float(width_) * float(texTileX);
00083 current->texCoord2y = float(y) / float(height_) * float(texTileY);
00084
00085 current++;
00086 }
00087 }
00088 }
00089
00090 void GraphicalLandscapeMap::setHeight(int w, int h, float height)
00091 {
00092 DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
00093
00094 HeightData &data = heightData_[(width_+1) * h + w];
00095 data.floatPosition[2] = height;
00096 }
00097
00098 void GraphicalLandscapeMap::setNormal(int w, int h, Vector &normal)
00099 {
00100 DIALOG_ASSERT(w >= 0 && h >= 0 && w<=width_ && h<=height_);
00101
00102 HeightData &data = heightData_[(width_+1) * h + w];
00103 data.floatNormal = normal;
00104 }
00105
00106 void GraphicalLandscapeMap::updateWholeBuffer()
00107 {
00108
00109 if (!GLStateExtension::hasVBO()) return;
00110
00111 bufferObject_->init_data(bufferSizeBytes_, heightData_, GL_STATIC_DRAW);
00112 }