# Line/Sphere Intersection Vertex Shader Problem

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

## Recommended Posts

Dear all, I've been trying to get the atmospheric scattering method discribed in GPU gems 2 going for awhile now, and I've been having lots of problems. I've got it narrowed down to the fact that my line/sphere intersection code isnt working properly(listed below) float3 v3Pos = gl_Vertex.xyz; float3 v3Ray = (v3Pos - v3CameraPos); float fFar = length(v3Ray); v3Ray /= fFar; // Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere) float B = 2.0 * dot(v3CameraPos, v3Ray); float C = fCameraHeight2 - fOuterRadius2; float fDet = max(0.0, B*B - 4.0 * C); float fNear = 0.5 * (-B - sqrt(fDet)); Seems simple enough, and copied directly from the book. Also when I run the sample program that he lists online, and have it jsut output fNear, I see what one whould expect, a sphere that fades to black, which grows larger as you zoom in, and smaller as you zoom out. When I run my code, I get a sphere that fades to black, but its sorrunded by another circle, and it gets smaller as you zoom in. Very confusing. I cant think what could be wrong with the code. Does anyone have any suggestions on debugging it? Regards, Jesse

##### Share on other sites
I have similar troubles. I dont know why, but I have to divide in.position by skydome radius. Which is wierd, because I do that on the CPU as well.

Length is the magnitude of the camera position from the center of the planet.

in .cpp
"
fCameraHeight = Length;
"

in .fx
"
float3 v3Pos = IN.Position / fSkydomeRadius; //<----
float3 v3Ray = v3Pos - v3CameraPos;
float fFar = length(v3Ray);
v3Ray /= fFar;

// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
float B = 2.0 * dot(v3CameraPos, v3Ray);
float C = fCameraHeight2 - fOuterRadius2;
float fDet = max(0.0, B*B - 4.0 * C);
float fNear = 0.5 * (-B - sqrt(fDet));
"

Also note that I am unable to get any other scale to work, ie
Are the only value that work.

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×