Browse Source

texture and file texture

Kajetan Johannes Hammerle 3 years ago
parent
commit
bbefefa414
5 changed files with 148 additions and 2 deletions
  1. 47 0
      gl/Texture.cpp
  2. 42 0
      gl/Texture.h
  3. 5 2
      meson.build
  4. 26 0
      rendering/FileTexture.cpp
  5. 28 0
      rendering/FileTexture.h

+ 47 - 0
gl/Texture.cpp

@@ -0,0 +1,47 @@
+#include "gl/Texture.h"
+
+Texture::Texture(Mode mode) : texture(0) {
+    glGenTextures(1, &texture);
+    glBindTexture(GL_TEXTURE_2D, texture);
+    switch(mode) {
+        case NEAREST:
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+            break;
+        case LINEAR:
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+            break;
+    }
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+}
+
+Texture::~Texture() {
+    glDeleteTextures(1, &texture);
+}
+
+void Texture::setColorData(int width, int height, const Color4* data) {
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+}
+
+void Texture::setColorData(int width, int height, const Color3* data) {
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+}
+
+void Texture::setColorData(int width, int height, const Color2* data) {
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width, height, 0, GL_RG, GL_UNSIGNED_BYTE, data);
+}
+
+void Texture::setColorData(int width, int height, const Color1* data) {
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
+}
+
+void Texture::setRGBFloatData(int width, int height, const float* data) {
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, data);
+}
+
+void Texture::bind(int index) const {
+    glActiveTexture(GL_TEXTURE0 + index);
+    glBindTexture(GL_TEXTURE_2D, texture);
+}

+ 42 - 0
gl/Texture.h

@@ -0,0 +1,42 @@
+#ifndef TEXTURE_H
+#define TEXTURE_H
+
+#include <GL/glew.h>
+
+#include "utils/Color.h"
+
+class Texture final {
+    GLuint texture;
+
+public:
+
+    enum Mode {
+        NEAREST, LINEAR
+    };
+
+    Texture(Mode mode = NEAREST);
+    ~Texture();
+    Texture(const Texture& other) = delete;
+    Texture(Texture&& other) = delete;
+    Texture& operator=(const Texture& other) = delete;
+    Texture& operator=(Texture&& other) = delete;
+
+    void setColorData(int width, int height, const Color4* data);
+    void setColorData(int width, int height, const Color3* data);
+    void setColorData(int width, int height, const Color2* data);
+    void setColorData(int width, int height, const Color1* data);
+
+    void setRGBFloatData(int width, int height, const float* data);
+
+    void bind(int index = 0) const;
+
+private:
+
+    template<int N>
+    void setColorData(int width, int height, int mode, const Color<N>* data) {
+        glBindTexture(GL_TEXTURE_2D, texture);
+        glTexImage2D(GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, data);
+    }
+};
+
+#endif

+ 5 - 2
meson.build

@@ -29,11 +29,14 @@ sources = ['Main.cpp',
     'utils/Clock.cpp',
     'tests/ClockTests.cpp',
     'images/PNGReader.cpp',
-    'tests/PNGReaderTests.cpp']
+    'tests/PNGReaderTests.cpp',
+    'gl/Texture.cpp']
 
+glewDep = dependency('glew')
+glfwDep = dependency('glfw3')
 pngDep = dependency('libpng')
 
 executable('tests', 
     sources: sources,
-    dependencies : [pngDep],
+    dependencies : [glewDep, glfwDep, pngDep],
     cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror'])

+ 26 - 0
rendering/FileTexture.cpp

@@ -0,0 +1,26 @@
+#include "rendering/FileTexture.h"
+
+FileTexture::FileTexture(const char* path) {
+    PNGReader png(path);
+    if(png.hasError()) {
+        return;
+    }
+    switch(png.getChannels()) {
+        case 1:
+            read<1>(png);
+            break;
+        case 2:
+            read<2>(png);
+            break;
+        case 3:
+            read<3>(png);
+            break;
+        case 4:
+            read<4>(png);
+            break;
+    }
+}
+
+void FileTexture::bind(int index) const {
+    texture.bind(index);
+}

+ 28 - 0
rendering/FileTexture.h

@@ -0,0 +1,28 @@
+#ifndef FILETEXTURE_H
+#define FILETEXTURE_H
+
+#include "gl/Texture.h"
+#include "images/PNGReader.h"
+
+class FileTexture final {
+    Texture texture;
+
+public:
+    FileTexture(const char* path);
+
+    void bind(int index = 0) const;
+
+private:
+
+    template<int N>
+    void read(PNGReader& png) {
+        Color<N>* buffer = new Color<N>[png.getBufferSize()];
+        if(png.readData(buffer)) {
+            return;
+        }
+        texture.setColorData(png.getWidth(), png.getHeight(), buffer);
+        delete[] buffer;
+    }
+};
+
+#endif