Jump to content


Photo

Easy Heightmap Scripts


  • Please log in to reply
42 replies to this topic

#1 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 15 March 2009 - 04:20 AM

  • Title: Easy Heightmap
  • Description: Create a working heightmap in 3 scripts
  • GM Version: Game Maker 7
  • Registered: Yes
  • File Type: .Zip (contains 2 files, an example and the scripts)
  • File Size: Approx 90KB
  • File Link: Download (Host-a-net)
  • You may also be interested in: GameDev Studio
Additional Info
Credit is always appreciated, but not required.


This is a very easy to use heightmap engine. It contains three scripts
terrain_create(); - creates the heightmap with the wanted settings
terrain_draw(); - Draw the terrain
terrain_get_z(); - Find the terrains Z at position [X,Y]


Screenshot:
Posted Image

Enjoy :P


Like this example? Then visit GM3D, You'll find many 3D things there :)

~~Brett14;

[EDIT]
After several requests for a script that stretches one texture over the whole terrain, I've decided instead of PM'ming them all the code just put it here. So if you want a stretched texture, then replace the create code with this. Thanks.

PLEASE NOTE: You'll need to remove the spaces between some lines that split the function names in half.
/*
****---- ~ Credit goes to: brett14 ~ ----****

THE TERRAIN SHOULD HAVE THE HIGHEST DEPTH (10000 etc. - it gets set to -1000 after initing this though)
loads the terrain from a heightmap sprite - it does not matter what the size of the sprite is (32x32, 64x64 or 128x128 is recommended)



Argument0 = terrain heightmap sprite
Argument1 = the size of the terrain squares (X,Y)
Argument2 = the height value of the terrain (Z)

*/
var counter,counter2,xx,yy;
global.swidth=sprite_get_width(argument0);
global.sheight=sprite_get_height(argument0);
global.terrainsize = argument1;
global.terraingrid=ds_grid_create(global.swidth,global.sheight);
global.terrainmodel=d3d_model_create();
draw_sprite_stretched(argument0,0,0,0,global.swidth+2,global.sheight
+2)
for(w=0;w<global.swidth+2;w+=1){
	for(h=0;h<global.sheight+2;h+=1){
		val=draw_getpixel(w,h)
		valex=color_get_value(val)
		ds_grid_set(global.terraingrid,w,h,valex/240*argument2)
		}
	}
counter2=0;
for(xx=0;xx<global.swidth-1;xx+=1){
d3d_model_primitive_begin(global.terrainmodel,pr_trianglestrip);
counter=0;
for (yy=0;yy<global.sheight;yy+=1){
  z2=ds_grid_get(global.terraingrid,yy,xx+1);
  d3d_model_vertex_normal_texture(global.terrainmodel,yy*global.terrainsize,(xx+1)*global.terrainsize,z2,0,0,1,counter,counter2+
1/global.swidth);
  z1=ds_grid_get(global.terraingrid,yy,xx);
  d3d_model_vertex_normal_texture(global.terrainmodel,yy*global.terrainsize,xx*g
lobal.terrainsize,z1,0,0,1,counter,counter2);  
  counter+=1/global.swidth;
  }
counter2+=1/global.swidth;
d3d_model_primitive_end(global.terrainmodel);
}
depth=-1000;
[/EDIT]

Edited by brett14, 14 March 2012 - 03:30 AM.

  • 3

#2 JohnnyDarkness

JohnnyDarkness

    GMC Member

  • New Member
  • 19 posts

Posted 25 March 2009 - 01:43 PM

Exellent Job Brett14, It does what it is supposed to. I hope this will work for my game. 5/5
  • 0

#3 §-«»-§

§-«»-§

    GMC Member

  • New Member
  • 83 posts

Posted 07 April 2009 - 07:06 AM

Very nice. Your demo seems to work perfectly, though since the camera starts pointing straight up it took me a while to figure out where the terrain actually was.

The simplicity of your system is impressive, it hides the nuts and bolts of terrain generation quite well. One question: Will lighting "just work" with this or will it take some modification of your scripts?
  • 0

#4 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 08 April 2009 - 04:00 AM

Maybe, I wrote these a long time ago, but if i remember correctly, lighting works fine :medieval: , if not it will only take a little modification.
  • 0

#5 the sivart

the sivart

    GMC Member

  • GMC Member
  • 145 posts
  • Version:GM:Studio

Posted 20 April 2009 - 03:59 PM

looks like you did a very good job :snitch:
  • 0

#6 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 21 April 2009 - 12:34 AM

looks like you did a very good job :snitch:

Yeah, I try to do my best on everything I give out on the gmc. All things are made to be extremely easy to use.
  • 0

#7 allewille

allewille

    GMC Member

  • New Member
  • 30 posts

Posted 29 November 2009 - 07:54 PM

how do you change the max height of the map ?? because ow the higest point isnt really high at all...
  • 0

#8 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 07 January 2010 - 10:58 PM

Thank you for such an amazing set of easy to use scripts.
You did a very good job on this!

What is the easiest way to create heightmap sprites?

how do you change the max height of the map ?? because ow the higest point isnt really high at all...

in the terrain sprite you can make the highest point pure white, or when you call terrain_create make the third argument bigger. Right now it's:
terrain_create(heightmap,256,2500);
you can change it to:
terrain_create(heightmap,256,4000);
to make the max height bigger.

Edited by Hockeyflyers, 08 January 2010 - 12:41 AM.

  • 0

#9 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 08 January 2010 - 12:55 AM

Glad you like them :(
  • 0

#10 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 08 January 2010 - 03:50 AM

I have two quick questions.
How would I get multiple terrains on the ground? Would the best way to be in the for loops in the terrain_create() script check for a terrain object and if it's there add it to the primitive?

and

What is the easiest way to create heightmap sprites?

Edited by Hockeyflyers, 08 January 2010 - 03:51 AM.

  • 0

#11 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 09 January 2010 - 01:01 AM

to create a heightmap: create a sprite with the desired dimensions, make it all black. Draw white lines where you want it to be higher at. Blur the sprite 3-5. Keep repeating these two steps until you get a result you like. If it is too high then do the same thing with black.

To create multiple terrains, what do you mean by that? Visible at the same time OR just create them all at the same time?
  • 0

#12 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 09 January 2010 - 03:21 AM

to create a heightmap: create a sprite with the desired dimensions, make it all black. Draw white lines where you want it to be higher at. Blur the sprite 3-5. Keep repeating these two steps until you get a result you like. If it is too high then do the same thing with black.

thanks

To create multiple terrains, what do you mean by that? Visible at the same time OR just create them all at the same time?

Oh sorry, I meant multiple textures in one terrain.
  • 0

#13 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 09 January 2010 - 07:18 AM

multiple textures is not supported by this engine. I could make it possible, but I'm not going too, as it would take too much time (a couple of hours probably). Sorry.
  • 0

#14 anatheron

anatheron

    GMC Member

  • New Member
  • 27 posts

Posted 21 January 2010 - 11:24 PM

Wow! I must addmit... This is quite awsome :whistle: And it will sure come in handy when it comes to getting the games to look better.
only prob is that I can't see the hightmap in the room editor wich makes it a bit difficoult to place other objects correctly. But else good work, It truly is awsome! ;P
  • 0

#15 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 02 March 2010 - 05:00 AM

I've added the script (in the top post) to create a terrain with one texture stretched over it, due to many requests via PM.
  • 0

#16 GMkizzle

GMkizzle

    GMC Member

  • GMC Member
  • 374 posts

Posted 09 May 2010 - 07:30 AM

These scripts are great!
Multiple textures would be great, but i know you said you wouldnt do it, but i think you should reconsider :)
  • 0

#17 michael pw

michael pw

    GMC Member

  • GMC Member
  • 1488 posts
  • Version:GM8.1

Posted 28 June 2010 - 11:00 PM

great engine, used it a few times. i have a problem though, basically i have a heightmap sprite but the terrain drawn is flat???

i had this problem before but managed to fix it. but i forgot what i did D:

pls help thanks. actually ill test get z to see if the terrain model array data list has been created first.

EDIT: the z value is consistently at 0

EDIT: soz i always do this, i accidently had another object calling the script but to a plain heightmap, sorry for fussing, it works now.

Edited by michael pw, 28 June 2010 - 11:05 PM.

  • 0

#18 nottud

nottud

    GMC Member

  • New Member
  • 129 posts

Posted 01 July 2010 - 06:26 AM

This is really good - I am impressed by the VERY high FPS count. I was getting like 1300!
  • 0

#19 solano

solano

    GMC Member

  • GMC Member
  • 460 posts
  • Version:GM8

Posted 01 July 2010 - 07:25 AM

How did you get it to look so good.
Is it because of the culling?
d3d_set_culling(1);
d3d_model_draw(global.terrainmodel,0,0,0,background_get_texture(argument0))
d3d_set_culling(0);

Or is it the culling and something else?
  • 0

#20 brett14

brett14

    GMC Member

  • GMC Member
  • 1150 posts
  • Version:GM8

Posted 01 July 2010 - 03:18 PM

@nottud @ solano
Nah, the culling doesn't make it look good. The culling just makes it run quite a bit faster.

@solano
To get it to look so good... Um? Culling just makes it so that faces that are not seen don't get drawn. I just used a nice texture, and I wrote the scripts to be efficient.

@nottud
GM8 or GM7? I have a faster version for GM8, its about 30% faster but it's not out to the public - think I should release it?

Edited by brett14, 01 July 2010 - 03:19 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users