Here it is so far, the actual heightmap code came from I think xIcedxEarthx
` This Program uses a simple function to load a heightmap and apply it to a matrix. It also
` Textures the matrix. This example includes a simple water effect as well. The function
` that creates the matrices with heightmaps is at the bottom. It has a hella lot of comments
` to help you understand it. Email me because I threw this together in one night, I'm sure
` that I made some mistakes. The code should run fine and hopefully I didn't make very many
` errors commenting all of the code. Good luck and have fun. Feedback is appreciated :P
` Author: Mason Cook
` Email: xIcedxEarthx@cox.net
Rem ***** Main Source File *****
sync on : sync rate 0 : ` Sets sync rate to max
set display mode 1024, 768, 16 : ` Sets up display mode
autocam off : hide mouse : disable escapekey : ` Turns off autocam and mouse, and turns
make object cube 10,1
position object 10,0,0,0 ` off escapekey. I use my own exit function.
global underWater as boolean : ` This will be used later for changing things underwater
` These are important, we are defining our heightmap. the "mapSize" variable is the actual
` size of our map which is 500. The segments are how many segments are going to be in the
` matrix. HeightSize is the height map file size, which is set to 256 since our actual
` heightmap.png is 256x256. If we made a heightmap that was 512x512, we would set heightSize
` to 512. The modifier is used when reading the heightmap. A height map is read by reading
` each pixel's color (0-255) and using that number to set our height on our matrix.
mapSize = 500
segments = 64
heightSize = 256
waterHeight = 45
rem Load Media : Loads all the required media.
load image "C:Program FilesDark Basic SoftwareDark Basic ProfessionalProjectsGravityhmp4.bmp", 1
load image "GFXgrass.png", 2
load image "GFXwater.png", 3
` This is calling the function that creates the heightmap. The inside the brackets are the
` functions parameters. the first number is the matrix id that we are creating. The second
` number is the heightmap image number that we loaded above. The third number is the texture
` number that we loaded above. The fourth parameter is the mapSize, the matrix size that we
` explained earlier. The heightSize is the heightMap size that we explained earlier. The
` segments is the amount of segments in the matrix that we also explained earlier. Calling
` this function will create our height map. At the end of this source, I will explain the
` actual function itself.
makeHeightMap(1, 1, 2, mapSize, heightSize, segments)
rem Creating the water. The alphamapping simply makes the water semi-transparent.
make object plain 1, mapSize, MapSize
texture object 1, 3
set alpha mapping on 1, 25
position object 1, mapSize / 2, waterHeight, mapSize / 2 : ` positioning the water in center.
color object 1,rgb(0,0,255)
xrotate object 1, -90 : ` adjusting its angle
position camera mapsize / 2, 50, mapsize / 2 : ` Placing camera in center of the world
rem Start Loop
do
ym# = get ground height (1,object position x(10),object position z(10))
cym# = get ground height (1,camera position x(0),camera position z(0))
rem Hud : Just getting some statistics on frames and polys
text 0, 0, "FPS: " +str$(screen fps())
text 0, 20, "Polys on screen: " +str$(statistic(1))
text 0,40, "Cube Y Pos: " + str$(ym#)
text 0,60, "Camera Y Pos: " + str$(camera position y(0))
if rightkey()
yrotate object 10,object angle y(10)+.1
endif
if leftkey()
yrotate object 10,object angle y(10)-.1
endif
if upkey()
move object 10,xs#
endif
if downkey()
move object 10,-xs#
endif
if object position y(10) <=ym#
y# = ym#
endif
if object position y(10) >=ym#
y# = ym#
endif
if spacekey()
xs# = .1
else
xs# = .025
endif
if camera position y(0) <= cym#+3
cy# = cym# + 3
endif
position object 10,object position x(10),y#,object position z(10)
set camera to follow object position x(10),object position y(10),object position z(10),object angle y(10),10,cym#-camera position y(0)+3,25,0
` A cool effect for when you go under the water. It colors the ambient light to a blue
` so it feels like your underwater. The underWater boolean prevents unnecessary
` changes to the ambient color.
if y# < 45 && underWater = 0
color ambient light rgb(0, 128, 255)
underWater = 1
endif
` Returns ambient color to normal when your above the water.
if y# > 45 && underWater = 1
color ambient light rgb(255, 255, 255)
underWater = 0
endif
rem Exit Program : This exits the program using a function that prevents memory leaks.
if escapekey() = 1 then escape()
rem End loop
sync
loop
` Okay, heres the makeHeightMap Function. The mId is simply the matrix number.the heightmap
` is the image number that you loaded heightmap.png into. In this example it is the number 1.
` The texture is the image number we loaded grass.png into which is the number 2.
` The mapsize is the actual size of our matrix, which in this example is 500. The heightSize
` is the actual size of our heightmap.png image, which in this case is 256 pixels. This may
` change according to the heightmap image. The segments are the number of segments that
` we will use for our matrix. The first command is make matrix. It uses the mId (matrix #),
` the size, and segments we described earlier. We now have a matrix thats 500x500 in size
` and 64x64 in segments. prepare matrix texture simply textures the matrix. cls is important
` in this case because it clears the screen and prepares for use to read the pixels on the
` image. Paste image places that heightmap onto the screen for pixel reading. The reason we
` want to read the pixels is to use the actual pixel number depending on its color to set
` the height for the matrix. I'll explain that in a bit. modifier = heightsize / segments
` is important since our image is larger then our segments. By dividing the heightsize by
` the segments, we are checking every other pixel. The next block of code is where the actual
` reading the image begins. We make two for commands using the variables x & y, not stopping
` until we reach segments(64). Inside is where the action happens. I will now explain the line
` pixelColor# = rgbr(point(x * modifier, y * modifier)). This line is taking the image and
` looking at it pixel by pixel. It gets to the first pixel, and uses the command rgbr, which
` retrieves the red color of a pixel. This rgbr is going to return a number 0-255, depending
` on its color. The darker it is, the closer it is to 0. In order to know what pixel rgbr
` is checking, it needs to use x * modifier and y * modifier. Since your x and y get larger
` through each for loop, it will eventually check the whole image. Summing it all up, that
` line of code retrieves the pixel color # which will be somewhere between 0 and 255. This
` line literally makes the height of our matrix. Using the new pixel color that we retrieved
` from the heightmap image, we can now set the height of the matrix depending on the value.
` If that value retrieved was 0, this means that the matrix tile we are currently on will be
` set to a height of 0. If we retrieve a value of 155, the tile we are currently on will be
` set to 155. The for loop goes through the whole image and matrix, creating our matrix.
` Last but not least, the matrix is updated to show the new look of the matrix. Whoohoo!
` Hopefully you got all that, but if not feel free to email me at xIcedxEarthx@cox.net
` I love feedback and I'll surely help you out! I learned how to do this myself using
` a tutorial in one of the TGC newsletters. From that I made my own function that makes
` creating matrices a hella lot easier.
function makeHeightMap(mId, heightmap, texture, mapSize, heightSize, segments)
make matrix mId, mapSize, mapSize, segments, segments
prepare matrix texture mId, texture, 1, 1
cls
paste image heightmap, 0, 0
modifier = heightSize / segments
for x = 0 to segments
for y = 0 to segments
pixelColor# = rgbr(point(x * modifier, y * modifier))
set matrix height 1, x, y, pixelColor#
next x
next y
update matrix 1
endfunction
Rem engine Function : The functinos below are used to exit the program without memory leaks.
function deleteAll()
for x = 1 to 2000
if image exist(x) = 1 then delete image x
if object exist(x) = 1 then delete object x
if sprite exist(x) = 1 then delete sprite x
if sound exist(x) = 1 then delete sound x
if music exist(x) = 1 then delete music x
if matrix exist(x) = 1 then delete matrix x
if particles exist(x) = 1 then delete particles x
if light exist(x) = 1 then delete light x
next x
endfunction deleteAll
rem Escape Game
function escape()
deleteAll()
end
endfunction escape
there are alot of missing remarks and its not even working right anymore but at least its not completely gone, really crappy terrain too:
[EDIT]
Ok, so I just got it back to how I think it was, but I still think theres something missing.
` This Program uses a simple function to load a heightmap and apply it to a matrix. It also
` Textures the matrix. This example includes a simple water effect as well. The function
` that creates the matrices with heightmaps is at the bottom. It has a hella lot of comments
` to help you understand it. Email me because I threw this together in one night, I'm sure
` that I made some mistakes. The code should run fine and hopefully I didn't make very many
` errors commenting all of the code. Good luck and have fun. Feedback is appreciated :P
` Author: Mason Cook
` Email: xIcedxEarthx@cox.net
Rem ***** Main Source File *****
sync on : sync rate 0 : ` Sets sync rate to max
set display mode 1024, 768, 16 : ` Sets up display mode
autocam off : hide mouse : disable escapekey : ` Turns off autocam and mouse, and turns
make object cube 10,1
position object 10,0,0,0 ` off escapekey. I use my own exit function.
global underWater as boolean : ` This will be used later for changing things underwater
` These are important, we are defining our heightmap. the "mapSize" variable is the actual
` size of our map which is 500. The segments are how many segments are going to be in the
` matrix. HeightSize is the height map file size, which is set to 256 since our actual
` heightmap.png is 256x256. If we made a heightmap that was 512x512, we would set heightSize
` to 512. The modifier is used when reading the heightmap. A height map is read by reading
` each pixel's color (0-255) and using that number to set our height on our matrix.
mapSize = 500
segments = 64
heightSize = 256
waterHeight = 109
rem Load Media : Loads all the required media.
load image "hmp4.bmp", 1
load image "grass.png", 2
load image "water.bmp", 3
` This is calling the function that creates the heightmap. The inside the brackets are the
` functions parameters. the first number is the matrix id that we are creating. The second
` number is the heightmap image number that we loaded above. The third number is the texture
` number that we loaded above. The fourth parameter is the mapSize, the matrix size that we
` explained earlier. The heightSize is the heightMap size that we explained earlier. The
` segments is the amount of segments in the matrix that we also explained earlier. Calling
` this function will create our height map. At the end of this source, I will explain the
` actual function itself.
makeHeightMap(1, 1, 2, mapSize, heightSize, segments)
rem Creating the water. The alphamapping simply makes the water semi-transparent.
make object plain 1, mapSize, MapSize
texture object 1, 3
set alpha mapping on 1, 25
position object 1, mapSize / 2, waterHeight, mapSize / 2 : ` positioning the water in center.
xrotate object 1, -90 : ` adjusting its angle
position camera mapsize / 2, 50, mapsize / 2 : ` Placing camera in center of the world
rem Start Loop
do
ym# = get ground height (1,object position x(10),object position z(10))
cym# = get ground height (1,camera position x(0),camera position z(0))
rem Hud : Just getting some statistics on frames and polys
text 0, 0, "FPS: " +str$(screen fps())
text 0, 20, "Polys on screen: " +str$(statistic(1))
text 0,40, "Cube Y Pos: " + str$(ym#)
text 0,60, "Camera Y Pos: " + str$(camera position y(0))
text 0,80, "CYM# " + str$(cym#)
text 0,100, "CYP-CYM: " + str$(camera position y(0)-cym#)
if rightkey()
yrotate object 10,object angle y(10)+.1
endif
if leftkey()
yrotate object 10,object angle y(10)-.1
endif
if upkey()
move object 10,xs#
endif
if downkey()
move object 10,-xs#
endif
if object position y(10) <=ym#
y# = ym#
endif
if object position y(10) >=ym#
y# = ym#
endif
if spacekey()
xs# = .1
else
xs# = .025
endif
if camera position y(0) <= cym#+3
cy# = cym# + 3
endif
if camera position y(0) >= cym#+3
cy# = cym# + 3
endif
point camera object position x(10),object position y(10), object position z(10)
position camera camera position x(0),cy#,camera position z(0)
position object 10,object position x(10),y#,object position z(10)
set camera to follow object position x(10),object position y(10),object position z(10),object angle y(10),10,cym#-object position y(10)+3,25,0
` A cool effect for when you go under the water. It colors the ambient light to a blue
` so it feels like your underwater. The underWater boolean prevents unnecessary
` changes to the ambient color.
if camera position y(0) < waterHeight && underWater = 0
color ambient light rgb(0, 128, 255)
underWater = 1
endif
` Returns ambient color to normal when your above the water.
if camera position y(0) > waterHeight && underWater = 1
color ambient light rgb(255, 255, 255)
underWater = 0
endif
rem Exit Program : This exits the program using a function that prevents memory leaks.
if escapekey() = 1 then escape()
rem End loop
sync
loop
` Okay, heres the makeHeightMap Function. The mId is simply the matrix number.the heightmap
` is the image number that you loaded heightmap.png into. In this example it is the number 1.
` The texture is the image number we loaded grass.png into which is the number 2.
` The mapsize is the actual size of our matrix, which in this example is 500. The heightSize
` is the actual size of our heightmap.png image, which in this case is 256 pixels. This may
` change according to the heightmap image. The segments are the number of segments that
` we will use for our matrix. The first command is make matrix. It uses the mId (matrix #),
` the size, and segments we described earlier. We now have a matrix thats 500x500 in size
` and 64x64 in segments. prepare matrix texture simply textures the matrix. cls is important
` in this case because it clears the screen and prepares for use to read the pixels on the
` image. Paste image places that heightmap onto the screen for pixel reading. The reason we
` want to read the pixels is to use the actual pixel number depending on its color to set
` the height for the matrix. I'll explain that in a bit. modifier = heightsize / segments
` is important since our image is larger then our segments. By dividing the heightsize by
` the segments, we are checking every other pixel. The next block of code is where the actual
` reading the image begins. We make two for commands using the variables x & y, not stopping
` until we reach segments(64). Inside is where the action happens. I will now explain the line
` pixelColor# = rgbr(point(x * modifier, y * modifier)). This line is taking the image and
` looking at it pixel by pixel. It gets to the first pixel, and uses the command rgbr, which
` retrieves the red color of a pixel. This rgbr is going to return a number 0-255, depending
` on its color. The darker it is, the closer it is to 0. In order to know what pixel rgbr
` is checking, it needs to use x * modifier and y * modifier. Since your x and y get larger
` through each for loop, it will eventually check the whole image. Summing it all up, that
` line of code retrieves the pixel color # which will be somewhere between 0 and 255. This
` line literally makes the height of our matrix. Using the new pixel color that we retrieved
` from the heightmap image, we can now set the height of the matrix depending on the value.
` If that value retrieved was 0, this means that the matrix tile we are currently on will be
` set to a height of 0. If we retrieve a value of 155, the tile we are currently on will be
` set to 155. The for loop goes through the whole image and matrix, creating our matrix.
` Last but not least, the matrix is updated to show the new look of the matrix. Whoohoo!
` Hopefully you got all that, but if not feel free to email me at xIcedxEarthx@cox.net
` I love feedback and I'll surely help you out! I learned how to do this myself using
` a tutorial in one of the TGC newsletters. From that I made my own function that makes
` creating matrices a hella lot easier.
function makeHeightMap(mId, heightmap, texture, mapSize, heightSize, segments)
make matrix mId, mapSize, mapSize, segments, segments
prepare matrix texture mId, texture, 1, 1
cls
paste image heightmap, 0, 0
modifier = heightSize / segments
for x = 0 to segments
for y = 0 to segments
pixelColor# = rgbr(point(x * modifier, y * modifier))
set matrix height 1, x, y, pixelColor#
next x
next y
update matrix 1
endfunction
Rem engine Function : The functinos below are used to exit the program without memory leaks.
function deleteAll()
for x = 1 to 2000
if image exist(x) = 1 then delete image x
if object exist(x) = 1 then delete object x
if sprite exist(x) = 1 then delete sprite x
if sound exist(x) = 1 then delete sound x
if music exist(x) = 1 then delete music x
if matrix exist(x) = 1 then delete matrix x
if particles exist(x) = 1 then delete particles x
if light exist(x) = 1 then delete light x
next x
endfunction deleteAll
rem Escape Game
function escape()
deleteAll()
end
endfunction escape
AMD Athlon 64 3200 - GeForce 6800 GT - 320GB Hard Drive
1024MB RAM