Jump to content

  • Log In with Google      Sign In   
  • Create Account

melak47

Member Since 02 Mar 2012
Offline Last Active Mar 08 2013 10:04 AM

Posts I've Made

In Topic: Sean O'Neils atmospheric scattering

05 March 2012 - 04:59 AM

You might want to think about working from the most current version of his shaders (poke around in 'downloads'), rather than the dated, and somewhat badly-commented version in GPU Gems...

The thing is, I am!
I have started the SkyFromSpace shader from scratch about 6 times now, once from the GPU Gems article, once from the current version, once from the article in nvidia FX Composer, trying to debug it, again from the current version in fx composer, once from someone else's source code on here, and once from Hyunkel's code here.

I figured out why I didn't get the orange transition. Before I got the lightScale value right, the whole sphere was lit up so I multiplied the resulting diffuse by the lightAngle to make the far side of the planet dark. Now that the lightScale works correctly though, it was fading the diffuse out before the transition.
Now that it's got the right value, the far side of the planet turns dark on it's own.

I'm kind of clueless about the SkyFromSpace shader though...I guess I will take another stab at it sometime.

In Topic: Sean O'Neils atmospheric scattering

04 March 2012 - 07:54 PM

v3LightPos is actually the light direction, not the light position, even though the name suggests otherwise.
It needs to be normalized.

mScaleDepth, which I am assuming is fInvScaleDepth needs to be 1 / ScaleDepth.

for "g" I would stick to -0.98f.
It doesn't really influence anything too much besides the fake sun when rendering the sky from atmosphere though, which you aren't doing.

Apart from that I can't find any problems.


Normalizing LighgtDir/Pos gives me more colored specks, they flicker extremely fast and are hard to capture:
Posted Image

In O'Neils C++ soruce code there are both rayleighScaleDepth and mieScaleDepth, though all the shaders only use the rayleigh one.
InvScaleDepth is only used in this one line, so I opted for
float startDepth = exp(-1 / rScaleDepth);

instead.

Is the orange day/night transition in your renders a result of O'Neils shader, or something that you are doing?

SkyFromSpace.fx:
cbuffer perObject
{
float4x4 Scale;
float4x4 Rotation;
float4x4 Position;
float4 objColor;
float4 objPos;
int wire;
int textured;
};
cbuffer perFrame
{
float4x4 view;
float4x4 projection;

float4 CamPosW;
float4 LightPosW;
float4 WaveLength;
float samples;
float pRadius;
float aRadius;
float KrESun;
float KmESun;
float Kr4PI;
float Km4PI;
float g;
float rScaleDepth;
float mScaleDepth;
};
struct appdata
{
	float4 Pos	: POSITION;
	float3 Normal : NORMAL;
	float3 UVW   : TEXCOORD;
};
struct v2p
{
float4 Pos : SV_POSITION;
float4 Color0 : COLOR0;
float4 Color1 : COLOR1;
float3 Direction : TEXCOORD;
};

