support for floating point images => needed for skybox
authorEleni Maria Stea <elene.mst@gmail.com>
Fri, 28 Jul 2017 11:00:18 +0000 (14:00 +0300)
committerEleni Maria Stea <elene.mst@gmail.com>
Fri, 28 Jul 2017 11:00:18 +0000 (14:00 +0300)
src/image.cc
src/image.h
src/opengl/texture-gl.cc
src/opengl/texture-gl.h
src/scene.cc
src/texture.cc

index 2bfdbd8..01ee08f 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <string.h>
 
 #include "imago2.h"
@@ -5,22 +6,29 @@
 
 Image::Image()
 {
-       w = h = 0;
+       w = h = psz = 0;
        pixels = 0;
+
+       is_float = false;
 }
 
 Image::~Image()
 {
-       delete [] pixels;
+       free(pixels);
 }
 
 Image::Image(const Image &image)
 {
        w = image.w;
        h = image.h;
+       is_float = image.is_float;
+       psz = image.psz;
 
-       pixels = new unsigned char[w * h * 4];
-       memcpy(pixels, image.pixels, w * h * 4);
+       if(!(pixels = malloc(w * h * psz))) {
+               fprintf(stderr, "Failed to allocate pixels.\n");
+               abort();
+       }
+       memcpy(pixels, image.pixels, w * h * psz);
 }
 
 Image &Image::operator =(const Image &image)
@@ -28,13 +36,18 @@ Image &Image::operator =(const Image &image)
        if(&image == this)
                return *this;
 
-       delete [] pixels;
+       free(pixels);
 
        w = image.w;
        h = image.h;
+       psz = image.psz;
+       is_float = image.is_float;
 
-       pixels = new unsigned char[w * h * 4];
-       memcpy(pixels, image.pixels, w * h * 4);
+       if(!(pixels = malloc(w * h * psz))) {
+               fprintf(stderr, "Failed to allocate pixels.\n");
+               abort();
+       }
+       memcpy(pixels, image.pixels, w * h * psz);
 
        return *this;
 }
@@ -43,6 +56,8 @@ Image::Image(Image &&image)
 {
        w = image.w;
        h = image.h;
+       psz = image.psz;
+       is_float = image.is_float;
 
        pixels = image.pixels;
        image.pixels = 0;
@@ -53,10 +68,12 @@ Image &Image::operator =(Image &&image)
        if(&image == this)
                return *this;
 
-       delete [] pixels;
+       free(pixels);
 
        w = image.w;
        h = image.h;
+       psz = image.psz;
+       is_float = image.is_float;
 
        pixels = image.pixels;
        image.pixels = 0;
@@ -66,17 +83,40 @@ Image &Image::operator =(Image &&image)
 
 bool Image::load(const char *fname)
 {
-       unsigned char *imago_pixels;
-       if(!(imago_pixels = (unsigned char *)img_load_pixels(fname, &w, &h))) {
-               fprintf(stderr, "Failed to load pixels from file: %s.\n", fname);
+       free(pixels);
+
+       img_pixmap ipm;
+       img_init(&ipm);
+
+       if(img_load(&ipm, fname) == -1) {
+               fprintf(stderr, "Failed to load image: %s.\n", fname);
+
+               img_destroy(&ipm);
+               return false;
+       }
+
+       printf("Successfully loaded image: %s\n", fname);
+
+       img_fmt format = img_is_float(&ipm) ? IMG_FMT_RGBAF : IMG_FMT_RGBA32;
+       if(img_convert(&ipm, format) == -1) {
+               fprintf(stderr, "Failed to convert image %s.\n", fname);
+
+               img_destroy(&ipm);
                return false;
        }
 
-       delete [] pixels;
-       pixels = new unsigned char[w * h * 4];
-       memcpy(pixels, imago_pixels, w * h * 4);
+       w = ipm.width;
+       h = ipm.height;
+       psz = ipm.pixelsz;
+       is_float = img_is_float(&ipm) ? true : false;
+
+       if(!(pixels = malloc(w * h * psz))) {
+               fprintf(stderr, "Failed to allocate pixels for image: %s.\n", fname);
+               return false;
+       }
 
-       img_free_pixels(imago_pixels);
+       memcpy(pixels, ipm.pixels, w * h * psz);
 
+       img_destroy(&ipm);
        return true;
 }
\ No newline at end of file
index d39e750..b09d5e7 100644 (file)
@@ -5,7 +5,10 @@ class Image {
 public:
        int w;
        int h;
-       unsigned char *pixels;
+       int psz;
+
+       bool is_float;
+       void *pixels;
 
        Image();
        ~Image();
index 607d014..2fbd05e 100644 (file)
@@ -1,4 +1,5 @@
 #include <GL/glew.h>
+#include <stdlib.h>
 
 #include "texture.h"
 #include "opengl/texture-gl.h"
@@ -40,9 +41,16 @@ void TextureGL::update()
                int w = images[i].w;
                int h = images[i].h;
 
-               unsigned char *pixels = images[i].pixels;
+               /* target */
                unsigned int t = is_cubemap() ? faces[i] : GL_TEXTURE_2D;
-               glTexImage2D(t, 0, GL_SRGB_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+
+               /* internal format */
+               unsigned int ifmt = images[i].is_float ? GL_RGBA16F : GL_SRGB_ALPHA;
+
+               /* data type of pixel data */
+               unsigned int type = images[i].is_float ? GL_FLOAT : GL_UNSIGNED_BYTE;
+
+               glTexImage2D(t, 0, ifmt, w, h, 0, GL_RGBA, type, images[i].pixels);
        }
 
        glGenerateMipmap(target);
@@ -50,5 +58,6 @@ void TextureGL::update()
 
 void TextureGL::bind()
 {
-       glBindTexture(GL_TEXTURE_2D, tex);
+       unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+       glBindTexture(target, tex);
 }
\ No newline at end of file
index 11c48a3..a08f159 100644 (file)
@@ -6,7 +6,7 @@
 class TextureGL : public Texture {
 private:
        unsigned int tex;
-       unsigned int target;
+       GLenum target;
        
        virtual void update() override;
 public:
index d033a8f..11281c3 100644 (file)
@@ -247,8 +247,6 @@ static Material *load_material(const aiScene *ascene, Scene *scene, unsigned int
 
                mat->dtex = scene->find_texture(tex_fname.c_str());
                if(!mat->dtex) {
-                       printf("!mat->dtex\n");
-
                        mat->dtex = gfx_create_texture();
 
                        if(!mat->dtex->load(tex_fname.c_str())) {
@@ -296,6 +294,5 @@ Texture *Scene::find_texture(const char *name)
                        return textures[i];
                }
        }
-       fprintf(stderr, "Texture %s not found.\n", name);
        return 0;
 }
\ No newline at end of file
index bbebd7f..1d2f37d 100644 (file)
@@ -21,8 +21,9 @@ bool Texture::load(const char *fname)
                return true;
        }
 
+       printf("%s NOT LOADED\n", fname);
        /* check if it is a cubemap */
-       return load_cubemap(fname); 
+       return load_cubemap(fname);
 }
 
 bool Texture::load_cubemap(const char *fname)
@@ -40,7 +41,8 @@ bool Texture::load_cubemap(const char *fname)
                if(suffix) {
                        memmove(suffix + 3, suffix, strlen(suffix) + 1);
                        memcpy(suffix, suffixes[i], 3);
-               } else {
+               }
+               else {
                        strcat(buf, suffixes[i]);
                }
 
@@ -49,7 +51,7 @@ bool Texture::load_cubemap(const char *fname)
                        images.clear();
                        return false;
                }
-               images.push_back(img);  
+               images.push_back(img);
        }
 
        update();
@@ -58,5 +60,5 @@ bool Texture::load_cubemap(const char *fname)
 
 bool Texture::is_cubemap() const
 {
-       return images.size() > 1;
+       return (images.size() > 1);
 }
\ No newline at end of file