-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (86 loc) · 3.32 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
101
102
103
104
105
106
107
108
109
110
#include <memory>
#include "imgui.h"
#include "glm/gtc/type_ptr.hpp"
#include "jug/application.h"
#include "jug/timer.h"
#include "jug/input.h"
#include "objects.h"
#include "textures.h"
#include "materials.h"
#include "scene.h"
#include "camera.h"
#include "renderer.h"
// #include "bvhNode.h"
using namespace Jug;
class RayTracingLayer : public Layer
{
public:
RayTracingLayer()
: camera(45.0f, 0.1f, 100.0f), scene("Main Scene")
{
auto earthImage = std::make_shared<ImageTexture>("textures/earthmap.jpg");
auto earth = std::make_shared<Lambertian>(earthImage);
// auto per1 = std::make_shared<NoiseTexture>(glm::vec3(1, 1, 1), 1.0f);
// auto noiseMat = std::make_shared<Lambertian>(per1);
auto metal = std::make_shared<Metal>(glm::vec3(1.0f, 1.0f, 1.0f), 0.5);
auto emissive = std::make_shared<DiffuseLight>(std::make_shared<NoiseTexture>(glm::vec3(1, 1, 1), 1.0f));
auto mirror = std::make_shared<Dieletric>(glm::vec3(1.0f, 1.0f, 1.0f), 2.0f);
scene.add(std::make_shared<Plane>("P1", glm::vec3(0.0f, -0.6f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f), mirror));
scene.add(std::make_shared<Triangle>("T1", glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), emissive));
scene.add(std::make_shared<Triangle>("T2", glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-1.0f, 1.0f, 0.0f), emissive));
scene.add(std::make_shared<Sphere>("S1", glm::vec3(0.0f, 0.5f, 0.8f), 0.5f, metal));
}
virtual void OnUpdate(float ts) override
{
if (camera.onUpdate(ts))
renderer.resetFrameIndex();
}
virtual void OnUIRender() override
{
ImGui::Begin("Status");
ImGui::Text("Last Render: %.3fms", lastRenderTime);
ImGui::Text("Frame Rate: %.3fFPS", frameRate);
ImGui::End();
renderer.renderUI();
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::Begin("Viewport", nullptr);
viewportWidth = ImGui::GetContentRegionAvail().x;
viewportHeight = ImGui::GetContentRegionAvail().y;
auto image = renderer.getFinalImage();
if (image)
ImGui::Image((void *)(intptr_t)image->getDescriptor(), {(float)image->getWidth(), (float)image->getHeight()}, ImVec2(0, 1), ImVec2(1, 0));
ImGui::End();
ImGui::PopStyleVar(3);
if (scene.renderUI())
renderer.resetFrameIndex();
render();
}
void render()
{
Timer timer;
camera.onResize(viewportWidth, viewportHeight);
renderer.onResize(viewportWidth, viewportHeight);
renderer.render(scene, camera);
lastRenderTime = timer.getTimeElapsedMillis();
frameRate = 100.0f / lastRenderTime;
}
private:
Renderer renderer;
uint32_t viewportWidth = 0,
viewportHeight = 0;
float lastRenderTime = 0;
float frameRate = 0;
Camera camera;
Scene scene;
};
int main()
{
Application *app = Application::createInstance("Rayz", 1000, 700);
std::shared_ptr<Layer> layer = std::make_shared<RayTracingLayer>();
app->addLayer(layer);
app->run();
delete app;
return 0;
}