12#ifndef CSCI441_OPENGL_ENGINE_HPP
13#define CSCI441_OPENGL_ENGINE_HPP
17#ifdef CSCI441_USE_GLEW
23#include <GLFW/glfw3.h>
77 virtual
void run() = 0;
107 [[maybe_unused]] [[nodiscard]]
virtual bool isExtensionEnabled(
const std::string EXT)
const noexcept final {
return _extensions.find(EXT) != _extensions.end(); }
129 [[maybe_unused]] [[nodiscard]]
virtual GLFWwindow*
getWindow() const noexcept final {
return mpWindow; }
139 [[nodiscard]]
virtual unsigned short getError() noexcept final {
140 const unsigned short storedErrorCode =
mErrorCode;
142 return storedErrorCode;
193 OpenGLEngine(
int OPENGL_MAJOR_VERSION,
int OPENGL_MINOR_VERSION,
int WINDOW_WIDTH,
int WINDOW_HEIGHT,
const char* WINDOW_TITLE,
bool WINDOW_RESIZABLE = GLFW_FALSE);
246 static void mErrorCallback(
const int error,
const char* DESCRIPTION) { fprintf(stderr,
"[ERROR]: %d\n\t%s\n", error, DESCRIPTION ); }
251 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) {
252 fprintf( stdout,
"[VERBOSE]: Debug Message (%d): source = %s, type = %s, severity = %s, message = %s\n",
254 CSCI441::OpenGLUtils::debugSourceToString(source),
255 CSCI441::OpenGLUtils::debugTypeToString(type),
256 CSCI441::OpenGLUtils::debugSeverityToString(severity),
356 virtual void mReloadShaders() final;
359 void _setupGLFunctions();
360 void _cleanupGLFunctions() {}
363 void _moveFromSource(OpenGLEngine&);
368 std::set< std::string > _extensions;
373 const int OPENGL_MAJOR_VERSION,
374 const int OPENGL_MINOR_VERSION,
375 const int WINDOW_WIDTH,
376 const int WINDOW_HEIGHT,
377 const char* WINDOW_TITLE,
378 const bool WINDOW_RESIZABLE
380 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
381 mOpenGLMajorVersion(OPENGL_MAJOR_VERSION),
382 mOpenGLMinorVersion(OPENGL_MINOR_VERSION),
383 mWindowWidth(WINDOW_WIDTH),
384 mWindowHeight(WINDOW_HEIGHT),
385 mWindowResizable(WINDOW_RESIZABLE),
386 mWindowTitle(nullptr),
388 _isInitialized(false),
399 mErrorCode(OPENGL_ENGINE_ERROR_NO_ERROR),
400 mOpenGLMajorVersion(0),
401 mOpenGLMinorVersion(0),
404 mWindowResizable(
false),
405 mWindowTitle(
nullptr),
407 _isInitialized(
false),
410 _moveFromSource(src);
416 _moveFromSource(src);
426inline void CSCI441::OpenGLEngine::_cleanupSelf() {
427 delete[] mWindowTitle;
428 mWindowTitle =
nullptr;
431inline void CSCI441::OpenGLEngine::_moveFromSource(OpenGLEngine& src) {
435 mErrorCode = src.mErrorCode;
436 src.mErrorCode = OPENGL_ENGINE_ERROR_NO_ERROR;
438 mOpenGLMajorVersion = src.mOpenGLMajorVersion;
439 src.mOpenGLMajorVersion = 0;
441 mOpenGLMinorVersion = src.mOpenGLMinorVersion;
442 src.mOpenGLMinorVersion = 0;
444 mWindowWidth = src.mWindowWidth;
445 src.mWindowWidth = 0;
447 mWindowHeight = src.mWindowHeight;
448 src.mWindowHeight = 0;
450 mWindowResizable = src.mWindowResizable;
451 src.mWindowResizable =
false;
453 mWindowTitle = src.mWindowTitle;
454 src.mWindowTitle =
nullptr;
456 mpWindow = src.mpWindow;
457 src.mpWindow =
nullptr;
459 _isInitialized = src._isInitialized;
460 src._isInitialized =
false;
462 _isCleanedUp = src._isCleanedUp;
463 src._isCleanedUp =
false;
465 _extensions = std::move(src._extensions);
469 if( !_isInitialized ) {
477 if( mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3) ) {
479 int flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
480 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
482 glEnable(GL_DEBUG_OUTPUT);
483 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
484 glDebugMessageCallback(mDebugMessageCallback,
nullptr);
485 glDebugMessageControl( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0,
nullptr, GL_TRUE );
489 CSCI441::OpenGLUtils::printOpenGLInfo();
497 _isInitialized =
true;
498 _isCleanedUp =
false;
499 if (DEBUG) fprintf(stdout,
"\n[INFO]: Setup complete\n");
507 glfwSetErrorCallback(mErrorCallback);
511 fprintf( stderr,
"[ERROR]: Could not initialize GLFW\n" );
512 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_INIT;
514 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW %d.%d.%d initialized\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION );
516 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, mOpenGLMajorVersion );
517 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, mOpenGLMinorVersion );
518 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
519 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
520 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE );
521 glfwWindowHint(GLFW_RESIZABLE, mWindowResizable );
525 && (mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3)) ) {
526 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT,
true);
530 mpWindow = glfwCreateWindow(mWindowWidth, mWindowHeight, mWindowTitle,
nullptr,
nullptr );
532 fprintf( stderr,
"[ERROR]: GLFW Window could not be created\n" );
534 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_WINDOW;
536 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW Window created\n" );
537 glfwMakeContextCurrent(mpWindow);
539 glfwSetInputMode(mpWindow, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
540 glfwSetWindowUserPointer(mpWindow, (
void*)
this);
541 glfwSetWindowSizeCallback(mpWindow, mWindowResizeCallback);
546inline void CSCI441::OpenGLEngine::_setupGLFunctions() {
548#ifdef CSCI441_USE_GLEW
549 glewExperimental = GL_TRUE;
550 const GLenum glewResult = glewInit();
553 if( glewResult != GLEW_OK ) {
554 fprintf( stderr,
"[ERROR]: Error initializing GLEW\n");
555 fprintf( stderr,
"[ERROR]: %s\n",
reinterpret_cast<const char *
>(glewGetErrorString(glewResult)) );
556 mErrorCode = OPENGL_ENGINE_ERROR_GLEW_INIT;
559 fprintf(stdout,
"\n[INFO]: GLEW initialized\n");
560 fprintf(stdout,
"[INFO]: Using GLEW %s\n",
reinterpret_cast<const char *
>(glewGetString(GLEW_VERSION)));
564 int version = gladLoadGL(glfwGetProcAddress);
566 fprintf(stderr,
"Failed to initialize GLAD\n" );
567 mErrorCode = OPENGL_ENGINE_ERROR_GLAD_INIT;
571 fprintf(stdout,
"\n[INFO]: GLAD initialized\n");
572 fprintf(stdout,
"[INFO]: Loaded OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
577 if(mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR) {
578 GLint numExtensions = 0;
579 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
580 for (
int i = 0; i < numExtensions; i++) {
581 _extensions.insert(
reinterpret_cast<const char *
>(glGetStringi(GL_EXTENSIONS, i)) );
587 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing window...\n" );
588 glfwDestroyWindow(mpWindow );
590 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing GLFW.....\n" );
595 if( !_isCleanedUp ) {
596 if (DEBUG) fprintf(stdout,
"\n[INFO]: Shutting down.......\n");
602 _cleanupGLFunctions();
604 if (DEBUG) fprintf(stdout,
"[INFO]: ..shut down complete!\n");
606 _isInitialized =
false;
611 const auto pEngine =
static_cast<OpenGLEngine *
>(glfwGetWindowUserPointer(pWindow));
615inline void CSCI441::OpenGLEngine::mReloadShaders() {
616 if (DEBUG) fprintf(stdout,
"\n[INFO]: Removing old shaders...\n");
618 if (DEBUG) fprintf(stdout,
"\n[INFO]: Reloading shaders...\n");
620 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:39
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:107
bool DEBUG
if information should be printed to console while running
Definition: OpenGLEngine.hpp:199
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:215
int mOpenGLMajorVersion
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:210
static constexpr unsigned short OPENGL_ENGINE_ERROR_NO_ERROR
no error is present, everything is currently working
Definition: OpenGLEngine.hpp:148
virtual void setCurrentWindowSize(const int WINDOW_WIDTH, const int WINDOW_HEIGHT) final
Set the new window size.
Definition: OpenGLEngine.hpp:117
virtual void initialize()
Initialize everything needed for OpenGL Rendering. This includes in order: GLFW, function pointers,...
Definition: OpenGLEngine.hpp:468
virtual void shutdown()
Cleanup everything needed for OpenGL Rendering. This includes freeing memory for data used in: any Sc...
Definition: OpenGLEngine.hpp:594
virtual unsigned short getError() noexcept final
Return current value of error code and clear the error code back to no error.
Definition: OpenGLEngine.hpp:139
virtual void mSetupTextures()
override to register any textures with the GPU
Definition: OpenGLEngine.hpp:315
virtual int getWindowHeight() const noexcept final
Return the height of the window.
Definition: OpenGLEngine.hpp:121
virtual bool isDebuggingEnabled() const noexcept final
Returns if logging is enabled.
Definition: OpenGLEngine.hpp:100
static void mWindowResizeCallback(GLFWwindow *pWindow, int width, int height)
Definition: OpenGLEngine.hpp:610
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLAD_INIT
an error occurred while initializing GLAD
Definition: OpenGLEngine.hpp:164
virtual void mCleanupBuffers()
override to cleanup any buffer objects from the GPU
Definition: OpenGLEngine.hpp:337
virtual void mCleanupScene()
override to cleanup any scene specific information
Definition: OpenGLEngine.hpp:327
virtual void mCleanupShaders()
override to cleanup any shaders from the GPU
Definition: OpenGLEngine.hpp:342
virtual void mCleanupTextures()
override to cleanup any textures from the GPU
Definition: OpenGLEngine.hpp:332
bool mWindowResizable
if the GLFW window can be resized while open
Definition: OpenGLEngine.hpp:230
OpenGLEngine(const OpenGLEngine &)=delete
do not allow engines to be copied
virtual void turnDebuggingOn() noexcept final
Enable logging to command line.
Definition: OpenGLEngine.hpp:91
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLEW_INIT
an error occurred while initializing GLEW
Definition: OpenGLEngine.hpp:160
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_WINDOW
an error occurred while creating the GLFW window
Definition: OpenGLEngine.hpp:156
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:246
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:175
unsigned int mErrorCode
tracks the current status of the OpenGL engine via error codes
Definition: OpenGLEngine.hpp:204
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:251
char * mWindowTitle
the title of the GLFW window
Definition: OpenGLEngine.hpp:234
int mWindowHeight
the window height of the requested GLFW window
Definition: OpenGLEngine.hpp:225
virtual void setWindowShouldClose() final
Tell our engine's window to close.
Definition: OpenGLEngine.hpp:134
static constexpr unsigned short OPENGL_ENGINE_ERROR_SIZE
stores the number of unique error codes that can be generated
Definition: OpenGLEngine.hpp:179
virtual void mSetupShaders()
override to register any shaders with the GPU
Definition: OpenGLEngine.hpp:303
GLFWwindow * mpWindow
pointer to the GLFW window object
Definition: OpenGLEngine.hpp:238
virtual void turnDebuggingOff() noexcept final
Disable logging to command line.
Definition: OpenGLEngine.hpp:96
virtual int getWindowWidth() const noexcept final
Return the width of the window.
Definition: OpenGLEngine.hpp:125
virtual void mSetupBuffers()
override to register any buffer objects with the GPU
Definition: OpenGLEngine.hpp:309
virtual ~OpenGLEngine()
cleans up our OpenGL Engine by destroying the OpenGL context, GLFW window, and cleaning up all GPU re...
Definition: OpenGLEngine.hpp:422
virtual void mCleanupGLFW()
Destroys the associated GLFW window and terminates the GLFW instance.
Definition: OpenGLEngine.hpp:586
int mWindowWidth
the window width of the requested GLFW window
Definition: OpenGLEngine.hpp:220
virtual void mCleanupOpenGL()
override to cleanup any specific OpenGL features
Definition: OpenGLEngine.hpp:347
static constexpr unsigned short OPENGL_ENGINE_ERROR_GLFW_INIT
an error occurred while initializing GLFW
Definition: OpenGLEngine.hpp:152
virtual void mSetupGLFW()
Used to setup everything GLFW related. This includes the OpenGL context and our window....
Definition: OpenGLEngine.hpp:503
virtual void mSetupScene()
override to setup any scene specific information
Definition: OpenGLEngine.hpp:321
static constexpr unsigned short OPENGL_ENGINE_ERROR_UNKNOWN
a new error that does not correspond to a predefined scenario has occurred
Definition: OpenGLEngine.hpp:168
virtual GLFWwindow * getWindow() const noexcept final
Return the window object.
Definition: OpenGLEngine.hpp:129
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17