Allegro.cc - Online Community

Allegro.cc Forums » The Depot » terrain drawing without sorting - uses camera azimuth

This thread is locked; no one can reply to it. rss feed Print
terrain drawing without sorting - uses camera azimuth
altalena
Member #13,639
October 2011
avatar

Here's the program I was working on until Skyrim came along and stole all my time.

I just fixed a bit of the terrain generation code. The camera rotation now uses a function to cut down on the number of lines of code.

The .exe should work OK for windows 7.

It's all my original code from scratch, except for the Mersenne Twister algorithm and some help with the Perlin noise 2d algorithm from SiegeLord. Edgar Reynaldo pointed out how to speed up the pixel drawing.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

So you draw the terrain from highest azimuth to lowest? How do you deal with two sections of terrain that pass through the same ray as cast from the camera?

The terrain looks neat though. I used to have a fractal terrain generator program that I loved to mess with all the time.

:P NSND[1] :P
{"name":"605114","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/4\/84b355e2057014382b560d1665437138.png","w":812,"h":482,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/4\/84b355e2057014382b560d1665437138"}605114

The large triangles in the upper middle look a little funny though - are they closer than the rest of the triangles, or are they larger and farther away?

References

  1. No screenshot, no download
altalena
Member #13,639
October 2011
avatar

I don't really know what ray casting is, but I'll show you how I made it sort the terrain without needing a sort function.

It still can draw the two triangles of each terrain square in the wrong order. I should fix that.

OK. I first had it draw the terrain in a simple 2-level nested for loop with 0-32 and 0-32. Then I noticed that from a certain range of angles (1/4 of camera rotation) the terrain drew closer squares on top of farther squares. I reversed the order for the opposite 1/4 of the camera rotation, going from 32-0 and 32-0. I then tried to use a nested for loop going from 0-32 and 32-0 for one way and 32-0 and 0-32 for the other way, for the remaining 2 quarters of the rotation, but it didn't work. I switched the outer for loop's counter variable with the inner for loop's variable, then made it shorter by using the m2 and m3, but switching to m3 and m2. Here's the final code.

#SelectExpand
1 camdir=0; // terrain is drawn in a specific order based on camera azimuth 2 if (ra__cam[3]>=051&&ra__cam[3]<151) camdir=3; 3 if ((ra__cam[3]>=351&&ra__cam[3]<=400)||(ra__cam[3]>=000&&ra__cam[3]<051)) camdir=2; 4 if (ra__cam[3]>=251&&ra__cam[3]<351) camdir=1; 5 if (ra__cam[3]>=151&&ra__cam[3]<251) camdir=0; 6 7 m2 = 0; 8 m3 = 0; 9 if (camdir==0) m2=0; 10 if (camdir==2) m2=31; 11 if (camdir==3) m3=0; 12 if (camdir==1) m3=31; 13 for (n2=0; n2<=31; n2+=1) 14 { 15 if (camdir==0) m3=0; 16 if (camdir==2) m3=31; 17 if (camdir==3) m2=0; 18 if (camdir==1) m2=31; 19 for (n3=0; n3<=31; n3+=1) 20 { 21 22 // terrain drawing goes here 23 24 if (camdir==0) m3+=1; // draws terrain in the correct order 25 if (camdir==2) m3-=1; // the m2 and m3 variables are important here 26 if (camdir==3) m2+=1; 27 if (camdir==1) m2-=1; 28 } 29 if (camdir==0) m2+=1; 30 if (camdir==2) m2-=1; 31 if (camdir==3) m3+=1; 32 if (camdir==1) m3-=1; 33 }

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

altalena
Member #13,639
October 2011
avatar

when the terrain draws in this order

01 - 02 - 03 - 04
05 - 06 - 07 - 08
09 - 10 - 11 - 12
13 - 14 - 15 - 16 (camera is pointing to the top left) ↖

it looks correct from 1/4 of the camera rotations. Note that for every square, the three that are behind - the ones above, to the left, and to the top left - are drawn before it.

From the opposite 1/4 of camera angles,

16 - 15 - 14 - 13
12 - 11 - 10 - 09
08 - 07 - 06 - 05
04 - 03 - 02 - 01 (camera is pointing to the bottom right) ↘

looks correct.

From the other two quarters of camera angles, the orders are

04 - 08 - 12 - 16
03 - 07 - 11 - 15
02 - 06 - 10 - 14
01 - 05 - 09 - 13 (camera is pointing to the bottom left) ↙

and

13 - 09 - 05 - 01
14 - 10 - 06 - 02
15 - 11 - 07 - 03
16 - 12 - 08 - 04 (camera is pointing to the top right) ↗

See how squares get drawn over squares that are farther away?

The m2 and m3 are the x and y coordinates of the corner point of the square on the 33x33 mesh that is used to draw the two triangles of the terrain square

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Go to: