Cycles: add support for "Use Alpha" option on image datablocks.
authorCarlo Andreacchio <carlorules@gmail.com>
Wed, 7 May 2014 14:36:44 +0000 (16:36 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 7 May 2014 14:43:50 +0000 (16:43 +0200)
Reviewed By: brecht

Differential Revision: https://developer.blender.org/D486

intern/cycles/blender/blender_mesh.cpp
intern/cycles/blender/blender_shader.cpp
intern/cycles/render/image.cpp
intern/cycles/render/image.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h

index c49086d83a74729f33f769ce4848008748b363f1..83514879477c07c01e745880a3983b08abbb24b6 100644 (file)
@@ -222,7 +222,7 @@ static void create_mesh_volume_attribute(BL::Object b_ob, Mesh *mesh, ImageManag
 
        volume_data->manager = image_manager;
        volume_data->slot = image_manager->add_image(Attribute::standard_name(std),
-               b_ob.ptr.data, animated, is_float, is_linear, INTERPOLATION_LINEAR);
+               b_ob.ptr.data, animated, is_float, is_linear, INTERPOLATION_LINEAR, true);
 }
 
 static void create_mesh_volume_attributes(Scene *scene, BL::Object b_ob, Mesh *mesh)
index 2eafcecf026ef1edbadd284f0977cc50454ba0ce..7f384d9a1e71b34c9b5467bc4513dbb2e8d7c71f 100644 (file)
@@ -548,6 +548,7 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                        image->animated = b_image_node.image_user().use_auto_refresh();
                }
                image->color_space = ImageTextureNode::color_space_enum[(int)b_image_node.color_space()];
+               image->use_alpha = b_image.use_alpha();
                image->projection = ImageTextureNode::projection_enum[(int)b_image_node.projection()];
                image->interpolation = (InterpolationType)b_image_node.interpolation();
                image->projection_blend = b_image_node.projection_blend();
@@ -576,6 +577,7 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                        }
                }
                env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
+               env->use_alpha = b_image.use_alpha();
                env->projection = EnvironmentTextureNode::projection_enum[(int)b_env_node.projection()];
                get_tex_mapping(&env->tex_mapping, b_env_node.texture_mapping());
                node = env;
index 6348c9e6706ac3eb6de70e157351f45d425f58ad..8dde642f70b16f6b98c75af293d21c09fc624fd0 100644 (file)
@@ -152,7 +152,7 @@ static bool image_equals(ImageManager::Image *image, const string& filename, voi
               image->interpolation == interpolation;
 }
 
-int ImageManager::add_image(const string& filename, void *builtin_data, bool animated, bool& is_float, bool& is_linear, InterpolationType interpolation)
+int ImageManager::add_image(const string& filename, void *builtin_data, bool animated, bool& is_float, bool& is_linear, InterpolationType interpolation, bool use_alpha)
 {
        Image *img;
        size_t slot;
@@ -194,6 +194,7 @@ int ImageManager::add_image(const string& filename, void *builtin_data, bool ani
                img->animated = animated;
                img->interpolation = interpolation;
                img->users = 1;
+               img->use_alpha = use_alpha;
 
                float_images[slot] = img;
        }
@@ -230,6 +231,7 @@ int ImageManager::add_image(const string& filename, void *builtin_data, bool ani
                img->animated = animated;
                img->interpolation = interpolation;
                img->users = 1;
+               img->use_alpha = use_alpha;
 
                images[slot] = img;
 
@@ -307,9 +309,13 @@ bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
                if(!in)
                        return false;
 
-               ImageSpec spec;
+               ImageSpec spec = ImageSpec();
+               ImageSpec config = ImageSpec();
+
+               if(img->use_alpha == false)
+                       config.attribute("oiio:UnassociatedAlpha", 1);
 
-               if(!in->open(img->filename, spec)) {
+               if(!in->open(img->filename, spec, config)) {
                        delete in;
                        return false;
                }
@@ -387,6 +393,12 @@ bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
                }
        }
 
+       if(img->use_alpha == false) {
+               for(int i = width*height*depth-1; i >= 0; i--) {
+                       pixels[i*4+3] = 255;
+               }
+       }
+
        return true;
 }
 
