/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Renderer.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */ /* Updated: 2025/01/25 03:25:48 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "RT.hpp" Renderer::Renderer(Scene *scene, Window *win) { _scene = scene; _win = win; _min = 0; _sec = 0; _fps = 30; _samples = 1; _testSamples = 1; _curSamples = 0; _destPathIndex = 0; _frameCount = 0; _frame = 0; _format = 0; _codec_context = 0; } void Renderer::initFfmpeg(std::string filename) { const AVCodec *codec; AVStream *stream; avformat_alloc_output_context2(&_format, nullptr, nullptr, filename.c_str()); codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) throw std::runtime_error("unable to find H264 audio/video codec, glhf"); _codec_context = avcodec_alloc_context3(codec); _codec_context->width = WIDTH; _codec_context->height = HEIGHT; _codec_context->time_base = {1, _fps}; _codec_context->framerate = {_fps, 1}; _codec_context->pix_fmt = AV_PIX_FMT_RGB24; _codec_context->gop_size = 10; _codec_context->max_b_frames = 1; if (_format->oformat->flags & AVFMT_GLOBALHEADER) _codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; if (avcodec_open2(_codec_context, codec, nullptr) < 0) throw std::runtime_error("Failed to open codec"); stream = avformat_new_stream(_format, codec); if (!stream) throw std::runtime_error("Failed to create stream"); stream->time_base = _codec_context->time_base; avcodec_parameters_from_context(stream->codecpar, _codec_context); if (!(_format->flags & AVFMT_NOFILE)) { if (avio_open(&_format->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) throw std::runtime_error("couldn't open " + filename); } (void)avformat_write_header(_format, nullptr); _frame = av_frame_alloc(); _frame->format = _codec_context->pix_fmt; _frame->width = _codec_context->width; _frame->height = _codec_context->height; av_image_alloc(_frame->data, _frame->linesize, WIDTH, HEIGHT, _codec_context->pix_fmt, 32); } void Renderer::addImageToRender(Shader &shader) { std::vector image; AVPacket *pkt; long int videoFrameOffset; long int outputImageOffset; image = shader.getOutputImage(); for (int x = 0; x < WIDTH; x++) { for(int y = 0; y < HEIGHT; y++) { videoFrameOffset = (y * _frame->linesize[0]) + (x * 3); outputImageOffset = (y * (WIDTH * 4)) + (x * 4); _frame->data[0][videoFrameOffset] = image[outputImageOffset]; _frame->data[0][videoFrameOffset + 1] = image[outputImageOffset + 1]; _frame->data[0][videoFrameOffset + 2] = image[outputImageOffset + 2]; } } _frame->pts = _frameCount; if (avcodec_send_frame(_codec_context, _frame) == 0) { pkt = av_packet_alloc(); while (avcodec_receive_packet(_codec_context, pkt) == 0) { av_interleaved_write_frame(_format, pkt); av_packet_unref(pkt); } av_packet_free(&pkt); } } void Renderer::endRender(void) { av_write_trailer(_format); av_frame_free(&_frame); avcodec_free_context(&_codec_context); avio_close(_format->pb); avformat_free_context(_format); _format = 0; _frame = 0; _codec_context = 0; } void Renderer::addPoint(void) { t_pathPoint newPoint; Camera *cam; std::vector::iterator pos; cam = _scene->getCamera(); newPoint.pos = cam->getPosition(); newPoint.dir = cam->getDirection(); newPoint.time = _min + ((float)_sec / 60); pos = _path.begin(); while(pos != _path.end() && pos->time <= newPoint.time) pos++; _path.insert(pos, newPoint); } void Renderer::update(Shader &shader) { double curTime; (void)shader; if(!_destPathIndex) return; curTime = glfwGetTime(); _curSamples++; if(_testMode && _curSamples == _testSamples) { makeMovement(curTime - _curSplitStart, curTime); _curSamples = 0; return; } //TODO: the rest } void Renderer::makeMovement(float timeFromStart, float curSplitTimeReset) { t_pathPoint from; t_pathPoint to; float pathTime; Camera *cam; glm::vec3 posStep; glm::vec2 dirStep; from = _path[_curPathIndex]; to = _path[_curPathIndex + 1]; cam = _scene->getCamera(); pathTime = (to.time - from.time) * 60; posStep.x = ((to.pos.x - from.pos.x) / pathTime) * timeFromStart; posStep.y = ((to.pos.y - from.pos.y) / pathTime) * timeFromStart; posStep.z = ((to.pos.z - from.pos.z) / pathTime) * timeFromStart; dirStep.x = ((to.dir.x - from.dir.x) / pathTime) * timeFromStart; dirStep.y = ((to.dir.y - from.dir.y) / pathTime) * timeFromStart; if(timeFromStart >= pathTime) { posStep = to.pos - from.pos; dirStep = to.dir - from.dir; _curSplitStart = curSplitTimeReset; _curPathIndex++; } cam->setPosition(from.pos + posStep); cam->setDirection(from.dir.x + dirStep.x, from.dir.y + dirStep.y); _win->setFrameCount(0); if(_curPathIndex == _destPathIndex) { _destPathIndex = 0; _testMode = 0; } } void Renderer::renderImgui(void) { ImGui::Begin("Renderer"); ImGui::SliderInt("test spi", &_testSamples, 1, 10); ImGui::SliderInt("render spi", &_samples, 1, 1000); ImGui::SliderInt("render fps", &_fps, 30, 120); if(_path.size() && ImGui::Button("try full path")) { _scene->getCamera()->setPosition(_path[0].pos); _scene->getCamera()->setDirection(_path[0].dir.x, _path[0].dir.y); _win->setFrameCount(-1); _curSplitStart = glfwGetTime(); _curPathIndex = 0; _destPathIndex = _path.size() - 1; _testMode = 1; } ImGui::Separator(); ImGui::SliderInt("minutes", &_min, 0, 2); ImGui::SliderInt("seconds", &_sec, 0, 60); if(ImGui::Button("add step")) addPoint(); ImGui::Separator(); for(unsigned long i = 0; i < _path.size(); i++) { ImGui::Text("pos : %f, %f, %f",_path[i].pos.x, _path[i].pos.y, _path[i].pos.z); ImGui::Text("dir : %f, %f",_path[i].dir.x, _path[i].dir.y); ImGui::Text("time : %dm%ds", (int)_path[i].time, (int)(((_path[i].time - (int)_path[i].time)) * 60)); if(ImGui::Button(("delete##" + std::to_string(i)).c_str())) { _path.erase(_path.begin() + i); } ImGui::SameLine(); if(ImGui::Button(("go to pos##" + std::to_string(i)).c_str())) { _scene->getCamera()->setPosition(_path[i].pos); _scene->getCamera()->setDirection(_path[i].dir.x, _path[i].dir.y); _win->setFrameCount(-1); } ImGui::SameLine(); if(ImGui::Button(("edit pos##" + std::to_string(i)).c_str())) { _path[i].pos = _scene->getCamera()->getPosition(); _path[i].dir = _scene->getCamera()->getDirection(); } if(i) ImGui::SameLine(); if(i && ImGui::Button(("test split##" + std::to_string(i)).c_str())) { _scene->getCamera()->setPosition(_path[i].pos); _scene->getCamera()->setDirection(_path[i].dir.x, _path[i].dir.y); _win->setFrameCount(-1); _curSplitStart = glfwGetTime(); _curPathIndex = i - 1; _destPathIndex = i; _testMode = 1; } ImGui::Separator(); } ImGui::End(); }