PDA

View Full Version : Drawing textured heightmaps



Kunasha
07-25-2010, 02:38 PM
So I'm a complete n00b at OpenGL and I'm messing around with textured heightmapping.

I have 2 problems, and 3 questions. (of which 2 of the questions pertain to the problems)

I've created a heightmap. I've managed to get all the data from an image file, and now I want to draw it. I use GL_TRIANGLE_STRIP vertically to do this. Untextured, it looks great, but when I texture it using the following code it doesn't quite work out:


void Draw()
{
float Left = -DistanceBetweenPoints * Width / 2;
float Top = -DistanceBetweenPoints * Length / 2;

glColor3f(1.f , 1.f , 1.f);

for (int i = 0; i < Width - 1; i++)
{
GroundTextures[TextureArray[i][0]].Bind();
glBegin(GL_TRIANGLE_STRIP);







//Tegn de 2 punkter mest til venstre i en ny stribe.

glTexCoord2f(0.f,0.f);
glNormal3f(NormalMap[i][0].x,NormalMap[i][0].y,NormalMap[i][0].z);
glVertex3f(Left + DistanceBetweenPoints * i , HeightArray->GetHeight(i , 0) , Top);
glTexCoord2f(1.f,0.f);
glNormal3f(NormalMap[i+1][0].x,NormalMap[i+1][0].y,NormalMap[i+1][0].z);
glVertex3f(Left + DistanceBetweenPoints * (i + 1) , HeightArray->GetHeight(i + 1 , 0) , Top);
for (int j = 1; j < Length; j++)
{
//GroundTextures[TextureArray[i][j]].Bind();
if (j%2 == 0) glTexCoord2f(1.f,0.f); else glTexCoord2f(1.f,1.f);
glNormal3f(NormalMap[i][j].x,NormalMap[i][j].y,NormalMap[i][j].z);
glVertex3f(Left + DistanceBetweenPoints * i , HeightArray->GetHeight(i , j) , Top + DistanceBetweenPoints * j );

if (j%2 == 0) glTexCoord2f(0.f,0.f); else glTexCoord2f(0.f,1.f);
glNormal3f(NormalMap[i+1][j].x,NormalMap[i+1][j].y,NormalMap[i+1][j].z);
glVertex3f(Left + DistanceBetweenPoints * (i+1) , HeightArray->GetHeight(i+1 , j) , Top + DistanceBetweenPoints * j );
}
glEnd();
}

So I suppose a bit of explanation is in order. I use SFML (Simple, Fast Multimedia Library) which has an image object on the sf namespace that is stored directly in VRAM. That means I can just load in the texture from a file, and then bind it for drawing with TexName.bind();

I run into two problems.
When I create a trianglestrip, I do it all within a single glBegin()/glEnd(). That means that if I set a texture to use at the very top of my heightmap (I use colorcoding for this, it works) it will draw that same texture aaaall the way down to the bottom no matter what I put in between.

Secondly, I create the following points (in order of number):
1 2
3 4
5 6
7 8

The first two triangles are drawn between points 1, 2, 3 and 2, 3, 4 respectively, and the texture is correctly mapped across them.

To do this, I bind textures like this:
1(0,0) 2(0,1)
3(1,0) 4(1,1)

I don't know how to reassign the 3rd and 4th vertice when drawing the next one, so I do this:
1(0,0) 2(0,1)
3(1,0) 4(1,1)
5(0,0) 6(0,1)

Which is obviously wrong and causes every second texture to be mirrored vertically.

To recap:
Here's a picture of it.
http://img137.imageshack.us/img137/6481/reallyuglytexturing.jpg

You can see that every second texture is mirrored vertically, which is just incredibly ugly. And then there's the whole literally "stripes" thing going on.

As for the 3rd question, which has no practical relevance until the first two are solved:
There are also some black lines, and I know they are there because I am not alphablending the terrain. I want to be able to define the textures at the points instead of on the vertices between them, and then draw them alphamapped over the 4 nearby vertices, so that I can create the illusion of smooth terrain texturing.

I'd really appreciate some help, even if just for one of the problems. If you need more info just ask. :)

I hope I'll become good at OpenGL in time. Definitely gonna keep working on this!
Thanks!