# HLSL to GLSL conversion: tex2Dgrad()

This topic is 4436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm trying to implement the Parallax Occlusion Example in GLSL and this is working fine so far - except for one part: I'm trying to find a GLSL counterpart for the HLSL function
vec4 texel = tex2Dgrad( sampler2D tex, vec2 UV, vec2 dx, vec2 dy);
I calculated the gradients dx & dy using dFdx()/dFdy() but I couldn't find anything similar in the GLSL reference manual for tex2Dgrad(). How do I get the same functionality in GLSL? thank you very much

##### Share on other sites
There is the "texture2D" function. It generates a 4-dimensional vector and takes some params I don't really know off-hand. This is how I use it to calculate a fragment's color based on the texture:

uniform sampler2D TexMap1;

void main()
{
vec4 color = texture2D(TexMap1, gl_TexCoords[0].st);
gl_FragColor = color;
}

##### Share on other sites
Thanks but I ment a texture2D lookup with additional gradients (dx & dy)?

In HLSL a tex2D(sampler, UV) exists, and a tex2Dgrad(sampler, UV, dx, dy)
In GLSL a texture2D(sampler, UV) exists, but I couldn't find any sort of texture2Dgrad(sampler, UV, dx, dy)?

##### Share on other sites
It isn't possible to calculate the gradients manually?

##### Share on other sites
I think you can add the result of dFdx/y simply to the texcoord you supply, I think.

##### Share on other sites
Quote:
 Original post by quastyThanks but I ment a texture2D lookup with additional gradients (dx & dy)?In HLSL a tex2D(sampler, UV) exists, and a tex2Dgrad(sampler, UV, dx, dy)In GLSL a texture2D(sampler, UV) exists, but I couldn't find any sort of texture2Dgrad(sampler, UV, dx, dy)?
There is no texture2DGrad function, yet. It is part of the proposed extension GL_ARB_shader_texture_lod. You can read more about it here. If you need it to get around that flow-control issue they talk about in the article, you can just move the entire texture2D call outside of flow-control for now, like in their Fragment Shader 3, and it should give the same results as using texture2DGrad inside flow-control would give. If you need it for something else I'm afraid you will have to wait.

##### Share on other sites
I am not sure what you would be using dy/dx for when it comes to textures, but IIRC GLSL has

float x = dFdx(10.0);float y = dFdy(10.0);

you could use them and send that to the texture function? Like I said I am not sure what you are trying to accomplish...

##### Share on other sites
Quote:
 Original post by KalidorThere is no texture2DGrad function, yet. It is part of the proposed extension GL_ARB_shader_texture_lod. You can read more about it here. If you need it to get around that flow-control issue they talk about in the article, you can just move the entire texture2D call outside of flow-control for now, like in their Fragment Shader 3, and it should give the same results as using texture2DGrad inside flow-control would give. If you need it for something else I'm afraid you will have to wait.

Thank you, although not the answer I was hoping for and ATM I don't see a way how this could work if a texture2D cannot be called from within a loop in GLSL. ??? But it still puzzles me - I found this approach of Parallax Occlusion Mapping using GLSL:
http://graphics.cs.brown.edu/games/SteepParallax/index.html

Although the shaders doesn't seemed to be complete, he somehow wasn't effected by this. In the PDF (http://graphics.cs.brown.edu/games/SteepParallax/mcguire-steepparallax.pdf) to this article a different shader is shown using texture2DLod() for the lookup in the loop. Does this somehow change things?

And I'm pretty sure I've seen OpenGL parallax occlusion mapping shaders out there. How could this work then?

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632577
• Total Posts
3007163

×