main.cpp 7.26 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "Material.h"
#include "Object.h"
#include "PointLight.h"
#include "Sphere.h"

#include <glad/glad.h>

#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

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

#include <memory>
#include <vector>

int width = 800;
int height = 800;

const int SPHERE_TYPE = 0;

class ExGpuTraceSolution : public CVK::GlfwBase {
  private:
  public:
    ExGpuTraceSolution();
    ~ExGpuTraceSolution();

    void display();
};

int main() {

    ExGpuTraceSolution demo;

    demo.display();

    return 0;
}

// Implementation

ExGpuTraceSolution::ExGpuTraceSolution() {

    m_window =
        glfwCreateWindow(width, height, "GPU Raytracer", nullptr, nullptr);
    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");
    }
}

ExGpuTraceSolution::~ExGpuTraceSolution() {}

void ExGpuTraceSolution::display() {

    // Data for fullscreen quad
    std::vector<glm::vec2> quadData = {{-1.0, -1.0}, {1.0, -1.0}, {-1.0, 1.0},
                                       {-1.0, 1.0},  {1.0, -1.0}, {1.0, 1.0}};

    // compile a shader program
    std::vector<std::string> shadernames = {
        CVK::State::getInstance()->getShaderPath() +
            "/GPUTraceSolution/simple.vert",
        CVK::State::getInstance()->getShaderPath() +
            "/GPUTraceSolution/trace.frag"};
    std::unique_ptr<CVK::ShaderSet> shader = std::make_unique<CVK::ShaderSet>(
        VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT, shadernames);

    // VAO and VBO for fullscreen quad
    GLuint vao = 0;
    glGenVertexArrays(1, &vao);

    glBindVertexArray(vao);
    GLuint vbo = 0;
    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * quadData.size(),
                 quadData.data(), GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Set Shader and get uniforms
    shader->useProgram();

    GLint pLoc = glGetUniformLocation(shader->getProgramID(), "P");
    GLint vLoc = glGetUniformLocation(shader->getProgramID(), "V");

    // trackball camera
    auto proj = std::make_shared<CVK::Perspective>(
        glm::radians(45.f), float(width) / float(height), 0.01f, 100.f);
    CVK::Trackball cam(m_window, width, height, proj);
    cam.setRadius(20);

    // Some materials
    Material red = {
        glm::vec3(1.0, 0.0, 0.0), // diffuse
        0.9f,                     // kd
        glm::vec3(1.0),           // specular
        0.9f,                     // ks
        16.f,                     // specExponent
        0                         // mirror

    };

    Material green = {
        glm::vec3(0.0, 1.0, 0.0), // diffuse
        0.9f,                     // kd
        glm::vec3(1.0),           // specular
        0.9f,                     // ks
        16.f,                     // specExponent
        0                         // mirror

    };

    Material blue = {
        glm::vec3(0.0, 0.0, 1.0), // diffuse
        0.9f,                     // kd
        glm::vec3(1.0),           // specular
        0.9f,                     // ks
        16.f,                     // specExponent
        0                         // mirror

    };

    Material whiteMirror = {
        glm::vec3(0.0, 0.0, 0.0), // diffuse
        0.9f,                     // kd
        glm::vec3(1.0),           // specular
        0.9f,                     // ks
        16.f,                     // specExponent
        1                         // mirror

    };

    // Vector containing ssbo ids
    std::vector<GLuint> ssbos(4);
    glGenBuffers(4, ssbos.data());

    // vectors containing scene data
    std::vector<Object> objects;
    std::vector<PointLight> pointlights;
    std::vector<Material> mats;
    std::vector<Sphere> spheres;

    // Add some spheres
    spheres.push_back({
        glm::vec3(0, 0, 0), // pos
        1.0f                // radius
    });
    spheres.push_back({
        glm::vec3(0, -101, 0), // pos
        100.0f                 // radius
    });
    spheres.push_back({
        glm::vec3(0, 5, -10), // pos
        4.0f                  // radius
    });
    spheres.push_back({
        glm::vec3(10, 3, 0), // pos
        3.0f                 // radius
    });

    // Add materials
    mats.push_back(red);
    mats.push_back(green);
    mats.push_back(whiteMirror);

    // Add lights
    pointlights.emplace_back(glm::vec3(0, 4, 4), glm::vec3(0.9f));
    pointlights.emplace_back(glm::vec3(7, 9, -4), glm::vec3(0.1f, 0.7f, 0.5f));

    // Add objects
    objects.push_back({
        SPHERE_TYPE, // type
        0,           // matIndex
        0            // geoIndex
    });
    objects.push_back({
        SPHERE_TYPE, // type
        1,           // matIndex
        1            // geoIndex
    });
    objects.push_back({
        SPHERE_TYPE, // type
        2,           // matIndex
        2            // geoIndex
    });
    objects.push_back({
        SPHERE_TYPE, // type
        0,           // matIndex
        3            // geoIndex
    });

    // Bind and upload data for buffers
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbos[0]);
    glBufferData(GL_SHADER_STORAGE_BUFFER, objects.size() * sizeof(Object),
                 objects.data(), GL_STATIC_COPY);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbos[0]);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbos[1]);
    glBufferData(GL_SHADER_STORAGE_BUFFER, mats.size() * sizeof(Material),
                 mats.data(), GL_STATIC_COPY);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbos[1]);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbos[2]);
    glBufferData(GL_SHADER_STORAGE_BUFFER,
                 pointlights.size() * sizeof(PointLight), pointlights.data(),
                 GL_STATIC_COPY);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssbos[2]);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbos[3]);
    glBufferData(GL_SHADER_STORAGE_BUFFER, spheres.size() * sizeof(Sphere),
                 spheres.data(), GL_STATIC_COPY);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbos[3]);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 3);

    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;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        cam.update(deltaTime);

        glUniformMatrix4fv(
            pLoc, 1, GL_FALSE,
            glm::value_ptr(cam.getProjection()->getProjMatrix()));
        glUniformMatrix4fv(vLoc, 1, GL_FALSE, glm::value_ptr(cam.getView()));

        glDrawArrays(GL_TRIANGLES, 0, 6);

        glfwSwapBuffers(m_window);
        glfwPollEvents();
    }

    glDeleteBuffers(1, &vao);
    glDeleteVertexArrays(1, &vao);
}