PDA

View Full Version : read txt file for heighfield (terrain)



be7logic
11-02-2009, 06:46 AM
Hello everyone, im newbie in opengl n c++... n I found this forum, hope it can help me..

how I can read txt file and put to GLFloat declaration, please see my code below:

GLfloat map[6][6] ={{0,0,0,0,0,0},
{0,0.2,0.3,0.1,0.2,0},
{0,1,1.5,3,2,0},
{0,1,2,1.5,2,0},
{0,1,0.4,0.2,0.2,0},
{0,0,0,0,0,0}};


void terrain::setUp(GLfloat xsizeZ, GLfloat zsizeZ, int xstepsZ, int zstepsZ)
{
xsize=xsizeZ;
zsize=zsizeZ;
xsteps=xstepsZ;
zsteps=zstepsZ;
}

int terrain::render()
{
vec3 v1,v2,v3,norm;

glPushMatrix();
preset();
mat->doMaterial();

GLfloat xdelta=xsize/xsteps;
GLfloat zdelta=zsize/zsteps;
glEnable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
for (int x=0; x<xsteps; x++)
for (int z=0; z<zsteps; z++)
{
v1.x=xdelta*x;
v1.y=map[x][z];
v1.z=zdelta*z;

v2.x=xdelta*x+xdelta;
v2.y=map[x+1][z];
v2.z=zdelta*z;

v3.x=xdelta*x;
v3.y=map[x][z+1];
v3.z=zdelta*z+zdelta;

//compute a normal - this should be done outside this loop for speed
norm=polygonNormal(v1,v3,v2);
glNormal3fv(norm.fv());

glVertex3fv(v1.fv());
glVertex3fv(v2.fv());
glVertex3fv(v3.fv());

// glVertex3f(xdelta*x,map[x][z],zdelta*z);
// glVertex3f(xdelta*x+xdelta,map[x+1][z],zdelta*z);
// glVertex3f(xdelta*x,map[x][z+1],zdelta*z+zdelta);

v1.x=xdelta*x+xdelta;
v1.y=map[x+1][z+1];
v1.z=zdelta*z+zdelta;

v2.x=xdelta*x;
v2.y=map[x][z+1];
v2.z=zdelta*z+zdelta;

v3.x=xdelta*x+xdelta;
v3.y=map[x+1][z];
v3.z=zdelta*z;

norm=polygonNormal(v1,v3,v2);
glNormal3fv(norm.fv());

glVertex3fv(v1.fv());
glVertex3fv(v2.fv());
glVertex3fv(v3.fv());

}

glEnd();
glPopMatrix();
return true;
}

its work fine with this code:

terrain::terrain()
{
mat= new materialClass();
matLines= new materialClass();
mat->setMaterialRedPlastic();
matLines->setMaterialBrass();
setUp(5,5,5,5);

if (!rc) printf("ERRR OR WITH HEIGHTFIELD FILE\n");

glEnable(GL_TEXTURE_2D);

}

But when I want to read the txt file, sort of like (test5x5.txt) :

5 5
-1 -1 -1 -1 -1
-1 -1 100 150 -1
-1 100 220 400 -1
-1 70 408 260 -1
-1 -1 -1 -1 -1

and put some read txt line:

int rc=readHeightField ("5x5Test.txt");

and it works... but the problem is, my heigh field just appear flat terrain... My question is:

what should I declare in glFloat at the first line?

Thank you everyone..