Fractal Mountain Generation with Qt and OpenGL
Submitted by esalazar on Mon, 02/04/2008 - 9:29pm.
::
The purpose of this project is to create 3d mountain terrain using a recursive midpoint displacement formula. For this project I decided a GUI would be useful, that way manipulations could be seen in real time. This would require a 3d rendering package. My choices were DirectX, OpenGL and Java3d. Since I prefer to do my development in Linux, DirectX was ruled out. I have not been very impressed with the performance of Java3d so that left me with OpenGL. After deciding on my rendering package I needed to choose a language and a GUI framework. GLUI is an excellent framework for leaning OpenGL, but lacks control over the layout of the interface. So I decided on QT by Trolltech which was just recently acquired by Nokia.
Midpoint Displacement Formula
The midpoint Displacement Formula is simply to take two points, find the midpoint and then add or subtract a random number. This formula can then be repeated for each segment creating a fractal. This same concept can be converted to 3d in what is known as the Diamond-Square algorithm. Instead of two points we take a plane defined by four points, calculate the midpoint and add a random number. We can start with one large plane and perform the algorithm. Then divide the plane into four sub-planes and repeat. A recursive function works well for this task. For the mountains to look real, it is best to shorten the range of random numbers as the algorithm progresses. Below is the midpoint Function I wrote for this program in C++.
/****************************************
*This is the recursive algorithm of the
* midpoint formula
****************************************/
int GenFractal::midpoint(int i,int start,int topx, int topy) {
//Base case
if(i<1) {
return 0;
}
//find lenght for this section
int len = (int)pow(2,i);
//Calculate midpoints
//Top middle
int x1 = topx + len/2;
int y1 = topy;
//Right middle
int x2 = topx + len;
int y2 = topy + len/2;
//Bottom middle
int x3 = x1;
int y3 = y1 + len;
//Left Middle
int x4 = x2 - len;
int y4 = y2;
//Midpoint
int xmid = x1;
int ymid = y2;
//Calculate the values of the midpoints
//Top middle
data[x1][y1] = data[topx][topy] +
(data[topx+len][topy] - data[topx][topy])/2;
//Right middle
data[x2][y2] = data[topx+len][topy] +
(data[topx+len][topy+len] - data[topx+len][topy])/2;
//Bottom middle
data[x3][y3] = data[topx][topy+len] +
(data[topx+len][topy+len] - data[topx][topy+len])/2;
//Left middle
data[x4][y4] = data[topx][topy] +
(data[topx][topy+len] - data[topx][topy])/2;
//Midpoint
data[xmid][ymid] = data[x1][y1] + (data[x3][y3] - data[x1][y1])/2;
//Decrease the varence of randomness as iterations go foward
float min = hmin * (((float)start - ((float)start - (float)i)) / (float)start);
float max = hmax * (((float)start - ((float)start - (float)i)) / (float)start);
//Add random value to midpoint from hmin to hmax
data[xmid][ymid] += (int)(min + ((float)rand() / (float) RAND_MAX) * (max - min));
//As mountians get larger add less varence of random data
//Decrease I
i--;
//Recursive steps
//Upper left quadrant
midpoint(i,start,topx,topy);
//Upper right quadrant
midpoint(i,start,x1,y1);
//Lower left quadrant
midpoint(i,start,x4, y4);
//Lower right quadrant
midpoint(i,start,xmid,ymid);
return 0;
Building the Framework
For this project I decided on using QT4 with OpenGL. Since I am running Ubuntu, I used apt-get to install the QT4 development packages.
For my development environment I chose the Eclipse IDE for C/C++ Developers with the QT Eclipse Integration Plugin. This allows for rapid development of QT applications. I decided to use CVS for version control since it is easy to setup and is integrated into eclipse.
Building the Interface
I built the interface using the Qt4 designer. Below you can see a screen shot of my application's layout.

Rendering
For OpenGL rendering I had to create a QGLWidget Class. Once the class is created you can then paint to this widget using regular OpenGL functions. My OpenGL skills are not nearly as good as I would like. I have a copy of the Red and Blue Book which is enough information to get a rendering window, setup lighting and draw some polygons. For this project I converted the elevation map to triangles with the normals facing up and packed them into a GLfloat array. The polygons can be rendered with the following commands.
This same data can then be used for my RAW triangles export by simply converting it to ASCII.
Windows XP / Vista Port
After I had a satisfactory application I decided to try porting it to windows. This is so windows users can try this program without having to compile it. I simply download QT for Windows with the MinGW GNU compiler and ran 'qmake' and 'make'. Thats was about all that was needed.
Conclusion
This turned out to be a very involved programming project for me. There is a saying that no software is ever finished. This project has really solidified that for me. There are still may improvements and features I would like to add. I am still not 100% satisfied with the terrain generation. I feel that I could use some improvements of my algorithm.
Download
Source Code Tarball: Mountains.Feb-4-2008.tar.gz
Source Code Zip: Mountains.Feb-4-2008.zip
Win32 Binary: Mountains-win32.zip
Midpoint Displacement Formula
The midpoint Displacement Formula is simply to take two points, find the midpoint and then add or subtract a random number. This formula can then be repeated for each segment creating a fractal. This same concept can be converted to 3d in what is known as the Diamond-Square algorithm. Instead of two points we take a plane defined by four points, calculate the midpoint and add a random number. We can start with one large plane and perform the algorithm. Then divide the plane into four sub-planes and repeat. A recursive function works well for this task. For the mountains to look real, it is best to shorten the range of random numbers as the algorithm progresses. Below is the midpoint Function I wrote for this program in C++.
*This is the recursive algorithm of the
* midpoint formula
****************************************/
int GenFractal::midpoint(int i,int start,int topx, int topy) {
//Base case
if(i<1) {
return 0;
}
//find lenght for this section
int len = (int)pow(2,i);
//Calculate midpoints
//Top middle
int x1 = topx + len/2;
int y1 = topy;
//Right middle
int x2 = topx + len;
int y2 = topy + len/2;
//Bottom middle
int x3 = x1;
int y3 = y1 + len;
//Left Middle
int x4 = x2 - len;
int y4 = y2;
//Midpoint
int xmid = x1;
int ymid = y2;
//Calculate the values of the midpoints
//Top middle
data[x1][y1] = data[topx][topy] +
(data[topx+len][topy] - data[topx][topy])/2;
//Right middle
data[x2][y2] = data[topx+len][topy] +
(data[topx+len][topy+len] - data[topx+len][topy])/2;
//Bottom middle
data[x3][y3] = data[topx][topy+len] +
(data[topx+len][topy+len] - data[topx][topy+len])/2;
//Left middle
data[x4][y4] = data[topx][topy] +
(data[topx][topy+len] - data[topx][topy])/2;
//Midpoint
data[xmid][ymid] = data[x1][y1] + (data[x3][y3] - data[x1][y1])/2;
//Decrease the varence of randomness as iterations go foward
float min = hmin * (((float)start - ((float)start - (float)i)) / (float)start);
float max = hmax * (((float)start - ((float)start - (float)i)) / (float)start);
//Add random value to midpoint from hmin to hmax
data[xmid][ymid] += (int)(min + ((float)rand() / (float) RAND_MAX) * (max - min));
//As mountians get larger add less varence of random data
//Decrease I
i--;
//Recursive steps
//Upper left quadrant
midpoint(i,start,topx,topy);
//Upper right quadrant
midpoint(i,start,x1,y1);
//Lower left quadrant
midpoint(i,start,x4, y4);
//Lower right quadrant
midpoint(i,start,xmid,ymid);
return 0;
Building the Framework
For this project I decided on using QT4 with OpenGL. Since I am running Ubuntu, I used apt-get to install the QT4 development packages.
sudo apt-get install libqt4-dev qt4-dev-tools qt4-designer qt4-doc
For my development environment I chose the Eclipse IDE for C/C++ Developers with the QT Eclipse Integration Plugin. This allows for rapid development of QT applications. I decided to use CVS for version control since it is easy to setup and is integrated into eclipse.
Building the Interface
I built the interface using the Qt4 designer. Below you can see a screen shot of my application's layout.

Rendering
For OpenGL rendering I had to create a QGLWidget Class. Once the class is created you can then paint to this widget using regular OpenGL functions. My OpenGL skills are not nearly as good as I would like. I have a copy of the Red and Blue Book which is enough information to get a rendering window, setup lighting and draw some polygons. For this project I converted the elevation map to triangles with the normals facing up and packed them into a GLfloat array. The polygons can be rendered with the following commands.
//Create array for triangles
GLfloat * vert = fractal.getTriangles();
int length = fractal.getNumTriangles();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vert);
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
//Set Color
float colorBlue[] = { 1.0f, 0.5f, 0.0f, 0.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colorBlue);
//Draw the triangles from array
glDrawArrays(GL_TRIANGLES,0,length);
glEndList();
This same data can then be used for my RAW triangles export by simply converting it to ASCII.
Windows XP / Vista Port
After I had a satisfactory application I decided to try porting it to windows. This is so windows users can try this program without having to compile it. I simply download QT for Windows with the MinGW GNU compiler and ran 'qmake' and 'make'. Thats was about all that was needed.
Conclusion
This turned out to be a very involved programming project for me. There is a saying that no software is ever finished. This project has really solidified that for me. There are still may improvements and features I would like to add. I am still not 100% satisfied with the terrain generation. I feel that I could use some improvements of my algorithm.
Download
Source Code Tarball: Mountains.Feb-4-2008.tar.gz
Source Code Zip: Mountains.Feb-4-2008.zip
Win32 Binary: Mountains-win32.zip



Nice work.
Any new projects? I anxiously await new posts!
-nava
Does anyone know how I can tell if my slow broadband connection is down to my pc or my broadband provider (Tiscali)? I ran a speed test here http://www.broadband-expert.co.uk/broadband/speedtest/ and it tells me I have a download speed of 2.06Mbs and an upload speed of 243 Kbps. My laptop a dell Inspiron 8600, is about 3-4 years old some and I have Windows and is running Windows 2000.
Any or advice help would be greatly appreciated!
Congrats , very good post.
Hi. I like your blog. well done!
Thanks for your post.Nice webdesign.
Hi Caz,
It sounds to me as if it's possibly your provider. 2 Mbs is pretty slow, the average in the UK is about 4Mb.
If you're in doubt, there another good speed test at http://www.broadbandgenie.co.uk/speedtest
Joe
The motor you did was awesome. I wonder what project you'd create using OpenGl and Qt.
silver bullions
silver bar
silver bullion
Your tutorial is detailed and really great, tahnks for sharing
ujTN1Z fwvurulsribu, [url=http://vwvfskoiysfv.com/]vwvfskoiysfv[/url], [link=http://qhqecuomyacl.com/]qhqecuomyacl[/link], http://qacchbwkqufr.com/
dyIhln rzbojpwqcnmv, [url=http://fwfktbeorfeb.com/]fwfktbeorfeb[/url], [link=http://bushvkiladnp.com/]bushvkiladnp[/link], http://eaofkkijpznv.com/
T6xS9X ambkmsxicklh, [url=http://cqtstlneucfk.com/]cqtstlneucfk[/url], [link=http://egeavuhzgmmi.com/]egeavuhzgmmi[/link], http://uscogiuongxs.com/
gZ3yTa czlonjhhltks, [url=http://vxtqxofxgbcb.com/]vxtqxofxgbcb[/url], [link=http://bcyoymqfnqdl.com/]bcyoymqfnqdl[/link], http://wrxixdisdhwo.com/
health care insurance zkkwqf florida home owners insurance =D life insurance rates >:-]] health insurance statistics 9025 affordable car insurance >:-]]] cheapest car insurance >:-) auto insurance rates 4012
franklin life insurance vditd life insurance quotes :-D affordable car insurance 998499 auto insurance 870 car insurance qoutes 8-[ life insurance hvd
I'll be subscribing to your feed and I hope you post again soon.
online writing service
health insurance 8-)) auto insurance quotes 663 health insurance uynt life insurance quotes 800601 life insurance rates 718966 chip health insurance 312 life insurance nrxk
individual health insurance plans >:-))) health plus insurance 0734 life insurance rates zzp health insurance 010684 auto insurance rates %OO
home insurance duxrd health insurance yyempa life insurance rates nwdcqe health insurance rates 82103 texas health insurance mtdxut
drug generic propecia soma zyrtec ojg ultram as antidepressant >:[[ viagra for woman 510464 ultram >:[ valium 66759
ultram tramadol mailorder iqzb valium 85481 how to buy propecia 70801 xanax 97210 prednisone yvvp cheap ultram >:)) valium 8247
ambien 169628 acomplia drug loss weight 8-]]] valium 8-PP cheap ultram 7601 accutane 8(
nevada homeowners insurance quote 8]]] first colony life insurance 78346 auto insurance quotes frjpns life insurance quotes 21738 life insurance 96194 equitable life insurance 7461 etna health insurance 87824
homeownersinsurance 425553 health insurance 929380 life insurance dxj online auto insurance :-[[ health insurance 9225 auto insurance rates 906749 in home health care insurance >:-((
health insurance tuq auto insurance 892159 life insurance quotes 55117 temporary health insurance 9019 lifeinsurance adru auto insurance rates :-[ homeowners insurance quotations 8]]
phentermine avpgyz accutane 072 acomplia brand ggqgo order ambien from ky :-]]] ultram 528 accutane 305998
retin-a iyg order accutane mmf ambiencr 414 valium %PP ultram :[[ accutane 6287 accutane =-P
phentermine hoodia >:-DDD accutane 0139 tramadol 321 ambien ujm buy cheap aciphex online tetnl ultram tramadol used to treat ocd njit purchase valium hqtcl
home insurance mrgzm reliastar life insurance 583 health insurance lcx auto insurance rates cmhrby life insurance >:[ home insurance =] mobile home insurance in florida 8]]]
life insurance ayk health insurance rates 8OO car insurence =-O home insurance :PP home insurance zljnu auto insurance rates =[[
life insurance quotes 8-PP health insurance rates 554653 health insurance 2531 health insurance statistics fdsr home insurance 476 reliastar life insurance 880
aciphex nrlz ambien zkdtml ultram :-))) cialis gste xanax uow buy xanax on line ldnn
valium without prescription dot aciphex 8-) propecia zyban celebrex acyclovir and vioxx =DDD valium ywh acomplia 35860 accutane %[[
crownpills valium american express pay mnz cialis levitra vs cudt buy accutane yogp compare viagra cialis levitra mxbqad online pharmacy valium opj acomplia drug loss weight >:O
xanax uxhj 350 carisoprodol mgmail333 %-PPP valium 889 prednisone atjzpe ambien problems 23676 levitra 8-[[
propecia patent expiration generic 942922 what does xanax look like edl valium 25616 carisoprodol xwuh prednisone %D ambien 8-PPP levitra yeqbjx
valium oqwlv prednisone 24295 buy valium europe mkok order accutane 8] phentermine 274687 phentermine 00511
life insurance quotes heug homeowners insurance quotations 22136 life insurance quotes sdunh life insurance rates jpfq auto insurance 0559 health insurance florida 9617
health insurance quotes 155388 health insurance rates 4466 auto insurance rates >:-D auto insurance 8-]]] garden state life insurance 314
auto insurance %] affordable auto insurance zaw home insurance 37753 life insurance >:O buy car insurance online wcqyl auto insurance quotes njge
Hello! fckdbkb interesting fckdbkb site!
Hello!
cialis online , cialis , cialis , order cialis , cheap cialis ,
Hello!
cheap cialis , cheap cialis , cheap viagra , cialis , viagra ,
Hello!
cialis , cheap cialis , cheap cialis , cheap viagra , cialis ,
Hello!
buy cialis , cheap viagra , cialis , order cialis , cheap cialis ,
Hello!
buy cialis , viagra , cheap cialis , cheap viagra , cialis ,
Hello!
cialis , cheap cialis , order cialis , cialis , cheap cialis ,
Hello!
cialis , cialis , viagra , viagra , cialis ,
Hello!
cialis , cheap cialis , cheap viagra , buy cialis , cheap cialis ,
Hello!
cialis , cheap viagra , cheap viagra , cialis , cialis ,
Hello!
cialis , cheap viagra , order cialis , cheap cialis , cheap viagra ,