float scale(float f)
{
float x = 1.0 - f;
return rScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}
// Calculates the Mie phase function
float getMiePhase(float fCos, float fCos2, float g, float g2)
{
return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(abs(1.0 + g2 - 2.0*g*fCos), 1.5);
}
// Calculates the Rayleigh phase function
float getRayleighPhase(float fCos2)
{
//return 1.0;
return 0.75 + 0.75*fCos2;
}
// Returns the near intersection point of a line and a sphere
float getNearIntersection(float3 v3Pos, float3 v3Ray, float fDistance2, float fRadius2)
{
float B = 2.0 * dot(v3Pos, v3Ray);
float C = fDistance2 - fRadius2;
float fDet = max(0.0, B*B - 4.0 * C);
return 0.5 * (-B - sqrt(fDet));
}
v2p VS(appdata input)
{
//calculate prerequisites
float4x4 WVP = transpose(mul(mul(mul(mul(projection, view), Position), Rotation), Scale));
float4x4 World = transpose(mul(mul(Position,Rotation),Scale));

float3 CamPos = CamPosW - objPos;
float3 LightPos = LightPosW - objPos;
LightPos /= length(LightPos);
  
float3 InvWaveLength = 1.0 / pow(WaveLength.xyz, 4);
float fscale = 1.0 / (aRadius - pRadius);
float scaleOverScaleDepth = fscale / rScaleDepth;
float altitude = length(CamPos);

float3 pos = mul(input.Pos, World).xyz - objPos.xyz;
float3 ray = pos - CamPos;
pos /= length(pos);
float far = length(ray);
ray /= far;
float near = getNearIntersection(CamPos, ray, altitude*altitude, aRadius*aRadius);
//initial values
float3 start = CamPos + ray * near;
far -= near;
float startAngle = dot(ray, start) / aRadius;
float startDepth = exp(-1 / rScaleDepth);
float startOffset = startDepth * scale(startAngle);
float sampleLength = far / samples;
float scaledLength = sampleLength * fscale;
float3 sampleRay = ray * sampleLength;
float3 samplePoint  = start + sampleRay * 0.5;
//loop through samples
float3 frontColor = float3(0,0,0);
for (int i=0; i<(int)samples; i++)
{
  float height = length(samplePoint.xyz);
  float depth = exp(scaleOverScaleDepth * (pRadius - height));
  float lightAngle = dot(LightPos, samplePoint) / height;
  float camAngle = dot(ray, samplePoint) / height;
  float scatter = (startOffset + depth * (scale(lightAngle) - scale(camAngle)));
  float3 attenuate = exp(-scatter * (InvWaveLength * Kr4PI + Km4PI));
  frontColor += attenuate * (depth * scaledLength);
  samplePoint += sampleRay;
}

float3 c0 = frontColor * (InvWaveLength.xyz * KrESun);
float3 c1 = frontColor * KmESun;
float3 direction = CamPos - pos;
float Cos = dot(LightPos, direction) / length(direction);

v2p output;
output.Pos = mul(input.Pos, WVP);
output.Color0 = float4(c0, Cos);
output.Color1 = float4(c1, 1);
output.Direction = float4(direction, 1);
return output;
}

float4 PS(v2p input) : SV_TARGET
{
float Cos = input.Color0.w;
float3 color = getRayleighPhase(Cos*Cos) * input.Color0 + getMiePhase(Cos, Cos*Cos, g, g*g) * input.Color1;
return float4(color, color.z);
}


And the GroundFromSpace.fx as well:
cbuffer perObject
{
float4x4 Scale;
float4x4 Rotation;
float4x4 Position;
float4 objColor;
float4 objPos;
int wire;
int textured;
};
cbuffer perFrame
{
float4x4 view;
float4x4 projection;

float4 CamPosW;
float4 LightPosW;
float4 WaveLength;
float samples;
float pRadius;
float aRadius;
float KrESun;
float KmESun;
float Kr4PI;
float Km4PI;
float g;
float rScaleDepth;
float mScaleDepth;
};
Texture2D objTexture;
SamplerState texSampler;

struct appdata
{
	float4 Pos	: POSITION;
	float3 Normal : NORMAL;
	float3 UVW   : TEXCOORD;
};
struct v2p
{
float4 Pos : SV_POSITION;
float4 Color0 : COLOR0;
float4 Color1 : COLOR1;
float3 TexCoord : TEXCOORD;
};

float scale(float fCos)
{
float x = 1.0 - fCos;
return rScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}
// Returns the near intersection point of a line and a sphere
float getNearIntersection(float3 v3Pos, float3 v3Ray, float fDistance2, float fRadius2)
{
float B = 2.0 * dot(v3Pos, v3Ray);
float C = fDistance2 - fRadius2;
float fDet = max(0.0, B*B - 4.0 * C);
return 0.5 * (-B - sqrt(fDet));
}

