From 1ded5ea7303c80aefbe58caf2f02b9eab8dfa757 Mon Sep 17 00:00:00 2001 From: illyum <90023277+itzilly@users.noreply.github.com> Date: Sun, 4 Aug 2024 18:07:03 -0600 Subject: [PATCH] Initial Commit --- .gitignore | 2 + .gitmodules | 6 ++ CMakeLists.txt | 136 +++++++++++++++++++++++++++++ README.md | 8 ++ src/main.cpp | 196 ++++++++++++++++++++++++++++++++++++++++++ thirdparty/glew-cmake | 1 + thirdparty/glfw | 1 + 7 files changed, 350 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 src/main.cpp create mode 160000 thirdparty/glew-cmake create mode 160000 thirdparty/glfw diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9c4971 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Ignore all build directories +build/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..55f443a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "thirdparty/glew-cmake"] + path = thirdparty/glew-cmake + url = https://github.com/Perlmint/glew-cmake +[submodule "thirdparty/glfw"] + path = thirdparty/glfw + url = https://github.com/glfw/glfw diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2e0e7fe --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,136 @@ +cmake_minimum_required(VERSION 3.24) + +set(PROJECT_NAME Sentinal) +project(${PROJECT_NAME} LANGUAGES CXX) + +set(project_version_major 0) +set(project_version_minor 0) +set(project_version_patch 1) +set(project_version_tweak 0) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Make sure this is here for nvim and fleet users +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# # # # # # # # # # # # +# Manage Dependencies # +# # # # # # # # # # # # + +include(ExternalProject) + +ExternalProject_Add( + imgui + GIT_REPOSITORY https://github.com/ocornut/imgui.git + GIT_TAG master + UPDATE_COMMAND "" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + BUILD_IN_SOURCE 1 +) + +ExternalProject_Get_Property(imgui source_dir) +file(GLOB imgui_sources + "${source_dir}/*.cpp" + "${source_dir}/*.h" +) + +add_library(imgui STATIC ${imgui_sources}) +target_include_directories(imgui PUBLIC ${source_dir}) + +ExternalProject_Add( + ffmpeg + GIT_REPOSITORY https://github.com/FFmpeg/FFmpeg.git + GIT_TAG master + UPDATE_COMMAND "" + CONFIGURE_COMMAND ./configure --prefix= --enable-shared --disable-static + BUILD_COMMAND make + INSTALL_COMMAND make install + BUILD_IN_SOURCE 1 +) + +ExternalProject_Get_Property(ffmpeg install_dir) + +set(FFMPEG_INCLUDE_DIR ${install_dir}/include) +set(FFMPEG_LIB_DIR ${install_dir}/lib) +set(FFMPEG_BIN_DIR ${install_dir}/bin) + +add_library(avcodec SHARED IMPORTED) +set_target_properties(avcodec PROPERTIES + IMPORTED_LOCATION ${FFMPEG_LIB_DIR}/libavcodec.so + INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIR} +) + +add_library(avformat SHARED IMPORTED) +set_target_properties(avformat PROPERTIES + IMPORTED_LOCATION ${FFMPEG_LIB_DIR}/libavformat.so + INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIR} +) + +add_library(avutil SHARED IMPORTED) +set_target_properties(avutil PROPERTIES + IMPORTED_LOCATION ${FFMPEG_LIB_DIR}/libavutil.so + INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIR} +) + +add_library(swscale SHARED IMPORTED) +set_target_properties(swscale PROPERTIES + IMPORTED_LOCATION ${FFMPEG_LIB_DIR}/libswscale.so + INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIR} +) + +add_library(FFmpeg::avcodec ALIAS avcodec) +add_library(FFmpeg::avformat ALIAS avformat) +add_library(FFmpeg::avutil ALIAS avutil) +add_library(FFmpeg::swscale ALIAS swscale) + +# Add GLFW and GLEW as subdirectories if they have their own CMake files +add_subdirectory(thirdparty/glfw) +add_subdirectory(thirdparty/glew-cmake) + +# # # # # # # # # # # # +# Dependencies Done # +# # # # # # # # # # # # + + +if(APPLE) + list(APPEND EXTRA_LIBS "-framework OpenGL") +elseif(WIN32) + find_package(OpenGL REQUIRED) +else() + list(APPEND EXTRA_LIBS "-lGL -lGLU -lX11") +endif() + +# ImGui glfw backends (GLFW + OpenGL3) +add_library(imgui_impl STATIC + thirdparty/imgui/backends/imgui_impl_glfw.cpp + thirdparty/imgui/backends/imgui_impl_opengl3.cpp +) + +target_include_directories(imgui_impl PRIVATE + thirdparty/imgui + thirdparty/imgui/backends + thirdparty/glfw/include +) + +include_directories( + thirdparty/imgui + thirdparty/imgui/backends + thirdparty/glfw/include + thirdparty/glew-cmake/include +) + +set(glew-cmake_BUILD_SHARED OFF) +set(glew-cmake_BUILD_STATIC ON) + +add_executable(${PROJECT_NAME} src/main.cpp) + +target_link_libraries(${PROJECT_NAME} PRIVATE imgui FFmpeg::avcodec FFmpeg::avformat FFmpeg::avutil FFmpeg::swscale glfw glew) + +add_dependencies(${PROJECT_NAME} ffmpeg) + +set_target_properties(${PROJECT_NAME} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" +) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9bf5fb0 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Sentinal +Security Camera Software + +## Building +Sentinal uses MinGW as it's main compiler. To clone the repo, use +```bash +git clone http://10.0.0.19:3001/sentinal/sentinal.git +``` \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e05c9a0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,196 @@ +// #include +// #include +// #include +// #include +// #include + +// int main(void) { +// GLFWwindow* window; +// if (!glfwInit()) { +// printf("Couldn't initialize GLFW!\n"); +// return 1; +// } + +// window = glfwCreateWindow(640, 480, "Sentinal", NULL, NULL); +// if (!window) { +// printf("Couldn't create window\n"); +// return 1; +// } + +// glfwMakeContextCurrent(window); + +// // Initialize OpenGL loader (you might need to include and initialize GLEW or glad here) + +// // Initialize ImGui +// IMGUI_CHECKVERSION(); +// ImGui::CreateContext(); +// ImGuiIO& io = ImGui::GetIO(); (void)io; +// ImGui::StyleColorsDark(); + +// // Initialize ImGui backends +// ImGui_ImplGlfw_InitForOpenGL(window, true); +// ImGui_ImplOpenGL3_Init("#version 130"); + +// while (!glfwWindowShouldClose(window)) { +// glfwPollEvents(); + +// // Start ImGui frame +// ImGui_ImplOpenGL3_NewFrame(); +// ImGui_ImplGlfw_NewFrame(); +// ImGui::NewFrame(); + +// // Your ImGui code here +// ImGui::Begin("Hello, ImGui!"); +// ImGui::Text("This is a simple window."); +// ImGui::End(); + +// // Render +// ImGui::Render(); +// int display_w, display_h; +// glfwGetFramebufferSize(window, &display_w, &display_h); +// glViewport(0, 0, display_w, display_h); +// glClearColor(0.45f, 0.55f, 0.60f, 1.00f); +// glClear(GL_COLOR_BUFFER_BIT); +// ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + +// glfwSwapBuffers(window); +// } + +// // Cleanup +// ImGui_ImplOpenGL3_Shutdown(); +// ImGui_ImplGlfw_Shutdown(); +// ImGui::DestroyContext(); + +// glfwDestroyWindow(window); +// glfwTerminate(); + +// return 0; +// } + +extern "C" { +#include +#include +#include +} + +#include +#include +#include +#include +#include +#include + +// Function to create a texture from an OpenCV frame +GLuint createTextureFromFrame(const cv::Mat& frame) { + GLuint textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_2D, textureID); + + // Set texture parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Upload the frame to the texture + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, frame.data); + + glBindTexture(GL_TEXTURE_2D, 0); + return textureID; +} + +int main(void) { + // Initialize GLFW + if (!glfwInit()) { + printf("Couldn't initialize GLFW!\n"); + return 1; + } + + // Create a windowed mode window and its OpenGL context + GLFWwindow* window = glfwCreateWindow(640, 480, "Sentinal", NULL, NULL); + if (!window) { + printf("Couldn't create window\n"); + glfwTerminate(); + return 1; + } + + // Make the window's context current + glfwMakeContextCurrent(window); + + // Initialize GLEW + if (glewInit() != GLEW_OK) { + printf("Couldn't initialize GLEW!\n"); + return 1; + } + + // Initialize ImGui + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + ImGui::StyleColorsDark(); + + // Initialize ImGui backends + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 130"); + + // OpenCV video capture + cv::VideoCapture cap(0); // Change the argument to a video file path if needed + if (!cap.isOpened()) { + printf("Couldn't open video capture\n"); + return 1; + } + + GLuint videoTexture = 0; + + // Main loop + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + // Capture frame + cv::Mat frame; + cap >> frame; // Get the next frame from the video + + if (!frame.empty()) { + // Create or update texture + if (videoTexture == 0) { + videoTexture = createTextureFromFrame(frame); + } else { + glBindTexture(GL_TEXTURE_2D, videoTexture); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.cols, frame.rows, GL_BGR, GL_UNSIGNED_BYTE, frame.data); + glBindTexture(GL_TEXTURE_2D, 0); + } + } + + // Start ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + // ImGui window with video + ImGui::Begin("Video Stream"); + if (videoTexture != 0) { + ImGui::Image((void*)(intptr_t)videoTexture, ImVec2((float)frame.cols, (float)frame.rows)); + } + ImGui::End(); + + // Render ImGui + ImGui::Render(); + int display_w, display_h; + glfwGetFramebufferSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(0.45f, 0.55f, 0.60f, 1.00f); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + // Swap buffers + glfwSwapBuffers(window); + } + + // Cleanup + glDeleteTextures(1, &videoTexture); + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + glfwDestroyWindow(window); + glfwTerminate(); + + return 0; +} \ No newline at end of file diff --git a/thirdparty/glew-cmake b/thirdparty/glew-cmake new file mode 160000 index 0000000..a5494db --- /dev/null +++ b/thirdparty/glew-cmake @@ -0,0 +1 @@ +Subproject commit a5494db414d62e7bcb19f9f8fd7ee660338ea08e diff --git a/thirdparty/glfw b/thirdparty/glfw new file mode 160000 index 0000000..b35641f --- /dev/null +++ b/thirdparty/glfw @@ -0,0 +1 @@ +Subproject commit b35641f4a3c62aa86a0b3c983d163bc0fe36026d