Skip to content

Commit

Permalink
Merge branch 'fix-camera-stretching' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr-deref committed Feb 1, 2022
2 parents 3a1a24a + 9effc52 commit df9fc12
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 12 deletions.
7 changes: 4 additions & 3 deletions gl/glstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Program.hpp"

namespace gl {
GLFWwindow * createDefaultWindow(const std::string &windowName) {
GLFWwindow * createDefaultWindow(const std::string &windowName, uint64_t width, uint64_t height) {
GLFWmonitor *pmonitor = glfwGetPrimaryMonitor();
const GLFWvidmode *vmode = glfwGetVideoMode(pmonitor);

Expand All @@ -18,8 +18,9 @@ namespace gl {
glfwWindowHint(GLFW_OPENGL_CORE_PROFILE, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow *wnd = glfwCreateWindow(vmode->width - 100, vmode->height - 100, windowName.c_str(), nullptr, nullptr);
glfwSetWindowPos(wnd, 50, 50);
const uint64_t actualWidth = width > vmode->width ? vmode->width : width;
const uint64_t actualHeight = height > vmode->height ? vmode->height : height;
GLFWwindow *wnd = glfwCreateWindow(actualWidth, actualHeight, windowName.c_str(), nullptr, nullptr);

return wnd;
}
Expand Down
2 changes: 1 addition & 1 deletion gl/glstuff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace gl {
class Texture;
GLFWwindow * createDefaultWindow(const std::string &windowName);
GLFWwindow * createDefaultWindow(const std::string &windowName, uint64_t width, uint64_t height);
void loadCVmat2GLTexture(const Texture &texture, const cv::Mat &image, bool shouldFlip = false);
Program loadDefaultShaders();
GLuint retrieveTypeSize(GLenum type);
Expand Down
4 changes: 3 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ int main(int argc, char **argv) {
try {
if (!glfwInit()) throw std::runtime_error("Could not initialize GLFW.");

GLFWwindow *wnd = gl::createDefaultWindow("Viewport");
GLFWwindow *wnd = gl::createDefaultWindow("Viewport",
2 * cam.frameData().width,
2 * cam.frameData().height);
glfwMakeContextCurrent(wnd);
glfwSwapInterval(1);

Expand Down
5 changes: 3 additions & 2 deletions vidIO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ cmake_minimum_required(VERSION 3.15)
project(vidIO LANGUAGES CXX)

add_library(vidIO STATIC
Camera.cpp
CVCameraAdapter.cpp
"Camera.cpp"
"CameraAdapter.cpp"
"CVCameraAdapter.cpp"
)

target_include_directories(vidIO PRIVATE ${opencv_INCLUDE_DIRS})
Expand Down
13 changes: 9 additions & 4 deletions vidIO/CVCameraAdapter.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include "CVCameraAdapter.hpp"

namespace vidIO {
CVCameraAdapter::CVCameraAdapter() { this->open(); }
CVCameraAdapter::CVCameraAdapter() {
this->open();
this->fdat.width = cap_.get(cv::CAP_PROP_FRAME_WIDTH);
this->fdat.height = cap_.get(cv::CAP_PROP_FRAME_HEIGHT);
}

bool CVCameraAdapter::open() {
return cap.open(0);
return cap_.open(0);
}

void CVCameraAdapter::close() { if (cap.isOpened()) cap.release(); }
void CVCameraAdapter::close() { if (cap_.isOpened()) cap_.release(); }
Frame CVCameraAdapter::nextFrame() {
cv::Mat frame;
if (!cap.read(frame))
if (!cap_.read(frame))
throw std::runtime_error("Device could not read frame.");

return frame;
Expand Down
2 changes: 1 addition & 1 deletion vidIO/CVCameraAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ namespace vidIO {
Frame nextFrame() override;

private:
cv::VideoCapture cap;
cv::VideoCapture cap_;
};
}
3 changes: 3 additions & 0 deletions vidIO/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ namespace vidIO {
bool Camera::open() { return adapter->open(); }
void Camera::close() { adapter->close(); }
Camera::~Camera() { adapter->close(); }
auto Camera::frameData() const -> const FrameData & {
return adapter->frameData();
}
}
1 change: 1 addition & 0 deletions vidIO/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace vidIO {
bool open();
void close();
Frame nextFrame();
auto frameData() const -> const FrameData &;
private:
std::unique_ptr<CameraAdapter> adapter = nullptr;
};
Expand Down
5 changes: 5 additions & 0 deletions vidIO/CameraAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "CameraAdapter.hpp"

auto vidIO::CameraAdapter::frameData() const -> const FrameData & {
return fdat;
}
8 changes: 8 additions & 0 deletions vidIO/CameraAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

namespace vidIO {
using Frame = cv::Mat;
struct FrameData {
uint64_t width;
uint64_t height;
};

class CameraAdapter {
public:
virtual bool open() = 0;
virtual void close() = 0;
virtual Frame nextFrame() = 0;
auto frameData() const -> const FrameData &;
protected:
FrameData fdat;
};
}

0 comments on commit df9fc12

Please sign in to comment.