PDA

View Full Version : Mapping texture onto a rendered terrain



Mukund
01-22-2011, 06:16 AM
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 (http://img16.imageshack.us/i/24750658.png/)

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.

ugluk
01-22-2011, 06:25 AM
Many glBegin()s but no glEnd()s?

dalemac
01-22-2011, 06:57 AM
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.

Mukund
01-22-2011, 07:25 AM
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.

Mukund
01-22-2011, 02:06 PM
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 (http://img411.imageshack.us/i/29835359.png/)

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

ugluk
01-22-2011, 02:35 PM
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.

Mukund
01-22-2011, 02:42 PM
Thanks for the reply.

Well i made it 49. There is little improvement.

Here is the pic:
IMAGE (http://img593.imageshack.us/f/45137907.png/)
Can i further improve it?

Mukund
01-22-2011, 02:46 PM
Well i tried with a larger image: 1024x1024 (different texture)

IMAGE (http://img130.imageshack.us/i/58569524.png/)

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

ugluk
01-22-2011, 03:33 PM
You weren't calcing the texture coords correctly, obviously.