Saving terrain to a .raw file

Started by
4 comments, last by Etus 20 years, 2 months ago
Hello, I''m currently in the middle of writing a 3D editor for my engine, and I stumbled into a problem. I generate my terrain using a .raw heightmap file in the usuall known way. What I want to do for my editor is to be able to save the terrain I edited to a new .raw heightmap file. While creating an exact duplicate of the terrain from the terrain vertices(which are "float"s, by the way) is fairly, it doesn''t work right when I raise or lower the terrain in some points. When I raise or lower the terrain, I raise or lower the Y coord of the point. I usually raise it in 5.0f; The problem is that when I try to save the edited terrain to a heightmap file, the Y value is too high - it''s above 255.0f or lower than 0.0f. If I try to clamp the values to 0-255, it doesn''t work as well. How should I save an edited terrain to a new heightmap file? Thanks, Etus
Advertisement
Open up your terrain loader and see what conversions it does. I doubt that a height of 255 in the RAW file corresponds to a Y value of 255.0f. Most likely it get''s divided by 4 or so, which means that 255 corresponds t 64.0f.

Next, calulate what Y values correspond to a RAW value of 0 and 255. Most likely you will get something like 0.0f and 64.0f. In your editor, do not allow the terrain to be lowered below 0.0f or raised above 64.0f.

When you save to a RAW file, you cannot output the floats themselves. First, convert the float values to the correct height (multiply by 4 in this example, so 64.0f becomes 255.0f) and then cast it to a char. Now output these char''s into your own RAW file, and you''re done

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I opened my terrain loader code, and I do load the .raw file exactly as it is. The 255 value in the .raw file is infact 255.0f of the Y value. I can, though, scale the Y value as I wish.

What should I do in this case?

Thanks,
Etus
Use 16 bit RAW data files

If you''re only using 8 bit then you''re limited to the 0-255 range.

If you want to clamp, you simply find the maximum value and the minimum value in the terrain and then

float ratio = (height-min)/(max-min)
char bytetosave = ratio * 255

Simple.

-------------------
echo j | format c:
1. Where can I read more about loading 16bit raw files? Currently, I use 8bit.
2. Regarding the second method you proposed - "bytetosave" should be multiplyed with each height I want to save?

Thanks,
Etus
"height" is the height at the point in your terrain.

"bytetosave" is what you write to the file. It''s the final result.

16 bit is the same as 8 bit but with an extra byte. Instead of multiplying ratio by 255 you multiply it by 65535 and bytetosave becomes a short and then you write that (2 bytes) instead of the current one byte char.

It''s a simple ratio problem. Alegebra.

This topic is closed to new replies.

Advertisement