diff --git a/.gitignore b/.gitignore index c85e4053eeb5b143ab4277d51b37dcf9137bdc4b..7ffc87e855c6add3e42dbd27d021e10741afa01c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +build ### C # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt index 37bec734734de28fc84bbe0da05208319ed2af35..b0ccef26f1af7b7081ccaad99f6b7f018f596343 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,15 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic") add_executable(${PROJECT_NAME} - src/main.cpp src/utility/constants.h src/utility/gl.h src/utility/application.cpp src/utility/application.h) + src/main.cpp + src/constants.h + src/utility/gl.h + src/application.cpp + src/application.h + src/scene.cpp + src/scene.h + ) + # opengl find_package(OpenGL REQUIRED) diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30fcc0cf639460bada0593507bed1f51f1d273c2 --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,109 @@ +#include "application.h" + +bool Application::dragging = false; + +Application::Application() { + window = glfwCreateWindow(WIDTH, HEIGHT, NAME, NULL, NULL); +} + +Application::~Application() { + glfwDestroyWindow(window); +} + +GLFWwindow* Application::get_window() { + return window; +} + +void Application::init() { + this->set_callbacks(); + + glViewport(0, 0, this->width, this->height); + glMatrixMode(GL_MODELVIEW); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glLoadIdentity(); + glClearColor(255.0f / 255.0f, 25.0f / 255.0f, 25.0f / 255.0f, 1.0f); +} + +void Application::error_callback(int error, const char* description) { + std::cerr << "Error: " << description << "." << std::endl; +} + +void Application::resize_callback(GLFWwindow *window, int w, int h) { + // TODO set application's width and height + if (h < 1) + h = 1; + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0f, (float) w / (float) h, 0.1f, 1000.0f); + gluLookAt(0.0f, 0.0f, 30, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); + glMatrixMode(GL_MODELVIEW); +} + +void Application::key_callback( GLFWwindow *window, + int key, + int scancode, + int action, + int mods) { + std::cout << key << " was pressed" << std::endl; + if(key == GLFW_KEY_ESCAPE) + glfwSetWindowShouldClose(window, GLFW_TRUE); +} + + +void Application::mouse_click_callback( GLFWwindow *window, + int button, + int action, + int mods) { + switch (button) { + case GLFW_MOUSE_BUTTON_1: + dragging = (action == GLFW_PRESS); + break; + } +} + + +void Application::mouse_motion_callback(GLFWwindow *window, + double x, + double y) { + printf("mouse"); +} + +void Application::set_callbacks() { + glfwSetErrorCallback( error_callback); + glfwSetWindowSizeCallback( window, resize_callback); + glfwSetKeyCallback( window, key_callback); + glfwSetMouseButtonCallback( window, mouse_click_callback); + glfwSetCursorPosCallback( window, mouse_motion_callback); +} + +void Application::start() { + while(!glfwWindowShouldClose(this->window)) { + this->run(); + glfwSwapBuffers(window); + glfwPollEvents(); + } + glfwTerminate(); +} + +void Application::run() { + // ticking every 24 FPS + bool tick = false; + + double delta_time = glfwGetTime() - time_since_last_frame; + time_since_last_frame = glfwGetTime(); + + if(time_since_last_frame >= 1.0 / 24.0) { + glfwSetTime(0.0f); + time_since_last_frame = 0.0f; + tick = true; + } + + // TODO think about rendering only in 24 FPS + scene->update(delta_time); + //clear color and depth buffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); //load identity matrix + scene->render(); +} diff --git a/src/application.h b/src/application.h new file mode 100644 index 0000000000000000000000000000000000000000..9cebea9b5bb76d0f4ba43001d21f01527d969c53 --- /dev/null +++ b/src/application.h @@ -0,0 +1,49 @@ +#ifndef MADID_APPLICATION_H +#define MADID_APPLICATION_H +#include <GL/glew.h> +#include <GLFW/glfw3.h> +#include <glm/glm.hpp> + +#include <iostream> +#include <memory> + +#include "constants.h" +#include "scene.h" + +// TODO make this class a singleton +class Application { + int keyArr[350]; + int width = WIDTH; + int height = HEIGHT; + + char *name = "Madid"; + + static bool dragging; + // Callback functions + static void error_callback(int error, const char* description); + static void resize_callback(GLFWwindow *window, int w, int h); + static void key_callback(GLFWwindow *window, int key, int scancode, + int action, int mods); + static void mouse_click_callback(GLFWwindow *window, int button, int action, + int mods); + static void mouse_motion_callback(GLFWwindow *window, double x, double y); + + std::shared_ptr<Scene> scene = std::make_shared<Scene>(); + + void run(); + double time_since_last_frame; +public: + Application(); + ~Application(); + + GLFWwindow *window; + + GLFWwindow *get_window(); + void init(); + void set_callbacks(); + + void start(); +}; + + +#endif //MADID_APPLICATION_H diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000000000000000000000000000000000000..feb637e4cbaaaee320528412d6f3f56eaf85ed9c --- /dev/null +++ b/src/constants.h @@ -0,0 +1,9 @@ +#ifndef MADID_CONSTANTS_H +#define MADID_CONSTANTS_H + +static int WIDTH = 500; +static int HEIGHT = 500; + +static char *NAME = "Madid"; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 433f1bce171e9f372317aeec1f89f1de038b26b5..3d6914d38794960c1addfef5d3bb315e5ab11659 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,44 +4,41 @@ #include <iostream> #include <memory> -#include "utility/application.h" +#include "application.h" +#include "constants.h" int main(int argc, char **argv) { - /* Creating a GLFW application */ - std::shared_ptr<Application> application = std::make_shared<Application>() - - glutInit( &argc, argv ); - - OpenTissue::glut::instance_pointer application = init_glut_application(argc,argv); - - OpenTissue::glut::set_application_instance(application); - - glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); - glutInitWindowSize( application->width(), application->height() ); - glutInitWindowPosition( 50, 50 ); - application->main_window() = glutCreateWindow( application->get_title() ); - - application->init_gl_state(); - int main_menu = glutCreateMenu( &OpenTissue::glut::menu ); + // start GL context and O/S window using the GLFW helper library + if (!glfwInit()) { + std::cerr << "ERROR: could not start GLFW3" << std::endl; + return 1; + } - application->init_right_click_menu( main_menu, &OpenTissue::glut::menu ); - - glutSetMenu( main_menu ); - glutAttachMenu( GLUT_RIGHT_BUTTON ); + /* Creating a GLFW application */ + /* Application creates its GLFW window too */ + std::shared_ptr<Application> application = std::make_shared<Application>(); + + if(!application->window) { + std::cerr << "ERROR: could not open window with GLFW3" << std::endl; + glfwTerminate(); + return 1; + } + + glfwMakeContextCurrent(application->get_window()); + + // start GLEW extension handler + glewExperimental = GL_TRUE; + glewInit(); + + // get version info + const GLubyte *renderer = glGetString(GL_RENDERER); // get renderer string + const GLubyte *version = glGetString(GL_VERSION); // version as a string + std::cerr << "Renderer: " << renderer << std::endl; + std::cerr << "OpenGL version supported: " << version << std::endl; application->init(); - - glutDisplayFunc ( &OpenTissue::glut::display ); - glutReshapeFunc ( &OpenTissue::glut::reshape ); - glutMouseFunc ( &OpenTissue::glut::mouse ); - glutMotionFunc ( &OpenTissue::glut::motion ); - glutPassiveMotionFunc( &OpenTissue::glut::motion ); - glutKeyboardFunc( &OpenTissue::glut::key ); - glutSpecialFunc ( &OpenTissue::glut::specialkey ); - - glutMainLoop(); - - application->shutdown(); + + application->start(); } diff --git a/src/scene.cpp b/src/scene.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4838952d1f3e13e02e4da9aef1ed2cf61198f86f --- /dev/null +++ b/src/scene.cpp @@ -0,0 +1,17 @@ +#include "scene.h" + +Scene::Scene() { + std::cout << "Scene built successfully" << std::endl; +} + +Scene::~Scene() { + std::cout << "Scene destroyed" << std::endl; +} + +void Scene::update(double dt) { + return; +} + +void Scene::render() { + return; +} diff --git a/src/scene.h b/src/scene.h new file mode 100644 index 0000000000000000000000000000000000000000..0f845aa5fe94fc473ba760753dcf8a47d5b7daaf --- /dev/null +++ b/src/scene.h @@ -0,0 +1,15 @@ +#ifndef MADID_SCENE_H +#define MADID_SCENE_H + +#include <iostream> + +class Scene { +public: + Scene(); + ~Scene(); + + void update(double dt); + void render(); +}; + +#endif diff --git a/src/utility/application.cpp b/src/utility/application.cpp deleted file mode 100644 index a77bd51e659b7f15ffdb70233a9640b9ad4319cd..0000000000000000000000000000000000000000 --- a/src/utility/application.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "application.h" - -Application::Application(){ - -} - -Application::~Application() { - -} \ No newline at end of file diff --git a/src/utility/application.h b/src/utility/application.h deleted file mode 100644 index 2a1074a90ed1d0c43d8bee55103e5d29f30da44b..0000000000000000000000000000000000000000 --- a/src/utility/application.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MADID_APPLICATION_H -#define MADID_APPLICATION_H -#include <GL/glew.h> -#include <GLFW/glfw3.h> -#include <glm/glm.hpp> - -#include <iostream> - -#include "constants.h" - - -class Application { - bool dragging = false; - int keyArr[350]; - -public: - Application(); - ~Application(); - -}; - - -#endif //MADID_APPLICATION_H diff --git a/src/utility/constants.h b/src/utility/constants.h deleted file mode 100644 index 985b142ccdec2ad3e94978f020973a1fc7d6d663..0000000000000000000000000000000000000000 --- a/src/utility/constants.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef MADID_CONSTANTS_H -#define MADID_CONSTANTS_H - - - -#endif //MADID_CONSTANTS_H