Disclaimer: I am not recommending that OO programming be generally accomplished in C, but rather am writing to argue how some OO constructs can be integrated into a program to improve its design / readability (i.e., elegance).
Many years ago when I was an undergraduate in Computer Science, one assigned project was to write a ray tracer. The provided framework was in C++, but having not studied the details of inheritance, overloading, etc, I elected to rewrite the framework in C. Since the framework was very simple, the few classes were merely renamed to struct and member functions provided a prefix. Yet, this is not the interesting part, merely the starting point.
In brief, ray tracers cast (i.e., test for intersection) a ray (i.e., vector) against the scene (i.e., a list of objects). The object that is first intersected provides the color for that part of the image. Different objects will have different intersection tests / other properties. In pseudo-C, the code for intersection might read:
object = objectList->head;
minDist = INF; minObject = NULL;
while (object != NULL) {
dist = Intersect(object, ray);
if (dist < minDist) {... }
object = object->next;
}The fundamental design problem is therefore how to properly set up the objects so that the ray tracer's operations upon its scene objects are still elegant. How to accomplish this will be discussed in part 2, along with how OO intrinsically works.
No comments:
Post a Comment