Advertisement

11.20.2008 at 06:02AM PST, ID: 23921466
[x]
Attachment Details

Diamond Square Algorithm Implementation Problem

[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8
Im working on an implementation of the Diamon Square algortihm, and I can't quite seem to get it to look right.

I'm working off this tutorial http://www.lighthouse3d.com/opengl/terrain/index.php?mpd2 which might help explain some of my code.

The function that I know I need to change somehow is below.

It simply takes the min and max X/Z coordinates for the current square being subdivided (which index into an array of Y-Values where these min's and max's already have a Y value computed) and a displacement.

I'm pretty sure the step where I calculate a new midpoint is correct (setting E) but the part that I know is wrong is where I set the midpoint of each side (F G H I)

The forumla seems like it should work, but on high levels of recursion I see almost veritcal drops instead of a smoother transition:

http://img185.imageshack.us/img185/1323/failyh0.png

An alternate approach I tried that -almost- worked was just doing:

   yMap[xMin][zMidpnt] = (A + C ) / 2;  // F
   yMap[xMidpnt][zMin] = (A + B) / 2 ;  // G
   yMap[xMax][zMidpnt] = (B + D)/ 2;  // H
   yMap[xMidpnt][zMax] = (C + D) / 2 ;  // I

Which produces a more better landscape but isn't really performing the square step of the algorithm correctly (I think) because it does not add any random displacement to these midpoints.

Any help on how to fix this would be appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
void genLandscapeRec(int xMin, int xMax, int zMin, int zMax, float disp) {
 
   float nDisp =  (float)(disp * (float)pow(2, -(double)roughnes)); // New displacement
 
   int xMidpnt = (xMin+xMax) / 2;
   int zMidpnt = (zMin+zMax) / 2;
 
   GLfloat A = yMap[xMin][zMin];
   GLfloat B = yMap[xMax][zMin];
   GLfloat C = yMap[xMin][zMax];
   GLfloat D = yMap[xMax][zMax];
 
   GLfloat E = (A + B + C + D) / 4 + randDisp(disp); // Midpoint height
 
   yMap[xMidpnt][zMidpnt] = E; // Set E
 
   yMap[xMin][zMidpnt] = (A + C + E) / 3 + randDisp(disp);  // F
   yMap[xMidpnt][zMin] = (A + B + E) / 3 + randDisp(disp);  // G
   yMap[xMax][zMidpnt] = (B + D + E) / 3 + randDisp(disp);  // H
   yMap[xMidpnt][zMax] = (C + D + E) / 3 + randDisp(disp);  // I
 
   if ((xMidpnt - xMin) > 1) { // Subdivide if new midpoint length will be > 1
 
      genLandscapeRec(xMin, xMidpnt, zMin, zMidpnt, nDisp);
      genLandscapeRec(xMidpnt, xMax, zMin, zMidpnt, nDisp);
      genLandscapeRec(xMin, xMidpnt, zMidpnt, zMax, nDisp);
      genLandscapeRec(xMidpnt, xMax, zMidpnt, zMax, nDisp);
   }
}
 
float randDisp(float disp) {
 
    return (((float)rand()) / ((float)RAND_MAX)) * (disp + (disp/3)) - disp/3;
}
 
 
 
Author Comment by iMr_Ki:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by ikework:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Author Comment by iMr_Ki:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by jgordos:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Expert Comment by jgordos:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
Accepted Solution by iMr_Ki:

All comments and solutions are available to Premium Service Members only. Start your 7-day free trial to view the solution to this question.

Already a member? Login to view this solution.

 
 
20081119-EE-VQP-45 - Hierarchy / EE_QW_2_20070628