12#ifndef CSCI441_OPENGL_ENGINE_HPP
13#define CSCI441_OPENGL_ENGINE_HPP
17#ifdef CSCI441_USE_GLEW
23#include <GLFW/glfw3.h>
61 virtual void run() = 0;
91 [[maybe_unused]] [[nodiscard]]
virtual bool isExtensionEnabled(
const std::string EXT)
const noexcept final {
return _extensions.find(EXT) != _extensions.end(); }
113 [[maybe_unused]] [[nodiscard]]
virtual GLFWwindow*
getWindow() const noexcept final {
return mpWindow; }
123 [[nodiscard]]
virtual unsigned short getError() noexcept final {
126 return storedErrorCode;
177 OpenGLEngine(
int OPENGL_MAJOR_VERSION,
int OPENGL_MINOR_VERSION,
int WINDOW_WIDTH,
int WINDOW_HEIGHT,
const char* WINDOW_TITLE,
bool WINDOW_RESIZABLE = GLFW_FALSE);
235 static void mErrorCallback(
int error,
const char* DESCRIPTION) { fprintf(stderr,
"[ERROR]: %d\n\t%s\n", error, DESCRIPTION ); }
240 static void mDebugMessageCallback(GLenum source, GLenum type, GLuint
id, GLenum severity, GLsizei length,
const GLchar* message,
const void* userParam) {
241 fprintf( stdout,
"[VERBOSE]: Debug Message (%d): source = %s, type = %s, severity = %s, message = %s\n",
243 CSCI441::OpenGLUtils::debugSourceToString(source),
244 CSCI441::OpenGLUtils::debugTypeToString(type),
245 CSCI441::OpenGLUtils::debugSeverityToString(severity),
346 void _setupGLFunctions();
347 void _cleanupGLFunctions() {}
352 std::set< std::string > _extensions;
356inline CSCI441::OpenGLEngine::OpenGLEngine(
const int OPENGL_MAJOR_VERSION,
const int OPENGL_MINOR_VERSION,
const int WINDOW_WIDTH,
const int WINDOW_HEIGHT,
const char* WINDOW_TITLE,
const bool WINDOW_RESIZABLE)
357 : mOpenGLMajorVersion(OPENGL_MAJOR_VERSION), mOpenGLMinorVersion(OPENGL_MINOR_VERSION), mWindowWidth(WINDOW_WIDTH), mWindowHeight(WINDOW_HEIGHT), mWindowResizable(WINDOW_RESIZABLE) {
359 mWindowTitle = (
char*)malloc(
sizeof(
char) * strlen(WINDOW_TITLE));
364 _isInitialized = _isCleanedUp =
false;
375 if( !_isInitialized ) {
383 if( mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3) ) {
385 int flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
386 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
388 glEnable(GL_DEBUG_OUTPUT);
389 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
390 glDebugMessageCallback(mDebugMessageCallback,
nullptr);
391 glDebugMessageControl( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0,
nullptr, GL_TRUE );
395 CSCI441::OpenGLUtils::printOpenGLInfo();
403 _isInitialized =
true;
404 _isCleanedUp =
false;
405 if (DEBUG) fprintf(stdout,
"\n[INFO]: Setup complete\n");
413 glfwSetErrorCallback(mErrorCallback);
417 fprintf( stderr,
"[ERROR]: Could not initialize GLFW\n" );
418 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_INIT;
420 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW %d.%d.%d initialized\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION );
422 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, mOpenGLMajorVersion );
423 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, mOpenGLMinorVersion );
424 glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
425 glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
426 glfwWindowHint( GLFW_DOUBLEBUFFER, GLFW_TRUE );
427 glfwWindowHint(GLFW_RESIZABLE, mWindowResizable );
431 && (mOpenGLMajorVersion > 4 || (mOpenGLMajorVersion == 4 && mOpenGLMinorVersion >= 3)) ) {
432 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT,
true);
436 mpWindow = glfwCreateWindow(mWindowWidth, mWindowHeight, mWindowTitle,
nullptr,
nullptr );
438 fprintf( stderr,
"[ERROR]: GLFW Window could not be created\n" );
440 mErrorCode = OPENGL_ENGINE_ERROR_GLFW_WINDOW;
442 if(DEBUG) fprintf( stdout,
"[INFO]: GLFW Window created\n" );
443 glfwMakeContextCurrent(mpWindow);
445 glfwSetInputMode(mpWindow, GLFW_LOCK_KEY_MODS, GLFW_TRUE);
446 glfwSetWindowUserPointer(mpWindow, (
void*)
this);
447 glfwSetWindowSizeCallback(mpWindow, mWindowResizeCallback);
452inline void CSCI441::OpenGLEngine::_setupGLFunctions() {
454#ifdef CSCI441_USE_GLEW
455 glewExperimental = GL_TRUE;
456 GLenum glewResult = glewInit();
459 if( glewResult != GLEW_OK ) {
460 fprintf( stderr,
"[ERROR]: Error initializing GLEW\n");
461 fprintf( stderr,
"[ERROR]: %s\n", glewGetErrorString(glewResult) );
462 mErrorCode = OPENGL_ENGINE_ERROR_GLEW_INIT;
465 fprintf(stdout,
"\n[INFO]: GLEW initialized\n");
466 fprintf(stdout,
"[INFO]: Using GLEW %s\n", glewGetString(GLEW_VERSION));
470 int version = gladLoadGL(glfwGetProcAddress);
472 fprintf(stderr,
"Failed to initialize GLAD\n" );
473 mErrorCode = OPENGL_ENGINE_ERROR_GLAD_INIT;
477 fprintf(stdout,
"\n[INFO]: GLAD initialized\n");
478 fprintf(stdout,
"[INFO]: Loaded OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
483 if(mErrorCode == OPENGL_ENGINE_ERROR_NO_ERROR) {
484 GLint numExtensions = 0;
485 glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
486 for (
int i = 0; i < numExtensions; i++) {
487 _extensions.insert((
const char*)glGetStringi(GL_EXTENSIONS, i) );
493 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing window...\n" );
494 glfwDestroyWindow(mpWindow );
496 if(DEBUG) fprintf( stdout,
"[INFO]: ...closing GLFW.....\n" );
501 if( !_isCleanedUp ) {
502 if (DEBUG) fprintf(stdout,
"\n[INFO]: Shutting down.......\n");
508 _cleanupGLFunctions();
510 if (DEBUG) fprintf(stdout,
"[INFO]: ..shut down complete!\n");
512 _isInitialized =
false;
517 auto pEngine = (
OpenGLEngine*) glfwGetWindowUserPointer(pWindow);
518 pEngine->setCurrentWindowSize(width, height);
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:91
static const unsigned short OPENGL_ENGINE_ERROR_NO_ERROR
no error is present, everything is currently working
Definition: OpenGLEngine.hpp:132
bool DEBUG
if information should be printed to console while running
Definition: OpenGLEngine.hpp:188
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:204
int mOpenGLMajorVersion
the major version of the requested OpenGL context
Definition: OpenGLEngine.hpp:199
virtual void setCurrentWindowSize(const int WINDOW_WIDTH, const int WINDOW_HEIGHT) final
Set the new window size.
Definition: OpenGLEngine.hpp:101
virtual void initialize()
Initialize everything needed for OpenGL Rendering. This includes in order: GLFW, function pointers,...
Definition: OpenGLEngine.hpp:374
virtual void shutdown()
Cleanup everything needed for OpenGL Rendering. This includes freeing memory for data used in: any Sc...
Definition: OpenGLEngine.hpp:500
virtual unsigned short getError() noexcept final
Return current value of error code and clear the error code back to no error.
Definition: OpenGLEngine.hpp:123
virtual void mSetupTextures()
override to register any textures with the GPU
Definition: OpenGLEngine.hpp:304
virtual int getWindowHeight() const noexcept final
Return the height of the window.
Definition: OpenGLEngine.hpp:105
virtual bool isDebuggingEnabled() const noexcept final
Returns if logging is enabled.
Definition: OpenGLEngine.hpp:84
static void mWindowResizeCallback(GLFWwindow *pWindow, int width, int height)
Definition: OpenGLEngine.hpp:516
virtual void mCleanupBuffers()
override to cleanup any buffer objects from the GPU
Definition: OpenGLEngine.hpp:326
virtual void mCleanupScene()
override to cleanup any scene specific information
Definition: OpenGLEngine.hpp:316
virtual void mCleanupShaders()
override to cleanup any shaders from the GPU
Definition: OpenGLEngine.hpp:331
static const unsigned short OPENGL_ENGINE_ERROR_UNKNOWN
a new error that does not correspond to a predefined scenario has occurred
Definition: OpenGLEngine.hpp:152
static const 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:159
static const unsigned short OPENGL_ENGINE_ERROR_GLAD_INIT
an error occurred while initializing GLAD
Definition: OpenGLEngine.hpp:148
virtual void mCleanupTextures()
override to cleanup any textures from the GPU
Definition: OpenGLEngine.hpp:321
bool mWindowResizable
if the GLFW window can be resized while open
Definition: OpenGLEngine.hpp:219
OpenGLEngine(const OpenGLEngine &)=delete
do not allow engines to be copied
virtual void turnDebuggingOn() noexcept final
Enable logging to command line.
Definition: OpenGLEngine.hpp:75
static const unsigned short OPENGL_ENGINE_ERROR_GLFW_INIT
an error occurred while initializing GLFW
Definition: OpenGLEngine.hpp:136
unsigned int mErrorCode
tracks the current status of the OpenGL engine via error codes
Definition: OpenGLEngine.hpp:193
virtual void run()=0
Initiate the draw loop.
char * mWindowTitle
the title of the GLFW window
Definition: OpenGLEngine.hpp:223
static void mDebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
callback called whenever a debug message is signaled
Definition: OpenGLEngine.hpp:240
static const unsigned short OPENGL_ENGINE_ERROR_GLFW_WINDOW
an error occurred while creating the GLFW window
Definition: OpenGLEngine.hpp:140
int mWindowHeight
the window height of the requested GLFW window
Definition: OpenGLEngine.hpp:214
virtual void setWindowShouldClose() final
Tell our engine's window to close.
Definition: OpenGLEngine.hpp:118
static const unsigned short OPENGL_ENGINE_ERROR_SIZE
stores the number of unique error codes that can be generated
Definition: OpenGLEngine.hpp:163
virtual void mSetupShaders()
override to register any shaders with the GPU
Definition: OpenGLEngine.hpp:292
GLFWwindow * mpWindow
pointer to the GLFW window object
Definition: OpenGLEngine.hpp:227
virtual void turnDebuggingOff() noexcept final
Disable logging to command line.
Definition: OpenGLEngine.hpp:80
virtual int getWindowWidth() const noexcept final
Return the width of the window.
Definition: OpenGLEngine.hpp:109
virtual void mSetupBuffers()
override to register any buffer objects with the GPU
Definition: OpenGLEngine.hpp:298
virtual ~OpenGLEngine()
cleans up our OpenGL Engine by destroying the OpenGL context, GLFW window, and cleaning up all GPU re...
Definition: OpenGLEngine.hpp:369
static const unsigned short OPENGL_ENGINE_ERROR_GLEW_INIT
an error occurred while initializing GLEW
Definition: OpenGLEngine.hpp:144
virtual void mCleanupGLFW()
Destroys the associated GLFW window and terminates the GLFW instance.
Definition: OpenGLEngine.hpp:492
int mWindowWidth
the window width of the requested GLFW window
Definition: OpenGLEngine.hpp:209
virtual void mCleanupOpenGL()
override to cleanup any specific OpenGL features
Definition: OpenGLEngine.hpp:336
virtual void mSetupGLFW()
Used to setup everything GLFW related. This includes the OpenGL context and our window....
Definition: OpenGLEngine.hpp:409
static void mErrorCallback(int error, const char *DESCRIPTION)
We will register this function as GLFW's error callback. When an error within OpenGL occurs,...
Definition: OpenGLEngine.hpp:235
virtual void mSetupScene()
override to setup any scene specific information
Definition: OpenGLEngine.hpp:310
virtual GLFWwindow * getWindow() const noexcept final
Return the window object.
Definition: OpenGLEngine.hpp:113
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17