Handle opengl internal format + format in image.cpp
authorLuke Lau <luke_lau@icloud.com>
Mon, 10 Feb 2020 02:19:38 +0000 (02:19 +0000)
committerLuke Lau <luke_lau@icloud.com>
Mon, 10 Feb 2020 02:19:38 +0000 (02:19 +0000)
image.cpp
image.hpp
main.cpp
skybox.cpp

index 02fd60b878ee1122bd9a027e38df10304d681ef8..fb534be70302ef92ca80ab2ba3a8255a10d6326c 100644 (file)
--- a/image.cpp
+++ b/image.cpp
@@ -51,17 +51,27 @@ GLenum Image::format() const {
        return GL_RGBA;
 }
 
+inline bool Image::usesFloat() const { return info & kCGBitmapFloatComponents; }
+
 GLint Image::internalFormat() const {
        switch (format()) {
                case GL_DEPTH_COMPONENT: return GL_DEPTH_COMPONENT;
-               case GL_RGB: return GL_RGB;
+        case GL_RGB:
+            if (bitsPerComponent == 16)
+                               return usesFloat() ? GL_RGB16F : GL_RGB16;
+            else if (bitsPerComponent == 8)
+                               return GL_RGB;
+            else abort();
                default: return GL_RGBA;
        }
 }
 
 GLenum Image::type() const {
-       bool isFloat = info & kCGBitmapFloatComponents;
-       if (isFloat) return GL_FLOAT;
+       if (usesFloat()) {
+               if (bitsPerComponent == 16) return GL_HALF_FLOAT;
+               else if (bitsPerComponent == 32) return GL_FLOAT;
+               abort();
+       }
        return GL_UNSIGNED_BYTE;
        
        //TODO:
index 5a75b0daa6add203c96c2f155ca44638c1ba0600..f87fd98a7370a7b3fd546ec7c9760cd8a0008eb9 100644 (file)
--- a/image.hpp
+++ b/image.hpp
@@ -28,6 +28,7 @@ class Image {
                CGColorSpaceRef colorSpace;
                GLfloat _width, _height;
                size_t bitsPerComponent;
+               bool usesFloat() const;
                #endif
 };
 
index 0b7cb229dd745e09d405234bfe89b5f107eab389..e972167424e11636c9e8e2ae5b08ff09f0bf8a9a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -196,7 +196,7 @@ void init() {
        glViewport(0, 0, windowWidth, windowHeight);
 }
 
-bool* keyStates = new bool[256];
+bool keyStates[256] = {false};
 
 void keyboard(unsigned char key, int x, int y) {
        keyStates[key] = true;
index d668d76d7025fc1f865ba477632ade63b72d7f85..3bf5f40ed679346a5babbc1f3190451f2976c925 100644 (file)
@@ -95,7 +95,7 @@ Skybox::Skybox(const Image img): program("skyboxvert.glsl", "skyboxfrag.glsl") {
        
        glBindTexture(GL_TEXTURE_2D, hdrTexId);
 
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, img.width(), img.height(), 0, GL_RGB, GL_HALF_FLOAT, (uint16_t*)img.data());
+       glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data());
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);