Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Members List Social Groups Calendar Mark Forums Read

Notices

 
 
Thread Tools
Old 12-18-2004, 07:46 AM   #1
CronoMan
Guest
\"slopes\" or whatever it\'s called
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.

Code:
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)
Attached Images
File Type: jpg 744944-rsshot.jpg (18.9 KB, 48 views)
 
Old 12-19-2004, 10:49 PM   #2
DudeMiester

DudeMiester's Avatar
Re: \"slopes\" or whatever it\'s called
Use interpolation. Find the hights of the four nearest points in the hightmap, and interpolate them to find the point you want.
__________________
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  
Old 12-20-2004, 06:35 AM   #3
CronoMan
Guest
Re: \"slopes\" or whatever it\'s called
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
 
Old 12-24-2004, 01:52 AM   #4
DudeMiester

DudeMiester's Avatar
Re: \"slopes\" or whatever it\'s called
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.
__________________
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  
Old 01-03-2005, 05:00 AM   #5
CronoMan
Guest
Re: \"slopes\" or whatever it\'s called
That was exactly what I needed

Thank you very much
 
Old 01-07-2005, 01:42 AM   #6
DudeMiester

DudeMiester's Avatar
Re: \"slopes\" or whatever it\'s called
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.
__________________
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  
Old 01-09-2005, 10:35 PM   #7
Destroyer
Re: \"slopes\" or whatever it\'s called
Quote:
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.

Code:
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.
Destroyer is offline  
Old 01-17-2005, 02:44 AM   #8
CronoMan
Guest
Re: \"slopes\" or whatever it\'s called
Quote:
Destroyer said:
hehe, that looks cool, i saw leave it like that.
Well, it's not the desired effect
 
Old 01-29-2005, 09:07 PM   #9
Night Hacker

Night Hacker's Avatar
Re: \"slopes\" or whatever it\'s called
Quote:

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...

__________________
"My general feeling is that computers are doing more harm than good." - Jay Miner ("father" of the Amiga)
Night Hacker is offline  
Old 01-30-2005, 12:02 AM   #10
DudeMiester

DudeMiester's Avatar
Re: \"slopes\" or whatever it\'s called
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/for...opic_id=135654

You'll probably want to read the link to the Sky Thread in the first post.
__________________
Forums are the Opiate of the Masses
Higher Game -- "Animal fur is renewable, unlike the oil used to make plastic!"
DudeMiester is offline  
Old 01-31-2005, 02:09 AM   #11
CronoMan
Guest
Re: \"slopes\" or whatever it\'s called
Quote:
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...


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"
 
Old 02-02-2005, 01:49 AM   #12
Night Hacker

Night Hacker's Avatar
Re: \"slopes\" or whatever it\'s called
Hmmm... I may just do that. I am still learning to do opengl, but I am loving it.

You can download my little test3d 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.
__________________
"My general feeling is that computers are doing more harm than good." - Jay Miner ("father" of the Amiga)
Night Hacker is offline  
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:26 PM.

Page generated in 0.15467811 seconds (100.00% PHP - 0% MySQL) with 17 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.

Website is 1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.