{"action":"created","issue":{"url":"https://api.github.com/repos/openscad/openscad/issues/1485","labels_url":"https://api.github.com/repos/openscad/openscad/issues/1485/labels{/name}","comments_url":"https://api.github.com/repos/openscad/openscad/issues/1485/comments","events_url":"https://api.github.com/repos/openscad/openscad/issues/1485/events","html_url":"https://github.com/openscad/openscad/pull/1485","id":116792382,"number":1485,"title":"Support alpha channel for transparent backgrounds","user":{"login":"thehans","id":252233,"avatar_url":"https://avatars.githubusercontent.com/u/252233?v=3","gravatar_id":"","url":"https://api.github.com/users/thehans","html_url":"https://github.com/thehans","followers_url":"https://api.github.com/users/thehans/followers","following_url":"https://api.github.com/users/thehans/following{/other_user}","gists_url":"https://api.github.com/users/thehans/gists{/gist_id}","starred_url":"https://api.github.com/users/thehans/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thehans/subscriptions","organizations_url":"https://api.github.com/users/thehans/orgs","repos_url":"https://api.github.com/users/thehans/repos","events_url":"https://api.github.com/users/thehans/events{/privacy}","received_events_url":"https://api.github.com/users/thehans/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/openscad/openscad/labels/GUI","name":"GUI","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":33,"created_at":"2015-11-13T15:38:47Z","updated_at":"2015-12-05T21:45:31Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/openscad/openscad/pulls/1485","html_url":"https://github.com/openscad/openscad/pull/1485","diff_url":"https://github.com/openscad/openscad/pull/1485.diff","patch_url":"https://github.com/openscad/openscad/pull/1485.patch"},"body":"This allows color-schemes to be specified with an alpha channel, and for exported png's to include that alpha data.\r\n\r\nCurrently all the color scheme values are 6 hex chars for the r,g,b bytes.  It now supports optional 4th byte.  So background is made transparent in the json file like so: \r\n`    \"background\" :         \"#00000000\",`\r\n\r\nThis makes it possible to export animations with transparencies for creating nice gifs or apngs.\r\n\r\nTODO:\r\n* [ ] Setting alpha to 0 causes black background in the GUI. We should probably communicate the intended background color to Qt. Seems to be a Mac-only issue. Setting alpha to _anything_ except 0 seems to work.\r\n* [x] Include one built-in colorscheme with transparent background.\r\n* [ ] Optional (or separate issue): Document how to create custom color schemes.\r\n* [ ] Add a test case using transparent background: Ideally this should contain: ```#``` and ```%``` operators, as well as transparent ```color()``` usage\r\n* [ ] Windows\r\n   * [ ] Test built-in color scheme in GUI\r\n   * [ ] Test case pass\r\n   * [ ] Save as PNG from GUI\r\n   * [ ] Test anim dump\r\n* [ ] Linux\r\n   * [x] Test built-in color scheme in GUI\r\n   * [ ] Test case pass\r\n   * [x] Save as PNG from GUI\r\n   * [x] Test anim dump\r\n* [ ] Mac\r\n   * [ ] Test built-in color scheme in GUI\r\n   * [ ] Test case pass\r\n   * [x] Save as PNG from GUI\r\n   * [ ] Test anim dump\r\n\r\nLike this:\r\n![openscad_gif3](https://cloud.githubusercontent.com/assets/252233/11148363/fbc273ee-89e1-11e5-81b8-536a6f050bc6.gif)\r\nIf image is viewed on another html page with different color background, that will show through.\r\nHere is a link to the same pic for example on grey bg http://imgur.com/ukK3ELb "},"comment":{"url":"https://api.github.com/repos/openscad/openscad/issues/comments/162250091","html_url":"https://github.com/openscad/openscad/pull/1485#issuecomment-162250091","issue_url":"https://api.github.com/repos/openscad/openscad/issues/1485","id":162250091,"user":{"login":"thehans","id":252233,"avatar_url":"https://avatars.githubusercontent.com/u/252233?v=3","gravatar_id":"","url":"https://api.github.com/users/thehans","html_url":"https://github.com/thehans","followers_url":"https://api.github.com/users/thehans/followers","following_url":"https://api.github.com/users/thehans/following{/other_user}","gists_url":"https://api.github.com/users/thehans/gists{/gist_id}","starred_url":"https://api.github.com/users/thehans/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thehans/subscriptions","organizations_url":"https://api.github.com/users/thehans/orgs","repos_url":"https://api.github.com/users/thehans/repos","events_url":"https://api.github.com/users/thehans/events{/privacy}","received_events_url":"https://api.github.com/users/thehans/received_events","type":"User","site_admin":false},"created_at":"2015-12-05T21:45:30Z","updated_at":"2015-12-05T21:45:30Z","body":"Changed the blend func again for alpha.  This time I am quite confident it is correct.  \r\n\r\nNow using:\r\n`glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);`\r\n\r\nThis time I actually derived the equation for setting the blending factors on the alpha components.  Below is my thought process.\r\n\r\nLets say every object has a transmittance value \"T\" that tells you what percentage of light is able to pass through the object.  Light<sub>in</sub> * T = Light<sub>out</sub>\r\n\r\nNow lets say we have a surface1 and surface2, light will pass through both.  The total transmittance is equal to the product of individual transmittances.\r\nLight<sub>out</sub> = Light<sub>in</sub> * T<sub>total</sub> = Light<sub>in</sub> * T<sub>1</sub> * T<sub>2</sub>\r\n\r\nNow, since we are working with alpha(opacity) instead of transmittance, lets substitue that in.\r\nA = 1 - T\r\nT<sub>total</sub> = T<sub>1</sub> * T<sub>2</sub>\r\nA<sub>total</sub> = 1 - T<sub>total</sub> = 1 - (T<sub>1</sub> * T<sub>2</sub>) = 1 - ((1-A<sub>1</sub>)(1-A<sub>2</sub>)) = 1 - (1 - A<sub>1</sub> - A<sub>2</sub> + A<sub>1</sub>A<sub>2</sub>) = A<sub>1</sub> + A<sub>2</sub> - A<sub>1</sub>A<sub>2</sub>\r\n\r\nNow instead of calling them surface 1 and 2, lets call then dst (the background existing in the framebuffer), and src (the new object being drawn on top)\r\nA<sub>total</sub> = A<sub>src</sub> + A<sub>dst</sub> - A<sub>src</sub>A<sub>dst</sub>\r\nor the same equation written slightly differently:\r\nA<sub>total</sub> = A<sub>src</sub> * 1 + A<sub>dst</sub> * (1 - A<sub>src</sub>)\r\nNow we finally see the blending factors of \"1\" and \"1-A<sub>src</sub>\" which the src and dst alpha values are scaled by respectively, and which are represented in the two last parameters of glBlendFuncSeparate."}}