#include "Player.h" Player::Player(){ hasVideo = false; } /* Basic ofVideoPlayer setup */ void Player::Setup(){ videoPlayer.setLoopState(OF_LOOP_NORMAL); videoPlayer.setVolume(0); } /* Updated the video player: (1) Allocates the required W x H for the model input (2) Updates the video texture, and sets the current frame value */ void Player::Update(ofImage &img, bool show_frame){ if(!img.isAllocated()){ img.allocate(ofGetWindowWidth() / 2, ofGetWindowHeight(), OF_IMAGE_COLOR); temp.allocate(ofGetWindowWidth() / 2, ofGetWindowHeight(), GL_RGB); } // Calculate the target width and height for model_output_fbo_1 float fbo_1_target_width = img.getWidth(); // 1/2 of the screen width (990px) float fbo_1_target_height = img.getHeight(); // Full height of the screen float fbo_aspect_ratio = fbo_1_target_width / fbo_1_target_height; float aspect_ratio; if(show_frame && frame.isAllocated()){ aspect_ratio = frame.getWidth() / frame.getHeight(); } if(videoPlayer.isLoaded() && !show_frame){ hasVideo = true; playerCurrentFrame = videoPlayer.getCurrentFrame(); std::cout << playerCurrentFrame << std::endl; videoPlayer.update(); aspect_ratio = videoPlayer.getWidth() / videoPlayer.getHeight(); } if (fbo_aspect_ratio > aspect_ratio) { // FBO is wider; scale by width to fill the FBO new_width = fbo_1_target_width; new_height = new_width / aspect_ratio; // Scale height to maintain aspect ratio } else { // FBO is taller; scale by height to fill the FBO new_height = fbo_1_target_height; new_width = new_height * aspect_ratio; // Scale width to maintain aspect ratio } // Center the video to ensure it fills the FBO and is cropped if necessary x_pos = (ofGetWindowWidth() * 0.25) - (new_width / 2); y_pos = (ofGetWindowHeight() - new_height) / 2; // Center vertically if(show_frame && frame.isAllocated()){ set_frame = true; temp.begin(); frame.draw(x_pos, y_pos, new_width, new_height); temp.end(); } if(videoPlayer.isLoaded() && !show_frame){ temp.begin(); videoPlayer.draw(x_pos, y_pos, new_width, new_height); temp.end(); } ofPixels pixels; temp.readToPixels(pixels); img.setFromPixels(pixels); } void Player::Draw(){ if(videoPlayer.isLoaded()){ videoPlayer.draw(0, 0); } } ofPixels Player::GetVideoPixels(){ return videoPlayer.getPixels(); } /* Loads the video from path: (1) Sets the frame (2) Allocates the fbo dims for final render */ void Player::SetVideoOnAwake(std::string path, ofFbo &fbo){ videoPlayer.load(path); videoPlayer.play(); videoPlayer.setFrame(800); /* pause video initially */ fbo.allocate(ofGetWindowWidth() / 2, ofGetWindowHeight(), GL_RGB); } void Player::SetVideo(std::string path, std::string frame, bool is_active){ if(path.empty()){ path = "videos/output.mp4"; } /* convert str frame -> int (remove _x)*/ int f_number = 0; try { size_t pos = frame.find("_"); std::string frame_number = frame.substr(0, pos); f_number = std::stoi(frame_number); } catch (exception e){ std::cout << "No underscore found in frame name" << std::endl; } if(!is_active){ if(videoPlayer.getMoviePath() != path){ videoPlayer.loadAsync(path); videoPlayer.play(); } if(set_frame){ if(videoPlayer.isPlaying() && playerCurrentFrame != f_number){ videoPlayer.setFrame(f_number); if(playerCurrentFrame == f_number) set_frame = false; } } } } /* Loads a frame from path */ void Player::SetFrame(std::string path){ frame.load(path); } // Sets a random frame in the active video void Player::SetRandomFrame(){ int randomFrame = ofRandom(0, videoPlayer.getTotalNumFrames()); std::cout << "setting frame: " << randomFrame << std::endl; videoPlayer.setFrame(randomFrame); } void Player::SetVideoPosition(ofFbo& output_fbo){ int playerW = videoPlayer.getWidth(); int playerH = videoPlayer.getHeight(); // Calculate the scaling to fit the 2/3 width and full height area float targetWidth = output_fbo.getWidth(); float targetHeight = output_fbo.getHeight(); float scaleX = targetWidth / playerW; float scaleY = targetHeight / playerH; // Use the larger scaling factor to ensure coverage float scale = std::max(scaleX, scaleY); // Calculate scaled dimensions int scaledWidth = playerW * scale; int scaledHeight = playerH * scale; // Center the video within the FBO centerPosition = glm::vec2((targetWidth - scaledWidth) / 2, (targetHeight - scaledHeight) / 2); }