mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Light array sent to gpu
This commit is contained in:
18
srcs/RT.cpp
18
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);
|
||||
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;
|
||||
glGenBuffers(1, &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);
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, 1, volumeUBO);
|
||||
|
||||
|
||||
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}}};
|
||||
@ -72,6 +81,11 @@ int main(int argc, char **argv)
|
||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
||||
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();
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUCamera), &camera_data);
|
||||
@ -79,12 +93,14 @@ int main(int argc, char **argv)
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, volumeUBO);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUVolume), &scene.getVolume());
|
||||
|
||||
|
||||
shader.set_int("u_frameCount", window.getFrameCount());
|
||||
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_float("u_time", (float)(glfwGetTime()));
|
||||
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||
|
||||
|
||||
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
|
||||
|
@ -139,6 +139,27 @@ void Scene::addMaterial(Material *material)
|
||||
_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
|
||||
{
|
||||
return (_gpu_objects);
|
||||
|
@ -257,12 +257,16 @@ bool SceneParser::parseLine(const std::string &line)
|
||||
if (it != object_parsers.end())
|
||||
{
|
||||
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)
|
||||
_scene->addObject(static_cast<Portal *>(obj)->createSupportQuad());
|
||||
|
||||
_scene->addObject(obj);
|
||||
|
||||
if (mat.emission > 0.0)
|
||||
_scene->updateLightAndObjects(obj->getMaterialIndex());
|
||||
}
|
||||
|
||||
if (identifier == "MAT")
|
||||
|
@ -197,7 +197,11 @@ void Window::imGuiRender()
|
||||
|
||||
ImGui::Text("Material %d", i);
|
||||
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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user