Browse Source

all gl calls moved to one cpp file

Kajetan Johannes Hammerle 3 years ago
parent
commit
e73a9844da

+ 9 - 12
wrapper/Attributes.cpp

@@ -1,35 +1,32 @@
-#include <GL/glew.h>
-
 #include "wrapper/Attributes.h"
 
 Attributes& Attributes::addFloat(int count) {
-    attributes.add({count, sizeof(float), GL_FLOAT, false});
+    attributes.add(GL::Attribute::newFloat(count));
     return *this;
 }
 
 Attributes& Attributes::addColor4() {
-    attributes.add({4, sizeof(unsigned char), GL_UNSIGNED_BYTE, true});
+    attributes.add(GL::Attribute::newColor(4));
     return *this;
 }
 
 Attributes& Attributes::addSpacer() {
-    attributes.add({0, 0, -1, false});
+    attributes.add(GL::Attribute::newDummy());
     return *this;
 }
 
 void Attributes::set() const {
     int size = 0;
-    for(const Attribute& a : attributes) {
-        size += a.count * a.size;
+    for(const GL::Attribute& a : attributes) {
+        size += a.getSize();
     }
     int index = 0;
     int offset = 0;
-    for(const Attribute& a : attributes) {
-        if(a.type != -1) {
-            glVertexAttribPointer(index, a.count, a.type, a.normalized, size, static_cast<char*>(0) + offset);
-            glEnableVertexAttribArray(index);
+    for(const GL::Attribute& a : attributes) {
+        if(!a.isDummy()) {
+            GL::vertexAttribPointer(index, a, size, offset);
         }
-        offset += a.count * a.size;
+        offset += a.getSize();
         index++;
     }
 }

+ 2 - 8
wrapper/Attributes.h

@@ -2,16 +2,10 @@
 #define ATTRIBUTES_H
 
 #include "utils/ArrayList.h"
+#include "wrapper/GL.h"
 
 class Attributes final {
-    struct Attribute final {
-        int count;
-        int size;
-        int type;
-        bool normalized;
-    };
-
-    ArrayList<Attribute, 10> attributes;
+    ArrayList<GL::Attribute, 10> attributes;
 
     friend class VertexBuffer;
 

+ 12 - 51
wrapper/Framebuffer.h

@@ -10,7 +10,7 @@
 template<int N>
 class Framebuffer final {
     ArrayList<Texture, N> textures;
-    GLuint buffer;
+    GL::Framebuffer buffer;
 
 public:
     template<typename... Args>
@@ -29,7 +29,7 @@ public:
     }
 
     ~Framebuffer() {
-        glDeleteFramebuffers(1, &buffer);
+        GL::deleteFramebuffers(buffer);
     }
 
     Framebuffer(const Framebuffer&) = delete;
@@ -38,31 +38,27 @@ public:
     Framebuffer& operator=(Framebuffer&&) = delete;
 
     bool init(const Size& size) {
-        glGenFramebuffers(1, &buffer);
-        glBindFramebuffer(GL_FRAMEBUFFER, buffer);
+        buffer = GL::genFramebuffer();
+        GL::bindFramebuffer(buffer);
 
-        ArrayList<GLenum, N> attachments;
+        ArrayList<GL::ColorAttachment, N> attachments;
         for(Texture& t : textures) {
             t.setData(size.width, size.height);
             if(t.format.depth) {
-                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
-                                       GL_TEXTURE_2D, t.texture, 0);
+                GL::framebufferDepthTexture2D(t.texture);
             } else {
-                GLenum c = GL_COLOR_ATTACHMENT0 + attachments.getLength();
-                glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D,
-                                       t.texture, 0);
-                attachments.add(c);
+                attachments.add(GL::framebufferColorTexture2D(
+                    t.texture, attachments.getLength()));
             }
         }
-        glDrawBuffers(attachments.getLength(), attachments.begin());
+        GL::drawBuffers(attachments.getLength(), attachments.begin());
 
-        return hasError();
+        return GL::printFramebufferError();
     }
 
     void bindAndClear() {
-        glBindFramebuffer(GL_FRAMEBUFFER, buffer);
-        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
-                GL_STENCIL_BUFFER_BIT);
+        GL::bindFramebuffer(buffer);
+        GL::clear();
     }
 
     void bindTextureTo(int index, int textureUnit) const {
@@ -74,41 +70,6 @@ public:
             t.setData(size.width, size.height);
         }
     }
-
-private:
-    bool hasError() const {
-        GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
-        if(error == GL_FRAMEBUFFER_COMPLETE) {
-            return false;
-        }
-        switch(error) {
-            case GL_FRAMEBUFFER_UNDEFINED:
-                std::cout << "undefined framebuffer\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
-                std::cout << "incomplete framebuffer attachment\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
-                std::cout << "incomplete missing framebuffer attachment\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
-                std::cout << "incomplete framebuffer draw buffer\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
-                std::cout << "incomplete framebuffer read buffer\n";
-                return true;
-            case GL_FRAMEBUFFER_UNSUPPORTED:
-                std::cout << "unsupported framebuffer\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
-                std::cout << "incomplete framebuffer multisample\n";
-                return true;
-            case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
-                std::cout << "incomplete framebuffer layer targets\n";
-                return true;
-        }
-        return "unknown error";
-    }
 };
 
 #endif

+ 348 - 29
wrapper/GL.cpp

@@ -1,38 +1,113 @@
 #include <GL/glew.h>
 #include <iostream>
+#include <type_traits>
 
+#include "utils/Array.h"
 #include "wrapper/GL.h"
 
-bool GL::checkAndPrintError(const char* message) {
+static_assert(std::is_same<GL::Shader, GLuint>::value,
+              "shader has invalid type");
+static_assert(std::is_same<GL::Program, GLuint>::value, "p has invalid type");
+static_assert(std::is_same<char, GLchar>::value, "char has invalid type");
+static_assert(std::is_same<int, GLint>::value, "int has invalid type");
+static_assert(std::is_same<float, GLfloat>::value, "float has invalid type");
+static_assert(std::is_same<GL::ShaderType, GLenum>::value,
+              "shader type has invalid type");
+static_assert(std::is_same<GL::Texture, GLuint>::value,
+              "texture has invalid type");
+static_assert(std::is_same<GL::Framebuffer, GLuint>::value,
+              "framebuffer has invalid type");
+static_assert(std::is_same<GL::ColorAttachment, GLenum>::value,
+              "color attachment has invalid type");
+static_assert(std::is_same<GL::VertexArray, GLuint>::value,
+              "vertex array has invalid type");
+static_assert(std::is_same<GL::Buffer, GLuint>::value,
+              "buffer has invalid type");
+
+GL::ShaderType GL::VERTEX_SHADER = GL_VERTEX_SHADER;
+GL::ShaderType GL::FRAGMENT_SHADER = GL_FRAGMENT_SHADER;
+GL::ShaderType GL::GEOMETRY_SHADER = GL_GEOMETRY_SHADER;
+
+GL::Attribute::Attribute(int count, int size, int type, bool normalized)
+    : count(count), size(size), type(type), normalized(normalized) {
+}
+
+GL::Attribute GL::Attribute::newFloat(int count) {
+    return GL::Attribute(count, sizeof(float), GL_FLOAT, false);
+}
+
+GL::Attribute GL::Attribute::newColor(int count) {
+    return GL::Attribute(count, sizeof(float), GL_FLOAT, false);
+}
+
+GL::Attribute GL::Attribute::newDummy() {
+    return GL::Attribute(0, 0, -1, false);
+}
+
+bool GL::Attribute::isDummy() const {
+    return type == -1;
+}
+
+int GL::Attribute::getSize() const {
+    return count * size;
+}
+
+GL::TextureFormat::TextureFormat(int internalformat, int format, int type)
+    : internalformat(internalformat), format(format), type(type) {
+}
+
+GL::TextureFormat GL::TextureFormat::color8(int channels) {
+    switch(channels) {
+        case 1: return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE);
+        case 2: return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE);
+        case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
+        case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
+    }
+    std::cout << channels << " is not a valid amount of channels\n";
+    return unknown();
+}
+
+GL::TextureFormat GL::TextureFormat::float16(int channels) {
+    switch(channels) {
+        case 1: return TextureFormat(GL_R16F, GL_RED, GL_FLOAT);
+        case 2: return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT);
+        case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
+        case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
+    }
+    std::cout << channels << " is not a valid amount of channels\n";
+    return unknown();
+}
+
+GL::TextureFormat GL::TextureFormat::float32(int channels) {
+    switch(channels) {
+        case 1: return TextureFormat(GL_R32F, GL_RED, GL_FLOAT);
+        case 2: return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT);
+        case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
+        case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
+    }
+    std::cout << channels << " is not a valid amount of channels\n";
+    return unknown();
+}
+
+GL::TextureFormat GL::TextureFormat::depth16() {
+    return TextureFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT);
+}
+
+GL::TextureFormat GL::TextureFormat::depth32() {
+    return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT);
+}
+
+GL::TextureFormat GL::TextureFormat::unknown() {
+    return TextureFormat(-1, -1, -1);
+}
+
+bool GL::printError(const char* message) {
     GLenum error = glGetError();
-    switch(error) {
-        case GL_NO_ERROR: return false;
-        case GL_INVALID_ENUM:
-            std::cout << message << ": an unacceptable value is specified for an enumerated argument\n";
-            break;
-        case GL_INVALID_VALUE: std::cout << message << ": a numeric argument is out of range\n"; break;
-        case GL_INVALID_OPERATION:
-            std::cout << message << ": the specified operation is not allowed in the current state\n";
-            break;
-        case GL_INVALID_FRAMEBUFFER_OPERATION:
-            std::cout << message << ": the framebuffer object is not complete\n";
-            break;
-        case GL_OUT_OF_MEMORY:
-            std::cout << message << ": there is not enough memory left to execute the command\n";
-            break;
-        case GL_STACK_UNDERFLOW:
-            std::cout << message
-                      << ": an attempt has been made to perform an operation that would cause an internal stack to "
-                         "underflow\n";
-            break;
-        case GL_STACK_OVERFLOW:
-            std::cout << message
-                      << ": an attempt has been made to perform an operation that would cause an internal stack to "
-                         "overflow\n";
-            break;
-        default: std::cout << message << ": unknown OpenGL error '" << error << "'\n";
+    if(error != GL_NO_ERROR) {
+        std::cout << message << ": " << gluErrorString(error) << '\n';
+        return true;
     }
-    return true;
+    return false;
 }
 
 void GL::enableDepthTesting() {
@@ -47,7 +122,7 @@ void GL::bindMainFramebuffer() {
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 
-void GL::clearFramebuffer() {
+void GL::clear() {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
@@ -63,4 +138,248 @@ void GL::disableBlending() {
 
 void GL::setViewport(int width, int height) {
     glViewport(0, 0, width, height);
+}
+
+void GL::vertexAttribPointer(int index, const Attribute& a, int stride,
+                             int offset) {
+    glVertexAttribPointer(index, a.count, a.type, a.normalized, stride,
+                          static_cast<char*>(0) + offset);
+    glEnableVertexAttribArray(index);
+}
+
+GL::Program GL::createProgram() {
+    return glCreateProgram();
+}
+
+void GL::attachShader(Program p, Shader s) {
+    glAttachShader(p, s);
+}
+
+void GL::linkProgram(Program p) {
+    glLinkProgram(p);
+}
+
+bool GL::logLinkerError(Program p) {
+    GLint linked;
+    glGetProgramiv(p, GL_LINK_STATUS, &linked);
+    if(!linked) {
+        Array<char, 1024> log;
+        glGetProgramInfoLog(p, log.getLength(), nullptr, log.begin());
+        std::cout << "linker log: " << log.begin() << "\n";
+        return true;
+    }
+    return false;
+}
+
+void GL::deleteShader(Shader s) {
+    glDeleteShader(s);
+}
+
+void GL::deleteProgram(Program p) {
+    glDeleteProgram(p);
+}
+
+GL::Shader GL::createShader(ShaderType type) {
+    return glCreateShader(type);
+}
+
+void GL::compileShader(Shader s, const char* code) {
+    glShaderSource(s, 1, &code, nullptr);
+    glCompileShader(s);
+}
+
+bool GL::logCompileError(Shader s) {
+    GLint compiled;
+    glGetShaderiv(s, GL_COMPILE_STATUS, &compiled);
+    if(!compiled) {
+        Array<char, 1024> log;
+        glGetShaderInfoLog(s, log.getLength(), nullptr, log.begin());
+        std::cout << "compiler log: " << log.begin() << "\n";
+        return true;
+    }
+    return false;
+}
+
+void GL::useProgram(Program p) {
+    glUseProgram(p);
+}
+
+void GL::setMatrix(Program p, const char* name, const float* data) {
+    glUniformMatrix4fv(glGetUniformLocation(p, name), 1, GL_TRUE, data);
+}
+
+void GL::setInt(Program p, const char* name, int data) {
+    glUniform1i(glGetUniformLocation(p, name), data);
+}
+
+void GL::setFloat(Program p, const char* name, float data) {
+    glUniform1f(glGetUniformLocation(p, name), data);
+}
+
+void GL::set2Float(Program p, const char* name, const float* data) {
+    glUniform2fv(glGetUniformLocation(p, name), 1, data);
+}
+
+void GL::set3Float(Program p, const char* name, const float* data) {
+    glUniform3fv(glGetUniformLocation(p, name), 1, data);
+}
+
+void GL::set4Float(Program p, const char* name, const float* data) {
+    glUniform4fv(glGetUniformLocation(p, name), 1, data);
+}
+
+void GL::texImage2D(const TextureFormat& tf, int width, int height,
+                    const void* data, int level) {
+    glTexImage2D(GL_TEXTURE_2D, level, tf.internalformat, width, height, 0,
+                 tf.format, tf.type, data);
+}
+
+GL::Texture GL::genTexture() {
+    Texture t;
+    glGenTextures(1, &t);
+    return t;
+}
+
+void GL::deleteTexture(Texture t) {
+    glDeleteTextures(1, &t);
+}
+
+void GL::setNearFilter2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+}
+
+void GL::setMipMapNearFilter2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+                    GL_NEAREST_MIPMAP_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+}
+
+void GL::setLinearFilter2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+}
+
+void GL::setMipMapLinearFilter2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+                    GL_LINEAR_MIPMAP_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+}
+
+void GL::setClampWrap2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+}
+
+void GL::setRepeatWrap2D() {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+}
+
+void GL::bindTexture2D(Texture t) {
+    glBindTexture(GL_TEXTURE_2D, t);
+}
+
+void GL::activeTexture(int index) {
+    glActiveTexture(GL_TEXTURE0 + index);
+}
+
+void GL::generateMipmap2D(int maxLevels) {
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevels);
+    glGenerateMipmap(GL_TEXTURE_2D);
+}
+
+void GL::deleteFramebuffers(Framebuffer fb) {
+    glDeleteFramebuffers(1, &fb);
+}
+
+GL::Framebuffer GL::genFramebuffer() {
+    Framebuffer fb;
+    glGenFramebuffers(1, &fb);
+    return fb;
+}
+
+void GL::bindFramebuffer(Framebuffer fb) {
+    glBindFramebuffer(GL_FRAMEBUFFER, fb);
+}
+
+void GL::framebufferDepthTexture2D(Texture t) {
+    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
+                           t, 0);
+}
+
+GL::ColorAttachment GL::framebufferColorTexture2D(Texture t, int index) {
+    GLenum c = GL_COLOR_ATTACHMENT0 + index;
+    glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D, t, 0);
+    return c;
+}
+
+void GL::drawBuffers(int length, ColorAttachment* c) {
+    glDrawBuffers(length, c);
+}
+
+bool GL::printFramebufferError() {
+    GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+    if(error == GL_FRAMEBUFFER_COMPLETE) {
+        std::cout << "framebuffer error: " << error << '\n';
+        return true;
+    }
+    return false;
+}
+
+GL::VertexArray GL::genVertexArray() {
+    VertexArray va;
+    glGenVertexArrays(1, &va);
+    return va;
+}
+
+GL::Buffer GL::genBuffer() {
+    Buffer b;
+    glGenBuffers(1, &b);
+    return b;
+}
+
+void GL::deleteBuffer(Buffer b) {
+    glDeleteBuffers(1, &b);
+}
+
+void GL::deleteVertexArray(VertexArray va) {
+    glDeleteVertexArrays(1, &va);
+}
+
+void GL::bindVertexArray(VertexArray va) {
+    glBindVertexArray(va);
+}
+
+void GL::bindBuffer(Buffer b) {
+    glBindBuffer(GL_ARRAY_BUFFER, b);
+}
+
+void GL::bufferDataStatic(int size, const void* data) {
+    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
+}
+
+void GL::bufferDataStream(int size, const void* data) {
+    glBufferData(GL_ARRAY_BUFFER, size, data, GL_STREAM_DRAW);
+}
+
+void GL::bufferDataDynamic(int size, const void* data) {
+    glBufferData(GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW);
+}
+
+void GL::bufferSubData(int offset, int size, const void* data) {
+    glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
+}
+
+void GL::drawTriangles(int offset, int vertices) {
+    glDrawArrays(GL_TRIANGLES, offset, vertices);
+}
+
+void GL::drawTriangleStrip(int offset, int vertices) {
+    glDrawArrays(GL_TRIANGLE_STRIP, offset, vertices);
+}
+
+void GL::drawPoints(int offset, int vertices) {
+    glDrawArrays(GL_POINTS, offset, vertices);
 }

