CSCI441 OpenGL Library 6.1.0.0
CS@Mines CSCI441 Computer Graphics Course Library
Loading...
Searching...
No Matches
OpenGLEngine.hpp
Go to the documentation of this file.
1
12#ifndef CSCI441_OPENGL_ENGINE_HPP
13#define CSCI441_OPENGL_ENGINE_HPP
14
15#include "constants.h"
16#include "FontUtils.hpp"
17#include "LogUtils.hpp"
18#include "OpenGLUtils.hpp"
19
20#ifdef CSCI441_USE_GLEW
21 #include <GL/glew.h>
22#else
23 #include <glad/gl.h>
24#endif
25
26#include <GLFW/glfw3.h>
27
28#ifdef CSCI441_OPENGL_ENGINE_IMPLEMENTATION
29#define STB_IMAGE_WRITE_IMPLEMENTATION
30#endif
31#include <stb_image_write.h>
32
33#include <chrono>
34#include <cstdio>
35#include <cstring>
36#include <ctime>
37#include <deque>
38#include <set>
39#include <string>
40#include <memory>
41
42namespace CSCI441 {
43
54 public:
58 OpenGLEngine(const OpenGLEngine&) = delete;
63
67 OpenGLEngine(OpenGLEngine&&) noexcept;
72 OpenGLEngine& operator=(OpenGLEngine&&) noexcept;
73
78 virtual ~OpenGLEngine();
79
87 virtual void initialize();
91 virtual void run() = 0;
99 virtual void shutdown();
100
104 [[maybe_unused]] virtual bool saveScreenshot(const char* FILENAME) noexcept final;
105
110 [[maybe_unused]] virtual void turnDebuggingOn() noexcept final { DEBUG = true; }
115 virtual void turnDebuggingOff() noexcept final { DEBUG = false; }
119 [[maybe_unused]] [[nodiscard]] virtual bool isDebuggingEnabled() const noexcept final { return DEBUG; }
120
126 [[maybe_unused]] [[nodiscard]] virtual bool isExtensionEnabled(const std::string EXT) const noexcept final { return _extensions.find(EXT) != _extensions.end(); }
127
136 virtual void setCurrentWindowSize(const int WINDOW_WIDTH, const int WINDOW_HEIGHT) final { mWindowWidth = WINDOW_WIDTH; mWindowHeight = WINDOW_HEIGHT; }
145 virtual void setCurrentFramebufferSize(const int FRAMEBUFFER_WIDTH, const int FRAMEBUFFER_HEIGHT) final { mFramebufferWidth = FRAMEBUFFER_WIDTH; mFramebufferHeight = FRAMEBUFFER_HEIGHT; }
149 [[maybe_unused]] [[nodiscard]] virtual int getWindowHeight() const noexcept final { return mWindowHeight; }
153 [[maybe_unused]] [[nodiscard]] virtual int getWindowWidth() const noexcept final { return mWindowWidth; }
157 [[maybe_unused]] [[nodiscard]] virtual GLFWwindow* getWindow() const noexcept final { return mpWindow; }
158
162 [[maybe_unused]] virtual void setWindowShouldClose() final { glfwSetWindowShouldClose(mpWindow, GLFW_TRUE); }
163
167 [[nodiscard]] virtual unsigned short getError() noexcept final {
168 const unsigned short storedErrorCode = mErrorCode; // store current error code
171 }
172 mErrorCode = OPENGL_ENGINE_ERROR_NO_ERROR; // reset error code
173 return storedErrorCode; // return previously stored error code
174 }
175
179 static constexpr unsigned short OPENGL_ENGINE_ERROR_NO_ERROR = 0;
183 static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_INIT = 1;
187 static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_WINDOW = 2;
191 static constexpr unsigned short OPENGL_ENGINE_ERROR_GLEW_INIT = 3;
195 static constexpr unsigned short OPENGL_ENGINE_ERROR_GLAD_INIT = 4;
199 static constexpr unsigned short OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT = 5;
203 static constexpr unsigned short OPENGL_ENGINE_ERROR_UNKNOWN = 6;
204 // note to developers: if more error codes are added, need to update LAST accordingly or
205 // update UNKNOWN to the last value and shift
206 // note to developers: if more error codes are added, need to add description to getErrorStringDescription()
211 static constexpr unsigned short OPENGL_ENGINE_ERROR_LAST = OPENGL_ENGINE_ERROR_UNKNOWN;
215 [[maybe_unused]] static constexpr unsigned short OPENGL_ENGINE_ERROR_SIZE = OPENGL_ENGINE_ERROR_LAST + 1;
216
222 static const char* getErrorStringDescription(unsigned short ERROR_CODE) noexcept;
223
224 protected:
237 OpenGLEngine(int OPENGL_MAJOR_VERSION, int OPENGL_MINOR_VERSION, int WINDOW_WIDTH, int WINDOW_HEIGHT, const char* WINDOW_TITLE, bool WINDOW_RESIZABLE = GLFW_FALSE, GLushort NUM_SAMPLES = 0);
238
243 bool DEBUG;
244
248 unsigned int mErrorCode;
249
318 GLFWwindow* mpWindow;
319
326 static void mErrorCallback(const int error, const char* DESCRIPTION) { CSCI441::LogUtils::logError("[ERROR]: %d\n\t%s\n", error, DESCRIPTION ); }
327
331 static void APIENTRY mDebugMessageCallback(const GLenum source, const GLenum type, const GLuint id, const GLenum severity, const GLsizei length, const GLchar* message, const void* userParam) {
332 CSCI441::LogUtils::log("[VERBOSE]: Debug Message (%d): source = %s, type = %s, severity = %s, message = %s\n",
333 id,
334 CSCI441::OpenGLUtils::debugSourceToString(source),
335 CSCI441::OpenGLUtils::debugTypeToString(type),
336 CSCI441::OpenGLUtils::debugSeverityToString(severity),
337 message
338 );
339 }
340
348 static void mWindowResizeCallback(GLFWwindow* pWindow, int width, int height);
349
357 static void mFramebufferResizeCallback(GLFWwindow* pWindow, int width, int height);
358
378 virtual void mSetupGLFW();
385 virtual void mSetupOpenGL() = 0;
392 virtual void mSetupShaders() {};
398 virtual void mSetupBuffers() {};
404 virtual void mSetupTextures() {}
410 virtual void mSetupFonts() {}
416 virtual void mSetupScene() {};
417
422 virtual void mCleanupScene() {};
427 virtual void mCleanupFonts() {}
432 virtual void mCleanupTextures() {}
437 virtual void mCleanupBuffers() {};
442 virtual void mCleanupShaders() {};
447 virtual void mCleanupOpenGL() {};
454 virtual void mCleanupGLFW();
455
460 virtual void mReloadShaders() final;
461
469 virtual GLdouble clockFrame() final;
470
477 virtual GLdouble fpsAverage() final;
478
479 private:
480 void _setupGLFunctions(); // initialize OpenGL functions
481 void _cleanupGLFunctions() {} // nothing to be done at this time
482
483 void _cleanupSelf(); // delete internal memory
484 void _moveFromSource(OpenGLEngine&);// move members from another instance
485
486 bool _isInitialized; // makes initialization a singleton process
487 bool _isCleanedUp; // makes cleanup a singleton process
488
489 std::set< std::string > _extensions;// set of all available OpenGL extensions
490
491 GLdouble _lastFrameTime;
492 std::deque<GLdouble> _frameTimes;
493 GLint _numFrames;
494 };
495}
496
497inline
499 const int OPENGL_MAJOR_VERSION,
500 const int OPENGL_MINOR_VERSION,
501 const int WINDOW_WIDTH,
502 const int WINDOW_HEIGHT,
503 const char* WINDOW_TITLE,
504 const bool WINDOW_RESIZABLE,
505 const GLushort NUM_SAMPLES
506) : DEBUG(true),
507 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
508 mOpenGLMajorVersion(OPENGL_MAJOR_VERSION),
509 mOpenGLMinorVersion(OPENGL_MINOR_VERSION),
510 mOpenGLMajorVersionRequested(OPENGL_MAJOR_VERSION),
511 mOpenGLMinorVersionRequested(OPENGL_MINOR_VERSION),
512 mOpenGLMajorVersionCreated(0),
513 mOpenGLMinorVersionCreated(0),
514 mWindowWidth(WINDOW_WIDTH),
515 mWindowHeight(WINDOW_HEIGHT),
516 mFramebufferWidth(WINDOW_WIDTH),
517 mFramebufferHeight(WINDOW_HEIGHT),
518 mWindowResizable(WINDOW_RESIZABLE),
519 mNumSamplesPerFragment(NUM_SAMPLES),
520 mWindowTitle(nullptr),
521 mpWindow(nullptr),
522 _isInitialized(false),
523 _isCleanedUp(false),
524 _lastFrameTime(0),
525 _numFrames(0)
526{
527 mWindowTitle = new char[ strlen(WINDOW_TITLE) + 1 ];
528 strncpy(mWindowTitle, WINDOW_TITLE, strlen(WINDOW_TITLE) + 1);
529 mWindowTitle[strlen(WINDOW_TITLE)] = '\0';
530
531 CSCI441::FontUtils::setWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
532 CSCI441::FontUtils::setFontSize(1.0f / static_cast<GLfloat>(WINDOW_WIDTH), 1.0f / static_cast<GLfloat>(WINDOW_HEIGHT) );
533}
534
535inline
538) noexcept :
539 DEBUG(true),
540 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
541 mOpenGLMajorVersion(0),
542 mOpenGLMinorVersion(0),
543 mOpenGLMajorVersionRequested(0),
544 mOpenGLMinorVersionRequested(0),
545 mOpenGLMajorVersionCreated(0),
546 mOpenGLMinorVersionCreated(0),
547 mWindowWidth(0),
548 mWindowHeight(0),
549 mFramebufferWidth(0),
550 mFramebufferHeight(0),
551 mWindowResizable(false),
552 mNumSamplesPerFragment(0),
553 mWindowTitle(nullptr),
554 mpWindow(nullptr),
555 _isInitialized(false),
556 _isCleanedUp(false),
557 _lastFrameTime(0),
558 _numFrames(0)
559{
560 _moveFromSource(src);
561}
562
563inline
565 OpenGLEngine&& src
566) noexcept
567{
568 if (this != &src) {
569 _cleanupSelf();
570 _moveFromSource(src);
571 }
572 return *this;
573}
574
575
576inline
578{
579 _cleanupSelf();
580}
581
582inline
583void CSCI441::OpenGLEngine::_cleanupSelf()
584{
585 delete[] mWindowTitle;
586 mWindowTitle = nullptr;
587}
588
589inline
590void CSCI441::OpenGLEngine::_moveFromSource(
591 OpenGLEngine& src
592) {
593 DEBUG = src.DEBUG;
594 src.DEBUG = false;
595
596 mErrorCode = src.mErrorCode;
597 src.mErrorCode = OPENGL_ENGINE_ERROR_NO_ERROR;
598
599 mOpenGLMajorVersion = src.mOpenGLMajorVersion;
600 src.mOpenGLMajorVersion = 0;
601
602 mOpenGLMinorVersion = src.mOpenGLMinorVersion;
603 src.mOpenGLMinorVersion = 0;
604
605 mOpenGLMajorVersionRequested = src.mOpenGLMajorVersionRequested;
606 src.mOpenGLMajorVersionRequested = 0;
607
608 mOpenGLMinorVersionRequested = src.mOpenGLMinorVersionRequested;
609 src.mOpenGLMinorVersionRequested = 0;
610
611 mOpenGLMajorVersionCreated = src.mOpenGLMajorVersionCreated;
612 src.mOpenGLMajorVersionCreated = 0;
613
614 mOpenGLMinorVersionCreated = src.mOpenGLMinorVersionCreated;
615 src.mOpenGLMinorVersionCreated = 0;
616
617 mWindowWidth = src.mWindowWidth;
618 src.mWindowWidth = 0;
619
620 mWindowHeight = src.mWindowHeight;
621 src.mWindowHeight = 0;
622
623 mWindowResizable = src.mWindowResizable;
624 src.mWindowResizable = false;
625
626 mNumSamplesPerFragment = src.mNumSamplesPerFragment;
627 src.mNumSamplesPerFragment = 0;
628
629 mWindowTitle = src.mWindowTitle;
630 src.mWindowTitle = nullptr;
631
632 mpWindow = src.mpWindow;
633 src.mpWindow = nullptr;
634
635 _isInitialized = src._isInitialized;
636 src._isInitialized = false;
637
638 _isCleanedUp = src._isCleanedUp;
639 src._isCleanedUp = false;
640
641 _extensions = std::move(src._extensions);
642}
643
644inline
646{
647 if( !_isInitialized ) {
648 const std::chrono::steady_clock::time_point initBegin = std::chrono::steady_clock::now();
649
650 if (DEBUG) {
651 CSCI441::LogUtils::log("[INFO]: Using CSCI441 Library v%d.%d.%d.%d\n", CSCI441::VERSION_MAJOR, CSCI441::VERSION_MINOR, CSCI441::VERSION_REVISION, CSCI441::VERSION_PATCH);
652 }
653
654 const std::chrono::steady_clock::time_point glfwBegin = std::chrono::steady_clock::now();
655 mSetupGLFW(); // initialize GLFW and set up a window
656 glfwGetFramebufferSize( mpWindow, &mFramebufferWidth, &mFramebufferHeight );
657 const std::chrono::steady_clock::time_point glfwEnd = std::chrono::steady_clock::now();
658
659 const std::chrono::steady_clock::time_point openGLBegin = std::chrono::steady_clock::now();
660 _setupGLFunctions(); // create OpenGL function pointers
661 mSetupOpenGL(); // create the OpenGL context
662
663 // enable/disable multisampling as appropriate
664 if (mNumSamplesPerFragment > 0) {
665 glEnable(GL_MULTISAMPLE);
666 } else {
667 glDisable(GL_MULTISAMPLE);
668 }
669
670 // get OpenGL context information
671 if( DEBUG ) {
672 // if wanting debug information with Version 4.3 or higher
673 if( mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3) ) {
674 // check if debug context was created
675 int flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
676 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
677 // register callback to synchronously print any debug messages without having to call glGetError()
678 glEnable(GL_DEBUG_OUTPUT);
679 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
680 glDebugMessageCallback(mDebugMessageCallback, nullptr);
681 glDebugMessageControl( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE );
682 }
683 }
684
685 CSCI441::OpenGLUtils::printOpenGLInfo();
686 }
687 const std::chrono::steady_clock::time_point openGLEnd = std::chrono::steady_clock::now();
688 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupOpenGL()");
689
690 const std::chrono::steady_clock::time_point shadersBegin = std::chrono::steady_clock::now();
691 mSetupShaders(); // transfer, compile, link shaders on GPU
692 const std::chrono::steady_clock::time_point shadersEnd = std::chrono::steady_clock::now();
693 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupShaders()");
694
695 const std::chrono::steady_clock::time_point buffersBegin = std::chrono::steady_clock::now();
696 mSetupBuffers(); // register Buffers on GPU
697 const std::chrono::steady_clock::time_point buffersEnd = std::chrono::steady_clock::now();
698 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupBuffers()");
699
700 const std::chrono::steady_clock::time_point texturesBegin = std::chrono::steady_clock::now();
701 mSetupTextures(); // register Textures on GPU
702 const std::chrono::steady_clock::time_point texturesEnd = std::chrono::steady_clock::now();
703 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupTextures()");
704
705 const std::chrono::steady_clock::time_point fontsBegin = std::chrono::steady_clock::now();
706 mSetupFonts(); // register Fonts on GPU
707 const std::chrono::steady_clock::time_point fontsEnd = std::chrono::steady_clock::now();
708 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupFonts()");
709
710 const std::chrono::steady_clock::time_point sceneBegin = std::chrono::steady_clock::now();
711 mSetupScene(); // setup any scene specific information
712 const std::chrono::steady_clock::time_point sceneEnd = std::chrono::steady_clock::now();
713 CSCI441::OpenGLUtils::checkOpenGLErrors("post mSetupScene()");
714
715 _isInitialized = true;
716 _isCleanedUp = false;
717
718 const std::chrono::steady_clock::time_point initEnd = std::chrono::steady_clock::now();
719 if (DEBUG) {
720 CSCI441::LogUtils::log("\n[INFO]: Setup complete\n");
721 CSCI441::LogUtils::log("[INFO]: Init Time: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(initEnd - initBegin ).count() ) / 1000000.0);
722 CSCI441::LogUtils::log("[INFO]: \tGLFW: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(glfwEnd - glfwBegin ).count() ) / 1000000.0);
723 CSCI441::LogUtils::log("[INFO]: \tOpenGL: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(openGLEnd - openGLBegin ).count() ) / 1000000.0);
724 CSCI441::LogUtils::log("[INFO]: \tShaders: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(shadersEnd - shadersBegin ).count() ) / 1000000.0);
725 CSCI441::LogUtils::log("[INFO]: \tBuffers: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(buffersEnd - buffersBegin ).count() ) / 1000000.0);
726 CSCI441::LogUtils::log("[INFO]: \tTextures: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(texturesEnd - texturesBegin).count() ) / 1000000.0);
727 CSCI441::LogUtils::log("[INFO]: \tFonts: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(fontsEnd - fontsBegin ).count() ) / 1000000.0);
728 CSCI441::LogUtils::log("[INFO]: \tScene: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(sceneEnd - sceneBegin ).count() ) / 1000000.0);
729 }
730 }
731}
732
733inline
735 const unsigned short ERROR_CODE
736) noexcept {
737 switch (ERROR_CODE) {
738 case OPENGL_ENGINE_ERROR_NO_ERROR: return "No error";
739 case OPENGL_ENGINE_ERROR_GLFW_INIT: return "Error initializing GLFW";
740 case OPENGL_ENGINE_ERROR_GLFW_WINDOW: return "Error initializing GLFW window";
741 case OPENGL_ENGINE_ERROR_GLEW_INIT: return "Error initializing GLEW";
742 case OPENGL_ENGINE_ERROR_GLAD_INIT: return "Error initializing GLAD";
743 case OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT: return "Error saving screenshot";
744 default: return "Unknown error code";
745 }
746}
747
748inline
750 const char* FILENAME
751) noexcept {
752 // Generate a name based on current timestamp if not provided
753 const std::string filename =
754 (
755 FILENAME == nullptr
756 ? "Screenshot_" + std::to_string(time(nullptr)) + ".png"
757 : FILENAME
758 );
759
760 // Get size
761 GLint viewport[4];
762 glGetIntegerv(GL_VIEWPORT, viewport);
763 const GLsizei x = viewport[0], y = viewport[1], width = viewport[2], height = viewport[3];
764
765 // Read pixel data
766 constexpr int CHANNELS = 4; // RGBA
767 const auto bytes = new GLubyte[width*height*CHANNELS];
768 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, bytes);
769
770 stbi_flip_vertically_on_write(true);
771
772 if( !stbi_write_png(filename.c_str(), width, height, CHANNELS, bytes, width*CHANNELS) ) {
773 CSCI441::LogUtils::logError("[ERROR]: Could not save screenshot\n");
774 mErrorCode = OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT;
775 } else if(DEBUG) {
776 CSCI441::LogUtils::log("[INFO]: Screenshot saved to %s\n", filename.c_str());
777 }
778
779 delete[] bytes;
780
781 return (mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR);
782}
783
784inline
786{
787 // set what function to use when registering errors
788 // this is the ONLY GLFW function that can be called BEFORE GLFW is initialized
789 // all other GLFW calls must be performed after GLFW has been initialized
790 glfwSetErrorCallback(mErrorCallback);
791
792 // initialize GLFW
793 if( !glfwInit() ) {
794 CSCI441::LogUtils::logError("[ERROR]: Could not initialize GLFW\n" );
795 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_INIT;
796 } else {
797 if(DEBUG) CSCI441::LogUtils::log("[INFO]: GLFW v%d.%d.%d initialized\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION );
798
799 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, mOpenGLMajorVersion ); // request OpenGL vX.
800 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, mOpenGLMinorVersion ); // request OpenGL v .X
801 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); // request forward compatible OpenGL context
802 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); // request OpenGL Core Profile context
803 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE ); // request double buffering
804 glfwWindowHint(GLFW_RESIZABLE, mWindowResizable ); // set if our window should be able to be resized
805 glfwWindowHint(GLFW_SAMPLES, mNumSamplesPerFragment); // request number of samples per fragment
806
807 // if wanting debug information with Version 4.3 or higher
808 if( DEBUG
809 && (mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3)) ) {
810 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); // request a debug context
811 }
812
813 // create a window for a given size, with a given title
814 mpWindow = glfwCreateWindow(mWindowWidth, mWindowHeight, mWindowTitle, nullptr, nullptr );
815 if( !mpWindow ) { // if the window could not be created, NULL is returned
816 CSCI441::LogUtils::logError("[ERROR]: GLFW Window could not be created\n" );
817 glfwTerminate();
818 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_WINDOW;
819 } else {
820 if(DEBUG) CSCI441::LogUtils::log("[INFO]: GLFW Window created\n" );
821 glfwMakeContextCurrent(mpWindow); // make the created window the current window
822 glfwSwapInterval(1); // update our screen after at least 1 screen refresh
823 glfwSetInputMode(mpWindow, GLFW_LOCK_KEY_MODS, GLFW_TRUE); // track state of Caps Lock and Num Lock keys
824 glfwSetWindowUserPointer(mpWindow, (void*)this);
825 glfwSetWindowSizeCallback(mpWindow, mWindowResizeCallback);
826 glfwSetFramebufferSizeCallback(mpWindow, mWindowResizeCallback);
827 }
828 }
829}
830
831inline
832void CSCI441::OpenGLEngine::_setupGLFunctions()
833{
834
835#ifdef CSCI441_USE_GLEW
836 glewExperimental = GL_TRUE;
837 const GLenum glewResult = glewInit(); // initialize GLEW
838
839 // check for an error
840 if( glewResult != GLEW_OK ) {
841 CSCI441::LogUtils::logError("[ERROR]: Error initializing GLEW\n");
842 CSCI441::LogUtils::logError("[ERROR]: %s\n", reinterpret_cast<const char *>(glewGetErrorString(glewResult)) );
843 mErrorCode = OPENGL_ENGINE_ERROR_GLEW_INIT;
844 } else {
845 if(DEBUG) {
846 CSCI441::LogUtils::log("[INFO]: GLEW initialized\n");
847 CSCI441::LogUtils::log("[INFO]: Using GLEW %s\n", reinterpret_cast<const char *>(glewGetString(GLEW_VERSION)));
848 }
849 }
850#else
851 int version = gladLoadGL(glfwGetProcAddress);
852 if(version == 0) {
853 CSCI441::LogUtils::logError("[ERROR]: Failed to initialize GLAD\n" );
854 mErrorCode = OPENGL_ENGINE_ERROR_GLAD_INIT;
855 } else {
856 if(DEBUG) {
857 // Successfully loaded OpenGL
858 CSCI441::LogUtils::log("[INFO]: GLAD initialized v%d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
859 }
860 }
861#endif
862
863 if(mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR) {
864 glGetIntegerv(GL_MAJOR_VERSION, &mOpenGLMajorVersionCreated);
865 glGetIntegerv(GL_MINOR_VERSION, &mOpenGLMinorVersionCreated);
866 if(DEBUG) {
867 CSCI441::LogUtils::log("[INFO]: OpenGL v%d.%d requested\n", mOpenGLMajorVersionRequested, mOpenGLMinorVersionRequested);
868 CSCI441::LogUtils::log("[INFO]: OpenGL v%d.%d created\n", mOpenGLMajorVersionCreated, mOpenGLMinorVersionCreated);
869 }
870
871 GLint numExtensions = 0;
872 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
873 for (int i = 0; i < numExtensions; i++) {
874 _extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)) );
875 }
876 }
877}
878
879inline
881{
882 if(DEBUG) CSCI441::LogUtils::log("[INFO]: ...closing window...\n" );
883 glfwDestroyWindow(mpWindow ); // close our window
884 mpWindow = nullptr;
885 if(DEBUG) CSCI441::LogUtils::log("[INFO]: ...closing GLFW.....\n" );
886 glfwTerminate();
887}
888
889inline
891{
892 if( !_isCleanedUp ) {
893 const std::chrono::steady_clock::time_point shutdownBegin = std::chrono::steady_clock::now();
894 if (DEBUG) CSCI441::LogUtils::log("\n[INFO]: Shutting down.......\n");
895
896 const std::chrono::steady_clock::time_point sceneBegin = std::chrono::steady_clock::now();
897 mCleanupScene(); // delete scene info from CPU
898 const std::chrono::steady_clock::time_point sceneEnd = std::chrono::steady_clock::now();
899 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupScene()");
900
901 const std::chrono::steady_clock::time_point fontsBegin = std::chrono::steady_clock::now();
902 mCleanupFonts(); // delete textures from GPU
903 const std::chrono::steady_clock::time_point fontsEnd = std::chrono::steady_clock::now();
904 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupFonts()");
905
906 const std::chrono::steady_clock::time_point texturesBegin = std::chrono::steady_clock::now();
907 mCleanupTextures(); // delete textures from GPU
908 const std::chrono::steady_clock::time_point texturesEnd = std::chrono::steady_clock::now();
909 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupTextures()");
910
911 const std::chrono::steady_clock::time_point buffersBegin = std::chrono::steady_clock::now();
912 mCleanupBuffers(); // delete VAOs/VBOs from GPU
913 const std::chrono::steady_clock::time_point buffersEnd = std::chrono::steady_clock::now();
914 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupBuffers()");
915
916 const std::chrono::steady_clock::time_point shadersBegin = std::chrono::steady_clock::now();
917 mCleanupShaders(); // delete shaders from GPU
918 const std::chrono::steady_clock::time_point shadersEnd = std::chrono::steady_clock::now();
919 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupShaders()");
920
921 const std::chrono::steady_clock::time_point openGLBegin = std::chrono::steady_clock::now();
922 mCleanupOpenGL(); // cleanup anything OpenGL related
923 _cleanupGLFunctions(); // cleanup anything function pointer related
924 const std::chrono::steady_clock::time_point openGLEnd = std::chrono::steady_clock::now();
925 CSCI441::OpenGLUtils::checkOpenGLErrors("post mCleanupOpenGL()");
926
927 const std::chrono::steady_clock::time_point glfwBegin = std::chrono::steady_clock::now();
928 mCleanupGLFW(); // shut down GLFW to clean up our context
929 const std::chrono::steady_clock::time_point glfwEnd = std::chrono::steady_clock::now();
930
931 const std::chrono::steady_clock::time_point shutdownEnd = std::chrono::steady_clock::now();
932 if (DEBUG) {
933 CSCI441::LogUtils::log("[INFO]: ..shut down complete!\n");
934 CSCI441::LogUtils::log("[INFO]: Shutdown: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(shutdownEnd - shutdownBegin).count() ) / 1000000.0);
935 CSCI441::LogUtils::log("[INFO]: \tScene: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(sceneEnd - sceneBegin ).count() ) / 1000000.0);
936 CSCI441::LogUtils::log("[INFO]: \tFonts: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(fontsEnd - fontsBegin ).count() ) / 1000000.0);
937 CSCI441::LogUtils::log("[INFO]: \tTextures: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(texturesEnd - texturesBegin).count() ) / 1000000.0);
938 CSCI441::LogUtils::log("[INFO]: \tBuffers: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(buffersEnd - buffersBegin ).count() ) / 1000000.0);
939 CSCI441::LogUtils::log("[INFO]: \tShaders: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(shadersEnd - shadersBegin ).count() ) / 1000000.0);
940 CSCI441::LogUtils::log("[INFO]: \tOpenGL: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(openGLEnd - openGLBegin ).count() ) / 1000000.0);
941 CSCI441::LogUtils::log("[INFO]: \tGLFW: %2.3fs\n", static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>(glfwEnd - glfwBegin ).count() ) / 1000000.0);
942 }
943 _isCleanedUp = true;
944 _isInitialized = false;
945 }
946}
947
948inline
950 GLFWwindow* pWindow,
951 const int width,
952 const int height
953) {
954 const auto pEngine = static_cast<OpenGLEngine *>(glfwGetWindowUserPointer(pWindow));
955 pEngine->setCurrentWindowSize(width, height);
957 CSCI441::FontUtils::setFontSize(1.0f / static_cast<GLfloat>(width), 1.0f / static_cast<GLfloat>(height) );
958}
959
960inline
962 GLFWwindow* pWindow,
963 const int width,
964 const int height
965) {
966 const auto pEngine = static_cast<OpenGLEngine *>(glfwGetWindowUserPointer(pWindow));
967 pEngine->setCurrentFramebufferSize(width, height);
968}
969
970inline
972{
973 if (DEBUG) CSCI441::LogUtils::log("\n[INFO]: Removing old shaders...\n");
974 mCleanupShaders();
975 if (DEBUG) CSCI441::LogUtils::log("\n[INFO]: Reloading shaders...\n");
976 mSetupShaders();
977 if (DEBUG) CSCI441::LogUtils::log("\n[INFO]: Shaders reloaded\n");
978}
979
980inline
982 const GLdouble currentTime = glfwGetTime();
983 ++_numFrames;
984 GLdouble fps = ( !_frameTimes.empty() ? _frameTimes.back() : 0.0 );
985 if (currentTime - _lastFrameTime > 0.1667) {
986 fps = static_cast<GLdouble>(_numFrames)/(currentTime - _lastFrameTime);
987 _numFrames = 0;
988 _lastFrameTime = currentTime;
989
990 if (_frameTimes.size() >= 10) _frameTimes.pop_front();
991 _frameTimes.push_back( fps );
992 }
993 return fps;
994}
995
996inline
998 GLdouble totalFPS = 0;
999 for( const auto& fpsUnit : _frameTimes ) {
1000 totalFPS += fpsUnit;
1001 }
1002 return totalFPS / static_cast<GLdouble>(_frameTimes.size());
1003}
1004
1005#endif //CSCI441_OPENGL_ENGINE_HPP
Helper functions to work with OpenGL 3.0+.
Abstract Class to run an OpenGL application. The following methods must be overridden:
Definition: OpenGLEngine.hpp:53
virtual void mSetupOpenGL()=0
override to enable specific OpenGL features
virtual bool isExtensionEnabled(const std::string EXT) const noexcept final
Returns if OpenGL extension exists.
Definition: OpenGLEngine.hpp:126
bool DEBUG
if information should be printed to console while running
Definition: OpenGLEngine.hpp:243
OpenGLEngine & operator=(const OpenGLEngine &)=delete
do not allow engines to be copied
int mFramebufferWidth
the framebuffer height of the requested GLFW window
Definition: OpenGLEngine.hpp:295
int mOpenGLMinorVersion
the minor version of the requested OpenGL context
Definition: OpenGLEngine.hpp:259
int mOpenGLMajorVersion
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:254
static constexpr unsigned short OPENGL_ENGINE_ERROR_NO_ERROR
no error is present, everything is currently working
Definition: OpenGLEngine.hpp:179
virtual bool saveScreenshot(const char *FILENAME) noexcept final
Save a PNG screenshot of the viewport.
Definition: OpenGLEngine.hpp:749
virtual void setCurrentWindowSize(const int WINDOW_WIDTH, const int WINDOW_HEIGHT) final
Stores the new window size.
Definition: OpenGLEngine.hpp:136
virtual void initialize()
Initialize everything needed for OpenGL Rendering. This includes in order: GLFW, function pointers,...
Definition: OpenGLEngine.hpp:645
virtual void shutdown()
Cleanup everything needed for OpenGL Rendering. This includes freeing memory for data used in: any Sc...
Definition: OpenGLEngine.hpp:890
virtual unsigned short getError() noexcept final
Return current value of error code and clear the error code back to no error.
Definition: OpenGLEngine.hpp:167
virtual void mSetupTextures()
override to register any textures with the GPU
Definition: OpenGLEngine.hpp:404
virtual int getWindowHeight() const noexcept final
Return the height of the window.
Definition: OpenGLEngine.hpp:149
virtual bool isDebuggingEnabled() const noexcept final
Returns if logging is enabled.
Definition: OpenGLEngine.hpp:119
static void mWindowResizeCallback(GLFWwindow *pWindow, int width, int height)
Definition: OpenGLEngine.hpp:949
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLAD_INIT
an error occurred while initializing GLAD
Definition: OpenGLEngine.hpp:195
virtual void mCleanupBuffers()
override to cleanup any buffer objects from the GPU
Definition: OpenGLEngine.hpp:437
virtual void mCleanupScene()
override to cleanup any scene specific information
Definition: OpenGLEngine.hpp:422
virtual void mSetupFonts()
override to register any fonts with the GPU
Definition: OpenGLEngine.hpp:410
virtual void setCurrentFramebufferSize(const int FRAMEBUFFER_WIDTH, const int FRAMEBUFFER_HEIGHT) final
Stores the framebuffer size.
Definition: OpenGLEngine.hpp:145
virtual void mCleanupShaders()
override to cleanup any shaders from the GPU
Definition: OpenGLEngine.hpp:442
virtual void mCleanupTextures()
override to cleanup any textures from the GPU
Definition: OpenGLEngine.hpp:432
virtual GLdouble clockFrame() final
measures the amount of time elapsed since the last time this method was called
Definition: OpenGLEngine.hpp:981
bool mWindowResizable
if the GLFW window can be resized while open
Definition: OpenGLEngine.hpp:304
int mOpenGLMinorVersionRequested
the minor version of the requested OpenGL context
Definition: OpenGLEngine.hpp:271
GLushort mNumSamplesPerFragment
how many samples to fire per fragment
Definition: OpenGLEngine.hpp:310
OpenGLEngine(const OpenGLEngine &)=delete
do not allow engines to be copied
static constexpr unsigned short OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT
an error occurred while taking a screenshot
Definition: OpenGLEngine.hpp:199
int mFramebufferHeight
the framebuffer width of the requested GLFW window
Definition: OpenGLEngine.hpp:299
virtual void turnDebuggingOn() noexcept final
Enable logging to command line.
Definition: OpenGLEngine.hpp:110
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLEW_INIT
an error occurred while initializing GLEW
Definition: OpenGLEngine.hpp:191
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_WINDOW
an error occurred while creating the GLFW window
Definition: OpenGLEngine.hpp:187
virtual void mCleanupFonts()
override to cleanup any fonts from the GPU
Definition: OpenGLEngine.hpp:427
int mOpenGLMajorVersionRequested
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:265
static void mErrorCallback(const int error, const char *DESCRIPTION)
We will register this function as GLFW's error callback. When an error within OpenGL occurs,...
Definition: OpenGLEngine.hpp:326
static constexpr unsigned short OPENGL_ENGINE_ERROR_LAST
stores the error code number of the last possible error, this corresponds to the max error code value...
Definition: OpenGLEngine.hpp:211
unsigned int mErrorCode
tracks the current status of the OpenGL engine via error codes
Definition: OpenGLEngine.hpp:248
virtual void run()=0
Initiate the draw loop.
static void APIENTRY mDebugMessageCallback(const GLenum source, const GLenum type, const GLuint id, const GLenum severity, const GLsizei length, const GLchar *message, const void *userParam)
callback called whenever a debug message is signaled
Definition: OpenGLEngine.hpp:331
static void mFramebufferResizeCallback(GLFWwindow *pWindow, int width, int height)
Definition: OpenGLEngine.hpp:961
char * mWindowTitle
the title of the GLFW window
Definition: OpenGLEngine.hpp:314
int mWindowHeight
the window height of the requested GLFW window
Definition: OpenGLEngine.hpp:291
virtual GLdouble fpsAverage() final
Definition: OpenGLEngine.hpp:997
virtual void setWindowShouldClose() final
Tell our engine's window to close.
Definition: OpenGLEngine.hpp:162
int mOpenGLMajorVersionCreated
the major version of the created OpenGL context
Definition: OpenGLEngine.hpp:276
static constexpr unsigned short OPENGL_ENGINE_ERROR_SIZE
stores the number of unique error codes that can be generated
Definition: OpenGLEngine.hpp:215
virtual void mReloadShaders() final
calls mCleanupShaders() followed by mSetupShaders() to reload shader source code from file
Definition: OpenGLEngine.hpp:971
virtual void mSetupShaders()
override to register any shaders with the GPU
Definition: OpenGLEngine.hpp:392
GLFWwindow * mpWindow
pointer to the GLFW window object
Definition: OpenGLEngine.hpp:318
virtual void turnDebuggingOff() noexcept final
Disable logging to command line.
Definition: OpenGLEngine.hpp:115
static const char * getErrorStringDescription(unsigned short ERROR_CODE) noexcept
Returns a string describing what the error code corresponds to.
Definition: OpenGLEngine.hpp:734
virtual int getWindowWidth() const noexcept final
Return the width of the window.
Definition: OpenGLEngine.hpp:153
virtual void mSetupBuffers()
override to register any buffer objects with the GPU
Definition: OpenGLEngine.hpp:398
virtual ~OpenGLEngine()
cleans up our OpenGL Engine by destroying the OpenGL context, GLFW window, and cleaning up all GPU re...
Definition: OpenGLEngine.hpp:577
virtual void mCleanupGLFW()
Destroys the associated GLFW window and terminates the GLFW instance.
Definition: OpenGLEngine.hpp:880
int mOpenGLMinorVersionCreated
the minor version of the created OpenGL context
Definition: OpenGLEngine.hpp:281
int mWindowWidth
the window width of the requested GLFW window
Definition: OpenGLEngine.hpp:286
virtual void mCleanupOpenGL()
override to cleanup any specific OpenGL features
Definition: OpenGLEngine.hpp:447
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_INIT
an error occurred while initializing GLFW
Definition: OpenGLEngine.hpp:183
virtual void mSetupGLFW()
Used to setup everything GLFW related. This includes the OpenGL context and our window....
Definition: OpenGLEngine.hpp:785
virtual void mSetupScene()
override to setup any scene specific information
Definition: OpenGLEngine.hpp:416
static constexpr unsigned short OPENGL_ENGINE_ERROR_UNKNOWN
a new error that does not correspond to a predefined scenario has occurred
Definition: OpenGLEngine.hpp:203
virtual GLFWwindow * getWindow() const noexcept final
Return the window object.
Definition: OpenGLEngine.hpp:157
void setFontSize(GLfloat scaleX, GLfloat scaleY)
set the amount to scale font when drawing
Definition: FontUtils.hpp:180
void setWindowSize(GLint width, GLint height)
store the size of the window
Definition: FontUtils.hpp:172
void log(const char *MSG,...)
log a message to both the standard output stream and file
Definition: LogUtils.hpp:116
void logError(const char *MSG,...)
log a message to both the standard error stream and file
Definition: LogUtils.hpp:128
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17