{"id":240,"date":"2010-03-24T23:26:35","date_gmt":"2010-03-24T21:26:35","guid":{"rendered":"http:\/\/labs.miaumiau.cat\/?p=240"},"modified":"2011-07-29T14:41:17","modified_gmt":"2011-07-29T14:41:17","slug":"probando-el-raytracing-en-as3","status":"publish","type":"post","link":"https:\/\/www.miaumiau.cat\/?p=240","title":{"rendered":"Probando el raytracing en AS3"},"content":{"rendered":"<p style=\"text-align: justify;\"><a title=\"Raytracer\" href=\"http:\/\/miaumiau.cat\/examples\/raytracer\/raytracer.swf\" target=\"_blank\">VIEW EXAMPLE<\/a><\/p>\n<p>Some time ago I wanted to program a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ray_tracing_%28graphics%29\">ray tracer<\/a> on real time. The main problem I faced is that ray tracing is difficult to compute, because the quantity of calculations grows when you place more objects on the scene. In order to get a good frame rate I needed to reduce the calculations per frame to a minimum.<\/p>\n<p>So I started with something very simple: one cube, one sphere and one light. The algorithm trows one ray per pixel of the image from the camera to the scene, checking if it collides with any object on the scene. In the first tries, besides the simplicity of the scene (five squares and one sphere), the performance was very bad. Since I couldn\u00b4t reduce more the quantity of objects to show, I had to make some improvements, sacrificing the code legibility, until the point of placing all the functionality in one single function in the style of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Spaghetti_code\">spaghetti code<\/a>. I wish I could have a <a href=\"http:\/\/en.wikipedia.org\/wiki\/GOTO\">goto<\/a> instruction in AS3!.<\/p>\n<p>Finally, as a final optimization, every image is drawn in a very small bitmapData, this data is then scaled through the smoothing option found in the Bitmap Class finding the final image with a more useful size. In this case the distortion is very clear, so I \u201ckilled\u201d the sphere.<\/p>\n<p>If you are curious, these raytracers are very interesting:<br \/>\n\u00b7 <a href=\"http:\/\/www.strille.net\/works\/as3\/raytracer\/\">www.strille.net\/works\/as3\/raytracer\/<\/a><br \/>\n\u00b7 <a href=\"http:\/\/www.laserpirate.com\/as3raytracer\/\">www.laserpirate.com\/as3raytracer<\/a><br \/>\n\u00b7 <a href=\"http:\/\/www.peternitsch.net\/blog\/?p=182\">www.peternitsch.net\/blog\/?p=182<\/a><\/p>\n<h4><em>Original post in Spanish<\/em><\/h4>\n<p>Hac\u00eda tiempo que ten\u00eda en mente programar un <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ray_tracing_%28graphics%29\">ray tracer<\/a> en tiempo real. El principal problema al que me enfrentaba es que el <em>raytracing <\/em>es costoso de computar, aumentando la cantidad de c\u00e1lculos cuantos m\u00e1s objetos hay en la escena. Para conseguir un buen <em>frame rate<\/em> necesitaba reducir al m\u00e1ximo los c\u00e1lculos por cada <em>frame<\/em>.<\/p>\n<p>As\u00ed que decid\u00ed probar con algo sencillo: un cubo, una esfera y una luz. B\u00e1sicamente, el algoritmo lanza un rayo por cada p\u00edxel de imagen desde la c\u00e1mara hacia la escena, comprobando si colisiona con alg\u00fan objeto de la escena. En las primeras pruebas, a pesar de la sencillez de la escena (cinco cuadrados y una esfera), el rendimiento era bastante malo. Ya que no pod\u00eda reducir m\u00e1s la cantidad de objetos a mostrar, tuve que echar mano de optimizaciones, sacrificando la legibilidad del c\u00f3digo, hasta el punto de meter casi toda la funcionalidad en una funci\u00f3n principal al estilo <a href=\"http:\/\/en.wikipedia.org\/wiki\/Spaghetti_code\">spaghetti code<\/a>. Incluso ech\u00e9 en falta una instruccion <a href=\"http:\/\/en.wikipedia.org\/wiki\/GOTO\">goto<\/a> en AS3.<\/p>\n<p>Finalmente, como \u00faltima optimizaci\u00f3n, cada imagen se dibuja en tama\u00f1o peque\u00f1o ampliandolo mediante la opci\u00f3n smoothing de la clase Bitmap a un tama\u00f1o m\u00e1s adecuado. En este caso la distorsi\u00f3n es muy clara, as\u00ed que elimin\u00e9 la esfera.<\/p>\n<p>Si ten\u00e9is curiosidad, estos raytracers son interesantes:<br \/>\n\u00b7 <a href=\"http:\/\/www.strille.net\/works\/as3\/raytracer\/\">www.strille.net\/works\/as3\/raytracer\/<\/a><br \/>\n\u00b7 <a href=\"http:\/\/www.laserpirate.com\/as3raytracer\/\">www.laserpirate.com\/as3raytracer<\/a><br \/>\n\u00b7 <a href=\"http:\/\/www.peternitsch.net\/blog\/?p=182\">www.peternitsch.net\/blog\/?p=182<\/a><\/p>\n<p><a href=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-completa.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-255\" title=\"Escena completa\" src=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-completa-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-completa-300x300.png 300w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-completa-150x150.png 150w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-completa.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-257\" title=\"Escena con suavizado\" src=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-2-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-2-300x300.png 300w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-2-150x150.png 150w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-2.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-256\" title=\"Escena con suavizado\" src=\"http:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-1-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-1-300x300.png 300w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-1-150x150.png 150w, https:\/\/www.miaumiau.cat\/wp-content\/uploads\/2010\/03\/raytracer-smooth-1.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>VIEW EXAMPLE Some time ago I wanted to program a ray tracer on real time. The main problem I faced is that ray tracing is difficult to compute, because the quantity of calculations grows when you place more objects on the scene. In order to get a good frame rate I needed to reduce the [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":669,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":3,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/668"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=\/wp\/v2\/media\/669"}],"wp:attachment":[{"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.miaumiau.cat\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}