<p>I have a scene rendered using texelFetch() and I am trying to modify the shader to use textureLod() instead.</p>

<p>This is the image generated with texelFetch:</p>

<p><a href="https://i.sstatic.net/9sMuT.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9sMuT.jpg" alt="enter image description here"></a></p>

<p>When I try to change the shader to use textureLod instead I get:</p>

<p><a href="https://i.sstatic.net/mDO4r.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mDO4r.jpg" alt="enter image description here"></a></p>

<p>So as you can see, areas that should not be darkend are now darkened. This seems like the downscaling to the 0-1 coordinate system was not done properly.</p>

<p>This is how I generated the first image:</p>

<pre><code>fragment_color = blinn_phong();

float v_size = cube_dim/voxel_resolution;
vec3 r = f_pos + f_norm*v_size*2;
vec3 dir = normalize(light-r);
vec3 start = r;
ivec3 first = ivec3(((r)*0.5/(cube_dim)+vec3(0.5))*(voxel_resolution-1));
float hit = 1.f;
while(incube(r, cube_dim) &amp;&amp; (length(start-r)&lt;=length(start-light)))
{
    r += dir*v_size*0.5;
    ivec3 hpos = ivec3(((r)*0.5/cube_dim+vec3(0.5))*(voxel_resolution-1));
    vec4 val = texelFetch(normal_map, hpos, 0);
    if(val.w &gt; 0 &amp;&amp; (first != hpos))
    {
        hit=atan(length(start-r)/200.f)/(PI/2.f)*0.9 + 0.1;
        break;
    }
}
fragment_color *=hit;
</code></pre>

<p>And this is how I generated the second:</p>

<pre><code>fragment_color = blinn_phong();

float v_size = cube_dim/voxel_resolution;
vec3 r = f_pos + f_norm*v_size*2;
vec3 dir = normalize(light-r);
vec3 start = r;
ivec3 first = ivec3(((r)*0.5/(cube_dim)+vec3(0.5)));
float hit = 1.f;
while(incube(r, cube_dim) &amp;&amp; (length(start-r)&lt;=length(start-light)))
{
    r += dir*v_size*0.5;
    vec3 hpos = vec3(((r)*0.5/cube_dim+vec3(0.5)));
    vec4 val = textureLod(normal_map, hpos, 0);
    if(val.w &gt; 0 &amp;&amp; (first != hpos))
    {
        hit=atan(length(start-r)/200.f)/(PI/2.f)*0.9 + 0.1;
        break;
    }
}
fragment_color *=hit;
</code></pre>

<p>In other words, the changes consist only on not scaling by the voxel  resolution, which should result in a 0 to 1 coordinate system, instead of the 0 to texture dimensions coordinate system.</p>

<p>Alas, it would seem that I am downscaling thins wrongly.</p>

CC BY-SA 4.0
2018-08-03T06:42:12.450Z
Did you run [a graphics debugger](https://www.khronos.org/opengl/wiki/Debugging_Tools) and inspect your variables?
Varaquilex
CC BY-SA 4.0
2018-08-03T19:32:04.777Z
I ran renderdoc
Makogan
<opengl><rendering><shader><shadow><linear-algebra>
Changing coordinate system from texelFetch to textureLod is breaking the algebra
Makogan
# Changing coordinate system from texelFetch to textureLod is breaking the algebra

(Asked by: username_0 on 2018-07-27)

I have a scene rendered using texelFetch() and I am trying to modify the shader to use textureLod() instead.

This is the image generated with texelFetch:

[![enter image description here](https://i.sstatic.net/9sMuT.jpg)](https://i.sstatic.net/9sMuT.jpg)

When I try to change the shader to use textureLod instead I get:

[![enter image description here](https://i.sstatic.net/mDO4r.jpg)](https://i.sstatic.net/mDO4r.jpg)

So as you can see, areas that should not be darkend are now darkened. This seems like the downscaling to the 0-1 coordinate system was not done properly.

This is how I generated the first image:

```
fragment_color = blinn_phong();

float v_size = cube_dim/voxel_resolution;
vec3 r = f_pos + f_norm*v_size*2;
vec3 dir = normalize(light-r);
vec3 start = r;
ivec3 first = ivec3(((r)*0.5/(cube_dim)+vec3(0.5))*(voxel_resolution-1));
float hit = 1.f;
while(incube(r, cube_dim) && (length(start-r)<=length(start-light)))
{
    r += dir*v_size*0.5;
    ivec3 hpos = ivec3(((r)*0.5/cube_dim+vec3(0.5))*(voxel_resolution-1));
    vec4 val = texelFetch(normal_map, hpos, 0);
    if(val.w > 0 && (first != hpos))
    {
        hit=atan(length(start-r)/200.f)/(PI/2.f)*0.9 + 0.1;
        break;
    }
}
fragment_color *=hit;

```

And this is how I generated the second:

```
fragment_color = blinn_phong();

float v_size = cube_dim/voxel_resolution;
vec3 r = f_pos + f_norm*v_size*2;
vec3 dir = normalize(light-r);
vec3 start = r;
ivec3 first = ivec3(((r)*0.5/(cube_dim)+vec3(0.5)));
float hit = 1.f;
while(incube(r, cube_dim) && (length(start-r)<=length(start-light)))
{
    r += dir*v_size*0.5;
    vec3 hpos = vec3(((r)*0.5/cube_dim+vec3(0.5)));
    vec4 val = textureLod(normal_map, hpos, 0);
    if(val.w > 0 && (first != hpos))
    {
        hit=atan(length(start-r)/200.f)/(PI/2.f)*0.9 + 0.1;
        break;
    }
}
fragment_color *=hit;

```

In other words, the changes consist only on not scaling by the voxel resolution, which should result in a 0 to 1 coordinate system, instead of the 0 to texture dimensions coordinate system.

Alas, it would seem that I am downscaling thins wrongly.

**Tags:** opengl, rendering, shader, shadow, linear-algebra

> **Comments:**
> **username_1**: Did you run [a graphics debugger](https://www.khronos.org/opengl/wiki/Debugging_Tools) and inspect your variables?  
> **username_0**: I ran renderdoc  

---

## 0 answers

