main.cpp 1.92 KB
Newer Older
Bastian Krayer's avatar
Bastian Krayer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <glad/glad.h>

#include <CVK_2/CVK_CompatibilityTools.h>
#include <CVK_2/CVK_ShaderSet.h>
#include <CVK_2/CVK_Teapot.h>
#include <CVK_2/CVK_Trackball.h>

#include "VoxelVisualizer.h"
#include "Voxelizer.h"

constexpr int width = 800;
constexpr int height = 800;

constexpr glm::ivec3 VOXEL_RESOLUTION{64};

class ExVoxelization : public CVK::GlfwBase {
  public:
    ExVoxelization();
    void display();
};

int main() {

    ExVoxelization demo;

    demo.display();

    return 0;
}

ExVoxelization::ExVoxelization() {
    m_window = glfwCreateWindow(width, height, "GPU Voxelization", 0, 0);
    glfwSetWindowPos(m_window, 600, 50);
    glfwMakeContextCurrent(m_window);

    // init opengl
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize OpenGL context" << std::endl;
        throw std::runtime_error("Failed to initialize OpenGL context");
    }
}

void ExVoxelization::display() {

    // Trackball camera
    auto proj = std::make_shared<CVK::Perspective>(
        glm::radians(45.0f), float(width) / float(height), 0.5f, 10.f);
    CVK::Trackball cam(m_window, width, height, proj);
    cam.setRadius(4);

    CVK::Teapot teapot;

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_3D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    Voxelizer voxelizer(VOXEL_RESOLUTION, teapot);
    VoxelVisualizer voxelVisualizer(VOXEL_RESOLUTION, voxelizer.getBB());

    double lastTime = glfwGetTime();
    double deltaTime = 0, nowTime = 0;

    while (!glfwWindowShouldClose(m_window) &&
           glfwGetKey(m_window, GLFW_KEY_Q) != GLFW_PRESS) {
        nowTime = glfwGetTime();
        deltaTime = nowTime - lastTime;
        lastTime = nowTime;

        cam.update(deltaTime);

        voxelizer.voxelize(teapot);

        voxelVisualizer.visualize(voxelizer.getVoxelTexture(), cam);

        glfwSwapBuffers(m_window);
        glfwPollEvents();
    }
}