-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
100 lines (70 loc) · 2.49 KB
/
main.cpp
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
#pragma once
#include <iostream>
#include <glad/glad.h>
#include "src/camera.h"
#include "src/window.h"
#include "src/shader.h"
#include "src/data.h"
#include "src/ui.h"
#include "src/bvh.h"
int main()
{
std::cout << "loading config\n";
nerv::config conf = nerv::loadConfig("E:\\nerv_engine\\nerv_pathtracer\\config.ini");
GLFWwindow* win = nerv::createWindow(&conf);
nerv::camera cam(&conf);
nerv::createUI(win);
nerv::renderData render = {
0,
5,
false,
0.0
};
nerv::shader mainShader("shader/raytraced.frag.glsl", "shader/static.vert.glsl");
int width;
int height;
glfwGetFramebufferSize(win, &width, &height);
nerv::material* arrMat = nerv::genRandomMaterial(51);
std::vector<nerv::triangle> triangles = nerv::loadObj("E:\\nerv_engine\\nerv_pathtracer\\model\\bunny-heavy.obj");
nerv::bvhNode* bvh = nerv::createNode(triangles, 0, triangles.size());
int size = nerv::countNode(bvh);
nerv::linearBvhNode * flatten = new nerv::linearBvhNode[size];
int offset = 0;
nerv::flattenBVH(flatten, bvh, &offset, 0);
size_t triangleBuffer = nerv::createBuffer(sizeof(nerv::triangle) * triangles.size(), triangles.data(), 0, GL_SHADER_STORAGE_BUFFER);
size_t colorBuffer = nerv::createBuffer(sizeof(float) * 4 * width * height, nullptr, 1, GL_SHADER_STORAGE_BUFFER);
size_t bvhBuffer = nerv::createBuffer(sizeof(nerv::linearBvhNode) * size, flatten, 2, GL_SHADER_STORAGE_BUFFER);
size_t materialBuffer = nerv::createBuffer(sizeof(nerv::material) * 51, arrMat, 3, GL_SHADER_STORAGE_BUFFER);
nerv::texture background = nerv::loadImage("resources/evening_road_01.jpg");
nerv::texture checkboard = nerv::loadImage("resources/checker-map.png");
while (!glfwWindowShouldClose(win))
{
//pool event the render
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//update camera if not in menu
if(!nerv::gUsingMenu)
render.isMoving = cam.updateCamera(&cam, win);
//send info to shader
nerv::sendInfo(&cam, &render, win);
mainShader.activateImage(&background, "background", 0);
mainShader.activateImage(&checkboard, "checkboard", 1);
//render
mainShader.use();
nerv::displayUI(win, &render, &cam);
//reload shader if needed
if (render.reload)
mainShader.reload();
//update camera time
cam.updateTime(&cam);
glfwSwapBuffers(win);
}
//nerv::closeUI();
glfwDestroyWindow(win);
glfwTerminate();
delete arrMat;
//delete arrSphere;
delete flatten;
return EXIT_SUCCESS;
}