c++ - Walking an object on a terrain (made using heightmap) in OpenGL -


to walk object on terrain, i'm keeping camera , object fixed, , moving terrain according keyboard button pressed (using gltranslatef). i'm able move along x , z axis easily, when i'm moving along y axis, i'm having lot of trouble. here essential parts of code:

i've made class called terrain uses bmp file height @ different points. essential code is:

float terrain::getheight(int x, int z) {     return hs[z][x]; }  terrain* loadterrain(const char* filename, float height) {     image* image = loadbmp(filename);     terrain* t = new terrain(image->width, image->height);     (int y = 0; y < image->height; y++) {         (int x = 0; x < image->width; x++) {             unsigned char color = (unsigned char) image->pixels[3                     * (y * image->width + x)];             float h = height * ((color / 255.0f) - 0.5f);             t->setheight(x, y, h);          }     }      delete image;     t->computenormals();     return t; } 

so i've stored them in array , can y using x , z anytime. here code drawterrain, takes 3 float values, xx,yy , zz, , use gltranslatef translate terrain in xx , zz directions (i haven't used yy yet).

void drawterrain(float xx, float yy, float zz, float aangle) {     float yyy;     glpushmatrix();     gltranslatef(0.0f, 0.0f, 0.0f);     glrotatef(-aangle, 0.0f, 1.0f, 0.0f);     gltranslatef(xx, yy, zz);      glfloat ambientcolor[] = { 0.4f, 0.4f, 0.4f, 1.0f };     gllightmodelfv(gl_light_model_ambient, ambientcolor);      glfloat lightcolor0[] = { 0.6f, 0.6f, 0.6f, 1.0f };     glfloat lightpos0[] = { -0.5f, 0.8f, 0.1f, 0.0f };     gllightfv(gl_light0, gl_diffuse, lightcolor0);     gllightfv(gl_light0, gl_position, lightpos0);     glenable(gl_texture_2d);     glbindtexture(gl_texture_2d, _textureid1);     gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest);     gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest);      float scale = 50.0f / max(_terrain->width() - 1, _terrain->length() - 1);     glscalef(scale, scale, scale);     gltranslatef(-(float) (_terrain->width() - 1) / 2, 0.0f,             -(float) (_terrain->length() - 1) / 2);      glcolor3f(1.0f, 1.0f, 1.0f);     (int z = 0; z < _terrain->length() - 1; z++) {         //makes opengl draw triangle @ every 3 consecutive vertices         glbegin(gl_triangle_strip);         (int x = 0; x < _terrain->width(); x += 2) {             /*if (x == 0 && z == 0)              gltexcoord2f(0.0f, 0.0f);              if (x == _terrain->width() - 1 && z == 0)              gltexcoord2f(1.0f, 0.0f);              if (x == 0 && z == _terrain->length() - 1)              gltexcoord2f(0.0f, 1.0f);              if (x == _terrain->width() - 1 && z == _terrain->length() - 1)              gltexcoord2f(1.0f, 1.0f);*/             vec3f normal = _terrain->getnormal(x, z);             glnormal3f(normal[0], normal[1], normal[2]);             gltexcoord2f((float) x / _terrain->width(),                     (float) z / _terrain->length());             glvertex3f(x, _terrain->getheight(x, z), z);             normal = _terrain->getnormal(x, z + 1);             glnormal3f(normal[0], normal[1], normal[2]);             gltexcoord2f((float) x / _terrain->width(),                     (float) (z + 1) / _terrain->length());             glvertex3f(x, _terrain->getheight(x, z + 1), z + 1);             glnormal3f(normal[0], normal[1], normal[2]);             gltexcoord2f((float) (x + 1) / _terrain->width(),                     (float) z / _terrain->length());             glvertex3f(x + 1, _terrain->getheight(x + 1, z), z);             glnormal3f(normal[0], normal[1], normal[2]);             gltexcoord2f((float) (x + 1) / _terrain->width(),                     (float) (z + 1) / _terrain->length());             glvertex3f(x + 1, _terrain->getheight(x + 1, z + 1), z + 1);          }         //_y = (float) _terrain->getheight((int) (_x * max(_terrain->width() - 1, _terrain->length() - 1) / 50.0f), (int) (_z * max(_terrain->width() - 1, _terrain->length() - 1) / 50.0f));         glend();     } 

so translate in y direction, i've tried use following code:

float xxx,yyy,zzz; xxx = (float) (_terrain->width() - 1) / (2 * scale); xxx -= xx / scale; zzz = (float) (_terrain->length() - 1) / (2 * scale); zzz -= zz / scale; yyy = scale * _terrain->getheight(xxx,zzz); gltranslatef(0.0f,yyy,0.0f); 

but translate still not working according height, , y translate not looks continuous (since i'm converting integer , float values). can fix code, or suggest other method can walk object on terrain properly? thanks.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -