applying a texture to GeoMipTerrain (c++)

Hi guys

this is my first message at code snippets section and i am not really sure if this is worth putting here :blush:

I would like to share a piece of code which creates terrain from a heightfield image and applies same texture over it repeatedly.

int main(int argc, char *argv[]) 
{
/// create window
PandaFramework framework;
WindowFramework * window;
framework.open_framework(argc, argv);
framework.set_window_title("terrain example");
window = framework.open_window();
window->setup_trackball();

/// create terrain
GeoMipTerrain *terrain;
terrain =  new GeoMipTerrain("myTerrain");
terrain->set_heightfield(Filename("maps/hf.png"));
terrain->set_bruteforce(true);
terrain->get_root().reparent_to(window->get_render()); 

/// below line is a must! 
/// otherwise terrain will be flat
terrain->get_root().set_sz(100);
terrain->get_root().set_sx(10);
terrain->get_root().set_sy(10);
terrain->generate();


/// create texture
TexturePool * texture_pool = TexturePool::get_global_ptr();
TextureStage * ts = new TextureStage("stage");
ts->set_sort(0);
ts->set_mode(TextureStage::M_replace);
Texture * terrain_texture = texture_pool->load_texture("grass2.jpg", "grass2.jpg");
terrain->get_root().set_texture(ts, terrain_texture);

/// this line applies same texture to terrain multiple times
/// if you are using a small texture over a large terrain
/// panda will expand the texture over all terrain unless you use below line
terrain->get_root().set_tex_scale(ts, 1000, 1000);
ts->set_saved_result(true);

framework.main_loop();
return 0;
}

hope it helps somebody someday :slight_smile:

Hi ugra.hasbek

thanks for your code, that helped me :slight_smile: