|
| 1 | +#if defined(USE_OPENGL_14) |
| 2 | + |
| 3 | +#include "renderers/Renderer.h" |
| 4 | +#include "math/Transform4x4f.h" |
| 5 | +#include "Log.h" |
| 6 | +#include "Settings.h" |
| 7 | + |
| 8 | +#include <SDL_opengl.h> |
| 9 | +#include <SDL.h> |
| 10 | + |
| 11 | +namespace Renderer |
| 12 | +{ |
| 13 | + |
| 14 | +#if defined(_DEBUG) |
| 15 | +#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function)) |
| 16 | + |
| 17 | + static void _GLCheckError(const char* _funcName) |
| 18 | + { |
| 19 | + const GLenum errorCode = glGetError(); |
| 20 | + |
| 21 | + if(errorCode != GL_NO_ERROR) |
| 22 | + LOG(LogError) << "OpenGLES error: " << _funcName << " failed with error code: " << errorCode; |
| 23 | + } |
| 24 | +#else |
| 25 | +#define GL_CHECK_ERROR(Function) (Function) |
| 26 | +#endif |
| 27 | + |
| 28 | + static SDL_GLContext sdlContext = nullptr; |
| 29 | + static GLuint whiteTexture = 0; |
| 30 | + |
| 31 | + static GLenum convertBlendFactor(const Blend::Factor _blendFactor) |
| 32 | + { |
| 33 | + switch(_blendFactor) |
| 34 | + { |
| 35 | + case Blend::ZERO: { return GL_ZERO; } break; |
| 36 | + case Blend::ONE: { return GL_ONE; } break; |
| 37 | + case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break; |
| 38 | + case Blend::ONE_MINUS_SRC_COLOR: { return GL_ONE_MINUS_SRC_COLOR; } break; |
| 39 | + case Blend::SRC_ALPHA: { return GL_SRC_ALPHA; } break; |
| 40 | + case Blend::ONE_MINUS_SRC_ALPHA: { return GL_ONE_MINUS_SRC_ALPHA; } break; |
| 41 | + case Blend::DST_COLOR: { return GL_DST_COLOR; } break; |
| 42 | + case Blend::ONE_MINUS_DST_COLOR: { return GL_ONE_MINUS_DST_COLOR; } break; |
| 43 | + case Blend::DST_ALPHA: { return GL_DST_ALPHA; } break; |
| 44 | + case Blend::ONE_MINUS_DST_ALPHA: { return GL_ONE_MINUS_DST_ALPHA; } break; |
| 45 | + default: { return GL_ZERO; } |
| 46 | + } |
| 47 | + |
| 48 | + } // convertBlendFactor |
| 49 | + |
| 50 | + static GLenum convertTextureType(const Texture::Type _type) |
| 51 | + { |
| 52 | + switch(_type) |
| 53 | + { |
| 54 | + case Texture::RGBA: { return GL_RGBA; } break; |
| 55 | + case Texture::ALPHA: { return GL_ALPHA; } break; |
| 56 | + default: { return GL_ZERO; } |
| 57 | + } |
| 58 | + |
| 59 | + } // convertTextureType |
| 60 | + |
| 61 | + unsigned int convertColor(const unsigned int _color) |
| 62 | + { |
| 63 | + // convert from rgba to abgr |
| 64 | + unsigned char r = ((_color & 0xff000000) >> 24) & 255; |
| 65 | + unsigned char g = ((_color & 0x00ff0000) >> 16) & 255; |
| 66 | + unsigned char b = ((_color & 0x0000ff00) >> 8) & 255; |
| 67 | + unsigned char a = ((_color & 0x000000ff) ) & 255; |
| 68 | + |
| 69 | + return ((a << 24) | (b << 16) | (g << 8) | (r)); |
| 70 | + |
| 71 | + } // convertColor |
| 72 | + |
| 73 | + unsigned int getWindowFlags() |
| 74 | + { |
| 75 | + return SDL_WINDOW_OPENGL; |
| 76 | + |
| 77 | + } // getWindowFlags |
| 78 | + |
| 79 | + void setupWindow() |
| 80 | + { |
| 81 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 82 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); |
| 83 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4); |
| 84 | + |
| 85 | + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
| 86 | + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
| 87 | + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
| 88 | + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 89 | + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 90 | + SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); |
| 91 | + |
| 92 | + } // setupWindow |
| 93 | + |
| 94 | + void createContext() |
| 95 | + { |
| 96 | + sdlContext = SDL_GL_CreateContext(getSDLWindow()); |
| 97 | + SDL_GL_MakeCurrent(getSDLWindow(), sdlContext); |
| 98 | + |
| 99 | + std::string vendor = glGetString(GL_VENDOR) ? (const char*)glGetString(GL_VENDOR) : ""; |
| 100 | + std::string renderer = glGetString(GL_RENDERER) ? (const char*)glGetString(GL_RENDERER) : ""; |
| 101 | + std::string version = glGetString(GL_VERSION) ? (const char*)glGetString(GL_VERSION) : ""; |
| 102 | + std::string extensions = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : ""; |
| 103 | + |
| 104 | + LOG(LogInfo) << "GL vendor: " << vendor; |
| 105 | + LOG(LogInfo) << "GL renderer: " << renderer; |
| 106 | + LOG(LogInfo) << "GL version: " << version; |
| 107 | + LOG(LogInfo) << "Checking available OpenGL extensions..."; |
| 108 | + std::string glExts = glGetString(GL_EXTENSIONS) ? (const char*)glGetString(GL_EXTENSIONS) : ""; |
| 109 | + LOG(LogInfo) << " ARB_texture_non_power_of_two: " << (extensions.find("ARB_texture_non_power_of_two") != std::string::npos ? "ok" : "MISSING"); |
| 110 | + |
| 111 | + uint8_t data[4] = {255, 255, 255, 255}; |
| 112 | + whiteTexture = createTexture(Texture::RGBA, false, true, 1, 1, data); |
| 113 | + |
| 114 | + GL_CHECK_ERROR(glClearColor(0.0f, 0.0f, 0.0f, 1.0f)); |
| 115 | + GL_CHECK_ERROR(glEnable(GL_TEXTURE_2D)); |
| 116 | + GL_CHECK_ERROR(glEnable(GL_BLEND)); |
| 117 | + GL_CHECK_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 1)); |
| 118 | + GL_CHECK_ERROR(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); |
| 119 | + GL_CHECK_ERROR(glEnableClientState(GL_VERTEX_ARRAY)); |
| 120 | + GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY)); |
| 121 | + GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY)); |
| 122 | + |
| 123 | + } // createContext |
| 124 | + |
| 125 | + void destroyContext() |
| 126 | + { |
| 127 | + SDL_GL_DeleteContext(sdlContext); |
| 128 | + sdlContext = nullptr; |
| 129 | + |
| 130 | + } // destroyContext |
| 131 | + |
| 132 | + unsigned int createTexture(const Texture::Type _type, const bool _linear, const bool _repeat, const unsigned int _width, const unsigned int _height, void* _data) |
| 133 | + { |
| 134 | + const GLenum type = convertTextureType(_type); |
| 135 | + unsigned int texture; |
| 136 | + |
| 137 | + GL_CHECK_ERROR(glGenTextures(1, &texture)); |
| 138 | + GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); |
| 139 | + |
| 140 | + GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE)); |
| 141 | + GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, _repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE)); |
| 142 | + |
| 143 | + GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _linear ? GL_LINEAR : GL_NEAREST)); |
| 144 | + GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); |
| 145 | + |
| 146 | + GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, type, _width, _height, 0, type, GL_UNSIGNED_BYTE, _data)); |
| 147 | + |
| 148 | + return texture; |
| 149 | + |
| 150 | + } // createTexture |
| 151 | + |
| 152 | + void destroyTexture(const unsigned int _texture) |
| 153 | + { |
| 154 | + GL_CHECK_ERROR(glDeleteTextures(1, &_texture)); |
| 155 | + |
| 156 | + } // destroyTexture |
| 157 | + |
| 158 | + void updateTexture(const unsigned int _texture, const Texture::Type _type, const unsigned int _x, const unsigned _y, const unsigned int _width, const unsigned int _height, void* _data) |
| 159 | + { |
| 160 | + const GLenum type = convertTextureType(_type); |
| 161 | + |
| 162 | + GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); |
| 163 | + GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, _width, _height, type, GL_UNSIGNED_BYTE, _data)); |
| 164 | + GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture)); |
| 165 | + |
| 166 | + } // updateTexture |
| 167 | + |
| 168 | + void bindTexture(const unsigned int _texture) |
| 169 | + { |
| 170 | + if(_texture == 0) GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture)); |
| 171 | + else GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, _texture)); |
| 172 | + |
| 173 | + } // bindTexture |
| 174 | + |
| 175 | + void drawLines(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor) |
| 176 | + { |
| 177 | + GL_CHECK_ERROR(glVertexPointer( 2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos)); |
| 178 | + GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex)); |
| 179 | + GL_CHECK_ERROR(glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col)); |
| 180 | + |
| 181 | + GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor))); |
| 182 | + |
| 183 | + GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, _numVertices)); |
| 184 | + |
| 185 | + } // drawLines |
| 186 | + |
| 187 | + void drawTriangleStrips(const Vertex* _vertices, const unsigned int _numVertices, const Blend::Factor _srcBlendFactor, const Blend::Factor _dstBlendFactor) |
| 188 | + { |
| 189 | + GL_CHECK_ERROR(glVertexPointer( 2, GL_FLOAT, sizeof(Vertex), &_vertices[0].pos)); |
| 190 | + GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].tex)); |
| 191 | + GL_CHECK_ERROR(glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Vertex), &_vertices[0].col)); |
| 192 | + |
| 193 | + GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(_srcBlendFactor), convertBlendFactor(_dstBlendFactor))); |
| 194 | + |
| 195 | + GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, _numVertices)); |
| 196 | + |
| 197 | + } // drawTriangleStrips |
| 198 | + |
| 199 | + void setProjection(const Transform4x4f& _projection) |
| 200 | + { |
| 201 | + GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION)); |
| 202 | + GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&_projection)); |
| 203 | + |
| 204 | + } // setProjection |
| 205 | + |
| 206 | + void setMatrix(const Transform4x4f& _matrix) |
| 207 | + { |
| 208 | + Transform4x4f matrix = _matrix; |
| 209 | + matrix.round(); |
| 210 | + |
| 211 | + GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW)); |
| 212 | + GL_CHECK_ERROR(glLoadMatrixf((GLfloat*)&matrix)); |
| 213 | + |
| 214 | + } // setMatrix |
| 215 | + |
| 216 | + void setViewport(const Rect& _viewport) |
| 217 | + { |
| 218 | + // glViewport starts at the bottom left of the window |
| 219 | + GL_CHECK_ERROR(glViewport( _viewport.x, getWindowHeight() - _viewport.y - _viewport.h, _viewport.w, _viewport.h)); |
| 220 | + |
| 221 | + } // setViewport |
| 222 | + |
| 223 | + void setScissor(const Rect& _scissor) |
| 224 | + { |
| 225 | + if((_scissor.x == 0) && (_scissor.y == 0) && (_scissor.w == 0) && (_scissor.h == 0)) |
| 226 | + { |
| 227 | + GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST)); |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + // glScissor starts at the bottom left of the window |
| 232 | + GL_CHECK_ERROR(glScissor(_scissor.x, getWindowHeight() - _scissor.y - _scissor.h, _scissor.w, _scissor.h)); |
| 233 | + GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST)); |
| 234 | + } |
| 235 | + |
| 236 | + } // setScissor |
| 237 | + |
| 238 | + void setSwapInterval() |
| 239 | + { |
| 240 | + // vsync |
| 241 | + if(Settings::getInstance()->getBool("VSync")) |
| 242 | + { |
| 243 | + // SDL_GL_SetSwapInterval(0) for immediate updates (no vsync, default), |
| 244 | + // 1 for updates synchronized with the vertical retrace, |
| 245 | + // or -1 for late swap tearing. |
| 246 | + // SDL_GL_SetSwapInterval returns 0 on success, -1 on error. |
| 247 | + // if vsync is requested, try normal vsync; if that doesn't work, try late swap tearing |
| 248 | + // if that doesn't work, report an error |
| 249 | + if(SDL_GL_SetSwapInterval(1) != 0 && SDL_GL_SetSwapInterval(-1) != 0) |
| 250 | + LOG(LogWarning) << "Tried to enable vsync, but failed! (" << SDL_GetError() << ")"; |
| 251 | + } |
| 252 | + else |
| 253 | + SDL_GL_SetSwapInterval(0); |
| 254 | + |
| 255 | + } // setSwapInterval |
| 256 | + |
| 257 | + void swapBuffers() |
| 258 | + { |
| 259 | + SDL_GL_SwapWindow(getSDLWindow()); |
| 260 | + GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); |
| 261 | + |
| 262 | + } // swapBuffers |
| 263 | + |
| 264 | +} // Renderer:: |
| 265 | + |
| 266 | +#endif // USE_OPENGL_14 |
0 commit comments