{"id":113,"date":"2008-06-04T10:36:39","date_gmt":"2008-06-04T09:36:39","guid":{"rendered":"http:\/\/www.ozone3d.net\/blogs\/lab\/?p=113"},"modified":"2011-09-07T20:45:18","modified_gmt":"2011-09-07T19:45:18","slug":"glsl-float-to-rgba8-encoder","status":"publish","type":"post","link":"https:\/\/www.ozone3d.net\/blogs\/lab\/20080604\/glsl-float-to-rgba8-encoder\/","title":{"rendered":"GLSL float to RGBA8 encoder"},"content":{"rendered":"<p>Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer:<\/p>\n<pre>\r\nvec4 packFloatToVec4i(const float value)\r\n{\r\n  const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);\r\n  const vec4 bitMsk = vec4(0.0, 1.0\/256.0, 1.0\/256.0, 1.0\/256.0);\r\n  vec4 res = fract(value * bitSh);\r\n  res -= res.xxyz * bitMsk;\r\n  return res;\r\n}\r\n<\/pre>\n<p>Unpacking a [0-1] float value from a 4D vector where each component was a 8-bits integer:<\/p>\n<pre>\r\nfloat unpackFloatFromVec4i(const vec4 value)\r\n{\r\n  const vec4 bitSh = vec4(1.0\/(256.0*256.0*256.0), 1.0\/(256.0*256.0), 1.0\/256.0, 1.0);\r\n  return(dot(value, bitSh));\r\n}\r\n<\/pre>\n<p>Source of these codes: <a href=\"http:\/\/www.gamedev.net\/community\/forums\/topic.asp?topic_id=463075\">Gamedev forums<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer: vec4 packFloatToVec4i(const float value) { const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0); const vec4 bitMsk = vec4(0.0, 1.0\/256.0, 1.0\/256.0, 1.0\/256.0); vec4 res = fract(value * bitSh); res -= res.xxyz * bitMsk; return res; } Unpacking a [0-1] float value from a 4D vector where each component was &hellip; <a href=\"https:\/\/www.ozone3d.net\/blogs\/lab\/20080604\/glsl-float-to-rgba8-encoder\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">GLSL float to RGBA8 encoder<\/span> <span class=\"meta-nav\">&raquo;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[297,18,637],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-opengl","category-programming","tag-float-to-rgba-encoder","tag-glsl","tag-programming"],"aioseo_notices":[],"views":9482,"_links":{"self":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":0,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ozone3d.net\/blogs\/lab\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}