terrain

Real-time terrain generation using marching cubes
git clone git://git.christianermann.dev/terrain
Log | Files | Refs | README | LICENSE

main.c (3855B)


      1 #include "engine/app.h"
      2 #include "engine/camera.h"
      3 #include "engine/chunk_manager.h"
      4 #include "engine/input.h"
      5 #include "engine/perlin.h"
      6 #include "engine/save.h"
      7 #include "engine/shader.h"
      8 #include "engine/transform.h"
      9 #include "engine/player.h"
     10 #include "engine/logger.h"
     11 #include "engine/types.h"
     12 
     13 #include "glad/glad.h"
     14 #include "GLFW/glfw3.h"
     15 
     16 #include <math.h>
     17 
     18 int main(int argc, char** argv)
     19 {
     20     LOGI("Application starting...");
     21 
     22     AppInfo app_info = {
     23         .title = "Marching Cubes Terrain",
     24         .width = 1024,
     25         .height = 768,
     26         .gl_major_version = 3,
     27         .gl_minor_version = 3
     28     };
     29     App *app = App_make(&app_info);
     30     if (!app) return 1;
     31 
     32     glEnable(GL_DEPTH_TEST);
     33     glEnable(GL_CULL_FACE);
     34 
     35     UserInput input = { 0 };
     36     UserInput_init(&input, app->window);
     37 
     38     Transform player_transform;
     39     Transform_init(
     40             &player_transform,
     41             0,                 // x
     42             0,                 // y
     43             -3,                // z
     44             0,                 // yaw
     45             0                  // pitch
     46     );
     47     load(&player_transform);
     48 
     49     Player player = {
     50         .speed = 0.1f,
     51         .sensitivity = 0.3f,
     52         .transform = &player_transform
     53     };
     54 
     55     Camera *camera = Camera_make(&player_transform);
     56 
     57     ChunkManager chunk_manager = ChunkManager_create(
     58             player_transform.position,
     59             5,
     60             terrainSDF,
     61             0.0f
     62     );
     63     ChunkManager_drawChunks(&chunk_manager, camera);
     64 
     65     Shader *shader = Shader_make("shaders/basic.vs", "shaders/basic.fs");
     66     Shader_use(shader);
     67     Shader_setInt(shader, "shade_normals", 0);
     68     
     69     i32 loop_count = 0;
     70 
     71     while (!glfwWindowShouldClose(app->window))
     72     {
     73         if (glfwGetKey(app->window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
     74         {
     75             glfwSetInputMode(app->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
     76         }
     77         if (glfwGetKey(app->window, GLFW_KEY_ENTER) == GLFW_PRESS)
     78         {
     79             glfwSetInputMode(app->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
     80         }
     81         if (glfwGetKey(app->window, GLFW_KEY_R) == GLFW_PRESS)
     82         {
     83             Shader_reload(shader);
     84             Shader_use(shader);
     85         }
     86         if (glfwGetKey(app->window, GLFW_KEY_Z) == GLFW_PRESS)
     87         {
     88             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
     89         }
     90         if (glfwGetKey(app->window, GLFW_KEY_X) == GLFW_PRESS)
     91         {
     92             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
     93         }
     94         if (glfwGetKey(app->window, GLFW_KEY_N) == GLFW_PRESS)
     95         {
     96             Shader_setInt(shader, "shade_normals", 1);
     97         }
     98         if (glfwGetKey(app->window, GLFW_KEY_M) == GLFW_PRESS)
     99         {
    100             Shader_setInt(shader, "shade_normals", 0);
    101         }
    102         if (glfwGetKey(app->window, GLFW_KEY_Q) == GLFW_PRESS)
    103         {
    104             glfwSetWindowShouldClose(app->window, GLFW_TRUE);
    105         }
    106 
    107         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    108 
    109         UserInput_update(&input, app->window);
    110 
    111         Player_move(&player, &input);
    112         Camera_updateMatrix(camera);
    113         Camera_updateFrustum(camera);
    114 
    115         Shader_setInt(shader, "time", loop_count);
    116         Shader_setMat4(shader, "camera", camera->matrix);
    117         Shader_setVec3(shader, "view_pos", player_transform.position);
    118         Shader_setVec3(shader, "pointlight_pos", player_transform.position);
    119 
    120         ChunkManager_recenter(&chunk_manager, player_transform.position);
    121         ChunkManager_drawChunks(&chunk_manager, camera);
    122 
    123         loop_count += 1;
    124         if (loop_count % 100000 == 0)
    125         {
    126             save(&player_transform);
    127         }
    128         
    129         glfwSwapBuffers(app->window);
    130         glfwPollEvents();
    131     }
    132 
    133     save(&player_transform);
    134 
    135     ChunkManager_free(&chunk_manager);
    136     
    137     App_free(app);
    138 
    139     LOGI("Application exiting...");
    140 }
    141