View Full Version : "slopes" or whatever it's called
CronoMan
12-18-2004, 07:46 AM
I have a problem in a program I'm writing in C++ , well it's really a game. But anyways, I have a problem with the terrain; I'm unable to think of a way to get the height of a single point on each polygon.
I believe I'm not the only one who has encountered this problem?
My terrain consists of 128*128 points, which are 16-bit per point.
TERRDATA TerrainData::GetHeight(float x, float y)
{
TERRDATA p1, p2, p3, p4, m = 0;
float xdiff, ydiff;
if(x >= 0 && x < this->pWidth && y >= 0 && y <= this->pHeight)
{
return this->HeightMap[this->GetPos(common::assembled::FloatToInt(x), common::assembled::FloatToInt(y))];
}
else
return 0;
}
TERRDATA is just a typedef of word.
this->HeightMap is just a pointer to the data, and GetPos just multiplies y with the width of the map and adds x. (common::assembled::FloatToInt is just a function for converting floats to int faster than regular type cast)
(screenshot attached)
DudeMiester
12-19-2004, 10:49 PM
Use interpolation. Find the hights of the four nearest points in the hightmap, and interpolate them to find the point you want.
CronoMan
12-20-2004, 06:35 AM
ok, but how?
I figured out I need to get the four heightpoints around, and get the average of the diagonal (eacho square is divided into to triangles, as you probably know, and this makes it even harder) but when I start to write the code, my brain just twirls :P
So if anyone has done this before and has a little sample-code, it would be very appreciated http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif
DudeMiester
12-24-2004, 01:52 AM
Just average the heights surrounding points weighted by the distance of those points from where you're calculating the new height. That in effect interpolates between them.
h_new=(h1*d1+h2*d2+h3*d3+h4*d4)/(d1+d2+d3+d4)
where, h_new is the new height, h1 to h4 are the heights of the surrounding points, and d1 to d2 are the distances from those points. Now I think the sum at the far right will always work out to the same value, just work it out on paper for a few points/distances to make sure.
So then when you're setting up all the heights for the vertices in your terrian mesh, generate the heights for each vertex from your heightmap using the above forumla. Now this is called bilinear interpolation (linear interpolation in two directions, vertically and horizontallu) so you will get sudden changes in slope and thus sharpish edges. If you want really smooth results, then google for bicubic interpolation or just cubic interpolation, then combine a horizontal and vertical cubic interpolation to get the forumla for bicubic.
CronoMan
01-03-2005, 05:00 AM
That was exactly what I needed
Thank you very much http://forums.3drealms.com/ubbthreads/images/graemlins/grin.gif
DudeMiester
01-07-2005, 01:42 AM
fyi, the identical technique is used by computer graphics when taking samples from textures, although often it is interpolated accross mip-map levels as well, giving 3 directions of filtering hence trilinear filtering.
Destroyer
01-09-2005, 10:35 PM
CronoMan said:
I have a problem in a program I'm writing in C++ , well it's really a game. But anyways, I have a problem with the terrain; I'm unable to think of a way to get the height of a single point on each polygon.
I believe I'm not the only one who has encountered this problem?
My terrain consists of 128*128 points, which are 16-bit per point.
TERRDATA TerrainData::GetHeight(float x, float y)
{
TERRDATA p1, p2, p3, p4, m = 0;
float xdiff, ydiff;
if(x >= 0 && x < this->pWidth && y >= 0 && y <= this->pHeight)
{
return this->HeightMap[this->GetPos(common::assembled::FloatToInt(x), common::assembled::FloatToInt(y))];
}
else
return 0;
}
TERRDATA is just a typedef of word.
this->HeightMap is just a pointer to the data, and GetPos just multiplies y with the width of the map and adds x. (common::assembled::FloatToInt is just a function for converting floats to int faster than regular type cast)
(screenshot attached)
hehe, that looks cool, i saw leave it like that.
CronoMan
01-17-2005, 02:44 AM
Destroyer said:
hehe, that looks cool, i saw leave it like that.
Well, it's not the desired effect http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif
Night Hacker
01-29-2005, 09:07 PM
and d1 to d2 are the distances from those points
I'm curious, how are you getting the distances? I think I know, but I need to clarify to be certain.
I'm also curious why you couldn't simply look at your heightmap for the height at that point? Unless of course you need the height between two points on the heightmap. In which case you don't really need a vertex at that spot, you could simply use larger polygons or better yet, a higher resolution heightmap for smoother terrain. I use a 512x512 heightmap (8bit greyscale) and find it perfect for nice smooth terrain.
This is my little project so far...
http://home.cogeco.ca/~silentsam/Test3D.jpg
DudeMiester
01-30-2005, 12:02 AM
I meant to say d1 to d4. All it is, is simple bilinear filtering. You take an arbitrary sample point, fetch the surounding heightmap values, and do a weighted average by those pixels' distances from the sample point. If the sample point lies directly on a pixel you still sample from the four surrounding pixels.
btw, NH you might want to search GameDev for Yann L's thread on atmospheric sky lighting effects. Very nice.
http://www.gamedev.net/community/forums/topic.asp?topic_id=135654
You'll probably want to read the link to the Sky Thread in the first post.
CronoMan
01-31-2005, 02:09 AM
Night Hacker said:
I use a 512x512 heightmap (8bit greyscale) and find it perfect for nice smooth terrain.
This is my little project so far...
http://home.cogeco.ca/~silentsam/Test3D.jpg
That looks really neat!
Well, I use 128x128 16-bit. The reason I have such a little terrain, it must run smooth at a very cheap computer. And the terrain features "dynamics" so I cannot store the terrain on the graphics card (which very efficiently steals my cpu). I was thinking about featuring a multi-player editor, so therefore the mesh needs to be able to be modified. And this gives me a fps at under 30 fps at this moment, even with a 128x128 terrain (+ a 64x64 mesh which is the water (which is also "dynamic")) and quadtree. The terrains is also tilebased, like the good old classics. So I can't really make it look "natural".
And you should use a 16-bit heightmap instead, it allows your terrain to be more "overwhelming" http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif
Night Hacker
02-02-2005, 01:49 AM
Hmmm... I may just do that. I am still learning to do opengl, but I am loving it.
You can download my little test3d (http://home.cogeco.ca/~silentsam/test3d.zip) program and try it out if you're curious. I even added in a few sound effects. The controls are pretty intuitive, you walk around, you can fly etc... I keep adding to this as I learn. I experiment with it etc.. I have no plans to make it into anything in particular (yet).
Fun stuff anyone. I wouldn't mind seeing what you're working on when you're done with it. Sounds interesting.
DudeMeister: Thanks for the link, I'll definately check that out.
vBulletin® v3.8.0 Release Candidate 1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.