From 9ab0212d0329dcd39c8a8d7c5d11c840c6a7a99f Mon Sep 17 00:00:00 2001 From: tomoron Date: Tue, 4 Feb 2025 19:06:41 +0100 Subject: [PATCH] renderer can now load a path --- Makefile | 2 +- imgui.ini | 2 +- includes/RT/Arguments.hpp | 14 ++++++++----- includes/RT/Renderer.hpp | 5 ++--- includes/RT/Window.hpp | 4 ++-- srcs/RT.cpp | 5 +++-- srcs/class/Arguments.cpp | 40 +++++++++++++++++++++++++++-------- srcs/class/Renderer.cpp | 44 +++++++++++++++++++++++++++------------ srcs/class/Scene.cpp | 3 +-- srcs/class/Window.cpp | 6 +++--- 10 files changed, 84 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 4cb0d8d..1a789cb 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ else RM := rm -rf DIR_DUP = mkdir -p $(@D) CC := clang++ - CFLAGS := -Wall -Wextra -Werror -g -O3 + CFLAGS := -Wall -Wextra -Werror -g #-O3 IFLAGS := -I./includes -I./includes/RT -I./includes/imgui LDFLAGS += -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lavformat -lavcodec -lavutil -lswscale -lswresample FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l) diff --git a/imgui.ini b/imgui.ini index 8ac2eb7..1a5d442 100644 --- a/imgui.ini +++ b/imgui.ini @@ -29,6 +29,6 @@ Pos=1556,610 Size=284,382 [Window][Settings] -Pos=532,13 +Pos=478,75 Size=336,287 diff --git a/includes/RT/Arguments.hpp b/includes/RT/Arguments.hpp index f0a41a6..62c5b12 100644 --- a/includes/RT/Arguments.hpp +++ b/includes/RT/Arguments.hpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/04 01:07:08 by tomoron #+# #+# */ -/* Updated: 2025/02/04 03:10:58 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 17:04:59 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,10 +26,14 @@ class Arguments { public : Arguments(int argc, char **argv); - bool getHeadless(void) const; - std::string &getSceneName(void); - std::string getRenderPathName(void); - bool error(void) const; + + bool getHeadless(void) const; + bool error(void) const; + void show(void); + + std::string &getSceneName(void); + std::string *getRenderPath(void); + bool getHeadless(void); private: void printUsage(); diff --git a/includes/RT/Renderer.hpp b/includes/RT/Renderer.hpp index 6eb54db..b26038e 100644 --- a/includes/RT/Renderer.hpp +++ b/includes/RT/Renderer.hpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/22 16:29:26 by tomoron #+# #+# */ -/* Updated: 2025/02/04 00:41:10 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 18:46:22 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,8 +36,7 @@ typedef struct s_pathPoint class Renderer { public: - Renderer(Scene *scene, Window *win); - Renderer(Scene *scene, Window *win, std::string filename); + Renderer(Scene *scene, Window *win, Arguments &args); void renderImgui(void); void update(Shader &shader); int rendering(void) const; diff --git a/includes/RT/Window.hpp b/includes/RT/Window.hpp index 72cbb6a..4c654d1 100644 --- a/includes/RT/Window.hpp +++ b/includes/RT/Window.hpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:15:41 by TheRed #+# #+# */ -/* Updated: 2025/01/25 03:09:23 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 16:46:37 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ class Scene; class Window { public: - Window(Scene *scene, int width, int height, const char *title, int sleep); + Window(Scene *scene, int width, int height, const char *title, int sleep, Arguments &args); ~Window(void); void display(); diff --git a/srcs/RT.cpp b/srcs/RT.cpp index a518583..2d0443e 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ -/* Updated: 2025/02/04 03:06:51 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 16:45:30 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,12 +15,13 @@ int main(int argc, char **argv) { Arguments args(argc, argv); + args.show(); if(args.error()) return(1); Scene scene(args.getSceneName()); if(scene.fail()) return(1); - Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0); + Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args); Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); // Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl"); diff --git a/srcs/class/Arguments.cpp b/srcs/class/Arguments.cpp index 3a6fbe1..9c25399 100644 --- a/srcs/class/Arguments.cpp +++ b/srcs/class/Arguments.cpp @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/04 01:05:44 by tomoron #+# #+# */ -/* Updated: 2025/02/04 03:16:25 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 17:10:24 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,25 +40,52 @@ Arguments::Arguments(int argc, char **argv) } } +void Arguments::initArguments() +{ + addArgument('r', "renderpath", 0); + addArgument('h', "headless", 1); +} + void Arguments::printUsage(void) { std::cerr << "usage : [options] [options]" << std::endl; std::cerr << R""""(options : - -r | --renderpath : filename for the renderer path + -r | --renderpath : filename for the renderer path -h | --headless : does the program need to start rendering as soon as it starts(and close automatically) )""""; } +void Arguments::show(void) +{ + for(std::map::iterator it = _values.begin();it != _values.end(); it++) + std::cout << (*it).first << ": " << (*it).second << std::endl; +} + bool Arguments::error(void) const { return(_err); } + std::string &Arguments::getSceneName(void) { return(_values["sceneName"]); } +std::string *Arguments::getRenderPath(void) +{ + if(_values.find("renderpath") != _values.end()) + return(&_values["renderpath"]); + else + return(0); +} + +bool Arguments::getHeadless(void) +{ + return(_values.find("headless") != _values.end()); +} + + void Arguments::addArgument(char shortName, std::string longName, int isFlag) { t_arg arg; @@ -69,11 +96,6 @@ void Arguments::addArgument(char shortName, std::string longName, int isFlag) _args.push_back(arg); } -void Arguments::initArguments() -{ - addArgument('r', "renderPath", 0); - addArgument('h', "headless", 1); -} int Arguments::handleArg(char **argv, int argc, int *i) { @@ -92,7 +114,7 @@ int Arguments::handleArg(char **argv, int argc, int *i) _values[(*it).longName] = "yes"; else if(*i == argc - 1) { - std::cerr << "missing option" << std::endl; + std::cerr << "missing option for --" << (*it).longName << std::endl; return(0); } else @@ -115,7 +137,7 @@ int Arguments::handleArg(char **argv, int argc, int *i) _values[(*it).longName] = "yes"; else if(*i == argc - 1) { - std::cerr << "missing option" << std::endl; + std::cerr << "missing option for --" << (*it).longName << std::endl; return(0); } else diff --git a/srcs/class/Renderer.cpp b/srcs/class/Renderer.cpp index 65e0a2e..cc24d00 100644 --- a/srcs/class/Renderer.cpp +++ b/srcs/class/Renderer.cpp @@ -6,24 +6,33 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */ -/* Updated: 2025/02/04 00:43:18 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 19:06:00 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "RT.hpp" -Renderer::Renderer(Scene *scene, Window *win) +Renderer::Renderer(Scene *scene, Window *win, Arguments &args) { - init(scene, win); - _headless = 0; -} + std::string *renderPath; -Renderer::Renderer(Scene *scene, Window *win, std::string filename) -{ init(scene, win); - _headless = 1; - loadPath(filename); - initRender(); + _headless = args.getHeadless(); + renderPath = args.getRenderPath(); + if(renderPath) + { + try + { + loadPath(*renderPath); + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + _shouldClose = 1; + } + } + if(_headless) + initRender(); } void Renderer::init(Scene *scene, Window *win) @@ -71,15 +80,20 @@ void Renderer::savePath(void) { std::ofstream outputFile; const AVCodec *codec; + (void)codec; codec = _codecList[_codecIndex]; outputFile.open("output.path", std::ios::binary); + if(!outputFile.is_open()) + std::cerr << "could open output.path for writing" << std::endl; outputFile.write(_outputFilename.c_str(), _outputFilename.length() + 1); outputFile.write((char *)&codec->id, sizeof(codec->id)); outputFile.write((char *)&_samples, sizeof(_samples)); outputFile.write((char *)&_fps, sizeof(_fps)); for(std::vector::iterator it = _path.begin(); it != _path.end(); it++) + { outputFile.write((char *)&(*it), sizeof(t_pathPoint)); + } outputFile.close(); } @@ -95,6 +109,8 @@ void Renderer::loadPath(std::string filename) _outputFilename = ""; _filenameBuffer[0] = 0; file.open(filename); + if(!file.is_open()) + std::cerr << "failed to open " << filename << std::endl; c = 1; while(c) { @@ -104,15 +120,17 @@ void Renderer::loadPath(std::string filename) if(c) _outputFilename += c; } + memcpy(_filenameBuffer, _outputFilename.c_str(), _outputFilename.length()); + _filenameBuffer[_outputFilename.length()] = 0; rawRead(file, &codecId, sizeof(codecId)); updateAvailableCodecs(2, codecId); if(_codecList.size() == 0) throw std::runtime_error("codec not available"); rawRead(file, &_samples, sizeof(_samples)); rawRead(file, &_fps, sizeof(_fps)); - if(_samples < 1 || _fps < 1) + if(_samples < 1 || _fps < 1 || _samples >= 1000 || _fps >= 120) throw std::runtime_error("invalid value provided in fps or samples"); - while(!file.eof()) + while(file.peek() != EOF) { rawRead(file, &pathPoint, sizeof(t_pathPoint)); if(pathPoint.time < .0f) @@ -201,7 +219,7 @@ void Renderer::updateAvailableCodecs(int mode, AVCodecID id) void Renderer::initRender(void) { - + //TODO: check values _codecOptions = 0; _destPathIndex = _path.size() - 1; _curPathIndex = 0; diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 9ac432a..36f5851 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -6,7 +6,7 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */ -/* Updated: 2025/02/04 03:11:24 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 16:43:33 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ Scene::Scene(std::string &name) { - std::cout << "name : " << name << std::endl; std::ifstream file(name); std::string line; _camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index ef37319..71c54f4 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -6,19 +6,19 @@ /* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ -/* Updated: 2025/02/04 00:42:01 by tomoron ### ########.fr */ +/* Updated: 2025/02/04 16:46:29 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "Window.hpp" -Window::Window(Scene *scene, int width, int height, const char *title, int sleep) +Window::Window(Scene *scene, int width, int height, const char *title, int sleep, Arguments &args) { _scene = scene; _fps = 0; _frameCount = 0; _pixelisation = 0; - _renderer = new Renderer(scene, this); + _renderer = new Renderer(scene, this, args); if (!glfwInit()) {