12#ifndef CSCI441_OPENGL_ENGINE_HPP
13#define CSCI441_OPENGL_ENGINE_HPP
18#ifdef CSCI441_USE_GLEW
24#include <GLFW/glfw3.h>
26#ifdef CSCI441_OPENGL_ENGINE_IMPLEMENTATION
27#define STB_IMAGE_WRITE_IMPLEMENTATION
29#include <stb_image_write.h>
87 virtual
void run() = 0;
100 [[maybe_unused]] virtual
bool saveScreenshot(const
char* FILENAME) noexcept final;
122 [[maybe_unused]] [[nodiscard]]
virtual bool isExtensionEnabled(
const std::string EXT)
const noexcept final {
return _extensions.find(EXT) != _extensions.end(); }
144 [[maybe_unused]] [[nodiscard]]
virtual GLFWwindow*
getWindow() const noexcept final {
return mpWindow; }
154 [[nodiscard]]
virtual unsigned short getError() noexcept final {
155 const unsigned short storedErrorCode =
mErrorCode;
160 return storedErrorCode;
223 OpenGLEngine(
int OPENGL_MAJOR_VERSION,
int OPENGL_MINOR_VERSION,
int WINDOW_WIDTH,
int WINDOW_HEIGHT,
const char* WINDOW_TITLE,
bool WINDOW_RESIZABLE = GLFW_FALSE);
298 static void mErrorCallback(
const int error,
const char* DESCRIPTION) { fprintf(stderr,
"[ERROR]: %d\n\t%s\n", error, DESCRIPTION ); }
303 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) {
304 fprintf( stdout,
"[VERBOSE]: Debug Message (%d): source = %s, type = %s, severity = %s, message = %s\n",
306 CSCI441::OpenGLUtils::debugSourceToString(source),
307 CSCI441::OpenGLUtils::debugTypeToString(type),
308 CSCI441::OpenGLUtils::debugSeverityToString(severity),
415 void _setupGLFunctions();
416 void _cleanupGLFunctions() {}
419 void _moveFromSource(OpenGLEngine&);
424 std::set< std::string > _extensions;
430 const int OPENGL_MAJOR_VERSION,
431 const int OPENGL_MINOR_VERSION,
432 const int WINDOW_WIDTH,
433 const int WINDOW_HEIGHT,
434 const char* WINDOW_TITLE,
435 const bool WINDOW_RESIZABLE
437 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
438 mOpenGLMajorVersion(OPENGL_MAJOR_VERSION),
439 mOpenGLMinorVersion(OPENGL_MINOR_VERSION),
440 mOpenGLMajorVersionRequested(OPENGL_MAJOR_VERSION),
441 mOpenGLMinorVersionRequested(OPENGL_MINOR_VERSION),
442 mOpenGLMajorVersionCreated(0),
443 mOpenGLMinorVersionCreated(0),
444 mWindowWidth(WINDOW_WIDTH),
445 mWindowHeight(WINDOW_HEIGHT),
446 mWindowResizable(WINDOW_RESIZABLE),
447 mWindowTitle(nullptr),
449 _isInitialized(false),
461 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
462 mOpenGLMajorVersion(0),
463 mOpenGLMinorVersion(0),
464 mOpenGLMajorVersionRequested(0),
465 mOpenGLMinorVersionRequested(0),
466 mOpenGLMajorVersionCreated(0),
467 mOpenGLMinorVersionCreated(0),
470 mWindowResizable(
false),
471 mWindowTitle(
nullptr),
473 _isInitialized(
false),
476 _moveFromSource(src);
486 _moveFromSource(src);
499void CSCI441::OpenGLEngine::_cleanupSelf()
501 delete[] mWindowTitle;
502 mWindowTitle =
nullptr;
506void CSCI441::OpenGLEngine::_moveFromSource(
512 mErrorCode = src.mErrorCode;
513 src.mErrorCode = OPENGL_ENGINE_ERROR_NO_ERROR;
515 mOpenGLMajorVersion = src.mOpenGLMajorVersion;
516 src.mOpenGLMajorVersion = 0;
518 mOpenGLMinorVersion = src.mOpenGLMinorVersion;
519 src.mOpenGLMinorVersion = 0;
521 mOpenGLMajorVersionRequested = src.mOpenGLMajorVersionRequested;
522 src.mOpenGLMajorVersionRequested = 0;
524 mOpenGLMinorVersionRequested = src.mOpenGLMinorVersionRequested;
525 src.mOpenGLMinorVersionRequested = 0;
527 mOpenGLMajorVersionCreated = src.mOpenGLMajorVersionCreated;
528 src.mOpenGLMajorVersionCreated = 0;
530 mOpenGLMinorVersionCreated = src.mOpenGLMinorVersionCreated;
531 src.mOpenGLMinorVersionCreated = 0;
533 mWindowWidth = src.mWindowWidth;
534 src.mWindowWidth = 0;
536 mWindowHeight = src.mWindowHeight;
537 src.mWindowHeight = 0;
539 mWindowResizable = src.mWindowResizable;
540 src.mWindowResizable =
false;
542 mWindowTitle = src.mWindowTitle;
543 src.mWindowTitle =
nullptr;
545 mpWindow = src.mpWindow;
546 src.mpWindow =
nullptr;
548 _isInitialized = src._isInitialized;
549 src._isInitialized =
false;
551 _isCleanedUp = src._isCleanedUp;
552 src._isCleanedUp =
false;
554 _extensions = std::move(src._extensions);
560 if( !_isInitialized ) {
562 fprintf(stdout,
"[INFO]: Using CSCI441 Library v%d.%d.%d.%d\n", CSCI441::VERSION_MAJOR, CSCI441::VERSION_MINOR, CSCI441::VERSION_REVISION, CSCI441::VERSION_PATCH);
572 if( mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3) ) {
574 int flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
575 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
577 glEnable(GL_DEBUG_OUTPUT);
578 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
579 glDebugMessageCallback(mDebugMessageCallback,
nullptr);
580 glDebugMessageControl( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0,
nullptr, GL_TRUE );
584 CSCI441::OpenGLUtils::printOpenGLInfo();
592 _isInitialized =
true;
593 _isCleanedUp =
false;
594 if (DEBUG) fprintf(stdout,
"\n[INFO]: Setup complete\n");
600 const unsigned short ERROR_CODE
602 switch (ERROR_CODE) {
603 case OPENGL_ENGINE_ERROR_NO_ERROR:
return "No error";
604 case OPENGL_ENGINE_ERROR_GLFW_INIT:
return "Error initializing GLFW";
605 case OPENGL_ENGINE_ERROR_GLFW_WINDOW:
return "Error initializing GLFW window";
606 case OPENGL_ENGINE_ERROR_GLEW_INIT:
return "Error initializing GLEW";
607 case OPENGL_ENGINE_ERROR_GLAD_INIT:
return "Error initializing GLAD";
608 case OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT:
return "Error saving screenshot";
609 default:
return "Unknown error code";
618 const std::string filename =
621 ?
"Screenshot_" + std::to_string(time(
nullptr)) +
".png"
627 glGetIntegerv(GL_VIEWPORT, viewport);
628 const GLsizei x = viewport[0], y = viewport[1], width = viewport[2], height = viewport[3];
631 constexpr int CHANNELS = 4;
632 const auto bytes =
new GLubyte[width*height*CHANNELS];
633 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, bytes);
635 stbi_flip_vertically_on_write(
true);
637 if( !stbi_write_png(filename.c_str(), width, height, CHANNELS, bytes, width*CHANNELS) ) {
638 fprintf(stderr,
"[ERROR]: Could not save screenshot\n");
639 mErrorCode = OPENGL_ENGINE_ERROR_TAKE_SCREENSHOT;
641 fprintf(stdout,
"[INFO]: Screenshot saved to %s\n", filename.c_str());
646 return (mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR);
655 glfwSetErrorCallback(mErrorCallback);
659 fprintf( stderr,
"[ERROR]: Could not initialize GLFW\n" );
660 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_INIT;
662 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW v%d.%d.%d initialized\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION );
664 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, mOpenGLMajorVersion );
665 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, mOpenGLMinorVersion );
666 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
667 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
668 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE );
669 glfwWindowHint(GLFW_RESIZABLE, mWindowResizable );
673 && (mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3)) ) {
674 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT,
true);
678 mpWindow = glfwCreateWindow(mWindowWidth, mWindowHeight, mWindowTitle,
nullptr,
nullptr );
680 fprintf( stderr,
"[ERROR]: GLFW Window could not be created\n" );
682 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_WINDOW;
684 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW Window created\n" );
685 glfwMakeContextCurrent(mpWindow);
687 glfwSetInputMode(mpWindow, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
688 glfwSetWindowUserPointer(mpWindow, (
void*)
this);
689 glfwSetWindowSizeCallback(mpWindow, mWindowResizeCallback);
695void CSCI441::OpenGLEngine::_setupGLFunctions()
698#ifdef CSCI441_USE_GLEW
699 glewExperimental = GL_TRUE;
700 const GLenum glewResult = glewInit();
703 if( glewResult != GLEW_OK ) {
704 fprintf( stderr,
"[ERROR]: Error initializing GLEW\n");
705 fprintf( stderr,
"[ERROR]: %s\n",
reinterpret_cast<const char *
>(glewGetErrorString(glewResult)) );
706 mErrorCode = OPENGL_ENGINE_ERROR_GLEW_INIT;
709 fprintf(stdout,
"[INFO]: GLEW initialized\n");
710 fprintf(stdout,
"[INFO]: Using GLEW %s\n",
reinterpret_cast<const char *
>(glewGetString(GLEW_VERSION)));
714 int version = gladLoadGL(glfwGetProcAddress);
716 fprintf(stderr,
"[ERROR]: Failed to initialize GLAD\n" );
717 mErrorCode = OPENGL_ENGINE_ERROR_GLAD_INIT;
721 fprintf(stdout,
"[INFO]: GLAD initialized v%d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
726 if(mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR) {
727 glGetIntegerv(GL_MAJOR_VERSION, &mOpenGLMajorVersionCreated);
728 glGetIntegerv(GL_MINOR_VERSION, &mOpenGLMinorVersionCreated);
730 fprintf(stdout,
"[INFO]: OpenGL v%d.%d requested\n", mOpenGLMajorVersionRequested, mOpenGLMinorVersionRequested);
731 fprintf(stdout,
"[INFO]: OpenGL v%d.%d created\n", mOpenGLMajorVersionCreated, mOpenGLMinorVersionCreated);
734 GLint numExtensions = 0;
735 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
736 for (
int i = 0; i < numExtensions; i++) {
737 _extensions.insert(
reinterpret_cast<const char *
>(glGetStringi(GL_EXTENSIONS, i)) );
745 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing window...\n" );
746 glfwDestroyWindow(mpWindow );
748 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing GLFW.....\n" );
755 if( !_isCleanedUp ) {
756 if (DEBUG) fprintf(stdout,
"\n[INFO]: Shutting down.......\n");
762 _cleanupGLFunctions();
764 if (DEBUG) fprintf(stdout,
"[INFO]: ..shut down complete!\n");
766 _isInitialized =
false;
776 const auto pEngine =
static_cast<OpenGLEngine *
>(glfwGetWindowUserPointer(pWindow));
783 if (DEBUG) fprintf(stdout,
"\n[INFO]: Removing old shaders...\n");
785 if (DEBUG) fprintf(stdout,
"\n[INFO]: Reloading shaders...\n");
787 if (DEBUG) fprintf(stdout,
"\n[INFO]: Shaders reloaded\n");
Helper functions to work with OpenGL 3.0+.
Abstract Class to run an OpenGL application. The following methods must be overridden:
Definition: OpenGLEngine.hpp:49
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:122
bool DEBUG
if information should be printed to console while running
Definition: OpenGLEngine.hpp:229
OpenGLEngine & operator=(const OpenGLEngine &)=delete
do not allow engines to be copied
int mOpenGLMinorVersion
the minor version of the requested OpenGL context
Definition: OpenGLEngine.hpp:245
int mOpenGLMajorVersion
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:240
static constexpr unsigned short OPENGL_ENGINE_ERROR_NO_ERROR
no error is present, everything is currently working
Definition: OpenGLEngine.hpp:166
virtual bool saveScreenshot(const char *FILENAME) noexcept final
Save a PNG screenshot of the viewport.
Definition: OpenGLEngine.hpp:614
virtual void setCurrentWindowSize(const int WINDOW_WIDTH, const int WINDOW_HEIGHT) final
Set the new window size.
Definition: OpenGLEngine.hpp:132
virtual void initialize()
Initialize everything needed for OpenGL Rendering. This includes in order: GLFW, function pointers,...
Definition: OpenGLEngine.hpp:558
virtual void shutdown()
Cleanup everything needed for OpenGL Rendering. This includes freeing memory for data used in: any Sc...
Definition: OpenGLEngine.hpp:753
virtual unsigned short getError() noexcept final
Return current value of error code and clear the error code back to no error.
Definition: OpenGLEngine.hpp:154
virtual void mSetupTextures()
override to register any textures with the GPU
Definition: OpenGLEngine.hpp:367
virtual int getWindowHeight() const noexcept final
Return the height of the window.
Definition: OpenGLEngine.hpp:136
virtual bool isDebuggingEnabled() const noexcept final
Returns if logging is enabled.
Definition: OpenGLEngine.hpp:115
static void mWindowResizeCallback(GLFWwindow *pWindow, int width, int height)
Definition: OpenGLEngine.hpp:771
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLAD_INIT
an error occurred while initializing GLAD
Definition: OpenGLEngine.hpp:182
virtual void mCleanupBuffers()
override to cleanup any buffer objects from the GPU
Definition: OpenGLEngine.hpp:389
virtual void mCleanupScene()
override to cleanup any scene specific information
Definition: OpenGLEngine.hpp:379
virtual void mCleanupShaders()
override to cleanup any shaders from the GPU
Definition: OpenGLEngine.hpp:394
virtual void mCleanupTextures()
override to cleanup any textures from the GPU
Definition: OpenGLEngine.hpp:384
bool mWindowResizable
if the GLFW window can be resized while open
Definition: OpenGLEngine.hpp:282
int mOpenGLMinorVersionRequested
the minor version of the requested OpenGL context
Definition: OpenGLEngine.hpp:257
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:186
virtual void turnDebuggingOn() noexcept final
Enable logging to command line.
Definition: OpenGLEngine.hpp:106
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLEW_INIT
an error occurred while initializing GLEW
Definition: OpenGLEngine.hpp:178
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_WINDOW
an error occurred while creating the GLFW window
Definition: OpenGLEngine.hpp:174
int mOpenGLMajorVersionRequested
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:251
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:298
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:198
unsigned int mErrorCode
tracks the current status of the OpenGL engine via error codes
Definition: OpenGLEngine.hpp:234
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:303
char * mWindowTitle
the title of the GLFW window
Definition: OpenGLEngine.hpp:286
int mWindowHeight
the window height of the requested GLFW window
Definition: OpenGLEngine.hpp:277
virtual void setWindowShouldClose() final
Tell our engine's window to close.
Definition: OpenGLEngine.hpp:149
int mOpenGLMajorVersionCreated
the major version of the created OpenGL context
Definition: OpenGLEngine.hpp:262
static constexpr unsigned short OPENGL_ENGINE_ERROR_SIZE
stores the number of unique error codes that can be generated
Definition: OpenGLEngine.hpp:202
virtual void mReloadShaders() final
calls mCleanupShaders() followed by mSetupShaders() to reload shader source code from file
Definition: OpenGLEngine.hpp:781
virtual void mSetupShaders()
override to register any shaders with the GPU
Definition: OpenGLEngine.hpp:355
GLFWwindow * mpWindow
pointer to the GLFW window object
Definition: OpenGLEngine.hpp:290
virtual void turnDebuggingOff() noexcept final
Disable logging to command line.
Definition: OpenGLEngine.hpp:111
static const char * getErrorStringDescription(unsigned short ERROR_CODE) noexcept
Returns a string describing what the error code corresponds to.
Definition: OpenGLEngine.hpp:599
virtual int getWindowWidth() const noexcept final
Return the width of the window.
Definition: OpenGLEngine.hpp:140
virtual void mSetupBuffers()
override to register any buffer objects with the GPU
Definition: OpenGLEngine.hpp:361
virtual ~OpenGLEngine()
cleans up our OpenGL Engine by destroying the OpenGL context, GLFW window, and cleaning up all GPU re...
Definition: OpenGLEngine.hpp:493
virtual void mCleanupGLFW()
Destroys the associated GLFW window and terminates the GLFW instance.
Definition: OpenGLEngine.hpp:743
int mOpenGLMinorVersionCreated
the minor version of the created OpenGL context
Definition: OpenGLEngine.hpp:267
int mWindowWidth
the window width of the requested GLFW window
Definition: OpenGLEngine.hpp:272
virtual void mCleanupOpenGL()
override to cleanup any specific OpenGL features
Definition: OpenGLEngine.hpp:399
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_INIT
an error occurred while initializing GLFW
Definition: OpenGLEngine.hpp:170
virtual void mSetupGLFW()
Used to setup everything GLFW related. This includes the OpenGL context and our window....
Definition: OpenGLEngine.hpp:650
virtual void mSetupScene()
override to setup any scene specific information
Definition: OpenGLEngine.hpp:373
static constexpr unsigned short OPENGL_ENGINE_ERROR_UNKNOWN
a new error that does not correspond to a predefined scenario has occurred
Definition: OpenGLEngine.hpp:190
virtual GLFWwindow * getWindow() const noexcept final
Return the window object.
Definition: OpenGLEngine.hpp:144
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17