Browse Source

support for images with 2 and 3 channels

Kajetan Johannes Hammerle 3 years ago
parent
commit
91760350e7
5 changed files with 21 additions and 11 deletions
  1. 1 1
      Game.cpp
  2. 0 4
      rendering/FileTexture.cpp
  3. 6 4
      rendering/wrapper/Texture.cpp
  4. 0 1
      rendering/wrapper/Texture.h
  5. 14 1
      utils/PNGReader.cpp

+ 1 - 1
Game.cpp

@@ -55,7 +55,7 @@ void Game::render(float lag, Renderer& renderer) const {
     renderer.translateTo(0.0f, 0.0f).update();
     renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0xFF0000FF);
     renderer.translateTo(0.0f, 0.0f).scale(4).update();
-    String s("&900A");
+    String s("A");
     s.append(": ").append(controller.getDownTime(0)).append("     ");
     renderer.drawString(0, 10, s);
 }

+ 0 - 4
rendering/FileTexture.cpp

@@ -8,16 +8,12 @@ FileTexture::FileTexture(const char* path) {
     if(reader.hasError()) {
         return;
     }
-    std::cout << reader.getChannels() << "\n";
     char* buffer = new char[reader.getBufferSize()];
     if(reader.readData(buffer)) {
         delete[] buffer;
         return;
     }
     texture.setColors(reader.getWidth(), reader.getHeight(), buffer, reader.getChannels());
-    for(int i = 0; i < reader.getWidth() * reader.getHeight(); i++) {
-        std::cout << (int) buffer[i] << "\n";
-    }
     delete[] buffer;
 }
 

+ 6 - 4
rendering/wrapper/Texture.cpp

@@ -20,16 +20,18 @@ void Texture::setColors(int width, int height, const char* data, int channels) {
         case 1:
             glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
             break;
+        case 2:
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, width, height, 0, GL_RG, GL_UNSIGNED_BYTE, data);
+            break;
+        case 3:
+            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
+            break;
         case 4:
             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
             break;
     }
 }
 
-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() const {
     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, texture);

+ 0 - 1
rendering/wrapper/Texture.h

@@ -15,7 +15,6 @@ public:
     Texture& operator=(Texture&& other) = delete;
     
     void setColors(int width, int height, const char* data, int channels);
-    void setRGBFloatData(int width, int height, const float* data);
     
     void bind() const;
 

+ 14 - 1
utils/PNGReader.cpp

@@ -62,7 +62,20 @@ int PNGReader::getBufferSize() const {
 }
 
 bool PNGReader::hasError() const {
-    return width == 0 || height == 0 || channels == 0;
+    if(channels < 1 || channels > 4) {
+        std::cout << "'" << path << "' has unsupported number of channels: " << channels << "\n";
+        return true;
+    } else if(width != height) {
+        std::cout << "width and height of '" << path << "' are not equal\n";
+        return true;
+    } else if(((width - 1) & width) != 0) {
+        std::cout << "width and height of '" << path << "' are not a multiple of 2\n";
+        return true;
+    } else if(width < 1 || width > 2048) {
+        std::cout << "width and height of '" << path << "' are too big\n";
+        return true;
+    }
+    return false;
 }
 
 bool PNGReader::checkSignature() {