Browse Source

more generic texture format, file texture is a template

Kajetan Johannes Hammerle 3 years ago
parent
commit
badafb1737
5 changed files with 46 additions and 91 deletions
  1. 1 0
      Main.cpp
  2. 0 26
      rendering/FileTexture.cpp
  3. 16 10
      rendering/FileTexture.h
  4. 26 39
      wrapper/TextureFormat.cpp
  5. 3 16
      wrapper/TextureFormat.h

+ 1 - 0
Main.cpp

@@ -21,6 +21,7 @@
 #include "tests/PNGReaderTests.h"
 #include "tests/BufferTests.h"
 #include "wrapper/Framebuffer.h"
+#include "rendering/FileTexture.h"
 
 int main(int argAmount, char** args) {
     if(argAmount < 2) {

+ 0 - 26
rendering/FileTexture.cpp

@@ -1,26 +0,0 @@
-#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);
-}

+ 16 - 10
rendering/FileTexture.h

@@ -4,25 +4,31 @@
 #include "wrapper/Texture.h"
 #include "images/PNGReader.h"
 
+template<int N>
 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)) {
+    FileTexture(const char* path) : texture(TextureFormat::color8(N)) {
+        PNGReader png(path);
+        if(png.hasError()) {
+            return;
+        }
+        if(png.getChannels() != N) {
+            std::cout << "expected " << N << " from '" << path << "' but got " << png.getChannels() << "\n";
             return;
         }
-        texture.setColorData(png.getWidth(), png.getHeight(), buffer);
+        Color<N>* buffer = new Color<N>[png.getBufferSize()];
+        if(!png.readData(buffer)) {
+            texture.setData(png.getWidth(), png.getHeight(), buffer);
+        }
         delete[] buffer;
     }
+
+    void bindTo(int index) const {
+        texture.bindTo(index);
+    }
 };
 
 #endif

+ 26 - 39
wrapper/TextureFormat.cpp

@@ -1,54 +1,41 @@
+#include <iostream>
+
 #include "wrapper/TextureFormat.h"
 
 TextureFormat::TextureFormat(GLint internalformat, GLenum format, GLenum type, bool depth) :
 internalformat(internalformat), format(format), type(type), depth(depth) {
 }
 
-TextureFormat TextureFormat::color1() {
-    return TextureFormat(GL_RED, GL_RED, GL_UNSIGNED_BYTE);
-}
-
-TextureFormat TextureFormat::color2() {
-    return TextureFormat(GL_RG, GL_RG, GL_UNSIGNED_BYTE);
-}
-
-TextureFormat TextureFormat::color3() {
-    return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
-}
-
-TextureFormat TextureFormat::color4() {
+TextureFormat 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 TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
 }
 
-TextureFormat TextureFormat::float16() {
-    return TextureFormat(GL_R16F, GL_RED, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float32() {
-    return TextureFormat(GL_R32F, GL_RED, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float16Vector2() {
-    return TextureFormat(GL_RG16F, GL_RG, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float16Vector3() {
-    return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float16Vector4() {
+TextureFormat 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 TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
 }
 
-TextureFormat TextureFormat::float32Vector2() {
-    return TextureFormat(GL_RG32F, GL_RG, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float32Vector3() {
-    return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
-}
-
-TextureFormat TextureFormat::float32Vector4() {
+TextureFormat 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 TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
 }
 

+ 3 - 16
wrapper/TextureFormat.h

@@ -9,22 +9,9 @@ struct TextureFormat final {
     const GLenum type;
     const bool depth;
 
-    static TextureFormat color1();
-    static TextureFormat color2();
-    static TextureFormat color3();
-    static TextureFormat color4();
-
-    static TextureFormat float16();
-    static TextureFormat float32();
-
-    static TextureFormat float16Vector2();
-    static TextureFormat float16Vector3();
-    static TextureFormat float16Vector4();
-
-    static TextureFormat float32Vector2();
-    static TextureFormat float32Vector3();
-    static TextureFormat float32Vector4();
-
+    static TextureFormat color8(int channels);
+    static TextureFormat float16(int channels);
+    static TextureFormat float32(int channels);
     static TextureFormat depth16();
     static TextureFormat depth32();