renderer works in headless mode now

This commit is contained in:
2025-02-04 22:43:01 +01:00
parent 9ab0212d03
commit 85b7b58bed
7 changed files with 126 additions and 58 deletions

View File

@ -28,7 +28,7 @@ else
RM := rm -rf RM := rm -rf
DIR_DUP = mkdir -p $(@D) DIR_DUP = mkdir -p $(@D)
CC := clang++ CC := clang++
CFLAGS := -Wall -Wextra -Werror -g #-O3 CFLAGS := -Wall -Wextra -Werror -g -O3
IFLAGS := -I./includes -I./includes/RT -I./includes/imgui IFLAGS := -I./includes -I./includes/RT -I./includes/imgui
LDFLAGS += -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lavformat -lavcodec -lavutil -lswscale -lswresample LDFLAGS += -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lavformat -lavcodec -lavutil -lswscale -lswresample
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l) FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)

View File

@ -29,6 +29,6 @@ Pos=1556,610
Size=284,382 Size=284,382
[Window][Settings] [Window][Settings]
Pos=478,75 Pos=83,57
Size=336,287 Size=336,330

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 01:07:08 by tomoron #+# #+# */ /* Created: 2025/02/04 01:07:08 by tomoron #+# #+# */
/* Updated: 2025/02/04 17:04:59 by tomoron ### ########.fr */ /* Updated: 2025/02/04 22:04:04 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,6 +41,7 @@ class Arguments
int handleArg(char **argv, int argc, int *i); int handleArg(char **argv, int argc, int *i);
void initArguments(void); void initArguments(void);
void addArgument(char shortName, std::string longName, int isFlag); void addArgument(char shortName, std::string longName, int isFlag);
int setArg(t_arg arg, char **argv, int argc, int *i);
void parseRenderPathName(char * path); void parseRenderPathName(char * path);

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 16:29:26 by tomoron #+# #+# */ /* Created: 2025/01/22 16:29:26 by tomoron #+# #+# */
/* Updated: 2025/02/04 18:46:22 by tomoron ### ########.fr */ /* Updated: 2025/02/04 21:56:58 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,6 +78,7 @@ class Renderer
bool _renderSettings; bool _renderSettings;
bool _ignoreUnavailableCodec; bool _ignoreUnavailableCodec;
bool _headless; bool _headless;
bool _tp;
bool _shouldClose; bool _shouldClose;
int _curPathIndex; int _curPathIndex;

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
/* Updated: 2025/02/04 16:45:30 by tomoron ### ########.fr */ /* Updated: 2025/02/04 22:10:33 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Arguments args(argc, argv); Arguments args(argc, argv);
args.show();
if(args.error()) if(args.error())
return(1); return(1);
Scene scene(args.getSceneName()); Scene scene(args.getSceneName());

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 01:05:44 by tomoron #+# #+# */ /* Created: 2025/02/04 01:05:44 by tomoron #+# #+# */
/* Updated: 2025/02/04 17:10:24 by tomoron ### ########.fr */ /* Updated: 2025/02/04 22:17:04 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -96,10 +96,25 @@ void Arguments::addArgument(char shortName, std::string longName, int isFlag)
_args.push_back(arg); _args.push_back(arg);
} }
int Arguments::setArg(t_arg arg, char **argv, int argc, int *i)
{
if(arg.isFlag)
_values[arg.longName] = "yes";
else if(*i == argc - 1)
{
std::cerr << "missing option for " << arg.longName << std::endl;
return(0);
}
else
_values[arg.longName] = argv[++(*i)];
return(1);
}
int Arguments::handleArg(char **argv, int argc, int *i) int Arguments::handleArg(char **argv, int argc, int *i)
{ {
std::string arg(argv[*i]); std::string arg(argv[*i]);
std::vector<t_arg>::iterator it;
(void)i; (void)i;
if(!arg.size()) if(!arg.size())
@ -109,18 +124,7 @@ int Arguments::handleArg(char **argv, int argc, int *i)
for(std::vector<t_arg>::iterator it = _args.begin(); it != _args.end(); it++) for(std::vector<t_arg>::iterator it = _args.begin(); it != _args.end(); it++)
{ {
if((*it).longName == arg.substr(2)) if((*it).longName == arg.substr(2))
{ return(setArg((*it), argv, argc, i));
if((*it).isFlag)
_values[(*it).longName] = "yes";
else if(*i == argc - 1)
{
std::cerr << "missing option for --" << (*it).longName << std::endl;
return(0);
}
else
_values[(*it).longName] = argv[++(*i)];
return(1);
}
} }
std::cerr<< "unrecognized option : " << arg << std::endl; std::cerr<< "unrecognized option : " << arg << std::endl;
return(0); return(0);
@ -129,26 +133,22 @@ int Arguments::handleArg(char **argv, int argc, int *i)
{ {
for(size_t j = 1; j < arg.size(); j++) for(size_t j = 1; j < arg.size(); j++)
{ {
for(std::vector<t_arg>::iterator it = _args.begin(); it != _args.end(); it++) for(it = _args.begin(); it != _args.end(); it++)
{ {
if((*it).shortName == arg[j]) if((*it).shortName == arg[j])
{ {
if((*it).isFlag) if(!setArg((*it), argv, argc, i))
_values[(*it).longName] = "yes";
else if(*i == argc - 1)
{
std::cerr << "missing option for --" << (*it).longName << std::endl;
return(0); return(0);
} break;
else
_values[(*it).longName] = argv[++(*i)];
return(1);
} }
} }
if(it == _args.end())
{
std::cerr << "unrecognized option : -" << arg[j] << std::endl; std::cerr << "unrecognized option : -" << arg[j] << std::endl;
return(0); return(0);
} }
} }
}
else else
{ {
if(_values.find("sceneName") == _values.end()) if(_values.find("sceneName") == _values.end())

View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */ /* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */
/* Updated: 2025/02/04 19:06:00 by tomoron ### ########.fr */ /* Updated: 2025/02/04 21:56:44 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,8 +31,17 @@ Renderer::Renderer(Scene *scene, Window *win, Arguments &args)
_shouldClose = 1; _shouldClose = 1;
} }
} }
try{
if(_headless) if(_headless)
initRender(); initRender();
}
catch(std::exception &e)
{
std::cerr << "\033[31m" << e.what() << "\033[0m" << std::endl;
if(_headless)
_shouldClose = 1;
}
} }
void Renderer::init(Scene *scene, Window *win) void Renderer::init(Scene *scene, Window *win)
@ -42,6 +51,7 @@ void Renderer::init(Scene *scene, Window *win)
_min = 0; _min = 0;
_sec = 0; _sec = 0;
_fps = 30; _fps = 30;
_tp = 0;
_shouldClose = 0; _shouldClose = 0;
_autoTime = 0; _autoTime = 0;
_samples = 1; _samples = 1;
@ -219,7 +229,13 @@ void Renderer::updateAvailableCodecs(int mode, AVCodecID id)
void Renderer::initRender(void) void Renderer::initRender(void)
{ {
//TODO: check values if(_path.size() < 2)
throw std::runtime_error("render path doesn't have enough path points");
if(_path[0].time != 0)
throw std::runtime_error("render path does not start at 0, aborting");
if(_path[_path.size() - 1].time - _path[0].time <= 0)
throw std::runtime_error("render path is 0 seconds long, aborting");
_codecOptions = 0; _codecOptions = 0;
_destPathIndex = _path.size() - 1; _destPathIndex = _path.size() - 1;
_curPathIndex = 0; _curPathIndex = 0;
@ -230,7 +246,7 @@ void Renderer::initRender(void)
_renderStartTime = glfwGetTime(); _renderStartTime = glfwGetTime();
_scene->getCamera()->setPosition(_path[0].pos); _scene->getCamera()->setPosition(_path[0].pos);
_scene->getCamera()->setDirection(_path[0].dir.x, _path[0].dir.y); _scene->getCamera()->setDirection(_path[0].dir.x, _path[0].dir.y);
_win->setFrameCount(-1); _win->setFrameCount(_headless ? 0 : -1);
avformat_alloc_output_context2(&_format, nullptr, nullptr, _outputFilename.c_str()); avformat_alloc_output_context2(&_format, nullptr, nullptr, _outputFilename.c_str());
_codec_context = avcodec_alloc_context3(_codecList[_codecIndex]); _codec_context = avcodec_alloc_context3(_codecList[_codecIndex]);
@ -248,19 +264,28 @@ void Renderer::initRender(void)
_codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; _codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
if (avcodec_open2(_codec_context, _codecList[_codecIndex], &_codecOptions) < 0) if (avcodec_open2(_codec_context, _codecList[_codecIndex], &_codecOptions) < 0)
{
endRender();
throw std::runtime_error("Failed to open codec"); throw std::runtime_error("Failed to open codec");
}
_stream = avformat_new_stream(_format, _codecList[_codecIndex]); _stream = avformat_new_stream(_format, _codecList[_codecIndex]);
if (!_stream) if (!_stream)
{
endRender();
throw std::runtime_error("Failed to create stream"); throw std::runtime_error("Failed to create stream");
}
_stream->time_base = _codec_context->time_base; _stream->time_base = _codec_context->time_base;
avcodec_parameters_from_context(_stream->codecpar, _codec_context); avcodec_parameters_from_context(_stream->codecpar, _codec_context);
if (!(_format->flags & AVFMT_NOFILE)) if (!(_format->flags & AVFMT_NOFILE))
{ {
if (avio_open(&_format->pb, _outputFilename.c_str(), AVIO_FLAG_WRITE) < 0) if (avio_open(&_format->pb, _outputFilename.c_str(), AVIO_FLAG_WRITE) < 0)
{
endRender();
throw std::runtime_error("couldn't open " + _outputFilename); throw std::runtime_error("couldn't open " + _outputFilename);
} }
}
(void)avformat_write_header(_format, nullptr); (void)avformat_write_header(_format, nullptr);
_rgb_frame = av_frame_alloc(); _rgb_frame = av_frame_alloc();
@ -327,6 +352,8 @@ void Renderer::endRender(void)
{ {
AVPacket *pkt; AVPacket *pkt;
if(_codec_context)
{
avcodec_send_frame(_codec_context, 0); avcodec_send_frame(_codec_context, 0);
pkt = av_packet_alloc(); pkt = av_packet_alloc();
while (avcodec_receive_packet(_codec_context, pkt) == 0) { while (avcodec_receive_packet(_codec_context, pkt) == 0) {
@ -336,20 +363,29 @@ void Renderer::endRender(void)
av_interleaved_write_frame(_format, pkt); av_interleaved_write_frame(_format, pkt);
av_packet_unref(pkt); av_packet_unref(pkt);
} }
}
av_packet_free(&pkt); av_packet_free(&pkt);
if(_format)
av_write_trailer(_format); av_write_trailer(_format);
if(_rgb_frame)
av_frame_free(&_rgb_frame); av_frame_free(&_rgb_frame);
if(_yuv_frame)
av_frame_free(&_yuv_frame); av_frame_free(&_yuv_frame);
if(_codec_context)
avcodec_free_context(&_codec_context); avcodec_free_context(&_codec_context);
if(_format)
avio_close(_format->pb); avio_close(_format->pb);
if(_format)
avformat_free_context(_format); avformat_free_context(_format);
if(_codecOptions)
av_dict_free(&_codecOptions); av_dict_free(&_codecOptions);
_format = 0; _format = 0;
_rgb_frame = 0; _rgb_frame = 0;
_yuv_frame = 0; _yuv_frame = 0;
_codec_context = 0; _codec_context = 0;
_destPathIndex = 0;
if(_headless) if(_headless)
_shouldClose = 1; _shouldClose = 1;
} }
@ -374,7 +410,6 @@ void Renderer::update(Shader &shader)
{ {
double curTime; double curTime;
(void)shader;
if(!_destPathIndex) if(!_destPathIndex)
return; return;
@ -585,21 +620,44 @@ void Renderer::imguiPathCreation(void)
ImGui::Separator(); ImGui::Separator();
if(ImGui::SliderInt("minutes", &_min, 0, 2)) if(ImGui::SliderInt("minutes", &_min, 0, 2))
{
_autoTime = 0; _autoTime = 0;
_tp = 0;
}
if(ImGui::SliderInt("seconds", &_sec, 0, 60)) if(ImGui::SliderInt("seconds", &_sec, 0, 60))
{
_autoTime = 0; _autoTime = 0;
_tp = 0;
}
if(_autoTime) if(_autoTime)
{ {
if(_path.size() > 1) if(_path.size() > 1)
time = _path[_path.size() - 1].time + (glm::distance(_path[_path.size() - 1].pos, _scene->getCamera()->getPosition()) / prevSpeed); time = _path[_path.size() - 1].time + (glm::distance(_path[_path.size() - 1].pos, _scene->getCamera()->getPosition()) / prevSpeed);
else else
time = (float)_path.size() / 60; time = (float)_path.size() / 60;
if(std::isnan(time))
time = _path[_path.size() - 1].time + (1.0f / 60);
_min = time;
_sec = (time - (int)time) * 60;
}
else if(_tp)
{
if(!_path.size())
time = 0;
else
time = _path[_path.size() - 1].time;
_min = time; _min = time;
_sec = (time - (int)time) * 60; _sec = (time - (int)time) * 60;
} }
else else
time = (float)_min + ((float)_sec / 60); time = (float)_min + ((float)_sec / 60);
ImGui::Checkbox("guess time automatically", &_autoTime); ImGui::Checkbox("guess time automatically", &_autoTime);
if(_autoTime)
_tp = 0;
ImGui::Checkbox("tp", &_tp);
if(_tp)
_autoTime = 0;
if(ImGui::Button("add step")) if(ImGui::Button("add step"))
addPoint(time); addPoint(time);
@ -743,12 +801,16 @@ void Renderer::imguiRenderSettings(void)
ImGui::SliderInt("render spi", &_samples, 1, 1000); ImGui::SliderInt("render spi", &_samples, 1, 1000);
ImGui::SliderInt("render fps", &_fps, 30, 120); ImGui::SliderInt("render fps", &_fps, 30, 120);
ImGui::Combo("codec", &_codecIndex, _codecListStr.data(), _codecListStr.size()); ImGui::Combo("codec", &_codecIndex, _codecListStr.data(), _codecListStr.size());
if(ImGui::Checkbox("show all codecs", &_ignoreUnavailableCodec))
updateAvailableCodecs(_ignoreUnavailableCodec, (AVCodecID)0);
if(ImGui::InputText("file name", _filenameBuffer, 512)) if(ImGui::InputText("file name", _filenameBuffer, 512))
{ {
_outputFilename = _filenameBuffer; _outputFilename = _filenameBuffer;
updateAvailableCodecs(_ignoreUnavailableCodec, (AVCodecID)0); updateAvailableCodecs(_ignoreUnavailableCodec, (AVCodecID)0);
} }
if(_path.size() > 1 && _codecList.size()) if(_path.size() > 1 && _codecList.size())
{
try
{ {
if(ImGui::Button("start render")) if(ImGui::Button("start render"))
initRender(); initRender();
@ -756,6 +818,11 @@ void Renderer::imguiRenderSettings(void)
if(ImGui::Button("save path")) if(ImGui::Button("save path"))
savePath(); savePath();
} }
catch(std::exception &e)
{
std::cerr << "\033[31m" << e.what() << "\033[0m" << std::endl;
}
}
if(ImGui::Button("go back")) if(ImGui::Button("go back"))
_renderSettings = 0; _renderSettings = 0;
} }