@@ -405,9 +417,13 @@ bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_
                if(!in)
                        return false;
 
-               ImageSpec spec;
+               ImageSpec spec = ImageSpec();
+               ImageSpec config = ImageSpec();
+
+               if(img->use_alpha == false)
+                       config.attribute("oiio:UnassociatedAlpha",1);
 
-               if(!in->open(img->filename, spec)) {
+               if(!in->open(img->filename, spec, config)) {
                        delete in;
                        return false;
                }
@@ -484,6 +500,12 @@ bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_
                }
        }
 
+       if(img->use_alpha == false) {
+               for(int i = width*height*depth-1; i >= 0; i--) {
+                       pixels[i*4+3] = 1.0f;
+               }
+       }
+
        return true;
 }
 
index a52ab2853b9e9b26b99214c80b18fecd17e5d1c3..a862ffce5c3a7bc3b3be892eca44501d30366d4a 100644 (file)
@@ -49,7 +49,7 @@ public:
        ImageManager();
        ~ImageManager();
 
-       int add_image(const string& filename, void *builtin_data, bool animated, bool& is_float, bool& is_linear, InterpolationType interpolation);
+       int add_image(const string& filename, void *builtin_data, bool animated, bool& is_float, bool& is_linear, InterpolationType interpolation, bool use_alpha);
        void remove_image(int slot);
        void remove_image(const string& filename, void *builtin_data, InterpolationType interpolation);
        bool is_float_image(const string& filename, void *builtin_data, bool& is_linear);
@@ -72,6 +72,7 @@ public:
                string filename;
                void *builtin_data;
 
+               bool use_alpha;
                bool need_load;
                bool animated;
                InterpolationType interpolation;
index 28d2a41a1138f3efa475e870bf264ced01daab35..e269074763c9334496dc628e13138a26515a7f88 100644 (file)
@@ -189,6 +189,7 @@ ImageTextureNode::ImageTextureNode()
        slot = -1;
        is_float = -1;
        is_linear = false;
+       use_alpha = true;
        filename = "";
        builtin_data = NULL;
        color_space = ustring("Color");
@@ -242,7 +243,7 @@ void ImageTextureNode::compile(SVMCompiler& compiler)
        image_manager = compiler.image_manager;
        if(is_float == -1) {
                bool is_float_bool;
-               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, interpolation);
+               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, interpolation, use_alpha);
                is_float = (int)is_float_bool;
        }
 
@@ -357,6 +358,7 @@ EnvironmentTextureNode::EnvironmentTextureNode()
        slot = -1;
        is_float = -1;
        is_linear = false;
+       use_alpha = true;
        filename = "";
        builtin_data = NULL;
        color_space = ustring("Color");
@@ -406,7 +408,7 @@ void EnvironmentTextureNode::compile(SVMCompiler& compiler)
        image_manager = compiler.image_manager;
        if(slot == -1) {
                bool is_float_bool;
-               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, INTERPOLATION_LINEAR);
+               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool, is_linear, INTERPOLATION_LINEAR, use_alpha);
                is_float = (int)is_float_bool;
        }
 
index 054ae0f613c027e82a391733358ed95c207ead74..d94d8ce6033ed6b6067f6f24071971b88c6483e0 100644 (file)
@@ -72,6 +72,7 @@ public:
        int slot;
        int is_float;
        bool is_linear;
+       bool use_alpha;
        string filename;
        void *builtin_data;
        ustring color_space;
@@ -95,6 +96,7 @@ public:
        int slot;
        int is_float;
        bool is_linear;
+       bool use_alpha;
        string filename;
        void *builtin_data;
        ustring color_space;