Ray tracing, sample and shade functions [Solved]

Hey folks,

So I’m taking a rendering course this semester and we were given a framework in C++ to create functions for ray tracing etc., and I’m at an end were I’m supposed to add in a sample and shade function.

I’ve tried to implement something, but it’s just returning a darker version of the image that I’m rendering, and I haven’t been given an image that shows how it’s supposed to look.

So I’m hoping that you guys can help me out a bit:

bool PointLight::sample(const float3& pos, float3& dir, float3& L) const
{
	// Compute output and return value given the following information.
	//
	// Input:  pos (the position of the geometry in the scene)
	//
	// Output: dir (the direction toward the light)
	//         L   (the radiance received from the direction dir)
	//
	// Return: true if not in shadow
	//
	// Relevant data fields that are available (see PointLight.h and Light.h):
	// shadows    (on/off flag for shadows)
	// tracer     (pointer to ray tracer)
	// light_pos  (position of the point light)
	// intensity  (intensity of the emitted light)
	//
	// Hint: Construct a shadow ray using the Ray datatype. Trace it using the
	//       pointer to the ray tracer.

	if(!shadows) {
		return true;
	}
	
	float3 normal = normalize(pos - light_pos);
	
	Ray ray(pos, normal, 0, 1e-10f);
	HitInfo hit;
	if(tracer->trace_to_closest(ray, hit)) {
	//	if(hit.position.x == light_pos.x && hit.position.y == light_pos.y
	//	    && light_pos.z == light_pos.z) {
			float v = length(light_pos - pos);
			dir = normal;
			L = intensity / (v * v);
			return false;
	//	}
	}

	return true;
}

and

float3 Lambertian::shade(const Ray& r, HitInfo& hit, bool emit) const
{
	float3 rho_d = get_diffuse(hit);
	float3 result = make_float3(0.0f);
	
	// Implement Lambertian reflection here.
	//
	// Input:  r          (the ray that hit the material)
	//         hit        (info about the ray-surface intersection)
	//         emit       (passed on to Emission::shade)
	//
	// Return: radiance reflected to where the ray was coming from
	//
	// Relevant data fields that are available (see Lambertian.h, HitInfo.h, and above):
	// lights             (vector of pointers to the lights in the scene)
	// hit.position       (position where the ray hit the material)
	// hit.shading_normal (surface normal where the ray hit the material)
	// rho_d              (difuse reflectance of the material)
	//
	// Hint: Call the sample function associated with each light in the scene.
	
	float3 dir, radiance;
	float3 diffuse = (rho_d / M_PIf);
	for(size_t i = 0; i < lights.size(); i++) {
		if(lights[i]->sample(hit.position, dir, radiance)) {
			result += diffuse * radiance * dot(hit.shading_normal, dir);
		}
	}
	
	return result + Emission::shade(r, hit, emit);
}

The code runs, but it doesn’t seem to “work,” or at least I’m not sure what kind of image I’m supposed to get out of it.

Any help/insight would be awesome! :slight_smile: