我一直在尝试使用光线追踪和phong来实现飞机。我还没能让它出现。
我目前有3个阴影效果的球体,但似乎无法使飞机起作用。任何人都可以发现为什么我的飞机没有显示以及为什么发生segFault的问题? (我解释了确切的行以及我如何避免在下面获得segFault,所以我觉得我已经找到了问题)
我希望这架飞机能够像地板一样可以看到阴影。我只包括相关的平面代码,并省略了球体的东西以便于阅读。如果有人要求,我可以加入。 (Sphere stuff working:http://imgur.com/i936UID,squarePlane代码的行“tmin = t”已注释掉以显示此内容。)
SegFault发生在
上的implementPhong函数中vec3 R = vec3 (0,0,0);
//normalize(reflect(-L, N));
//vec3 (0,0,0);
如果我使用注释掉的normalize函数而不是我的临时vec3(0,0,0)
。
如果我移动
,它也将是segFaultsquarePlane.normal = normalize(vec3(0,0.5,3)); // to (vec3(0,1,0))
//********** Global Variables *********
RgbImage * myImage;
vec3 nEye(0.0,0.0,-5.0);
vec3 lightSource1(-2.0,-1.5,0.0);
vec3 lightSource2(0.0,-2.0,0.0);
float tmin=100.0;
struct Ray
{
vec3 pt;
vec3 vecDir;
};
struct Prim
{
vec3 origin;
double radius;
double underSquare;
vec3 ambient;
vec3 diffuse_albedo;
vec3 specular_albedo;
float specular_power;
vec3 light_intensity;
vec3 normal;
vec3 pt1;
vec3 pt2;
vec3 pt3;
};
vec3 trace(Ray ray){
Prim squarePlane;
Prim pickSphere;
tmin = 100;
vec3 pixelColor(0,0,1);
squarePlane.pt1 = vec3(-0.2,0.0,0.0);
squarePlane.pt2 = vec3(0.2,0.0,0.0);
squarePlane.pt3 = vec3(-0.2,0.1,0.0);
squarePlane.ambient=vec3(0.329412, 0.223529, 0.027451);
squarePlane.diffuse_albedo=vec3(0.780392, 0.568627, 0.113725);
squarePlane.specular_albedo = vec3(0.992157,0.941176,0.807843);
squarePlane.specular_power = 27.8974;
squarePlane.light_intensity = vec3(1,1,1);
squarePlane.normal = normalize(vec3(0,1,0));
float D = 0;
float denom = dot(squarePlane.normal, nVecDir);
float numer = dot(squarePlane.normal, nEye) + D;
vec3 hitTemp;
vec3 hitPoint;
if (denom!=0){
float t= -(dot(squarePlane.normal, nEye) + D) / denom;
if (t>=0){
tmin=t;
}
}
//pickSphere = squarePlane;
pickSphere = checkForIntersection(nEye, nVecDir, spheres, false); // last parameter is for print debugging
if (tmin != 100){ //if tmin has changed, we found and intersection, send shadow ray
vec3 hitTemp(nVecDir.x*tmin, nVecDir.y*tmin, nVecDir.z*tmin);
vec3 hitPoint = (nEye + hitTemp);
shadowRay.vecDir = (normalize(lightSource1 - (hitPoint)));
tmin = 100; // Set tmin for a new ray trace.
checkForIntersection(hitPoint, shadowRay.vecDir, spheres, true);
if (tmin != 100){ //if tmin does not equal 100, we found an intersection, this point is in shadow, return ambient.
pixelColor = pickSphere.ambient;
return pixelColor;
}
else {
pickSphere.normal = hitPoint - pickSphere.origin;
pixelColor = implementPhong(pickSphere,nEye,hitPoint);
//pixelColor = vec3(0,1,0);
return pixelColor;
}
}
else{
return pixelColor;
}
}
vec3 implementPhong(Prim drawSphere, vec3 camera, vec3 hitPoint){
vec3 L = drawSphere.light_intensity * normalize(lightSource1 - hitPoint);
vec3 N = normalize(drawSphere.normal);
vec3 V = normalize(camera - hitPoint);
vec3 R = vec3 (0,0,0); //normalize(reflect(-L, N)); //vec3 (0,0,0);
float zero = 0;
vec3 diffuse = std::max<float>(dot(N, L), zero) * drawSphere.diffuse_albedo;
vec3 specular = pow(std::max<float>(dot(R, V), zero), drawSphere.specular_power) * drawSphere.specular_albedo;
vec3 color = drawSphere.ambient + diffuse + specular;
return color;
}