{"id":141,"date":"2014-06-18T18:38:55","date_gmt":"2014-06-18T18:38:55","guid":{"rendered":"https:\/\/blogs.scummvm.org\/josejx\/?p=141"},"modified":"2022-05-21T18:40:58","modified_gmt":"2022-05-21T18:40:58","slug":"revisiting-the-lava-texturing-problem","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/josejx\/2014\/06\/18\/revisiting-the-lava-texturing-problem\/","title":{"rendered":"Revisiting the Lava Texturing Problem"},"content":{"rendered":"<p>In a <a href=\"https:\/\/blogs.scummvm.org\/josejx\/2014\/06\/06\/the-mixed-up-lava-puzzle\/\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, I commented on a texturing problem with the lava in the scene <i>lav<\/i>. As I indicated in that post, while the work around that I submitted did fix the problem for the scenes with issues, the workaround itself was sort of a hack. I had assumed that if a texture was larger than 128 pixels on a side, then that texture should be clamped at the edges. Clamping prevents the OpenGL renderer from repeating the texture over the surface, resulting in lines when the textures don&#8217;t repeat cleanly. So why not just always use clamping? Well, in many cases we <b>DO<\/b> want the texture to repeat. Think of a typical grass pattern or a sandy beach texture, or in this case, the lava!<\/p>\n<p>Although my approach did fix the problem, the solution was a hack. There&#8217;s nothing that really indicates that that is a cutoff for which approach to take. To investigate possible solutions, I used <i><a href=\"http:\/\/apitrace.github.io\/\" target=\"_blank\" rel=\"noopener\">apitrace<\/a><\/i>, a tool that records OpenGL state information, allowing you to replay, investigate and examine what OpenGL calls a program is executing to render the screen.<\/p>\n<p>To capture a trace, we&#8217;ll use this command with the retail version:<\/p>\n<ul>\n<li>apitrace trace -o retail.trace wine .\/Monkey4.exe -gl -w<\/li>\n<\/ul>\n<p>And this with ResidualVM:<\/p>\n<ul>\n<li>apitrace trace -o residual.trace residualvm<\/li>\n<\/ul>\n<p>Of note, to trace 32-bit Windows applications in wine, you will need a 32-bit version of <i>apitrace<\/i>.<\/p>\n<p>To view the traces, I used the program <i>qapitrace<\/i>, a GUI viewer for replaying traces from <i>apitrace<\/i>. To run <i>qapitrace<\/i> on an existing trace:<\/p>\n<ul>\n<li>qapitrace retail.trace<\/li>\n<\/ul>\n<p>With these traces in hand and in the viewer, I took a look at the size of the textures and when the <i>glTexParameteri<\/i> function was used on them in the retail version. To make the trace more manageable, I filtered the results for Texture Events using Trace-&gt;Options and configuring it like shown below.<\/p>\n<figure id=\"attachment_143\" aria-describedby=\"caption-attachment-143\" style=\"width: 571px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/qapitraceFilter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-143\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/qapitraceFilter.png\" alt=\"\" width=\"571\" height=\"334\" srcset=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/qapitraceFilter.png 571w, https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/qapitraceFilter-300x175.png 300w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/a><figcaption id=\"caption-attachment-143\" class=\"wp-caption-text\">Filtering for Texture Events<\/figcaption><\/figure>\n<p>After some investigation, I found that there were definitely textures that used clamping when the size was lower than 128 pixels and repeating when greater. Rats, that approach definitely was wrong!<\/p>\n<p>It was at this point that <a href=\"https:\/\/github.com\/Akz-\" target=\"_blank\" rel=\"noopener\">Akz-<\/a> suggested that I try clamping sprites, which are generally images painted on top of the scene (such as the candles&#8217; flame or the Dainty at dock on Lucre Island) and repeat all other textures. This approach seemed to work well, also resolving all of the issues resolved by the previous patch. For this approach, I had originally thought of passing a texture parameter to the <i>Material<\/i> (which loads the texture) using an <i>enum<\/i>, but I ran into the issue of having to predeclare the <i>enum<\/i> when used in places that had also predeclared the <i>Material<\/i> class. In C++, unlike C, the <i>enum<\/i> has a variable storage size and isn&#8217;t assumed to be <i>int<\/i>. In C++11, there is support for predeclaring the type, but ResidualVM targets the C++03 standard, which doesn&#8217;t include this feature.<\/p>\n<p>In the end, I realized that the retail version only used GL_CLAMP_TO_EDGE and GL_REPEAT, so instead of using an <i>enum<\/i>, I replaced it with a <i>bool<\/i> indicating that the texture should be clamped when true. The completed patch for this work can be found in <a href=\"https:\/\/github.com\/residualvm\/residualvm\/pull\/910\" target=\"_blank\" rel=\"noopener\">PR #910<\/a>. It fixes both the lava, as shown in the previous post, with a repeating texture, and the Dainty at the dock on Lucre Island, where the texture needed to be clamped to remove the lines as shown below. Note that in these pictures, they&#8217;re not exactly in the same place because the Dainty texture moves to simulate waves.<\/p>\n<figure id=\"attachment_144\" aria-describedby=\"caption-attachment-144\" style=\"width: 179px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/Unclamped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-144\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/Unclamped.png\" alt=\"\" width=\"179\" height=\"189\" \/><\/a><figcaption id=\"caption-attachment-144\" class=\"wp-caption-text\">Without Clamping<\/figcaption><\/figure>\n<figure id=\"attachment_145\" aria-describedby=\"caption-attachment-145\" style=\"width: 179px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/WithClamping.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-145\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/06\/WithClamping.png\" alt=\"\" width=\"179\" height=\"189\" \/><\/a><figcaption id=\"caption-attachment-145\" class=\"wp-caption-text\">With Clamping<\/figcaption><\/figure>\n<p>In the next post, I will get back to collisions and the actual lava puzzle issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, I commented on a texturing problem with the lava in the scene lav. As I indicated in that post, while the work around that I submitted did fix the problem for the scenes with issues, the workaround itself was sort of a hack. I had assumed that if a texture was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/141\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}