+ 106 - 2
wrapper/GL.h

@@ -2,14 +2,118 @@
 #define GL_H
 
 namespace GL {
-    bool checkAndPrintError(const char* message);
+    typedef unsigned int Shader;
+    typedef unsigned int Program;
+    typedef unsigned int ShaderType;
+    typedef unsigned int Texture;
+    typedef unsigned int Framebuffer;
+    typedef unsigned int ColorAttachment;
+    typedef unsigned int VertexArray;
+    typedef unsigned int Buffer;
+
+    extern ShaderType VERTEX_SHADER;
+    extern ShaderType FRAGMENT_SHADER;
+    extern ShaderType GEOMETRY_SHADER;
+
+    class Attribute final {
+        int count;
+        int size;
+        int type;
+        bool normalized;
+
+        Attribute(int count, int size, int type, bool normalized);
+
+        friend void vertexAttribPointer(int index, const Attribute& a,
+                                        int stride, int offset);
+
+    public:
+        bool isDummy() const;
+        int getSize() const;
+
+        static Attribute newFloat(int count);
+        static Attribute newColor(int count);
+        static Attribute newDummy();
+    };
+
+    class TextureFormat final {
+        int internalformat;
+        int format;
+        int type;
+
+        TextureFormat(int internalformat, int format, int type);
+
+        friend void texImage2D(const TextureFormat& format, int width,
+                               int height, const void* data, int level);
+
+    public:
+        static TextureFormat color8(int channels);
+        static TextureFormat float16(int channels);
+        static TextureFormat float32(int channels);
+        static TextureFormat depth16();
+        static TextureFormat depth32();
+        static TextureFormat unknown();
+    };
+
+    bool printError(const char* message);
     void enableDepthTesting();
     void disableDepthTesting();
     void bindMainFramebuffer();
-    void clearFramebuffer();
+    void clear();
     void enableBlending();
     void disableBlending();
     void setViewport(int width, int height);
+    void vertexAttribPointer(int index, const Attribute& a, int stride,
+                             int offset);
+    Program createProgram();
+    void attachShader(Program p, Shader s);
+    void linkProgram(Program p);
+    bool logLinkerError(Program p);
+    void deleteShader(Shader s);
+    void deleteProgram(Program p);
+    Shader createShader(ShaderType type);
+    void compileShader(Shader s, const char* code);
+    bool logCompileError(Shader s);
+    void useProgram(Program p);
+    void setMatrix(Program p, const char* name, const float* data);
+    void setInt(Program p, const char* name, int data);
+    void setFloat(Program p, const char* name, float data);
+    void set2Float(Program p, const char* name, const float* data);
+    void set3Float(Program p, const char* name, const float* data);
+    void set4Float(Program p, const char* name, const float* data);
+    void texImage2D(const TextureFormat& format, int width, int height,
+                    const void* data, int level = 0);
+    Texture genTexture();
+    void deleteTexture(Texture t);
+    void setNearFilter2D();
+    void setMipMapNearFilter2D();
+    void setLinearFilter2D();
+    void setMipMapLinearFilter2D();
+    void setClampWrap2D();
+    void setRepeatWrap2D();
+    void setMipMapLinearFilter2D();
+    void bindTexture2D(Texture t);
+    void activeTexture(int index);
+    void generateMipmap2D(int maxLevels);
+    void deleteFramebuffers(Framebuffer fb);
+    Framebuffer genFramebuffer();
+    void bindFramebuffer(Framebuffer fb);
+    void framebufferDepthTexture2D(Texture t);
+    ColorAttachment framebufferColorTexture2D(Texture t, int index);
+    void drawBuffers(int length, ColorAttachment* c);
+    bool printFramebufferError();
+    VertexArray genVertexArray();
+    Buffer genBuffer();
+    void deleteBuffer(Buffer b);
+    void deleteVertexArray(VertexArray va);
+    void bindVertexArray(VertexArray va);
+    void bindBuffer(Buffer b);
+    void bufferDataStatic(int size, const void* data);
+    void bufferDataStream(int size, const void* data);
+    void bufferDataDynamic(int size, const void* data);
+    void bufferSubData(int offset, int size, const void* data);
+    void drawTriangles(int offset, int vertices);
+    void drawTriangleStrip(int offset, int vertices);
+    void drawPoints(int offset, int vertices);
 }
 
 #endif

