Advertisement

11.20.2008 at 06:02AM PST, ID: 23921466
[x]
Attachment Details
[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

Diamond Square Algorithm Implementation Problem

Asked by iMr_Ki in OpenGL Graphics & Game Programming, 3D Game Programming, Microsoft Visual C++

Tags:

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.
Start Free Trial
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;
}
 
Loading Advertisement...
 
[+][-]11.20.2008 at 06:05AM PST, ID: 23003693

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2008 at 06:31AM PST, ID: 23028140

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.04.2008 at 12:05PM PST, ID: 23098769

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.04.2008 at 08:38PM PST, ID: 23102404

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.04.2008 at 08:41PM PST, ID: 23102417

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.10.2008 at 05:20AM PST, ID: 23138563

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: OpenGL Graphics & Game Programming, 3D Game Programming, Microsoft Visual C++
Tags: OpenGL/C
Sign Up Now!
Solution Provided By: iMr_Ki
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628