1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Hammad Mazhar
13 // =============================================================================
14 // Base Class for all opengl related classes
15 // =============================================================================
16 
17 #ifndef CHOPENGLBASE_H
18 #define CHOPENGLBASE_H
19 
20 #include <GL/glew.h>
21 
22 #ifdef __APPLE__
23 #define GLFW_INCLUDE_GLCOREARB
24 #define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED  // fixes warnings
25 #endif
26 
27 #define GLM_FORCE_RADIANS
28 #define _CRT_SECURE_NO_WARNINGS
29 
30 #include <cassert>
31 #include <iostream>
32 #include <string>
33 //#include <string>
34 //#include <iomanip>
35 //#include <fstream>
36 //#include <sstream>
37 //#include <limits>
38 //#include <time.h>
39 //#include <math.h>
40 //#include <vector>
41 
42 #include "chrono_opengl/core/ChApiOpenGL.h"
43 
44 namespace chrono {
45 namespace opengl {
46 
47 /// @addtogroup opengl_module
48 /// @{
49 
50 #ifndef BAD_GL_VALUE
51 #define BAD_GL_VALUE GLuint(-1)
52 #endif
53 
54 /// Convert error enum to error string.
GetErrorString(GLenum error)55 static std::string GetErrorString(GLenum error) {
56     std::string ret_val;
57     switch (error) {
58         case GL_NO_ERROR:
59             break;
60         case GL_INVALID_ENUM:
61             ret_val = "GL_INVALID_ENUM";
62             break;
63         case GL_INVALID_VALUE:
64             ret_val = "GL_INVALID_VALUE";
65             break;
66         case GL_INVALID_OPERATION:
67             ret_val = "GL_INVALID_OPERATION";
68             break;
69         case GL_INVALID_FRAMEBUFFER_OPERATION:
70             ret_val = "GL_INVALID_FRAMEBUFFER_OPERATION";
71             break;
72         case GL_OUT_OF_MEMORY:
73             ret_val = "GL_OUT_OF_MEMORY";
74             break;
75         case GL_STACK_UNDERFLOW:
76             ret_val = "GL_STACK_UNDERFLOW";
77             break;
78         case GL_STACK_OVERFLOW:
79             ret_val = "GL_STACK_OVERFLOW";
80             break;
81     }
82     return ret_val;
83 }
84 
85 /// Checks if there are any errors in the opengl context.
GLReturnedError(std::string err)86 static bool GLReturnedError(std::string err) {
87     bool return_error = false;
88     GLenum glerror;
89     while ((glerror = glGetError()) != GL_NO_ERROR) {
90         return_error = true;
91         std::cerr << err << ": " << GetErrorString(glerror) << std::endl;
92     }
93     return return_error;
94 }
95 
96 /// Base class for all OpenGL related classes.
97 class CH_OPENGL_API ChOpenGLBase {
98   public:
ChOpenGLBase()99     ChOpenGLBase() {}
~ChOpenGLBase()100     virtual ~ChOpenGLBase() {}
101 
102     // Children must implement this function
103     virtual void TakeDown() = 0;
104 
105     // Check for opengl Errors and output if error along with input char strings
GLReturnedError(const char * s)106     bool GLReturnedError(const char* s) {
107         bool return_error = false;
108         GLenum glerror;
109         while ((glerror = glGetError()) != GL_NO_ERROR) {
110             return_error = true;
111             std::cerr << s << ": " << GetErrorString(glerror) << std::endl;
112         }
113         return return_error;
114     }
115 };
116 
117 /// @} opengl_module
118 
119 }
120 }
121 
122 #endif
123