Mapping texture onto a rendered terrain

Hello,

im new to textures. im having some trouble mapping textures onto a rendered terrain. im trying to map it onto a rendered terrain the code for which i found online.

Here is a pic.

PIC

The left one is the output. The right one is my texture. i just wanted to map it onto it. The texture is a 256x256 bmp image.

im using this for mapping texture:


for(int z = 0; z < _terrain->length() - 1; z++) {
	glBegin(GL_QUAD_STRIP);
	for(int x = 0; x < _terrain->width(); x++) {
            /* set normal and set vertex */
            glTexCoord2d((double)x / _terrain->width(), (double)z / _terrain->length());

             // Two
             /* set normal and set vertex */
             glTexCoord2d((double)x / _terrain->width(), (double)z + 1 / _terrain->length());
        }
}

But im not able to map the texture. im using a 50x50 height map for the terrain. Can anyone please correct me?

Thanks.

Many glBegin()s but no glEnd()s?

i agree. You are calling glBegin recursively, but never ending the process. remember, openGL is a state machine, once something like this is initiated, it must be terminated.

Thanks for the replies. Sorry missed out on the glEnd(). Here is the code. Not sure why the texture is not mapped.

for(int z = 0; z < _terrain->length() - 1; z++) {
	glBegin(GL_QUAD_STRIP);
	for(int x = 0; x < _terrain->width(); x++) {
            /* set normal and set vertex */
            glTexCoord2d((double)x / _terrain->width(), (double)z / _terrain->length());

             // Two
             /* set normal and set vertex */
             glTexCoord2d((double)x / _terrain->width(), (double)z + 1 / _terrain->length());
        }
        glEnd();
}

//a glutSwapBuffers() here.

Well i managed to map the texture with this:

for(int z = 0; z < _terrain->length() - 1; z++, count1++) {
	glBegin(GL_QUAD_STRIP);
	for(int x = 0; x < _terrain->width(); x++, count2++) {
		// Set Normal and vertex
		..
		glTexCoord2d(x / 50.0, z / 50.0);

		// Set normal and vertex
                ..  

		glTexCoord2d(x/ 50.0, (z + 1) / 50.0);
	}
	glEnd();
}
glutSwapBuffers();

Well this is the output im getting:

IMAGE

The image on the right is the texture im using.

im getting “squares” in the terrain. What could be wrong?
The terrain image is 510x510

The height map is 50x50(hence the division by 50 in the loop viz the length and width of height map).

Thanks

z != _terrain->length - 1, so it is max _terrain->length-2, if you add 1 to this you get _terrain->length-1 and divide by 50, you don’t get exactly 1. Maybe try to divide by 49.

Thanks for the reply.

Well i made it 49. There is little improvement.

Here is the pic:
IMAGE
Can i further improve it?

Well i tried with a larger image: 1024x1024 (different texture)

IMAGE

The terrain looks better. But i still don’t get why the previous one was not working so well. It was a 510x510

You weren’t calcing the texture coords correctly, obviously.