+ Parsing object system

This commit is contained in:
TheRedShip
2024-12-26 23:06:49 +01:00
parent 2f217b50e9
commit 8941169f4a
13 changed files with 218 additions and 58 deletions

View File

@ -12,26 +12,29 @@
#include "RT.hpp"
int main(void)
int main(int argc, char **argv)
{
Window window(WIDTH, HEIGHT, "RT_GPU", 1);
Scene scene;
if (argc > 1 && !scene.parseScene(argv[1]))
return (1);
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 1);
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0};
float radius = 30.0f;
for (int i = 0; i < 150; i++) {
float angle = (2.0f * M_PI * i) / 150.0f;
float x = radius * cos(angle);
float z = radius * sin(angle);
float y = 2.0f * sin(angle * 3.0f);
glm::vec3 position(x, y, z);
float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f);
window.getScene()->addObject(new Sphere(position, sphereSize, &redMaterial));
}
// Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0};
// for (int i = 0; i < 150; i++)
// {
// float angle = (2.0f * M_PI * i) / 150.0f;
// float x = 30.0f * cos(angle);
// float z = 30.0f * sin(angle);
// float y = 2.0f * sin(angle * 3.0f);
// glm::vec3 position(x, y, z);
// float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f);
// scene.addObject(new Sphere(position, sphereSize, &redMaterial));
// }
GLuint objectSSBO;
glGenBuffers(1, &objectSSBO);
@ -40,16 +43,13 @@ int main(void)
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}}};
size_t size = sizeof(vertices) / sizeof(Vertex) / 3;
shader.setupVertexBuffer(vertices, size);
while (!window.shouldClose())
{
glUseProgram(shader.getProgramCompute());
const std::vector<GPUObject> &gpu_data = window.getScene()->getGPUData();
window.getScene()->updateGPUData();
const std::vector<GPUObject> &gpu_data = scene.getGPUData();
// Update SSBO with latest object data
glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO);
@ -58,8 +58,8 @@ int main(void)
shader.set_int("u_objectsNum", gpu_data.size());
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
shader.set_vec3("u_cameraPosition", window.getScene()->getCamera()->get_position());
shader.set_mat4("u_viewMatrix", window.getScene()->getCamera()->get_view_matrix());
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position());
shader.set_mat4("u_viewMatrix", scene.getCamera()->get_view_matrix());
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
@ -67,7 +67,6 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader.getProgram());
shader.drawTriangles(size);
std::cout << "\rFPS: " << int(window.getFps()) << " " << std::flush;

View File

@ -22,40 +22,71 @@ Scene::~Scene()
delete (_camera);
}
Camera *Scene::getCamera(void) const
bool Scene::parseScene(char *name)
{
return (_camera);
std::ifstream file(name);
std::string line;
if (!file.is_open())
{
std::cout << "Error opening the file" << std::endl;
file.close();
return (false);
}
SceneParser scene_parser(this);
while (std::getline(file, line))
{
if (!scene_parser.parseLine(line))
{
std::cerr << line << std::endl;
file.close();
return (false);
}
}
file.close();
return (true);
}
void Scene::addObject(Object *object)
{
_objects.push_back(object);
this->updateGPUData();
}
void Scene::updateGPUData()
{
_gpuObjects.clear();
_gpu_objects.clear();
for (const auto& obj : _objects)
{
GPUObject gpuObj;
gpuObj.position = obj->getPosition();
gpuObj.color = obj->getMaterial()->color;
gpuObj.roughness = obj->getMaterial()->roughness;
gpuObj.specular = obj->getMaterial()->specular;
gpuObj.type = static_cast<int>(obj->getType());
GPUObject gpu_obj;
gpu_obj.position = obj->getPosition();
gpu_obj.color = obj->getMaterial()->color;
gpu_obj.roughness = obj->getMaterial()->roughness;
gpu_obj.specular = obj->getMaterial()->specular;
gpu_obj.type = static_cast<int>(obj->getType());
if (obj->getType() == Object::Type::SPHERE) {
if (obj->getType() == Object::Type::SPHERE)
{
auto sphere = static_cast<const Sphere*>(obj);
gpuObj.radius = sphere->getRadius();
gpu_obj.radius = sphere->getRadius();
}
_gpuObjects.push_back(gpuObj);
std::cout << gpu_obj.position.x << " " << gpu_obj.position.y << " " << gpu_obj.position.z << " " << gpu_obj.radius << " " << gpu_obj.roughness << " " << gpu_obj.specular << std::endl;
_gpu_objects.push_back(gpu_obj);
}
}
const std::vector<GPUObject>& Scene::getGPUData() const
{
return (_gpuObjects);
return (_gpu_objects);
}
Camera *Scene::getCamera(void) const
{
return (_camera);
}

View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* SceneParser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */
/* Updated: 2024/12/26 21:43:51 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#include "SceneParser.hpp"
SceneParser::SceneParser(Scene *scene) : _scene(scene)
{
object_parsers["sp"] = [](std::stringstream& ss) -> Object*
{
try { return (new Sphere(ss)); }
catch (const std::exception& e) { throw; }
};
}
bool SceneParser::parseLine(const std::string &line)
{
if (line.empty() || line[0] == '#')
return (true);
std::stringstream ss(line);
std::string identifier;
ss >> identifier;
auto it = object_parsers.find(identifier);
if (it != object_parsers.end())
{
try {
Object *obj = it->second(ss);
_scene->addObject(obj);
} catch (const std::exception& e) {
std::cerr << "Error parsing sphere: " << e.what() << std::endl;
return false;
}
}
return (true);
}

View File

@ -12,10 +12,10 @@
#include "Window.hpp"
Window::Window(int width, int height, const char *title, int sleep)
Window::Window(Scene *scene, int width, int height, const char *title, int sleep)
{
_scene = new Scene();
_scene = scene;
if (!glfwInit())
{
fprintf( stderr, "Failed to initialize GLFW\n" );
@ -130,11 +130,6 @@ GLFWwindow *Window::getWindow(void) const
return (_window);
}
Scene *Window::getScene(void) const
{
return (_scene);
}
float Window::getFps(void) const
{
return (_fps);