More boilerplate
This commit is contained in:
+4
-1
@@ -1,6 +1,9 @@
|
|||||||
target_sources(RayTracer
|
target_sources(RayTracer
|
||||||
PRIVATE
|
PRIVATE
|
||||||
main.cpp)
|
main.cpp
|
||||||
|
Minimal.h
|
||||||
|
ThreadPool.h
|
||||||
|
ThreadPool.cpp)
|
||||||
|
|
||||||
add_subdirectory(scene/)
|
add_subdirectory(scene/)
|
||||||
add_subdirectory(window/)
|
add_subdirectory(window/)
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#include "ThreadPool.h"
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
ThreadPool::ThreadPool(uint32_t numThreads)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < numThreads; ++i)
|
||||||
|
{
|
||||||
|
workers.push_back(std::thread(&ThreadPool::work, this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadPool::~ThreadPool()
|
||||||
|
{
|
||||||
|
running.store(false);
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
queueCV.notify_all();
|
||||||
|
}
|
||||||
|
for(auto& worker : workers)
|
||||||
|
{
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::addJob(std::function<void()>&& job)
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
taskQueue.push_back(job);
|
||||||
|
queueCV.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::waitIdle()
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
if(taskQueue.empty())
|
||||||
|
return;
|
||||||
|
completedCV.wait(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::work()
|
||||||
|
{
|
||||||
|
while(running)
|
||||||
|
{
|
||||||
|
std::function<void()> job;
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
if(taskQueue.empty())
|
||||||
|
{
|
||||||
|
queueCV.wait(l);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
job = taskQueue.front();
|
||||||
|
taskQueue.pop_front();
|
||||||
|
}
|
||||||
|
job();
|
||||||
|
{
|
||||||
|
std::unique_lock l(queueLock);
|
||||||
|
completedCV.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <functional>
|
||||||
|
#include <list>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
class ThreadPool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ThreadPool(uint32_t numWorkers = std::thread::hardware_concurrency());
|
||||||
|
~ThreadPool();
|
||||||
|
void addJob(std::function<void()>&& job);
|
||||||
|
void waitIdle();
|
||||||
|
private:
|
||||||
|
std::atomic_bool running = true;
|
||||||
|
void work();
|
||||||
|
std::mutex queueLock;
|
||||||
|
std::condition_variable queueCV;
|
||||||
|
std::condition_variable completedCV;
|
||||||
|
std::list<std::function<void()>> taskQueue;
|
||||||
|
std::vector<std::thread> workers;
|
||||||
|
};
|
||||||
+1
-1
@@ -9,7 +9,7 @@ int main()
|
|||||||
bvh.generate();
|
bvh.generate();
|
||||||
Window w(1920, 1080);
|
Window w(1920, 1080);
|
||||||
std::vector<unsigned char> texture(1920 * 1080 * 3);
|
std::vector<unsigned char> texture(1920 * 1080 * 3);
|
||||||
for (int j = 10; j < 100; ++j)
|
for (int j = 100; j < 200; ++j)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 1920; ++i)
|
for (int i = 0; i < 1920; ++i)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include "util/Ray.h"
|
||||||
|
|
||||||
struct AABB
|
struct AABB
|
||||||
{
|
{
|
||||||
@@ -42,6 +43,11 @@ struct AABB
|
|||||||
max = glm::vec3(std::max(max.x, transformed.x), std::max(max.y, transformed.y), std::max(max.z, transformed.z));
|
max = glm::vec3(std::max(max.x, transformed.x), std::max(max.y, transformed.y), std::max(max.z, transformed.z));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bool intersects(Ray ray)
|
||||||
|
{
|
||||||
|
assert(false && "TODO");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
static AABB combine(AABB lhs, AABB rhs)
|
static AABB combine(AABB lhs, AABB rhs)
|
||||||
{
|
{
|
||||||
AABB result = {
|
AABB result = {
|
||||||
|
|||||||
+34
-1
@@ -1,5 +1,6 @@
|
|||||||
#include "BVH.h"
|
#include "BVH.h"
|
||||||
#include <list>
|
#include <algorithm>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
void BVH::addModel(PModel model, glm::mat4 transform)
|
void BVH::addModel(PModel model, glm::mat4 transform)
|
||||||
{
|
{
|
||||||
@@ -54,3 +55,35 @@ void BVH::generate()
|
|||||||
}
|
}
|
||||||
hierarchy = std::move(pendingNodes[0]);
|
hierarchy = std::move(pendingNodes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<IntersectionInfo> BVH::traceRay(Ray ray)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<IntersectionInfo> BVH::generateIntersections(PNode& currentNode, Ray ray)
|
||||||
|
{
|
||||||
|
if(!currentNode->aabb.intersects(ray))
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if(currentNode->model != nullptr)
|
||||||
|
{
|
||||||
|
auto result = currentNode->model->intersect(ray);
|
||||||
|
if(result.has_value())
|
||||||
|
{
|
||||||
|
return {*result};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto leftResults = generateIntersections(currentNode->left, ray);
|
||||||
|
auto rightResults = generateIntersections(currentNode->right, ray);
|
||||||
|
|
||||||
|
for(auto it : rightResults)
|
||||||
|
{
|
||||||
|
leftResults.push_back(it);
|
||||||
|
}
|
||||||
|
return leftResults;
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "util/Model.h"
|
#include "util/Model.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include "util/Ray.h"
|
||||||
|
|
||||||
|
|
||||||
class BVH
|
class BVH
|
||||||
{
|
{
|
||||||
@@ -11,6 +13,8 @@ class BVH
|
|||||||
void addModels(std::vector<PModel> models, glm::mat4 transform);
|
void addModels(std::vector<PModel> models, glm::mat4 transform);
|
||||||
void generate();
|
void generate();
|
||||||
|
|
||||||
|
std::optional<IntersectionInfo> traceRay(Ray ray);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DECLARE_REF(Node)
|
DECLARE_REF(Node)
|
||||||
struct Node
|
struct Node
|
||||||
@@ -24,4 +28,6 @@ class BVH
|
|||||||
};
|
};
|
||||||
PNode hierarchy;
|
PNode hierarchy;
|
||||||
std::vector<PModel> models;
|
std::vector<PModel> models;
|
||||||
|
|
||||||
|
std::vector<IntersectionInfo> generateIntersections(PNode& currentNode, Ray ray);
|
||||||
};
|
};
|
||||||
@@ -1,3 +1,9 @@
|
|||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
|
|
||||||
|
Scene::Scene()
|
||||||
|
: window(1920, 1080)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void Scene::render() {}
|
void Scene::render() {}
|
||||||
|
|||||||
+3
-1
@@ -5,9 +5,11 @@
|
|||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Scene();
|
||||||
|
~Scene();
|
||||||
void render();
|
void render();
|
||||||
private:
|
private:
|
||||||
Window window;
|
Window window;
|
||||||
std::vector<uint32_t> image;
|
std::vector<unsigned char> image;
|
||||||
BVH bvh;
|
BVH bvh;
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "Model.h"
|
||||||
|
|
||||||
|
std::optional<IntersectionInfo> Model::intersect(Ray ray)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
@@ -3,11 +3,24 @@
|
|||||||
#include "scene/AABB.h"
|
#include "scene/AABB.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// material infos
|
||||||
|
// shading parameter
|
||||||
|
// hit data
|
||||||
|
struct IntersectionInfo
|
||||||
|
{
|
||||||
|
float t;
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec3 normal;
|
||||||
|
glm::vec3 albedo;
|
||||||
|
glm::vec3 emissive;
|
||||||
|
//....
|
||||||
|
};
|
||||||
class Model
|
class Model
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AABB boundingBox;
|
AABB boundingBox;
|
||||||
std::vector<glm::vec3> positions;
|
std::vector<glm::vec3> positions;
|
||||||
std::vector<uint32_t> indices;
|
std::vector<uint32_t> indices;
|
||||||
|
std::optional<IntersectionInfo> intersect(Ray ray);
|
||||||
};
|
};
|
||||||
DECLARE_REF(Model)
|
DECLARE_REF(Model)
|
||||||
+3
-13
@@ -18,18 +18,8 @@ Window::Window(int width, int height) : width(width), height(height)
|
|||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glGenTextures(1, &texture);
|
glGenTextures(1, &texture);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
std::vector<unsigned char> texture(1920 * 1080 * 4);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
||||||
for (int j = 0; j < 1080; ++j)
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
{
|
|
||||||
for (int i = 0; i < 1920; ++i)
|
|
||||||
{
|
|
||||||
texture[(j * 1920 + i) * 3] = 0xff;
|
|
||||||
texture[(j * 1920 + i) * 3 + 1] = 0xff;
|
|
||||||
texture[(j * 1920 + i) * 3 + 2] = 0x0;
|
|
||||||
texture[(j * 1920 + i) * 3 + 2] = 0xff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.data());
|
|
||||||
program = glCreateProgram();
|
program = glCreateProgram();
|
||||||
vertShader = glCreateShader(GL_VERTEX_SHADER);
|
vertShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
|
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
@@ -90,7 +80,7 @@ void Window::update(const std::vector<unsigned char>& textureData)
|
|||||||
{
|
{
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, textureData.data());
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, textureData.data());
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|||||||
Reference in New Issue
Block a user