Merge branch 'master' of github.com:Dynamitos/RayTracer
This commit is contained in:
@@ -11,6 +11,19 @@
|
|||||||
"buildCommandArgs": "",
|
"buildCommandArgs": "",
|
||||||
"ctestCommandArgs": "",
|
"ctestCommandArgs": "",
|
||||||
"cmakeExecutable": "C:/Program Files/CMake/bin/cmake.exe"
|
"cmakeExecutable": "C:/Program Files/CMake/bin/cmake.exe"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Release",
|
||||||
|
"generator": "Ninja",
|
||||||
|
"configurationType": "RelWithDebInfo",
|
||||||
|
"buildRoot": "${projectDir}\\bin\\${name}",
|
||||||
|
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||||
|
"cmakeExecutable": "C:/Program Files/CMake/bin/cmake.exe",
|
||||||
|
"cmakeCommandArgs": "",
|
||||||
|
"buildCommandArgs": "",
|
||||||
|
"ctestCommandArgs": "",
|
||||||
|
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||||
|
"variables": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
+1
-1
@@ -45,7 +45,7 @@ void ThreadPool::work()
|
|||||||
std::function<void()> job;
|
std::function<void()> job;
|
||||||
{
|
{
|
||||||
std::unique_lock l(queueLock);
|
std::unique_lock l(queueLock);
|
||||||
if (taskQueue.front().jobs.empty())
|
if (taskQueue.empty() || taskQueue.front().jobs.empty())
|
||||||
{
|
{
|
||||||
queueCV.wait(l);
|
queueCV.wait(l);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
+23
-11
@@ -1,40 +1,52 @@
|
|||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
Scene::Scene() {}
|
Scene::Scene() {}
|
||||||
|
|
||||||
Scene::~Scene() {}
|
Scene::~Scene() {}
|
||||||
|
|
||||||
|
static bool firstTime = true;
|
||||||
void Scene::render(Camera cam, RenderParameter params)
|
void Scene::render(Camera cam, RenderParameter params)
|
||||||
|
{
|
||||||
|
if (!firstTime)
|
||||||
{
|
{
|
||||||
pendingCancel = true;
|
pendingCancel = true;
|
||||||
worker.join();
|
worker.join();
|
||||||
|
firstTime = false;
|
||||||
|
}
|
||||||
pendingCancel = false;
|
pendingCancel = false;
|
||||||
|
image.clear();
|
||||||
|
accumulator.clear();
|
||||||
|
image.resize(params.width * params.height);
|
||||||
|
accumulator.resize(params.width * params.height);
|
||||||
worker = std::thread(
|
worker = std::thread(
|
||||||
[&]()
|
[&]()
|
||||||
{
|
{
|
||||||
image.clear();
|
|
||||||
accumulator.clear();
|
|
||||||
image.resize(params.width * params.height * 3);
|
|
||||||
accumulator.resize(params.width * params.height * 3);
|
|
||||||
for (int samp = 0; samp < params.numSamples; ++samp)
|
for (int samp = 0; samp < params.numSamples; ++samp)
|
||||||
{
|
{
|
||||||
if (pendingCancel)
|
if (pendingCancel)
|
||||||
return;
|
return;
|
||||||
Batch batch;
|
Batch batch;
|
||||||
for (int w = 0; w < params.width; ++w)
|
for (int w = 0; w < params.width; ++w)
|
||||||
|
{
|
||||||
|
batch.jobs.push_back(
|
||||||
|
[&, w]()
|
||||||
{
|
{
|
||||||
for (int h = 0; h < params.height; ++h)
|
for (int h = 0; h < params.height; ++h)
|
||||||
{
|
{
|
||||||
batch.jobs.push_back(
|
// Ray r = Ray();
|
||||||
[&]()
|
// bvh.traceRay(r);
|
||||||
{
|
accumulator[w + h * params.width] +=
|
||||||
Ray r = Ray();
|
glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0);
|
||||||
bvh.traceRay(r);
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
threadPool.runBatch(std::move(batch));
|
threadPool.runBatch(std::move(batch));
|
||||||
std::memcpy(image.data(), accumulator.data(), accumulator.size());
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
|
||||||
|
std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -17,14 +17,14 @@ class Scene
|
|||||||
Scene();
|
Scene();
|
||||||
~Scene();
|
~Scene();
|
||||||
void render(Camera cam, RenderParameter params);
|
void render(Camera cam, RenderParameter params);
|
||||||
constexpr const std::vector<unsigned char>& getImage() const { return image; }
|
constexpr const std::vector<glm::vec3>& getImage() const { return image; }
|
||||||
private:
|
private:
|
||||||
std::atomic_bool pendingCancel = false;
|
std::atomic_bool pendingCancel = false;
|
||||||
ThreadPool threadPool;
|
ThreadPool threadPool;
|
||||||
std::thread worker;
|
std::thread worker;
|
||||||
// the thing being displayed
|
// the thing being displayed
|
||||||
std::vector<unsigned char> image;
|
std::vector<glm::vec3> image;
|
||||||
// radiance accumulator
|
// radiance accumulator
|
||||||
std::vector<unsigned char> accumulator;
|
std::vector<glm::vec3> accumulator;
|
||||||
BVH bvh;
|
BVH bvh;
|
||||||
};
|
};
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
#include <assert.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define GLSL(...) "#version 400\n" #__VA_ARGS__
|
#define GLSL(...) "#version 400\n" #__VA_ARGS__
|
||||||
@@ -7,18 +6,18 @@
|
|||||||
Window::Window(int width, int height) : width(width), height(height)
|
Window::Window(int width, int height) : width(width), height(height)
|
||||||
{
|
{
|
||||||
glewExperimental = true;
|
glewExperimental = true;
|
||||||
assert(glfwInit());
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
|
||||||
window = glfwCreateWindow(width, height, "RayTracer", nullptr, nullptr);
|
window = glfwCreateWindow(width, height, "RayTracer", nullptr, nullptr);
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
assert(!glewInit());
|
glewInit();
|
||||||
glGenVertexArrays(1, &vao);
|
glGenVertexArrays(1, &vao);
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glGenTextures(1, &texture);
|
glGenTextures(1, &texture);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
glBindTexture(GL_TEXTURE_2D, texture);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_FLOAT, nullptr);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
program = glCreateProgram();
|
program = glCreateProgram();
|
||||||
vertShader = glCreateShader(GL_VERTEX_SHADER);
|
vertShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
@@ -76,11 +75,11 @@ Window::Window(int width, int height) : width(width), height(height)
|
|||||||
|
|
||||||
Window::~Window() {}
|
Window::~Window() {}
|
||||||
|
|
||||||
void Window::update(const std::vector<unsigned char>& textureData)
|
void Window::update(const std::vector<glm::vec3>& 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_FLOAT, textureData.data());
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|||||||
+2
-1
@@ -2,13 +2,14 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <glfw/glfw3.h>
|
#include <glfw/glfw3.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Window
|
class Window
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Window(int width, int height);
|
Window(int width, int height);
|
||||||
~Window();
|
~Window();
|
||||||
void update(const std::vector<unsigned char>& textureData);
|
void update(const std::vector<glm::vec3>& textureData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int width;
|
int width;
|
||||||
|
|||||||
Reference in New Issue
Block a user