v2p VS(appdata input)
{
//calculate prerequisites
float4x4 WVP = transpose(mul(mul(mul(mul(projection, view), Position), Rotation), Scale));
float4x4 World = transpose(mul(mul(Position,Rotation),Scale));

float3 CamPos = CamPosW - objPos;
float3 LightPos = LightPosW - objPos;
float3 LightDir = LightPos / length(LightPos);
float4 InvWaveLength = 1.0 / float4(pow(WaveLength.x, 4), pow(WaveLength.y, 4), pow(WaveLength.z, 4), 0);
float fscale = 1.0 / (aRadius - pRadius);
float scaleOverScaleDepth = fscale / rScaleDepth;
float altitude = length(CamPos);

float3 pos = mul(input.Pos, World).xyz - objPos.xyz;
float3 ray = pos - CamPos;
pos /= length(pos);
float far = length(ray);
ray /= far;
float near = getNearIntersection(CamPos, ray, altitude*altitude, aRadius*aRadius);
//initial values
float3 start = CamPos + ray * near;
far -= near;
float depth = exp((pRadius - aRadius) / rScaleDepth);
float camAngle = dot(-ray, pos);
float lightAngle = dot(LightDir, pos);
float camScale = scale(camAngle);
float lightScale = scale(lightAngle);
float camOffset = depth*camScale;
float temp = lightScale + camScale;
float sampleLength = far / samples;
float scaledLength = sampleLength * fscale;
float3 sampleRay = ray * sampleLength;
float3 samplePoint  = start + sampleRay * 0.5;
//loop through samples
float3 frontColor = float3(0,0,0);
float3 attenuate;
for (int i=0; i<(int)samples; i++)
{
  float height = length(samplePoint.xyz);
  depth = exp(scaleOverScaleDepth * (pRadius - height));
  float scatter = (depth*temp) - camOffset;
  attenuate = exp(-scatter * (InvWaveLength * Kr4PI + Km4PI));
  frontColor += attenuate * (depth * scaledLength);
  samplePoint += sampleRay;
}
v2p output;
output.Pos = mul(input.Pos, WVP);
output.Color0 = float4(frontColor * (InvWaveLength.xyz * KrESun + KmESun), lightAngle);
output.Color1 = float4(attenuate, 1);
output.TexCoord = input.UVW;
return output;
}
float4 PS(v2p input) : SV_TARGET
{
float4 planetColor;
if (textured) planetColor =  objTexture.Sample(texSampler, input.TexCoord.xyz);
else planetColor = objColor;
float lightAngle = input.Color0.w;
float4 color = input.Color0 + (planetColor * input.Color1);
color *= lightAngle;
color.w = 1.0;
return color;
}

In Topic: Sean O'Neils atmospheric scattering

04 March 2012 - 06:20 PM

I pass the object position to the shader and use vertex, camera and light position relative to that.

These are my shader constants:
   constBuffer.samples = 3;
   float Kr = 0.0025f;
   float Km = 0.0010f;
   float ESun = 20.0f;
   float pi = 3.14159265358979323846f;
   constBuffer.Kr4PI = Kr * 4 * pi;
   constBuffer.Km4PI = Km * 4 * pi;
   constBuffer.KrESun = Kr * ESun;
   constBuffer.KmESun = Km * ESun;
   constBuffer.pRadius = 1.0f;
   constBuffer.aRadius = 1.025f;
  
   constBuffer.rScaleDepth = 0.25f;
   constBuffer.mScaleDepth = 0.1f;

   constBuffer.CamPosW = XMFLOAT4(sin(t) * 5, 5, cos(t) * 5, 0.0f);
   constBuffer.LightPos = XMFLOAT4(1000, 1000, 1000, 0.0f);
   constBuffer.g = -0.99f;
   constBuffer.WaveLength = XMFLOAT4(0.65f, 0.57f, 0.475f, 0.0f);

A few others I calculate in the shader:
	float3 CamPos = CamPosW - objPos;
	float3 LightPos = LightPosW - objPos;
	float3 InvWaveLength = 1.0 / pow(WaveLength.xyz, 4);
	float fscale = 1.0 / (aRadius - pRadius);
	float scaleOverScaleDepth = fscale / rScaleDepth;
	float altitude = length(CamPos);

	float3 pos = mul(input.Pos, World).xyz - objPos.xyz;

In Topic: Monitoring GPU activity for a DirectX app

04 March 2012 - 05:18 PM

nvidia inspector will let you monitor GPU load, VRAM usage, clock speed and temperature.

If what you are drawing so far is relatively simple, your app will push the GPU as fast as it can to draw more, and you will get thousands or even tenthousands of fps, with your GPU at 100% load.

Presenting with vsync will make your Present call block until a vsync occurs (every 1/60th second, for example), so if you had 10.000 fps before, your GPU load might drop as low as 1% with vsync.

It's as easy as calling Present(1, 0) instead of Present(0,0).
IDXGISwapChain::Present
You could also wait for more vsyncs to save power if you like, for example when your app loses focus.

In Topic: Cartesian system in DirectX

04 March 2012 - 04:50 PM

The easiest thing would probably be to split it into two draw calls.
set the primitive topology to pointlist before you draw your points, then to linelist before you draw your axes.

your vertex buffer for the axes might look like this:

XMFLOAT3 vertices[] = { XMFLOAT3(0, 0, 0), XMFLOAT3(10, 0, 0),
	 	 	 XMFLOAT3(0, 0, 0), XMFLOAT3(0, 10, 0),
	 	 	 XMFLOAT3(0, 0, 0), XMFLOAT3(0, 0, 10), };

PARTNERS