Ray tracing: Problems with Shadow Rays Output
Before this is tagged as a duplicate, I am having the exact same problem as the individual who asked this question.
Unfortunately, it doesn't have an answer that helped at all, so hopefully this will spark a better discussion. Using C++, the task at hand is to program a classic ray tracer. I am using a Phong shading model, and I am able to illuminate my scenes accurately up until I need to cast shadow rays.
Just for reference, here is a picture of what I have so far without attempting shadows:
Following a traditional ray tracing algorithm where I look for the closest object to my pinhole camera, I call the following function that computes the resulting pixel colour:
glm::vec3 Scene::illuminate(Ray* primary, Surface* object, glm::vec3 hitPt)
{
float red, green, blue;
Material m = object->getMaterial();
float p = m.phongCoef;
glm::vec3 kD = m.diffuse;
glm::vec3 kS = m.specular;
glm::vec3 kA = m.ambient;
float iA = m.ambienceIntensity;
float i = lightSrc.lightIntensity;
glm::vec3 n = object->getSurfaceNormalAt(hitPt); //normalized before return.
glm::vec3 viewRay = primary->getDirection(); //normalized class member.
glm::vec3 lightRay = glm::normalize((lightSrc.getOrigin()) - hitPt); //origin not normalized before here
glm::vec3 h = glm::normalize(viewRay + lightRay);
float nDotlightRay = glm::dot(n,lightRay);
float nDoth = glm::dot(n, h);
bool inShadow = false;
Ray shadowRay;
//hitPt = glm::normalize(hitPt);
shadowRay.setOrigin(hitPt);
shadowRay.setDirectionVector(lightRay);
for (int k = 0; k < objects.size(); ++k) {
if (objects.at(k)->intersect(shadowRay)) {
//std::cout<<shadowRay.getDirection().x<<","<< shadowRay.getDirection().y <<","<<shadowRay.getDirection().z<<"n";
inShadow = true;
break;}}
//plug in to shading model
if (inShadow)
{
red = kA.x*iA;
green = kA.y*iA ;
blue = kA.z*iA ;
}
else{
red = kA.x*iA + kD.x*i*(fmax(0,nDotlightRay)) + kS.x*i*(powf((fmax(0, nDoth)),p));
green = kA.y*iA + kD.y*i*(fmax(0,nDotlightRay)) + kS.y*i*(powf((fmax(0, nDoth)),p));
blue = kA.z*iA + kD.z*i*(fmax(0,nDotlightRay)) + kS.z*i*(powf((fmax(0, nDoth)),p));
}
glm::vec3 pixelColor = glm::vec3(red, green, blue);
return pixelColor;
}
I have tried to use descriptive variable/function names so as to minimize the explanation that accompanies my code. glm::vec3
is just a data structure from the GLM math library that holds a 3D vector on which matrix/vector manipulations can be done.
As in the question I linked above, here is what my scene looks like after trying shadows.
The light position for this scene is (0, 2.5, -7.75) expressed in the camera reference frame (which has an origin of (0,0,0) and is facing the negative z-direction). For another scene, the light source is at (4, 6, -1) and for that the output isn't even consistent with the problems of the other scene I tried to render as you can see here; it only has a thin grey line across the top of the scene, no shadows anywhere else? I am completely lost and have spent about 10 hours on this problem alone; any advice would be great.
Please let me know if there is any other information else I can provide, such as how I calculate distance and hitPoint?? I dont know if I should look into those since the shading part works perfectly, or do you guys think it Could still cause this problem? Depth and such shouldn't be a problem since the objects look correct up until shadows?
3d raytracing phong
add a comment |
Before this is tagged as a duplicate, I am having the exact same problem as the individual who asked this question.
Unfortunately, it doesn't have an answer that helped at all, so hopefully this will spark a better discussion. Using C++, the task at hand is to program a classic ray tracer. I am using a Phong shading model, and I am able to illuminate my scenes accurately up until I need to cast shadow rays.
Just for reference, here is a picture of what I have so far without attempting shadows:
Following a traditional ray tracing algorithm where I look for the closest object to my pinhole camera, I call the following function that computes the resulting pixel colour:
glm::vec3 Scene::illuminate(Ray* primary, Surface* object, glm::vec3 hitPt)
{
float red, green, blue;
Material m = object->getMaterial();
float p = m.phongCoef;
glm::vec3 kD = m.diffuse;
glm::vec3 kS = m.specular;
glm::vec3 kA = m.ambient;
float iA = m.ambienceIntensity;
float i = lightSrc.lightIntensity;
glm::vec3 n = object->getSurfaceNormalAt(hitPt); //normalized before return.
glm::vec3 viewRay = primary->getDirection(); //normalized class member.
glm::vec3 lightRay = glm::normalize((lightSrc.getOrigin()) - hitPt); //origin not normalized before here
glm::vec3 h = glm::normalize(viewRay + lightRay);
float nDotlightRay = glm::dot(n,lightRay);
float nDoth = glm::dot(n, h);
bool inShadow = false;
Ray shadowRay;
//hitPt = glm::normalize(hitPt);
shadowRay.setOrigin(hitPt);
shadowRay.setDirectionVector(lightRay);
for (int k = 0; k < objects.size(); ++k) {
if (objects.at(k)->intersect(shadowRay)) {
//std::cout<<shadowRay.getDirection().x<<","<< shadowRay.getDirection().y <<","<<shadowRay.getDirection().z<<"n";
inShadow = true;
break;}}
//plug in to shading model
if (inShadow)
{
red = kA.x*iA;
green = kA.y*iA ;
blue = kA.z*iA ;
}
else{
red = kA.x*iA + kD.x*i*(fmax(0,nDotlightRay)) + kS.x*i*(powf((fmax(0, nDoth)),p));
green = kA.y*iA + kD.y*i*(fmax(0,nDotlightRay)) + kS.y*i*(powf((fmax(0, nDoth)),p));
blue = kA.z*iA + kD.z*i*(fmax(0,nDotlightRay)) + kS.z*i*(powf((fmax(0, nDoth)),p));
}
glm::vec3 pixelColor = glm::vec3(red, green, blue);
return pixelColor;
}
I have tried to use descriptive variable/function names so as to minimize the explanation that accompanies my code. glm::vec3
is just a data structure from the GLM math library that holds a 3D vector on which matrix/vector manipulations can be done.
As in the question I linked above, here is what my scene looks like after trying shadows.
The light position for this scene is (0, 2.5, -7.75) expressed in the camera reference frame (which has an origin of (0,0,0) and is facing the negative z-direction). For another scene, the light source is at (4, 6, -1) and for that the output isn't even consistent with the problems of the other scene I tried to render as you can see here; it only has a thin grey line across the top of the scene, no shadows anywhere else? I am completely lost and have spent about 10 hours on this problem alone; any advice would be great.
Please let me know if there is any other information else I can provide, such as how I calculate distance and hitPoint?? I dont know if I should look into those since the shading part works perfectly, or do you guys think it Could still cause this problem? Depth and such shouldn't be a problem since the objects look correct up until shadows?
3d raytracing phong
add a comment |
Before this is tagged as a duplicate, I am having the exact same problem as the individual who asked this question.
Unfortunately, it doesn't have an answer that helped at all, so hopefully this will spark a better discussion. Using C++, the task at hand is to program a classic ray tracer. I am using a Phong shading model, and I am able to illuminate my scenes accurately up until I need to cast shadow rays.
Just for reference, here is a picture of what I have so far without attempting shadows:
Following a traditional ray tracing algorithm where I look for the closest object to my pinhole camera, I call the following function that computes the resulting pixel colour:
glm::vec3 Scene::illuminate(Ray* primary, Surface* object, glm::vec3 hitPt)
{
float red, green, blue;
Material m = object->getMaterial();
float p = m.phongCoef;
glm::vec3 kD = m.diffuse;
glm::vec3 kS = m.specular;
glm::vec3 kA = m.ambient;
float iA = m.ambienceIntensity;
float i = lightSrc.lightIntensity;
glm::vec3 n = object->getSurfaceNormalAt(hitPt); //normalized before return.
glm::vec3 viewRay = primary->getDirection(); //normalized class member.
glm::vec3 lightRay = glm::normalize((lightSrc.getOrigin()) - hitPt); //origin not normalized before here
glm::vec3 h = glm::normalize(viewRay + lightRay);
float nDotlightRay = glm::dot(n,lightRay);
float nDoth = glm::dot(n, h);
bool inShadow = false;
Ray shadowRay;
//hitPt = glm::normalize(hitPt);
shadowRay.setOrigin(hitPt);
shadowRay.setDirectionVector(lightRay);
for (int k = 0; k < objects.size(); ++k) {
if (objects.at(k)->intersect(shadowRay)) {
//std::cout<<shadowRay.getDirection().x<<","<< shadowRay.getDirection().y <<","<<shadowRay.getDirection().z<<"n";
inShadow = true;
break;}}
//plug in to shading model
if (inShadow)
{
red = kA.x*iA;
green = kA.y*iA ;
blue = kA.z*iA ;
}
else{
red = kA.x*iA + kD.x*i*(fmax(0,nDotlightRay)) + kS.x*i*(powf((fmax(0, nDoth)),p));
green = kA.y*iA + kD.y*i*(fmax(0,nDotlightRay)) + kS.y*i*(powf((fmax(0, nDoth)),p));
blue = kA.z*iA + kD.z*i*(fmax(0,nDotlightRay)) + kS.z*i*(powf((fmax(0, nDoth)),p));
}
glm::vec3 pixelColor = glm::vec3(red, green, blue);
return pixelColor;
}
I have tried to use descriptive variable/function names so as to minimize the explanation that accompanies my code. glm::vec3
is just a data structure from the GLM math library that holds a 3D vector on which matrix/vector manipulations can be done.
As in the question I linked above, here is what my scene looks like after trying shadows.
The light position for this scene is (0, 2.5, -7.75) expressed in the camera reference frame (which has an origin of (0,0,0) and is facing the negative z-direction). For another scene, the light source is at (4, 6, -1) and for that the output isn't even consistent with the problems of the other scene I tried to render as you can see here; it only has a thin grey line across the top of the scene, no shadows anywhere else? I am completely lost and have spent about 10 hours on this problem alone; any advice would be great.
Please let me know if there is any other information else I can provide, such as how I calculate distance and hitPoint?? I dont know if I should look into those since the shading part works perfectly, or do you guys think it Could still cause this problem? Depth and such shouldn't be a problem since the objects look correct up until shadows?
3d raytracing phong
Before this is tagged as a duplicate, I am having the exact same problem as the individual who asked this question.
Unfortunately, it doesn't have an answer that helped at all, so hopefully this will spark a better discussion. Using C++, the task at hand is to program a classic ray tracer. I am using a Phong shading model, and I am able to illuminate my scenes accurately up until I need to cast shadow rays.
Just for reference, here is a picture of what I have so far without attempting shadows:
Following a traditional ray tracing algorithm where I look for the closest object to my pinhole camera, I call the following function that computes the resulting pixel colour:
glm::vec3 Scene::illuminate(Ray* primary, Surface* object, glm::vec3 hitPt)
{
float red, green, blue;
Material m = object->getMaterial();
float p = m.phongCoef;
glm::vec3 kD = m.diffuse;
glm::vec3 kS = m.specular;
glm::vec3 kA = m.ambient;
float iA = m.ambienceIntensity;
float i = lightSrc.lightIntensity;
glm::vec3 n = object->getSurfaceNormalAt(hitPt); //normalized before return.
glm::vec3 viewRay = primary->getDirection(); //normalized class member.
glm::vec3 lightRay = glm::normalize((lightSrc.getOrigin()) - hitPt); //origin not normalized before here
glm::vec3 h = glm::normalize(viewRay + lightRay);
float nDotlightRay = glm::dot(n,lightRay);
float nDoth = glm::dot(n, h);
bool inShadow = false;
Ray shadowRay;
//hitPt = glm::normalize(hitPt);
shadowRay.setOrigin(hitPt);
shadowRay.setDirectionVector(lightRay);
for (int k = 0; k < objects.size(); ++k) {
if (objects.at(k)->intersect(shadowRay)) {
//std::cout<<shadowRay.getDirection().x<<","<< shadowRay.getDirection().y <<","<<shadowRay.getDirection().z<<"n";
inShadow = true;
break;}}
//plug in to shading model
if (inShadow)
{
red = kA.x*iA;
green = kA.y*iA ;
blue = kA.z*iA ;
}
else{
red = kA.x*iA + kD.x*i*(fmax(0,nDotlightRay)) + kS.x*i*(powf((fmax(0, nDoth)),p));
green = kA.y*iA + kD.y*i*(fmax(0,nDotlightRay)) + kS.y*i*(powf((fmax(0, nDoth)),p));
blue = kA.z*iA + kD.z*i*(fmax(0,nDotlightRay)) + kS.z*i*(powf((fmax(0, nDoth)),p));
}
glm::vec3 pixelColor = glm::vec3(red, green, blue);
return pixelColor;
}
I have tried to use descriptive variable/function names so as to minimize the explanation that accompanies my code. glm::vec3
is just a data structure from the GLM math library that holds a 3D vector on which matrix/vector manipulations can be done.
As in the question I linked above, here is what my scene looks like after trying shadows.
The light position for this scene is (0, 2.5, -7.75) expressed in the camera reference frame (which has an origin of (0,0,0) and is facing the negative z-direction). For another scene, the light source is at (4, 6, -1) and for that the output isn't even consistent with the problems of the other scene I tried to render as you can see here; it only has a thin grey line across the top of the scene, no shadows anywhere else? I am completely lost and have spent about 10 hours on this problem alone; any advice would be great.
Please let me know if there is any other information else I can provide, such as how I calculate distance and hitPoint?? I dont know if I should look into those since the shading part works perfectly, or do you guys think it Could still cause this problem? Depth and such shouldn't be a problem since the objects look correct up until shadows?
3d raytracing phong
3d raytracing phong
edited Nov 25 '18 at 17:36
Rabbid76
40.8k123252
40.8k123252
asked Nov 25 '18 at 13:49
assistantToTheRegionalMngrassistantToTheRegionalMngr
113
113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Answering my own question: The specific shadow artifacts mentioned above were a result of omitting the ray origin in my intersection calculations. Originally I didn't bother since my camera reference frame was at (0,0,0). But for shadow rays, the origin is not a zero vector and thus reusing the same intersection-test functions gave results that were totally off. Making sure to use normalized direction vectors is also key or the outputs will make no sense, making it difficult to narrow down what the bug could be.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468139%2fray-tracing-problems-with-shadow-rays-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Answering my own question: The specific shadow artifacts mentioned above were a result of omitting the ray origin in my intersection calculations. Originally I didn't bother since my camera reference frame was at (0,0,0). But for shadow rays, the origin is not a zero vector and thus reusing the same intersection-test functions gave results that were totally off. Making sure to use normalized direction vectors is also key or the outputs will make no sense, making it difficult to narrow down what the bug could be.
add a comment |
Answering my own question: The specific shadow artifacts mentioned above were a result of omitting the ray origin in my intersection calculations. Originally I didn't bother since my camera reference frame was at (0,0,0). But for shadow rays, the origin is not a zero vector and thus reusing the same intersection-test functions gave results that were totally off. Making sure to use normalized direction vectors is also key or the outputs will make no sense, making it difficult to narrow down what the bug could be.
add a comment |
Answering my own question: The specific shadow artifacts mentioned above were a result of omitting the ray origin in my intersection calculations. Originally I didn't bother since my camera reference frame was at (0,0,0). But for shadow rays, the origin is not a zero vector and thus reusing the same intersection-test functions gave results that were totally off. Making sure to use normalized direction vectors is also key or the outputs will make no sense, making it difficult to narrow down what the bug could be.
Answering my own question: The specific shadow artifacts mentioned above were a result of omitting the ray origin in my intersection calculations. Originally I didn't bother since my camera reference frame was at (0,0,0). But for shadow rays, the origin is not a zero vector and thus reusing the same intersection-test functions gave results that were totally off. Making sure to use normalized direction vectors is also key or the outputs will make no sense, making it difficult to narrow down what the bug could be.
answered Nov 26 '18 at 2:37
assistantToTheRegionalMngrassistantToTheRegionalMngr
113
113
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468139%2fray-tracing-problems-with-shadow-rays-output%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown