Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Eliminating creases in diamond square fractal terrain

This thread is locked; no one can reply to it. rss feed Print
Eliminating creases in diamond square fractal terrain
Arthur Kalliokoski
Second in Command
February 2005
avatar

I stumbled across something called "xmountains" which claimed to eliminate the creases commonly seen when using the diamond square fractal algorithm. It seems to work.

#SelectExpand
1 for(sqrsize=width-1;sqrsize>1;sqrsize/=2) 2 { 3 printf("sqrsize %u\n",sqrsize); 4 for(y=0;;y+=sqrsize) 5 { 6 endy = y + sqrsize; 7 if(endy > width-1) 8 { 9 break; 10 } 11 for(x=0;;x+=sqrsize) 12 { 13 endx = x + sqrsize; 14 if(endx > width-1) 15 { 16 break; 17 } 18 midx = x + (sqrsize/2); 19 midy = y + (sqrsize/2); 20 21 //calculate midpoint 22 arrow[midx][midy] = 23 (arrow[x][y] + arrow[endx][y] + arrow[x][endy] + 24 arrow[endx][endy]) * QUARTERWIDTH + getrand(); 25 26 //top point of diamond 27 arrow[midx][y] = (arrow[x][y] + arrow[endx][y]) * HALFWIDTH + getrand(); 28 29 //left point of diamond 30 arrow[x][midy] = (arrow[x][y] + arrow[x][endy]) * HALFWIDTH + getrand(); 31 32 //right point of diamond 33 arrow[endx][midy] = (arrow[endx][y] + arrow[endx][endy]) * 34 HALFWIDTH + getrand(); 35 36 //bottom point of diamond 37 arrow[midx][endy] = (arrow[x][endy] + arrow[endx][endy]) * 38 HALFWIDTH + getrand(); 39 40 //new here, recalculate corner points from edge midpoints 41 arrow[x][y] = (arrow[midx][y] + arrow[x][midy]) * HALFWIDTH + getrand(); 42 arrow[endx][y] = (arrow[midx][y] + arrow[endx][midy]) * HALFWIDTH + getrand(); 43 arrow[x][endy] = (arrow[midx][endy] + arrow[x][midy]) * HALFWIDTH + getrand(); 44 arrow[endx][endy] = (arrow[midx][endy] + arrow[endx][midy]) * HALFWIDTH + getrand(); 45 } 46 } 47 }

The entire C file with a header is here. You need the Merseinne Twister or you can change a couple of lines in the code for some other random generator.

This is the old way (showing several creases running north/south and east/west)
{"name":"605536","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/0\/a0090c3ff42bcbb39c0831d18999eb92.png","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/0\/a0090c3ff42bcbb39c0831d18999eb92"}605536

And this is the new way
{"name":"605537","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/7\/c7f2a3b9604ee2b7458aacb159d74c46.png","w":1024,"h":768,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/7\/c7f2a3b9604ee2b7458aacb159d74c46"}605537

Both heightfields had the same seed, although I changed the ranges in height a bit, and averaged each element with its neighbors one time.

They all watch too much MSNBC... they get ideas.

Trent Gamblin
Member #261
April 2000
avatar

By creases do you just mean the edges?

(being visible)

Arthur Kalliokoski
Second in Command
February 2005
avatar

I meant the ridges of mountains and valleys. In the top image that deep valley just to the right of center runs north/south, there's that high ridge to the right of it doing the same, and there's a couple short runs to the left of the valley.

They all watch too much MSNBC... they get ideas.

Trent Gamblin
Member #261
April 2000
avatar

I didn't notice.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Here's a couple shots of the same heightfield that simply darken a pixel if the height to its left is higher and lightens it if it's lower.

old way with creases
{"name":"605539","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/0673a0b18540fd0e4db89facb6949670.png","w":1024,"h":1024,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/0673a0b18540fd0e4db89facb6949670"}605539

new way
{"name":"605540","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/0\/4086391161bdb0ac7d029c80e05ad419.png","w":1024,"h":1024,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/0\/4086391161bdb0ac7d029c80e05ad419"}605540

[EDIT]

These were from an A4 program, it seems KSnapShot doesn't like them very well...

They all watch too much MSNBC... they get ideas.

Trent Gamblin
Member #261
April 2000
avatar

Ya very noticeable there in those flat pics.

Luiji99
Member #12,254
September 2010

For anybody who's curious, XMountain's source code, including that segment up there, is licensed under:

#SelectExpand
1/* $Id: copyright.h,v 1.2 1994/03/28 15:21:32 spb Exp $ */ 2/* 3 4Copyright 1994 by Stephen Booth, the University of Edinburgh 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation, and that the name of the author not be used in advertising or 11publicity pertaining to distribution of the software without specific, 12written prior permission. The author makes no representations about the 13suitability of this software for any purpose. It is provided "as is" 14without express or implied warranty. 15 16*/

I thought some of you might find that information useful.

As for the comparison between the old and new way, it seems to me that the new way looks a bit smoother from for away. The more jagged look of the old way might be nice in some uses. (?)

Programming should be fun. That's why I hate Java.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Luiji99 said:

XMountain's source code, including that segment up there, is licensed

It's wildly apparent you didn't read his source code, as it doesn't resemble the code I posted in the least.

They all watch too much MSNBC... they get ideas.

Luiji99
Member #12,254
September 2010

Pardon me, I'm sorry, I was working off an incorrect assumption (obviously, as you said). Never mind. (-.-*)

Programming should be fun. That's why I hate Java.

Go to: