terrain working - no culling
[demo] / src / main.cc
index 50eea41..e5572e2 100644 (file)
+#include <GL/glew.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
 
+#include <gmath/gmath.h>
+
+#include "gfxapi.h"
 #include "global.h"
 
 /* TODO: fix those */
 #include "camera.h"
 #include "mesh.h"
 #include "object.h"
+#include "renderer.h"
 #include "scene.h"
-#include "shader_manager.h"
+#include "terrain.h"
+#include "texture.h"
 
 #include "opengl/opengl.h"
 #include "vulkan/vk.h"
 
 /* static functions */
 
-static bool init();
+static bool init(Gfx_API api);
 static void cleanup();
 static void display();
 
 /* glfw callbacks */
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
-static void motion_clbk(GLFWwindow *win, double x, double y);
-static void mouse_clbk(GLFWwindow *win, int button, int action, int mods);
+
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
+static void clbk_motion(GLFWwindow *win, double x, double y);
+static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
+static void clbk_reshape(GLFWwindow *win, int width, int height);
 
 /* global variables */
-bool use_vulkan;
+
+Mat4 mprojection;
 
 GLFWwindow *win;
 int win_w = 800;
 int win_h = 600;
 
-OrbitCamera *camera;
+float phi = 25;
+float theta = 0;
+float dist = 16;
+
 ShaderManager *sdr_man;
 
 /* variables */
-static float phi = 25;
-static float theta = 0;
-static float dist = 4;
 
-// TODO: remove just for test:
-static Scene scene;
+static float aspect;
+static OrbitCamera *camera;
+
+static Scene *cow_scene;
+static Renderer *cow_rend;
+
+static Scene *ground_scene;
+static Renderer *ground_rend; // default renderer
+static Texture *skybox_tex;
+
+static Renderer *terrain_rend;
+static Material terrain_mat;
+static Texture *terrain_tex;
+static Terrain terrain;
+
+/* *** */
 
 int main(int argc, char **argv)
 {
+       Gfx_API api;
+
        for(int i=0; i<argc; ++i) {
                if(strcmp(argv[i], "-opengl") == 0) {
-                       use_vulkan = false;
+                       api = GFX_GL;
                        printf("Backend: OpenGL.\n");
                }
                else if(strcmp(argv[i], "-vulkan") == 0) {
-                       use_vulkan = true;
+                       api = GFX_VK;
                        printf("Backend: Vulkan.\n");
                }
                else {
+                       api = GFX_GL;
                        printf("No backend specified. Using OpenGL.\n");
                }
        }
 
-       if(!init()) {
+       if(!init(api)) {
                fprintf(stderr, "Failed to initialize program.\n");
                return 1;
        }
 
-       glfwSetKeyCallback(win, key_clbk);
-       glfwSetCursorPosCallback(win, motion_clbk);
-       glfwSetMouseButtonCallback(win, mouse_clbk);
+       glfwSetKeyCallback(win, clbk_key);
+       glfwSetCursorPosCallback(win, clbk_motion);
+       glfwSetMouseButtonCallback(win, clbk_mouse);
+       glfwSetWindowSizeCallback(win, clbk_reshape);
+
+       glfwGetWindowSize(win, &win_w, &win_h);
+       clbk_reshape(win, win_w, win_h);
 
        while(!glfwWindowShouldClose(win)) {
                display();
@@ -76,45 +107,106 @@ int main(int argc, char **argv)
                glfwPollEvents();
        }
 
-       atexit(cleanup);
+       cleanup();
+       // atexit(cleanup);
        return 0;
 }
 
-static bool init()
+static bool init(Gfx_API api)
 {
-       /* TODO */
-       /*
-               TODO changes:
-               1- create cam
-               2- scene
-               3- renderers
-       */
-       if(use_vulkan) {
-               if(!init_vulkan())
-                       return false;
-       }
-       else {
-               if(!init_opengl())
-                       return false;
-       }
+       if(!gfx_init(api))
+               return false;
 
        sdr_man = new ShaderManager;
 
        camera = new OrbitCamera;
-       camera->set_orbit_params(phi, theta, dist);
 
-       if(!scene.load("data/spot/spot_control_mesh.obj")) {
-               fprintf(stderr, "Failed to load scene.\n");
+       // ground_scene = new Scene;
+       // if(!ground_scene->load("data/ground.obj")) {
+       //      fprintf(stderr, "Failed to load scene: ground.obj.\n");
+       //      return false;
+       // }
+
+       // ground_rend = new Renderer;
+       // ground_rend->camera = camera;
+       // ground_rend->scene = ground_scene;
+
+       // if(!ground_rend->create()) {
+       //      fprintf(stderr, "Failed to create default renderer.\n");
+       //      return false;
+       // }
+
+       // skybox_tex = gfx_create_texture();
+       // skybox_tex->load("data/cubemap/cubemap.hdr");
+       // ground_rend->set_sky_tex(skybox_tex);
+
+       cow_scene = new Scene;
+       if(!cow_scene->load("data/spot/spot.obj")) {
+               fprintf(stderr, "Failed to load scene: spot.obj.\n");
+               return false;
+       }
+
+       cow_rend = new Renderer;
+       cow_rend->camera = camera;
+       cow_rend->scene = cow_scene;
+
+       if(!cow_rend->create()) {
+               fprintf(stderr, "Failed to create renderer for cows.\n");
+               return false;
+       }
+
+       terrain_tex = gfx_create_texture();
+       if(!terrain_tex->load("data/grass.jpeg")) {
+               fprintf(stderr, "Failed to load ground texture.\n");
+               return false;
+       }
+
+       Image ter_hmap;
+       if(!ter_hmap.load("data/terhmap.png")) {
+               fprintf(stderr, "Failed to load terrain heightmap.\n");
                return false;
        }
 
-       for(size_t i=0; i<scene.objects.size(); ++i) {
-               printf("object: %d\n", (int)i);
-               printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
-               printf("material: %s\n", scene.objects[i]->material->name.c_str());
-               printf("transform:\n");
-               scene.objects[i]->transform.print();
+       TerrainParams p;
+       p.xsz = 50;
+       p.ysz = 50;
+       p.max_height = 5;
+       p.xtiles = 20;
+       p.ytiles = 20;
+       p.tile_usub = 10;
+       p.tile_vsub = 10;
+       p.num_octaves = 3;
+       p.noise_freq = 10;
+       p.coarse_heightmap = &ter_hmap;
+
+       terrain.init();
+       terrain.generate(p);
+
+       terrain_mat.diffuse = Vec3(1, 1, 1);
+       terrain_mat.specular = Vec3(0, 0, 0);
+       terrain_mat.shininess = 40;
+       terrain_mat.dtex = terrain_tex;
+       terrain_mat.name = "tt";
+
+       terrain.material = terrain_mat;
+
+       terrain_rend = new Renderer;
+       terrain_rend->camera = camera;
+       terrain_rend->scene = terrain.get_visible(camera);
+       if(!terrain_rend->create()) {
+               fprintf(stderr, "terrain fail\n");
+               return false;
        }
+
+
+// TODO delete: debugging
+       // for(size_t i=0; i<ground_scene->objects.size(); ++i) {
+       //      printf("object: %d\n", (int)i);
+       //      printf("mesh: %s\n", ground_scene->objects[i]->mesh->name.c_str());
+       //      printf("material: %s\n", ground_scene->objects[i]->material->name.c_str());
+       //      printf("transform:\n");
+       //      ground_scene->objects[i]->transform.print();
+       // }
        return true;
 }
 
@@ -123,15 +215,19 @@ static void cleanup()
        delete sdr_man;
        delete camera;
 
-       if(use_vulkan) {
-               cleanup_vulkan();
-       }
-       else {
-               cleanup_opengl();
-       }
+       delete cow_scene;
+       delete cow_rend;
+
+       // delete ground_scene;
+       // delete ground_rend;
+
+//TODO
+       delete terrain_tex;
+       delete terrain_rend;
+       gfx_cleanup();
 }
 
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
 {
        if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
                glfwSetWindowShouldClose(win, GLFW_TRUE);
@@ -139,47 +235,58 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod
 }
 
 static double prev_x, prev_y;
-static int button;
+static bool button[8];
 
-static void motion_clbk(GLFWwindow *win, double x, double y)
+static void clbk_motion(GLFWwindow *win, double x, double y)
 {
-       switch(button) {
-       case GLFW_MOUSE_BUTTON_LEFT:
-               theta += x - prev_x;
-               phi += y - prev_y;
+       double dx = x - prev_x;
+       double dy = y - prev_y;
+
+       prev_x = x;
+       prev_y = y;
+
+       if(button[0]) {
+               theta += dx * 0.5;
+               phi += dy * 0.5;
 
-               if(phi < -90)
-                       phi = -90;
+               if(phi < 0)
+                       phi = 0;
                if(phi > 90)
                        phi = 90;
+       }
 
-               break;
-
-       case GLFW_MOUSE_BUTTON_RIGHT:
-               dist *= (y - prev_y) * 0.01 + 1;
+       if(button[1]) {
+               dist += dy * 0.1;
                if(dist < 0.0) {
                        dist = 0.0;
                }
-               break;
        }
-
-       prev_x = x;
-       prev_y = y;
 }
 
-static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
+static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
 {
-       button = bn;
+       button[bn] = action == GLFW_PRESS;
        glfwGetCursorPos(win, &prev_x, &prev_y);
 }
 
+static void clbk_reshape(GLFWwindow *win, int width, int height)
+{
+       gfx_viewport(0, 0, width, height);
+       aspect = (float)width / (float)height;
+       mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
+
+       win_h = height;
+       win_w = width;
+}
+
 static void display()
 {
-       if(use_vulkan) {
-               display_vulkan();
-       }
-       else {
-               display_opengl();
-               scene.objects[0]->mesh->draw();
-       }
+       camera->set_orbit_params(theta, phi, dist);
+
+       // gfx_clear(0.76, 0.3, 0.43);
+       gfx_clear(0.1, 0.1, 0.1);
+
+       terrain_rend->draw();
+//     ground_rend->draw();
+       cow_rend->draw();
 }
\ No newline at end of file