• Create Account

# NoMonkey89

Member Since 13 Feb 2013
Offline Last Active Mar 13 2013 07:19 AM

### In Topic: Calculating viewspace coordinates from depth and texture coordinates

19 February 2013 - 06:27 AM

I've used some reconstruction code from MJP's website too. Specifically from this page. The relevant code from there is as follows:

```//In the vertex shader:

void VSBoundingVolume(  in float3 in_vPositionOS       : POSITION,
out float4 out_vPositionCS     : POSITION,
out float3 out_vPositionVS    : TEXCOORD0 )
{
out_vPositionCS = mul(in_vPositionOS, g_matWorldViewProj);

// Pass along the view-space vertex position to the pixel shader
out_vPositionVS = mul(in_vPositionOS, g_matWorldView);
}

//Then in our pixel shader, we calculate the ray and reconstruct position like this:

float3 VSPositionFromDepth(float2 vTexCoord, float3 vPositionVS)
{
// Calculate the frustum ray using the view-space position.
// g_fFarCip is the distance to the camera's far clipping plane.
// Negating the Z component only necessary for right-handed coordinates
float3 vFrustumRayVS = vPositionVS.xyz * (g_fFarClip/-vPositionVS.z);
return tex2D(DepthSampler, vTexCoord).x * vFrustumRayVS;
}
```

Hi,

I took a look at the code you posted and I'm not 100% sure if I can do what I want with it.

I have a fullscreenquad with my depth texture on it and my shader should calculate normals in view space on it. Therefore I need to calculate the view space position of my current fragment and the neighboring fragments.

As far as I unterstand the code above it's only possible for the current fragment. Or am I wrong?

### In Topic: Calculating viewspace coordinates from depth and texture coordinates

14 February 2013 - 06:31 AM

Ok just looking over your code compared to that in the slides... Why are you doing the following, I don't see it in the example code from the slides?

ddx1 = -ddx2 + ddx1;

I tried my own approach of a derivative to see if it changes anything. I now put it back to the original code from the slides

And in regards to the following and what you asked earlier:

+ float2(texelSize, 0)

What this code does is sample neighbouring texels. So if you have a non-square texture then replace float2(texelSize, 0) with float2(1.0 / texSizeX, 0) and replace float2(0, texelSize) with float2(0, 1.0 / texSizeY).

Allright, I figured that out but even if I'm using not the correct texelSize i figure it's not the cause of my problem. But I will fix this now.

I'd also check your depth format. Check you're using a 32bit floating point target as 16bit may not be enough.

It's a 32-bit target (called Ogre::PF_FLOAT32_R)

I also assume you are calculating posEye as follows (cause it's not defined in your original post)

`float3 posEye = uvToEye(texCoord, depth);`

Yeah, it's float3 posEye = uvToEye(input.texco, depth, invProj_m);

Dont know why it's not in my original post, It's definitively in my program

### In Topic: Calculating viewspace coordinates from depth and texture coordinates

14 February 2013 - 03:47 AM

double post, can't find a button to delete it..

### In Topic: Calculating viewspace coordinates from depth and texture coordinates

14 February 2013 - 03:47 AM

Hey, I just came a little bit further using the code in my first post. It turned out I gave a wrong inverted projection matrix. I fixed that, I guess. At least my screen isn't black anymore :-D This is what it looks like:

My guess is there is something wrong with my depth texture. When I view it, all the spheres are just white except when I come really close. That would explain why my normals are black inside the spheres. There is no difference in the depth, so my derivatives will be zero and so will be the calculated normal.

I don't think that I want to let the normals face the light. I just want to have the normals on my spheres so I can use them and their position to shade the surface.

Regards,

Kilian

### In Topic: Calculating viewspace coordinates from depth and texture coordinates

14 February 2013 - 03:19 AM

Regards,

Kilian

Edit:

You recommended:

float texelSize = 1.0 / textureSize; //eg: 1.0 / 1024.0

What should I do if my texture is not square?

PARTNERS