8/18/2009

Creating 3d data values for drawing ground


To create 3d data values for drawing ground have to calculate from center (0f, 0f, 0f). The left picture shows the four parts which are divided by center.
Every x and y values are increased and decreased by 1.0 or -1.0 except z values. The increase and decrease values have a same pattern. According many OpenGL examples from Internet, we can draw rectangle with 4 vertex points. Each vertex point has x, y and z value. Therefore, the program has to generate rectangle points’ value. Of course, the generated file has also color values.

int main(int argc, char **argv)
{
int nwidth = atoi(argv[1]);
int nheight = atoi(argv[2]);

fnCVertextFile(nheight, nwidth);
}

Example) program 8 8
When the program was executed like an example, then the vector values will be generated 16 * 16 sizes.

typedef struct {
float x, y, z;
} M3DPoint;

typedef struct {
M3DPoint left, top, right, bottom;
} M3DRect;

// the function writes structure values and string values to file.
void fprintfex(FILE *fvfile, M3DRect row1, char *value)
{
fprintf(fvfile, "%f %f %f %s\n", row1.left.x, row1.left.y, row1.left.z, value);
fprintf(fvfile, "%f %f %f %s\n", row1.top.x, row1.top.y, row1.top.z, value);
fprintf(fvfile, "%f %f %f %s\n", row1.right.x, row1.right.y, row1.right.z, value);
fprintf(fvfile, "%f %f %f %s\n", row1.bottom.x, row1.bottom.y, row1.bottom.z, value);
}

void fnCVertextFile(int aRow, int aColumn)
{
M3DRect row1;

float nleft = 0.0f;
float ntop = 0.0f;

char value1[] = {"0.3 0.3 0.3"};
char value2[] = {"0.5 0.5 0.5"};
char *value;
bool toggle = false;

FILE *fvfile = fopen("vector.dat", "w");
if(fvfile == NULL) {
printf("file cannot open!\n");
exit(-1);
}

// count of vertex
fprintf(fvfile, "%d\n", aRow * aColumn * 4 * 4);

for(int j = 0; j < aRow; j++)
{
for(int i = 0; i < aColumn; i++)
{
row1.left.x = nleft; row1.left.y = ntop; row1.left.z = 1.5f;
row1.top.x = nleft+1.0f; row1.top.y = ntop; row1.top.z = 1.5f;
row1.right.x = nleft+1.0f; row1.right.y = ntop+1.0f; row1.right.z = 1.5f;
row1.bottom.x = nleft; row1.bottom.y = ntop+1.0f; row1.bottom.z = 1.5f;
value = (toggle) ? value1 : value2;
fprintfex(fvfile, row1, value);
toggle = !toggle;
nleft += 1.0f;
}
ntop += 1.0f;
nleft = 0.0f;
toggle = !toggle;
}

// was ommited below section of source