+ 43 - 53
wrapper/Shader.cpp

@@ -7,30 +7,25 @@
 Shader::Shader(const char* vertexPath, const char* fragmentPath,
                const char* geometryPath)
     : vertexShader(0), geometryShader(0), fragmentShader(0), program(0) {
-    if(readFileAndCompile(vertexPath, vertexShader, GL_VERTEX_SHADER) ||
-       readFileAndCompile(fragmentPath, fragmentShader, GL_FRAGMENT_SHADER)) {
+    if(compile(vertexPath, vertexShader, GL::VERTEX_SHADER) ||
+       compile(fragmentPath, fragmentShader, GL::FRAGMENT_SHADER)) {
         return;
     }
     if(geometryPath != nullptr &&
-       readFileAndCompile(geometryPath, geometryShader, GL_GEOMETRY_SHADER)) {
+       compile(geometryPath, geometryShader, GL::GEOMETRY_SHADER)) {
         return;
     }
-    program = glCreateProgram();
-    glAttachShader(program, vertexShader);
+    program = GL::createProgram();
+    GL::attachShader(program, vertexShader);
     if(geometryPath != nullptr) {
-        glAttachShader(program, geometryShader);
+        GL::attachShader(program, geometryShader);
     }
-    glAttachShader(program, fragmentShader);
-    glLinkProgram(program);
-    if(GL::checkAndPrintError("cannot link")) {
+    GL::attachShader(program, fragmentShader);
+    GL::linkProgram(program);
+    if(GL::printError("cannot link")) {
         return;
     }
-    GLint linked;
-    glGetProgramiv(program, GL_LINK_STATUS, &linked);
-    if(!linked) {
-        ErrorLog log;
-        glGetProgramInfoLog(program, log.getLength(), nullptr, log.begin());
-        std::cout << "linker log: " << log.begin() << "\n";
+    if(GL::logLinkerError(program)) {
         clean();
         return;
     }
@@ -41,31 +36,36 @@ Shader::~Shader() {
 }
 
 void Shader::clean() {
-    glDeleteShader(vertexShader);
-    glDeleteShader(geometryShader);
-    glDeleteShader(fragmentShader);
-    glDeleteProgram(program);
+    GL::deleteShader(vertexShader);
+    GL::deleteShader(geometryShader);
+    GL::deleteShader(fragmentShader);
+    GL::deleteProgram(program);
     program = 0;
 }
 
-bool Shader::readFileAndCompile(const char* path, GLuint& shader,
-                                GLenum shaderType) {
+bool Shader::compile(const char* path, GL::Shader& s, GL::ShaderType st) {
     std::cout << "shader: " << path << '\n';
-    Code code;
+    List<char> code;
     if(readFile(code, path)) {
         return true;
     }
-    return compile(shader, code, shaderType);
+    return compile(s, code, st);
 }
 
-bool Shader::readFile(Code& code, const char* path) const {
+bool Shader::readFile(List<char>& code, const char* path) const {
     std::ifstream in;
     in.open(path);
     if(!in.good()) {
         std::cout << "cannot read file\n";
         return true;
     }
-    in.get(code.begin(), code.getLength(), EOF);
+    while(true) {
+        int c = in.get();
+        if(c == EOF) {
+            return false;
+        }
+        code.add(c);
+    }
     return false;
 }
 
@@ -73,49 +73,39 @@ bool Shader::hasError() const {
     return vertexShader == 0 || fragmentShader == 0 || program == 0;
 }
 
-bool Shader::compile(GLuint& shader, const Code& code, GLenum shaderType) {
-    shader = glCreateShader(shaderType);
-    const GLchar* buffer = code.begin();
-    glShaderSource(shader, 1, &buffer, nullptr);
-    glCompileShader(shader);
-    if(GL::checkAndPrintError("compile error")) {
-        return true;
-    }
-    GLint compiled;
-    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
-    if(!compiled) {
-        ErrorLog log;
-        glGetShaderInfoLog(shader, log.getLength(), nullptr, log.begin());
-        std::cout << "compiler log: " << log.begin() << "\n";
+bool Shader::compile(GL::Shader& s, const List<char>& code, GL::ShaderType st) {
+    s = GL::createShader(st);
+    GL::compileShader(s, code.begin());
+    if(GL::printError("compile error")) {
         return true;
     }
-    return false;
+    return GL::logCompileError(s);
 }
 
 void Shader::use() const {
-    glUseProgram(program);
+    GL::useProgram(program);
 }
 
-void Shader::setMatrix(const GLchar* name, const GLfloat* data) {
-    glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_TRUE, data);
+void Shader::setMatrix(const char* name, const float* data) {
+    GL::setMatrix(program, name, data);
 }
 
-void Shader::setInt(const GLchar* name, GLint data) {
-    glUniform1i(glGetUniformLocation(program, name), data);
+void Shader::setInt(const char* name, int data) {
+    GL::setInt(program, name, data);
 }
 
-void Shader::setFloat(const GLchar* name, GLfloat data) {
-    glUniform1f(glGetUniformLocation(program, name), data);
+void Shader::setFloat(const char* name, float data) {
+    GL::setFloat(program, name, data);
 }
 
-void Shader::setVector(const GLchar* name, const Vector2& v) {
-    glUniform2fv(glGetUniformLocation(program, name), 1, &(v[0]));
+void Shader::setVector(const char* name, const Vector2& v) {
+    GL::set2Float(program, name, &(v[0]));
 }
 
-void Shader::setVector(const GLchar* name, const Vector3& v) {
-    glUniform3fv(glGetUniformLocation(program, name), 1, &(v[0]));
+void Shader::setVector(const char* name, const Vector3& v) {
+    GL::set3Float(program, name, &(v[0]));
 }
 
-void Shader::setVector(const GLchar* name, const Vector4& v) {
-    glUniform4fv(glGetUniformLocation(program, name), 1, &(v[0]));
+void Shader::setVector(const char* name, const Vector4& v) {
+    GL::set4Float(program, name, &(v[0]));
 }

+ 15 - 19
wrapper/Shader.h

@@ -1,16 +1,15 @@
 #ifndef SHADER_H
 #define SHADER_H
 
-#include <GL/glew.h>
-
 #include "math/Vector.h"
-#include "utils/Array.h"
+#include "utils/List.h"
+#include "wrapper/GL.h"
 
 class Shader final {
-    GLuint vertexShader;
-    GLuint geometryShader;
-    GLuint fragmentShader;
-    GLuint program;
+    GL::Shader vertexShader;
+    GL::Shader geometryShader;
+    GL::Shader fragmentShader;
+    GL::Program program;
 
 public:
     Shader(const char* vertexPath, const char* fragmentPath,
@@ -24,22 +23,19 @@ public:
     bool hasError() const;
 
     void use() const;
-    void setMatrix(const GLchar* name, const GLfloat* data);
-    void setInt(const GLchar* name, GLint data);
-    void setFloat(const GLchar* name, GLfloat data);
+    void setMatrix(const char* name, const float* data);
+    void setInt(const char* name, int data);
+    void setFloat(const char* name, float data);
 
-    void setVector(const GLchar* name, const Vector2& v);
-    void setVector(const GLchar* name, const Vector3& v);
-    void setVector(const GLchar* name, const Vector4& v);
+    void setVector(const char* name, const Vector2& v);
+    void setVector(const char* name, const Vector3& v);
+    void setVector(const char* name, const Vector4& v);
 
 private:
     void clean();
-    typedef Array<GLchar, 1024 * 128> Code;
-    typedef Array<GLchar, 1024> ErrorLog;
-    bool readFileAndCompile(const char* path, GLuint& shader,
-                            GLenum shaderType);
-    bool readFile(Code& code, const char* path) const;
-    bool compile(GLuint& shader, const Code& code, GLenum shaderType);
+    bool compile(const char* path, GL::Shader& s, GL::ShaderType st);
+    bool readFile(List<char>& code, const char* path) const;
+    bool compile(GL::Shader& s, const List<char>& code, GL::ShaderType st);
 };
 
 #endif

+ 19 - 28
wrapper/Texture.cpp

@@ -1,73 +1,64 @@
 #include "wrapper/Texture.h"
 
-Texture::Texture(const TextureFormat& format, int maxMipMaps) : format(format), texture(0), maxMipMaps(maxMipMaps) {
-    glGenTextures(1, &texture);
+Texture::Texture(const TextureFormat& format, int maxMipMaps)
+    : format(format), texture(GL::genTexture()), maxMipMaps(maxMipMaps) {
     setNearestFilter();
     setRepeatWrap();
 }
 
-Texture::Texture(int maxMipMaps) : Texture(TextureFormat::unknown(), maxMipMaps) {
+Texture::Texture(int maxMipMaps)
+    : Texture(TextureFormat::unknown(), maxMipMaps) {
 }
 
 Texture::~Texture() {
-    glDeleteTextures(1, &texture);
+    GL::deleteTexture(texture);
 }
 
 void Texture::setFormat(const TextureFormat& tf) {
     format = tf;
 }
 
-void Texture::setFilter(GLint minParam, GLint maxParam) {
-    bind();
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minParam);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxParam);
-}
-
 void Texture::setNearestFilter() {
+    bind();
     if(maxMipMaps > 0) {
-        setFilter(GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST);
+        GL::setMipMapNearFilter2D();
     } else {
-        setFilter(GL_NEAREST, GL_NEAREST);
+        GL::setNearFilter2D();
     }
 }
 
 void Texture::setLinearFilter() {
+    bind();
     if(maxMipMaps > 0) {
-        setFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
+        GL::setMipMapLinearFilter2D();
     } else {
-        setFilter(GL_LINEAR, GL_LINEAR);
+        GL::setLinearFilter2D();
     }
 }
 
-void Texture::setWrap(GLint param) {
-    bind();
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
-}
-
 void Texture::setRepeatWrap() {
-    setWrap(GL_REPEAT);
+    bind();
+    GL::setRepeatWrap2D();
 }
 
 void Texture::setClampWrap() {
-    setWrap(GL_CLAMP_TO_EDGE);
+    bind();
+    GL::setClampWrap2D();
 }
 
 void Texture::setData(int width, int height, const void* data) {
     bind();
-    glTexImage2D(GL_TEXTURE_2D, 0, format.internalformat, width, height, 0, format.format, format.type, data);
+    GL::texImage2D(format.format, width, height, data);
     if(maxMipMaps > 0) {
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxMipMaps);
-        glGenerateMipmap(GL_TEXTURE_2D);
+        GL::generateMipmap2D(maxMipMaps);
     }
 }
 
 void Texture::bind() const {
-    glBindTexture(GL_TEXTURE_2D, texture);
+    GL::bindTexture2D(texture);
 }
 
 void Texture::bindTo(int index) const {
-    glActiveTexture(GL_TEXTURE0 + index);
+    GL::activeTexture(index);
     bind();
 }

+ 2 - 5
wrapper/Texture.h

@@ -1,13 +1,12 @@
 #ifndef TEXTURE_H
 #define TEXTURE_H
 
-#include <GL/glew.h>
-
+#include "wrapper/GL.h"
 #include "wrapper/TextureFormat.h"
 
 class Texture final {
     TextureFormat format;
-    GLuint texture;
+    GL::Texture texture;
     int maxMipMaps;
 
     template<int N>
@@ -32,8 +31,6 @@ public:
     void bindTo(int index = 0) const;
 
 private:
-    void setFilter(GLint minParam, GLint maxParam);
-    void setWrap(GLint param);
     void bind() const;
 };
 

+ 9 - 31
wrapper/TextureFormat.cpp

@@ -1,52 +1,30 @@
-#include <iostream>
-
 #include "wrapper/TextureFormat.h"
 
-TextureFormat::TextureFormat(GLint internalformat, GLenum format, GLenum type, bool linear, bool depth)
-    : internalformat(internalformat), format(format), type(type), linear(linear), depth(depth) {
+TextureFormat::TextureFormat(const GL::TextureFormat& tf, bool linear,
+                             bool depth)
+    : format(tf), linear(linear), depth(depth) {
 }
 
 TextureFormat TextureFormat::color8(int channels, bool linear) {
-    switch(channels) {
-        case 1: return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE, linear);
-        case 2: return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE, linear);
-        case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, linear);
-        case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, linear);
-    }
-    std::cout << channels << " is not a valid amount of channels\n";
-    return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, linear);
+    return TextureFormat(GL::TextureFormat::color8(channels), linear);
 }
 
 TextureFormat TextureFormat::float16(int channels, bool linear) {
-    switch(channels) {
-        case 1: return TextureFormat(GL_R16F, GL_RED, GL_FLOAT, linear);
-        case 2: return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT, linear);
-        case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT, linear);
-        case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT, linear);
-    }
-    std::cout << channels << " is not a valid amount of channels\n";
-    return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT, linear);
+    return TextureFormat(GL::TextureFormat::float16(channels), linear);
 }
 
 TextureFormat TextureFormat::float32(int channels, bool linear) {
-    switch(channels) {
-        case 1: return TextureFormat(GL_R32F, GL_RED, GL_FLOAT, linear);
-        case 2: return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT, linear);
-        case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT, linear);
-        case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT, linear);
-    }
-    std::cout << channels << " is not a valid amount of channels\n";
-    return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT, linear);
+    return TextureFormat(GL::TextureFormat::float32(channels), linear);
 }
 
 TextureFormat TextureFormat::depth16(bool linear) {
-    return TextureFormat(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT, linear, true);
+    return TextureFormat(GL::TextureFormat::depth16(), linear, true);
 }
 
 TextureFormat TextureFormat::depth32(bool linear) {
-    return TextureFormat(GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_FLOAT, linear, true);
+    return TextureFormat(GL::TextureFormat::depth32(), linear, true);
 }
 
 TextureFormat TextureFormat::unknown() {
-    return TextureFormat(-1, -1, -1, false, false);
+    return TextureFormat(GL::TextureFormat::unknown(), false, false);
 }

+ 3 - 5
wrapper/TextureFormat.h

@@ -1,12 +1,10 @@
 #ifndef TEXTUREDATA_H
 #define TEXTUREDATA_H
 
-#include <GL/glew.h>
+#include "wrapper/GL.h"
 
 struct TextureFormat final {
-    GLint internalformat;
-    GLenum format;
-    GLenum type;
+    GL::TextureFormat format;
     bool linear;
     bool depth;
 
@@ -18,7 +16,7 @@ struct TextureFormat final {
     static TextureFormat unknown();
 
 private:
-    TextureFormat(GLint internalformat, GLenum format, GLenum type, bool linear, bool depth = false);
+    TextureFormat(const GL::TextureFormat& tf, bool linear, bool depth = false);
 };
 
 #endif

+ 19 - 24
wrapper/VertexBuffer.cpp

@@ -1,21 +1,20 @@
 #include "wrapper/VertexBuffer.h"
 
-VertexBuffer::VertexBuffer() : vertexArray(0), vertexBuffer(0) {
-    glGenVertexArrays(1, &vertexArray);
-    glGenBuffers(1, &vertexBuffer);
+VertexBuffer::VertexBuffer()
+    : vertexArray(GL::genVertexArray()), vertexBuffer(GL::genBuffer()) {
 }
 
 VertexBuffer::~VertexBuffer() {
-    glDeleteBuffers(1, &vertexBuffer);
-    glDeleteVertexArrays(1, &vertexArray);
+    GL::deleteBuffer(vertexBuffer);
+    GL::deleteVertexArray(vertexArray);
 }
 
 void VertexBuffer::bindArray() const {
-    glBindVertexArray(vertexArray);
+    GL::bindVertexArray(vertexArray);
 }
 
 void VertexBuffer::bindBuffer() const {
-    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
+    GL::bindBuffer(vertexBuffer);
 }
 
 void VertexBuffer::setAttributes(const Attributes& attributes) {
@@ -24,41 +23,37 @@ void VertexBuffer::setAttributes(const Attributes& attributes) {
     attributes.set();
 }
 
-void VertexBuffer::setData(int size, const void* data, int flag) {
-    bindBuffer();
-    glBufferData(GL_ARRAY_BUFFER, size, data, flag);
-}
-
 void VertexBuffer::setStaticData(int size, const void* data) {
-    setData(size, data, GL_STATIC_DRAW);
+    bindBuffer();
+    GL::bufferDataStatic(size, data);
 }
 
 void VertexBuffer::setStreamData(int size, const void* data) {
-    setData(size, data, GL_STREAM_DRAW);
+    bindBuffer();
+    GL::bufferDataStream(size, data);
 }
 
 void VertexBuffer::setDynamicData(int size, const void* data) {
-    setData(size, data, GL_DYNAMIC_DRAW);
+    bindBuffer();
+    GL::bufferDataDynamic(size, data);
 }
 
 void VertexBuffer::updateData(int offset, int size, const void* data) {
     bindBuffer();
-    glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
-}
-
-void VertexBuffer::drawMode(int vertices, int mode, int offset) {
-    bindArray();
-    glDrawArrays(mode, offset, vertices);
+    GL::bufferSubData(offset, size, data);
 }
 
 void VertexBuffer::draw(int vertices, int offset) {
-    drawMode(vertices, GL_TRIANGLES, offset);
+    bindArray();
+    GL::drawTriangles(offset, vertices);
 }
 
 void VertexBuffer::drawStrip(int vertices, int offset) {
-    drawMode(vertices, GL_TRIANGLE_STRIP, offset);
+    bindArray();
+    GL::drawTriangleStrip(offset, vertices);
 }
 
 void VertexBuffer::drawPoints(int vertices, int offset) {
-    drawMode(vertices, GL_POINTS, offset);
+    bindArray();
+    GL::drawPoints(offset, vertices);
 }

+ 3 - 6
wrapper/VertexBuffer.h

@@ -1,13 +1,12 @@
 #ifndef VERTEXBUFFER_H
 #define VERTEXBUFFER_H
 
-#include <GL/glew.h>
-
 #include "wrapper/Attributes.h"
+#include "wrapper/GL.h"
 
 class VertexBuffer final {
-    GLuint vertexArray;
-    GLuint vertexBuffer;
+    GL::VertexArray vertexArray;
+    GL::Buffer vertexBuffer;
 
 public:
     VertexBuffer();
@@ -28,8 +27,6 @@ public:
     void drawPoints(int vertices, int offset = 0);
 
 private:
-    void drawMode(int vertices, int mode, int offset = 0);
-    void setData(int size, const void* data, int flag);
     void bindArray() const;
     void bindBuffer() const;
 };