From ceae87033f199ea0288399b5876fa4d1451eae3e Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Mon, 10 Feb 2020 02:19:38 +0000 Subject: [PATCH] Handle opengl internal format + format in image.cpp --- image.cpp | 16 +++++++++++++--- image.hpp | 1 + main.cpp | 2 +- skybox.cpp | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/image.cpp b/image.cpp index 02fd60b..fb534be 100644 --- 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: diff --git a/image.hpp b/image.hpp index 5a75b0d..f87fd98 100644 --- a/image.hpp +++ b/image.hpp @@ -28,6 +28,7 @@ class Image { CGColorSpaceRef colorSpace; GLfloat _width, _height; size_t bitsPerComponent; + bool usesFloat() const; #endif }; diff --git a/main.cpp b/main.cpp index 0b7cb22..e972167 100644 --- 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; diff --git a/skybox.cpp b/skybox.cpp index d668d76..3bf5f40 100644 --- a/skybox.cpp +++ b/skybox.cpp @@ -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); -- 2.30.2