00001 #include "heightmap.h"
00002
00003 HeightMap::HeightMap()
00004 {
00005 }
00006
00007 bool HeightMap::Load(string FileName)
00008 {
00009 string tmp;
00010
00011 ifstream t_in(string(FileName + ".dim").c_str(),ios_base::in);
00012 if(!t_in.is_open())
00013 return false;
00014
00015 t_in >> tmp >> x_dim >> tmp >> z_dim >> tmp >> x_spacing >> tmp >> z_spacing >> tmp
00016 >> h_color.color[0] >> h_color.color[1] >> h_color.color[2] >> h_color.color[3];
00017
00018 h_map = new float*[x_dim];
00019 for (int j = 0; j < x_dim; j++)
00020 h_map[j] = new float[z_dim];
00021
00022 normalA = new glVector*[x_dim];
00023 for (int j = 0; j < x_dim; j++)
00024 normalA[j] = new glVector[z_dim];
00025 normalB = new glVector*[x_dim];
00026 for (int j = 0; j < x_dim; j++)
00027 normalB[j] = new glVector[z_dim];
00028
00029 ifstream v_in(string(FileName + ".ht").c_str(),ios_base::in);
00030 if(!v_in.is_open())
00031 return false;
00032
00033 v_in >> tmp;
00034 float tmp2;
00035 for(int i = 0; i < z_dim; i++)
00036 for(int j = 0; j < z_dim; j++)
00037 {
00038 v_in >> tmp2;
00039 h_map[j][i] = tmp2;
00040 }
00041
00042 for(int b = 0; b < z_dim - 1; b++)
00043 for(int a = 0; a < x_dim - 1; a++)
00044 {
00045 float scale_a = float(a) * x_spacing;
00046 float scale_b = float(b) * z_spacing;
00047
00048 Vertex pt1 = glVector(scale_a, h_map[a][b], scale_b);
00049 Vertex pt2 = glVector(scale_a, h_map[a][b+1], scale_b + z_spacing);
00050 Vertex pt3 = glVector(scale_a + x_spacing, h_map[a+1][b], scale_b);
00051 Vertex pt4 = glVector(scale_a + x_spacing, h_map[a+1][b+1], scale_b + z_spacing);
00052
00053 glVector v1 = glVector(pt1 - pt2);
00054 glVector v2 = glVector(pt1 - pt3);
00055 normalA[a][b].SetVector(!glVector(v1 % v2));
00056
00057 v1 = glVector(pt2 - pt3);
00058 v2 = glVector(pt4 - pt2);
00059 normalB[a][b].SetVector(!glVector(v1 % v2));
00060 }
00061 return true;
00062 }
00063
00064 void HeightMap::Draw()
00065 {
00066 glMaterialfv(GL_FRONT, GL_DIFFUSE, h_color.color);
00067 glBegin(GL_TRIANGLES);
00068
00069 for(int b = 0; b < z_dim - 1; b++)
00070 for(int a = 0; a < x_dim - 1; a++)
00071 {
00072 float scale_a = float(a) * x_spacing;
00073 float scale_b = float(b) * z_spacing;
00074
00075 glNormal3fv(normalA[a][b].data);
00076 glVertex3f(scale_a, h_map[a][b], scale_b);
00077 glVertex3f(scale_a, h_map[a][b+1], scale_b+z_spacing);
00078 glVertex3f(scale_a+x_spacing, h_map[a+1][b], scale_b);
00079
00080 glNormal3fv(normalB[a][b].data);
00081 glVertex3f(scale_a+x_spacing, h_map[a+1][b], scale_b);
00082 glVertex3f(scale_a, h_map[a][b+1], scale_b+z_spacing);
00083 glVertex3f(scale_a+x_spacing, h_map[a+1][b+1], scale_b+z_spacing);
00084 }
00085 glEnd();
00086 }
00087