heightmap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /** Demo showing the heightmap environment. */
  2. #define CAMERA_STEP 200
  3. #define HEIGHTMAP_3D_RESOLUTION 32
  4. #define HEIGHTMAP_3D_STEP (TPE_F * 2)
  5. #define MAP_LIMIT ((HEIGHTMAP_3D_RESOLUTION * HEIGHTMAP_3D_STEP) / 2)
  6. #include "helper.h"
  7. /** For given heightmap node at [x,y] returns its height. Here the function uses
  8. sines/cosines to generate a simple procedural heightmap but it could also
  9. retrieve the height e.g. from an image. */
  10. TPE_Unit height(int32_t x, int32_t y)
  11. {
  12. x *= 8;
  13. y *= 8;
  14. return
  15. TPE_sin(x + TPE_cos(y * 2)) * TPE_sin(y * 2 + TPE_cos(x * 4)) / (TPE_F / 2);
  16. }
  17. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  18. {
  19. return TPE_envHeightmap(p,TPE_vec3(0,0,0),HEIGHTMAP_3D_STEP,height,maxD);
  20. }
  21. int main(void)
  22. {
  23. helper_init();
  24. helper_debugDrawOn = 0;
  25. // here we just set up the graphical 3D model of the heigtmap:
  26. for (int y = 0; y < HEIGHTMAP_3D_RESOLUTION; ++y)
  27. for (int x = 0; x < HEIGHTMAP_3D_RESOLUTION; ++x)
  28. helper_setHeightmapPoint(x,y,height(x - HEIGHTMAP_3D_RESOLUTION / 2,y - HEIGHTMAP_3D_RESOLUTION / 2));
  29. tpe_world.environmentFunction = environmentDistance;
  30. helper_addBox(700,700,700,300,1000);
  31. TPE_bodyMoveTo(&helper_lastBody,TPE_vec3(0,5000,0));
  32. s3l_scene.camera.transform.rotation.x = -1 * TPE_F / 8;
  33. while (helper_running)
  34. {
  35. helper_frameStart();
  36. TPE_Vec3 bodyCenter = TPE_bodyGetCenterOfMass(&tpe_world.bodies[0]);
  37. s3l_scene.camera.transform.translation.x = bodyCenter.x;
  38. s3l_scene.camera.transform.translation.y = bodyCenter.y + 3000;
  39. s3l_scene.camera.transform.translation.z = bodyCenter.z - 3000;
  40. if (bodyCenter.x < -1 * MAP_LIMIT)
  41. TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(2 * MAP_LIMIT,TPE_F,0));
  42. else if (bodyCenter.x > MAP_LIMIT)
  43. TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(-2 * MAP_LIMIT,TPE_F,0));
  44. if (bodyCenter.z < -1 * MAP_LIMIT)
  45. TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(0,TPE_F,2 * MAP_LIMIT));
  46. else if (bodyCenter.z > MAP_LIMIT)
  47. TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(0,TPE_F,-2 * MAP_LIMIT));
  48. if (helper_frame % 32 == 0)
  49. helper_printCPU();
  50. helper_set3DColor(0,200,100);
  51. helper_drawModel(&heightmapModel,TPE_vec3(-HEIGHTMAP_3D_STEP / 2,0,-HEIGHTMAP_3D_STEP / 2),TPE_vec3(512,512,512),TPE_vec3(0,0,0));
  52. helper_set3DColor(100,100,200);
  53. helper_draw3DBox(bodyCenter,TPE_vec3(2 * TPE_F,2 * TPE_F,2 * TPE_F),
  54. TPE_bodyGetRotation(&tpe_world.bodies[0],0,1,2));
  55. for (int i = 0; i < tpe_world.bodyCount; ++i)
  56. TPE_bodyApplyGravity(&tpe_world.bodies[i],7);
  57. TPE_worldStep(&tpe_world);
  58. #define ACC (TPE_F / 50)
  59. if (sdl_keyboard[SDL_SCANCODE_UP])
  60. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,0,ACC));
  61. else if (sdl_keyboard[SDL_SCANCODE_DOWN])
  62. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,0,-1 * ACC));
  63. if (sdl_keyboard[SDL_SCANCODE_LEFT])
  64. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-1 * ACC,0,0));
  65. else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
  66. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(ACC,0,0));
  67. if (helper_debugDrawOn)
  68. helper_debugDraw(1);
  69. #undef ACC
  70. helper_frameEnd();
  71. }
  72. helper_end();
  73. return 0;
  74. }