LandPatch.cpp

00001 #include "LandPatch.h"
00002 
00003 
00004 LandPatch::LandPatch (float * height_field, int size_x, int size_y, int offset_x, int offset_y, int total_x, int total_y, float stride, int xy_jump)
00005 {
00006         int x, y;
00007         int additional_vertex_space;                    // additional space used to cover one level above
00008         int additional_index_space;                             // additional space used to cover one level above
00009         int offset;
00010 
00011         // if xy_jump equals one, there is no level above
00012         if (xy_jump > 1)
00013         {
00014                 additional_vertex_space = 3 * (2*(size_x-1) + 2*(size_y-1));
00015                 additional_index_space = 3 * (2*(size_x-1) + 2*(size_y-1));
00016         }
00017         else
00018         {
00019                 additional_vertex_space = 0;
00020                 additional_index_space = 0;
00021         }
00022 
00023         // allocate space
00024         data = new float[3*size_x*size_y + additional_vertex_space];
00025         indices = new GLuint[(size_x-1)*(size_y-1) * 2 * 3 + additional_index_space];
00026 
00027         // fill regular grid data
00028         for (y = 0; y < size_y; y++)
00029         {
00030                 for (x = 0; x < size_x; x++)
00031                 {
00032                         data [3*(x + y*size_x)] = x * stride * xy_jump;
00033                         data [3*(x + y*size_x) + 1] = y * stride * xy_jump; 
00034                         data [3*(x + y*size_x) + 2] = height_field [(x*xy_jump + offset_x) + ((y*xy_jump + offset_y) * total_x)]; 
00035                 }
00036         }
00037 
00038         // fill regular grid indices
00039         for (y = 0; y < size_y - 1; y++)
00040         {
00041                 for (x = 0; x < size_x - 1; x++)
00042                 {
00043                         indices [6*(x + y*(size_x-1))+0] = x + y * size_x;
00044                         indices [6*(x + y*(size_x-1))+1] = (x+1) + (y+1) * size_x;
00045                         indices [6*(x + y*(size_x-1))+2] = (x) + (y+1) * size_x;
00046                         indices [6*(x + y*(size_x-1))+3] = x + y * size_x;
00047                         indices [6*(x + y*(size_x-1))+4] = (x+1) + y * size_x;
00048                         indices [6*(x + y*(size_x-1))+5] = (x+1) + (y+1) * size_x;
00049                 }
00050         }
00051 
00052         if (xy_jump > 1)
00053         {
00054                 // generate data and indices to cover one level above...
00055                 offset = 3 * size_x * size_y;
00056                 for (x = 0; x < size_x - 1; x++)
00057                 {
00058                         data [3*x + offset] = x * stride * xy_jump + stride * xy_jump / 2;
00059                         data [3*x + 1 + offset] = 0.0f;
00060                         data [3*x + 2 + offset] = height_field [(x*xy_jump + offset_x + xy_jump/2) + offset_y * total_x]; 
00061                 }
00062                 offset += 3 * (size_x - 1);
00063                 for (x = 0; x < size_x - 1; x++)
00064                 {
00065                         data [3*x + offset] = x * stride * xy_jump + stride * xy_jump / 2;
00066                         data [3*x + 1 + offset] = (size_y-1) * stride * xy_jump;
00067                         data [3*x + 2 + offset] = height_field [(x*xy_jump + offset_x + xy_jump/2) + (((size_y-1)*xy_jump + offset_y) * total_x)]; 
00068                 }
00069                 offset += 3 * (size_x - 1);
00070                 for (y = 0; y < size_y - 1; y++)
00071                 {
00072                         data [3*y + offset] = 0.0f;
00073                         data [3*y + 1 + offset] = y * stride * xy_jump + stride * xy_jump / 2;
00074                         data [3*y + 2 + offset] = height_field [offset_x + (y*xy_jump + offset_y + xy_jump/2) * total_x];
00075                 }
00076                 offset += 3 * (size_y - 1);
00077                 for (y = 0; y < size_y - 1; y++)
00078                 {
00079                         data [3*y + offset] = (size_x-1) * stride * xy_jump;
00080                         data [3*y + 1 + offset] = y * stride * xy_jump + stride * xy_jump / 2;;
00081                         data [3*y + 2 + offset] = height_field [(size_x-1)*xy_jump + offset_x + (y*xy_jump + offset_y + xy_jump/2) * total_x];
00082                 }
00083 
00084                 offset = (size_x-1)*(size_y-1) * 2 * 3;
00085                 for (x = 0; x < size_x - 1; x++)
00086                 {
00087                         indices [3*x + offset] = x + 1;
00088                         indices [3*x + 1 + offset] = x;
00089                         indices [3*x + 2 + offset] = size_x * size_y + x;
00090                 }
00091                 offset += 3 * (size_x - 1);
00092                 for (x = 0; x < size_x - 1; x++)
00093                 {
00094                         indices [3*x + offset] = x + (size_y-1)*size_x;
00095                         indices [3*x + 1 + offset] = x + 1 + (size_y-1)*size_x;
00096                         indices [3*x + 2 + offset] = size_x * size_y + (size_x - 1) + x;
00097                 }
00098                 offset += 3 * (size_x - 1);
00099                 for (y = 0; y < size_y - 1; y++)
00100                 {
00101                         indices [3*y + offset] = (y) * size_x;
00102                         indices [3*y + 1 + offset] = (y+1) * size_x;
00103                         indices [3*y + 2 + offset] =  size_x * size_y + 2 * (size_x - 1) + y;
00104                 }
00105                 offset += 3 * (size_y - 1);
00106                 for (y = 0; y < size_y - 1; y++)
00107                 {
00108                         indices [3*y + offset] = (size_x-1) + (y+1) * size_x;
00109                         indices [3*y + 1 + offset] = (size_x-1) + (y) * size_x;
00110                         indices [3*y + 2 + offset] = size_x * size_y + 2 * (size_x - 1) + (size_y - 1) + y;
00111                 }
00112         }
00113 
00114 
00115         // set up object with the data.
00116         SetVertexData (data, size_x * size_y + additional_vertex_space / 3);
00117         SetVertexIndices (indices, (size_x-1)*(size_y-1)*2 + additional_index_space / 3);
00118 
00119         ComputeNormals (false);
00120 }
00121 
00122 LandPatch::~LandPatch ()
00123 {
00124         // nothing to do
00125 }

Generated on Sun Jul 2 13:20:39 2006 for Demo by  doxygen 1.4.6-NO