Short Programming Assignment #4

Additional Notes for Converting camera.c to camera.cpp

For the short proramming assignment #4, you will be converting some of your C modules to C++. You need to convert the following:

camera.c - - > to camera.cpp

plane.c - - - > to plane.cpp

sphere.c - - > to sphere.cpp

Some of the files that you copy to get started on this assignment include:

material.cpp

object.cpp

model.cpp

and also

parser.c- - > explained below and in more detail in additional parser notes

ray.h- - > remember, this file has all the class/structure definitions; you may need to refer to this on occasion

rayhdrs.h- - > this file is now really short; it contains prototypes for C functions, which there are only 8 now in this file

The files that you provide are your own list.cpp, which will be completed in lab; vector.c, vector.h, image.c, and raytrace.c.

If you use the given parser.c, then that might make things easier because the files that are given can be used as is with no modifications (material.cpp, object.cpp, model.cpp) where you won’t have to add any load_attributes functions anywhere.

Use the material.cpp file as a comparison for doing your camera.cpp file. In general, it is very similar.

If you look in ray.h, you’ll see the camera class definition. Under the public section are the methods that need to be implemented, except for the first constructor (there is nothing to do with that) and I think, for now at least, you don’t need to worry about the last method, getpixsize( ). So that leaves 8 methods to implement (next 2 pages).

In the Unit 11 Notes, starting on page 10, is where the discussion of the camera class begins.
1. camera constructor that takes in the argument FILE *in

It’s very similar to the material constructor, except of course, the section with the cam_parse array is specific to the camera. The parse tables are in Unit 9 Notes, around page 16.

If you’re going to use the parser, each “thing” in our raytracer - - camera, material, plane, sphere, etc (anything with attributes) - - has a parse array associated with it. Each item in the array is a pparm_t (defined in ray.h). A pparm_t has 5 parts:

attrname: the attribute name - such as “pixeldim”

numvals: the number of values - such as 2 if it is pixeldim, or 3 if it is viewpoint, etc

valsize: the size of each value - sizeof(int) for pixeldim, or sizeof(double) for worlddim and viewpoint

fmtstr: the format of the value - such as “%d” or “%lf”

loc: and the starting location - which by default starts out as 0

So, the camera parse array looks like this:

pparm_t cam_parse[ ] =

{

{"pixeldim", 2, sizeof(int), "%d", 0},

{"worlddim", 2, sizeof(double), "%lf", 0},

{"viewpoint", 3, sizeof(double), "%lf", 0},

};

#define NUM_ATTRS (sizeof(cam_parse) / sizeof(pparm_t))

where the first one with each part identified:

{"pixeldim", 2, sizeof(int), "%d", 0}

attrname numvals valsizefmtstr loc

Remember, this parse array, along with the parser.c module means that you won’t have to write a camera_load_attributes method, or a plane_load_attributes method, or a sphere_load_attributes method, etc. which if you look over, those functions are somewhat lengthy. In parser.c, there is a function called parser( ) that replaces all those load attribute functions.

If you don’t use the parser that is provided, then you will have to add the load attribute methods to the class definitions of camera, plane, sphere, etc. And, in the camera.cpp file, and plane.cpp file, and sphere.cpp file, etc, you’ll have to implement those functions also.

So, back to the constructor for the camera… this takes the place of camera_init( ) which initializes all 6 of the private data items of the camera:

1. The cookie is initialized.

2. The name is initialized.

6. The pixmap is malloc’d like we did before.

2. cameraprint method

Nothing different here from your camera.c file, except with the syntax , i.e. instead of cam->name, it’s just name.

Remember, in C++, we no longer need to send in the camera as an argument; we are invoking these methods on a particular camera which is why we don’t need to specify cam->name .

Remember also, here in these method implementations, you CAN access pixel_dim[0] because these implementations are AS IF they are in the actual class definition, so private data members can be accessed directly.

3-5. cameragetter methods

getxdim( )

getydim( ) - - this is in Unit 11 Notes, page 13

getviewpt(vec_t *view)

These are needed so that in the make_row( ) function in image.c, for example, you will be able to access pixel_dim[0] indirectly via the getxdim( ) method so that you can use that in the for loop because that make_row( ) function is not inside the class definition, so the private data members cannot be accessed there.

6. cameragetdir method

Nothing different here from your camera.c file except the syntax (again, drop the cam->).

7. camerastore_pixel method

Nothing different here from your camera.c file except the syntax (again, drop the cam->).

8. camerawrite_image method

Nothing different here from your camera.c file except the syntax (again, drop the cam->).