mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-28 02:58:35 +02:00
+ | Light array sent to gpu
This commit is contained in:
@ -38,6 +38,7 @@
|
|||||||
# include <vector>
|
# include <vector>
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <memory>
|
# include <memory>
|
||||||
|
# include <set>
|
||||||
# include <map>
|
# include <map>
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
|
@ -68,6 +68,9 @@ class Scene
|
|||||||
void addObject(Object *object);
|
void addObject(Object *object);
|
||||||
void addMaterial(Material *material);
|
void addMaterial(Material *material);
|
||||||
|
|
||||||
|
void updateLightAndObjects(int mat_id);
|
||||||
|
std::set<int> getGPULights();
|
||||||
|
|
||||||
const std::vector<GPUObject> &getObjectData() const;
|
const std::vector<GPUObject> &getObjectData() const;
|
||||||
std::vector<GPUMaterial> &getMaterialData();
|
std::vector<GPUMaterial> &getMaterialData();
|
||||||
GPUVolume &getVolume();
|
GPUVolume &getVolume();
|
||||||
@ -79,6 +82,8 @@ class Scene
|
|||||||
std::vector<GPUObject> _gpu_objects;
|
std::vector<GPUObject> _gpu_objects;
|
||||||
std::vector<GPUMaterial> _gpu_materials;
|
std::vector<GPUMaterial> _gpu_materials;
|
||||||
|
|
||||||
|
std::set<int> _gpu_lights;
|
||||||
|
|
||||||
GPUVolume _gpu_volume;
|
GPUVolume _gpu_volume;
|
||||||
|
|
||||||
Camera *_camera;
|
Camera *_camera;
|
||||||
|
@ -27,4 +27,4 @@ sp 0 1 5 1 7
|
|||||||
|
|
||||||
cy 0 1 2 0.5 2 -1.5 0 0.75 1
|
cy 0 1 2 0.5 2 -1.5 0 0.75 1
|
||||||
|
|
||||||
OBJ obj/Lowpoly_tree_sample.obj
|
# OBJ obj/Lowpoly_tree_sample.obj
|
||||||
|
@ -61,6 +61,12 @@ layout(std430, binding = 2) buffer MaterialBuffer
|
|||||||
GPUMaterial materials[];
|
GPUMaterial materials[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
layout(std430, binding = 3) buffer LightsBuffer
|
||||||
|
{
|
||||||
|
int lightsIndex[];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
layout(std140, binding = 0) uniform CameraData
|
layout(std140, binding = 0) uniform CameraData
|
||||||
{
|
{
|
||||||
GPUCamera camera;
|
GPUCamera camera;
|
||||||
@ -71,7 +77,9 @@ layout(std140, binding = 1) uniform VolumeData
|
|||||||
GPUVolume volume;
|
GPUVolume volume;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
uniform int u_objectsNum;
|
uniform int u_objectsNum;
|
||||||
|
uniform int u_lightsNum;
|
||||||
uniform vec2 u_resolution;
|
uniform vec2 u_resolution;
|
||||||
uniform int u_pixelisation;
|
uniform int u_pixelisation;
|
||||||
uniform int u_frameCount;
|
uniform int u_frameCount;
|
||||||
|
@ -71,9 +71,10 @@ vec3 sampleLights(vec3 position, inout uint rng_state)
|
|||||||
{
|
{
|
||||||
vec3 light = vec3(0.0);
|
vec3 light = vec3(0.0);
|
||||||
|
|
||||||
for (int i = 0; i < u_objectsNum; i++)
|
for (int i = 0; i < u_lightsNum; i++)
|
||||||
{
|
{
|
||||||
GPUObject obj = objects[i];
|
int light_index = lightsIndex[i];
|
||||||
|
GPUObject obj = objects[light_index];
|
||||||
GPUMaterial mat = materials[obj.mat_index];
|
GPUMaterial mat = materials[obj.mat_index];
|
||||||
if (mat.emission > 0.0)
|
if (mat.emission > 0.0)
|
||||||
{
|
{
|
||||||
@ -83,7 +84,7 @@ vec3 sampleLights(vec3 position, inout uint rng_state)
|
|||||||
Ray shadow_ray = Ray(position + light_dir * 0.01, light_dir);
|
Ray shadow_ray = Ray(position + light_dir * 0.01, light_dir);
|
||||||
hitInfo shadow_hit = traceRay(shadow_ray);
|
hitInfo shadow_hit = traceRay(shadow_ray);
|
||||||
|
|
||||||
if (shadow_hit.obj_index == i)
|
if (shadow_hit.obj_index == light_index)
|
||||||
light += mat.emission * mat.color / (light_dist);
|
light += mat.emission * mat.color / (light_dist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,6 +117,6 @@ vec3 sampleLights(vec3 position, inout uint rng_state)
|
|||||||
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
|
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
|
||||||
{
|
{
|
||||||
color *= mat.color;
|
color *= mat.color;
|
||||||
light += mat.emission * mat.color;
|
// light += mat.emission * mat.color;
|
||||||
// light += sampleLights(hit.position, rng_state);
|
light += sampleLights(hit.position, rng_state);
|
||||||
}
|
}
|
16
srcs/RT.cpp
16
srcs/RT.cpp
@ -44,6 +44,14 @@ int main(int argc, char **argv)
|
|||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUMaterial) * material_data.size(), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUMaterial) * material_data.size(), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, materialSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, materialSSBO);
|
||||||
|
|
||||||
|
|
||||||
|
GLuint lightSSBO;
|
||||||
|
glGenBuffers(1, &lightSSBO);
|
||||||
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
|
||||||
|
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
|
||||||
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, lightSSBO);
|
||||||
|
|
||||||
|
|
||||||
GLuint cameraUBO;
|
GLuint cameraUBO;
|
||||||
glGenBuffers(1, &cameraUBO);
|
glGenBuffers(1, &cameraUBO);
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
|
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
|
||||||
@ -56,6 +64,7 @@ int main(int argc, char **argv)
|
|||||||
glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUVolume), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUVolume), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_UNIFORM_BUFFER, 1, volumeUBO);
|
glBindBufferBase(GL_UNIFORM_BUFFER, 1, volumeUBO);
|
||||||
|
|
||||||
|
|
||||||
shader.attach();
|
shader.attach();
|
||||||
|
|
||||||
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
|
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
|
||||||
@ -72,6 +81,11 @@ int main(int argc, char **argv)
|
|||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
||||||
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, material_data.size() * sizeof(GPUMaterial), material_data.data());
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, material_data.size() * sizeof(GPUMaterial), material_data.data());
|
||||||
|
|
||||||
|
std::set<int> gpu_lights = scene.getGPULights();
|
||||||
|
std::vector<int> gpu_lights_array(gpu_lights.begin(), gpu_lights.end());
|
||||||
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
|
||||||
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, gpu_lights_array.size() * sizeof(int), gpu_lights_array.data());
|
||||||
|
|
||||||
GPUCamera camera_data = scene.getCamera()->getGPUData();
|
GPUCamera camera_data = scene.getCamera()->getGPUData();
|
||||||
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
|
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
|
||||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUCamera), &camera_data);
|
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUCamera), &camera_data);
|
||||||
@ -79,8 +93,10 @@ int main(int argc, char **argv)
|
|||||||
glBindBuffer(GL_UNIFORM_BUFFER, volumeUBO);
|
glBindBuffer(GL_UNIFORM_BUFFER, volumeUBO);
|
||||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUVolume), &scene.getVolume());
|
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUVolume), &scene.getVolume());
|
||||||
|
|
||||||
|
|
||||||
shader.set_int("u_frameCount", window.getFrameCount());
|
shader.set_int("u_frameCount", window.getFrameCount());
|
||||||
shader.set_int("u_objectsNum", object_data.size());
|
shader.set_int("u_objectsNum", object_data.size());
|
||||||
|
shader.set_int("u_lightsNum", gpu_lights.size());
|
||||||
shader.set_int("u_pixelisation", window.getPixelisation());
|
shader.set_int("u_pixelisation", window.getPixelisation());
|
||||||
shader.set_float("u_time", (float)(glfwGetTime()));
|
shader.set_float("u_time", (float)(glfwGetTime()));
|
||||||
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||||
|
@ -139,6 +139,27 @@ void Scene::addMaterial(Material *material)
|
|||||||
_gpu_materials.push_back(gpu_mat);
|
_gpu_materials.push_back(gpu_mat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Scene::updateLightAndObjects(int mat_id)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < _gpu_objects.size(); i++)
|
||||||
|
{
|
||||||
|
if (_gpu_objects[i].mat_index == mat_id)
|
||||||
|
_gpu_lights.insert(i);
|
||||||
|
}
|
||||||
|
for (auto it = _gpu_lights.begin(); it != _gpu_lights.end(); )
|
||||||
|
{
|
||||||
|
if (_gpu_materials[_gpu_objects[*it].mat_index].emission <= 0.0)
|
||||||
|
it = _gpu_lights.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::set<int> Scene::getGPULights()
|
||||||
|
{
|
||||||
|
return (_gpu_lights);
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<GPUObject>& Scene::getObjectData() const
|
const std::vector<GPUObject>& Scene::getObjectData() const
|
||||||
{
|
{
|
||||||
return (_gpu_objects);
|
return (_gpu_objects);
|
||||||
|
@ -257,12 +257,16 @@ bool SceneParser::parseLine(const std::string &line)
|
|||||||
if (it != object_parsers.end())
|
if (it != object_parsers.end())
|
||||||
{
|
{
|
||||||
Object *obj = it->second(ss);
|
Object *obj = it->second(ss);
|
||||||
(void) _scene->getMaterial(obj->getMaterialIndex()); //verify material
|
|
||||||
|
GPUMaterial mat = _scene->getMaterial(obj->getMaterialIndex()); //verify material
|
||||||
|
|
||||||
if (obj->getType() == Object::Type::PORTAL)
|
if (obj->getType() == Object::Type::PORTAL)
|
||||||
_scene->addObject(static_cast<Portal *>(obj)->createSupportQuad());
|
_scene->addObject(static_cast<Portal *>(obj)->createSupportQuad());
|
||||||
|
|
||||||
_scene->addObject(obj);
|
_scene->addObject(obj);
|
||||||
|
|
||||||
|
if (mat.emission > 0.0)
|
||||||
|
_scene->updateLightAndObjects(obj->getMaterialIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (identifier == "MAT")
|
if (identifier == "MAT")
|
||||||
|
@ -197,7 +197,11 @@ void Window::imGuiRender()
|
|||||||
|
|
||||||
ImGui::Text("Material %d", i);
|
ImGui::Text("Material %d", i);
|
||||||
has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]);
|
has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]);
|
||||||
has_changed |= ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f);
|
if (ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f))
|
||||||
|
{
|
||||||
|
has_changed = 1;
|
||||||
|
_scene->updateLightAndObjects(i);
|
||||||
|
}
|
||||||
|
|
||||||
if (mat.type == 0)
|
if (mat.type == 0)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user