diff --git a/CMakeLists.txt b/CMakeLists.txt index f6d55b0..03d97a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,9 +18,7 @@ add_executable(tests_opengl src/DataHandling/Texture.h src/DataHandling/Model3D.cpp src/DataHandling/Model3D.h - src/types.h - src/KeyStateManager.cpp - src/KeyStateManager.h) + src/types.h) target_link_directories(tests_opengl PRIVATE src) diff --git a/README.md b/README.md deleted file mode 100644 index 5a4de64..0000000 --- a/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# OpenGL course - -The goal of the course is to introduce us to the OpenGL render pipeline and how to use the library. - -The code here was, in part, inspired by the code which was given as part of the course but could not run on windows. -It is aimed to replace it and serve as a base for my own project as part of the course. - -## TODOs - - - [ ] Camera movement - - [ ] Entities - - [ ] Quaternion computations? Maybe use OpenGLMathematics - - [ ] Loading other file types - -## Dependencies - -The following libraries must be installed and findable by CMake: - - OpenGL - - Freeglut 3 \ No newline at end of file diff --git a/src/DataHandling/Model3D.cpp b/src/DataHandling/Model3D.cpp index b46e711..185eb13 100644 --- a/src/DataHandling/Model3D.cpp +++ b/src/DataHandling/Model3D.cpp @@ -55,24 +55,36 @@ void Model3D::draw_model() { Vec3i face = faces[i]; - Vec3i* face_texture; + Vec3i face_texture; if (is_textured) { - face_texture = &face_textures[i]; + face_texture = face_textures[i]; } - // Draw each vertex and texture it if needed - for (int j = 0; j < 3; j++) + // FIXME : Find a better way to draw the three vertices + Vec3f vertex = vertices[face.x]; + if (is_textured) { - Vec3f& vertex = vertices[face.coordinates[j]]; - - if (is_textured) - { - Vec2f vertex_texture = texture_coordinates[face_texture->coordinates[j]]; - glTexCoord2f(vertex_texture.x,vertex_texture.y); - } - glVertex3f(vertex.x,vertex.y,vertex.z); + Vec2f vertex_texture = texture_coordinates[face_texture.x]; + glTexCoord2f(vertex_texture.x,vertex_texture.y); } + glVertex3f(vertex.x,vertex.y,vertex.z); + + vertex = vertices[face.y]; + if (is_textured) + { + Vec2f vertex_texture = texture_coordinates[face_texture.y]; + glTexCoord2f(vertex_texture.x,vertex_texture.y); + } + glVertex3f(vertex.x,vertex.y,vertex.z); + + vertex = vertices[face.z]; + if (is_textured) + { + Vec2f vertex_texture = texture_coordinates[face_texture.z]; + glTexCoord2f(vertex_texture.x,vertex_texture.y); + } + glVertex3f(vertex.x,vertex.y,vertex.z); } glEnd(); diff --git a/src/KeyStateManager.cpp b/src/KeyStateManager.cpp deleted file mode 100644 index 788ab67..0000000 --- a/src/KeyStateManager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// -// Created by trotfunky on 07/10/2019. -// - -#include "KeyStateManager.h" -#include - -std::map KeyStateManager::ascii_keys_status = {}; -std::map KeyStateManager::special_keys_status = {}; - -void KeyStateManager::register_glut_callbacks() -{ - glutKeyboardFunc(KeyStateManager::key_press); - glutKeyboardUpFunc(KeyStateManager::key_up); - glutSpecialFunc(KeyStateManager::special_key_press); - glutSpecialUpFunc(KeyStateManager::special_key_up); -} - -bool KeyStateManager::is_key_pressed(unsigned char key) -{ - if (ascii_keys_status.find(key) == ascii_keys_status.end()) - { - return false; - } - return ascii_keys_status.at(key); -} - -bool KeyStateManager::is_special_key_pressed(int key) -{ - if (special_keys_status.find(key) == special_keys_status.end()) - { - return false; - } - return special_keys_status.at(key); -} - -void KeyStateManager::key_press(unsigned char event_key, int mouse_x, int mouse_y) -{ - update_key(event_key,true); -} - -void KeyStateManager::key_up(unsigned char event_key, int mouse_x, int mouse_y) -{ - update_key(event_key,false); -} - -void KeyStateManager::special_key_press(int event_key, int mouse_x, int mouse_y) -{ - update_special_key(event_key,true); -} - -void KeyStateManager::special_key_up(int event_key, int mouse_x, int mouse_y) -{ - update_special_key(event_key,false); -} - -void KeyStateManager::update_key(unsigned char event_key, bool new_status) -{ - if (ascii_keys_status.find(event_key) != ascii_keys_status.end()) - { - ascii_keys_status.at(event_key) = new_status; - } - else - { - ascii_keys_status.insert({event_key,new_status}); - } -} - -void KeyStateManager::update_special_key(int event_key, bool new_status) -{ - if (special_keys_status.find(event_key) != special_keys_status.end()) - { - special_keys_status.at(event_key) = new_status; - } - else - { - special_keys_status.insert({event_key,new_status}); - } -} diff --git a/src/KeyStateManager.h b/src/KeyStateManager.h deleted file mode 100644 index cb0693a..0000000 --- a/src/KeyStateManager.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// Created by trotfunky on 07/10/2019. -// - -#ifndef TESTS_OPENGL_KEYSTATEMANAGER_H -#define TESTS_OPENGL_KEYSTATEMANAGER_H - -#include -#include - - -/// Handles the key events from glut and keep the status of keys up to date. -/// "Static class" -class KeyStateManager { -public: - KeyStateManager() = delete; - - static void register_glut_callbacks(); - - // FIXME: Not happy with having two functions : char auto promoted to int if same name - static bool is_key_pressed(unsigned char key); - static bool is_special_key_pressed(int key); - - // Glut callbacks for input events - static void key_press(unsigned char event_key, int mouse_x, int mouse_y); - static void key_up(unsigned char event_key, int mouse_x, int mouse_y); - static void special_key_press(int event_key, int mouse_x, int mouse_y); - static void special_key_up(int event_key, int mouse_x, int mouse_y); - -private: - // The maps are used to keep track of the keys which were pressed or released during runtime. - static std::map ascii_keys_status; - static std::map special_keys_status; - - // These functions are called by *_press and *_up to update their values - static void update_key(unsigned char event_key, bool new_status); - static void update_special_key(int event_key, bool new_status); -}; - - -#endif //TESTS_OPENGL_KEYSTATEMANAGER_H diff --git a/src/main.cpp b/src/main.cpp index 0ce588c..e3a126b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,38 +4,15 @@ #include "displayers.h" #include "DataHandling/Texture.h" #include "DataHandling/Model3D.h" -#include "KeyStateManager.h" + volatile unsigned long long int timer_ticks = 0; static Texture tree_texture; static Model3D raptor; static Texture raptor_texture; -void manage_inputs() -{ - if (KeyStateManager::is_key_pressed(' ')) - { - glClearColor(0.5,0,0,1); - } - else - { - glClearColor(0,0,0,1); - } - - if (KeyStateManager::is_special_key_pressed(GLUT_KEY_RIGHT)) - { - timer_ticks += 5; - } - if (KeyStateManager::is_special_key_pressed(GLUT_KEY_LEFT)) - { - timer_ticks -= 5; - } -} - void display() { - manage_inputs(); - float angleY = 1*timer_ticks; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -77,7 +54,6 @@ int main(int argc, char** argv) // Generate window with GLUT glutInitDisplayMode(GLUT_RGB| GLUT_DEPTH | GLUT_DOUBLE); glutCreateWindow("Hello"); - glutIgnoreKeyRepeat(true); // Init OpenGL glClearColor(0,0,0,1); @@ -120,7 +96,6 @@ int main(int argc, char** argv) glutIdleFunc(display); glutTimerFunc(50,update_angle,0); - KeyStateManager::register_glut_callbacks(); // Enters main loop, managed by GLUT glutMainLoop(); diff --git a/src/types.h b/src/types.h index 6c3349e..e052454 100644 --- a/src/types.h +++ b/src/types.h @@ -7,33 +7,14 @@ #include -/// Group of coordinates with the input operator overloaded. -/// \tparam T Content of the vector -/// \tparam N Number of coordinates -template -struct CoordinatesVector -{ - T coordinates[N]; - - friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vector) - { - for (unsigned int i = 0;i> vector.coordinates[i]; - } - return stream; - } -}; - template -struct CoordinatesVector +struct Vec2 { - T coordinates[2]; - T& x = coordinates[0]; - T& y = coordinates[1]; + T x; + T y; - friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vec2) + friend std::fstream& operator>>(std::fstream& stream, Vec2& vec2) { stream >> vec2.x >> vec2.y; return stream; @@ -41,15 +22,13 @@ struct CoordinatesVector }; template -struct CoordinatesVector +struct Vec3 { - T coordinates[3]; + T x; + T y; + T z; - T& x = coordinates[0]; - T& y = coordinates[1]; - T& z = coordinates[2]; - - friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector& vec3) + friend std::fstream& operator>>(std::fstream& stream, Vec3& vec3) { stream >> vec3.x >> vec3.y >> vec3.z; return stream; @@ -58,11 +37,11 @@ struct CoordinatesVector // Define aliases for common types -typedef CoordinatesVector Vec2i; -typedef CoordinatesVector Vec2f; +typedef Vec2 Vec2i; +typedef Vec2 Vec2f; -typedef CoordinatesVector Vec3i; -typedef CoordinatesVector Vec3f; +typedef Vec3 Vec3i; +typedef Vec3 Vec3f; #endif //TESTS_OPENGL_TYPES_H