From: ip217@brems.ii.uib.no (Torgeir Veimo)
Subject: Re: Ray Transformation
Keywords: raytracing, transformation
Organization: University of Bergen, Norway
Date: Thu, 15 Oct 92 16:42:20 GMT


paulf@halcyon.com (Paul Forgey) writes:

>belme@waterloo.hp.com (Frank Belme) writes:

>>I was wondering what a good way is to calculate the direction of each ray 
>>for ray tracing a scene given an eyepoint, eye direction, and field of view
>>angle.  Any help would be appreciated.  Thanks.

>A method that I used, which is probably pretty clumsy, but it worked,
>was to take point (0, 1, 0), treat it as a 3d stick, and rotate it for
>the angles needed.

Sorry for the long lines guys, but since this includes c code i didn't bother to 
break it down to 79 chars per line.

The best way to do this is to use vectors defining a virtual screen in object space.
i have included some code for doing this. This is a standard way of doing this, 
which would be found in many textbooks concerning raytracing or other 
3d projections.

There are some constants defined elsewhere in the program:

aov - angle of view
columns - number of columns of pixels
rows - number of rows of pixels
PI -3.14159etc.

FROM - a position vector defining the viewpoint
AT - a position vector defining the point in the middle of the scene
UP - vector defining the up direction for the camera/eye
ratio - aspect ratio x/y

The vector operations are written in c++ where vector*vector is the dotproduct
of two vectors and vector^vector is the cross product of two vectors.

void traceimage(int columns, int rows, double ratio, double aov, char *filename)
{
	int col, row, halfcol = columns + 1 >>1, halfrow = rows+1 >> 1;
	vector dir, cop, u, v; //direction_of_ray, center_of_projection
	vector vrp, vup, vpn; //view_reference_point, vectro_up etc..
	double width; intensity c; FILE *imagefile;

	cop = FROM; vrp = AT; vup = UP;
	width = tan(aov*PI/360); //tan() takes radiand, aov is in degrees

	vpn = vrp - cop; //vpn is a vector normal to the virtual screen, the viewplane
	unitize(vpn); //vpn = view_plane_normal

	v = vup - (vpn*vup)*vpn; //v is vertical along the viewplane
	unitize(v);

	u = vpn^v; //u is horizontal along the viewplane
	unitize(u);

	u *= width/halfcol; //scaling since we are raytracing at both sides of the vrp
				//and we also scale for the wideness of our view, aov
	v *= ratio*width*rows/columns/halfrow; //scaling according to the aspect ratio

	if ((imagefile = fopen(filename, "wb")) == NULL) error("cannot open file.");

	for (row = 0; row < rows; row++) {
		for (col = 0; col < columns; col++) {

			dir = vpn + (col - halfcol)*u + (halfrow - row)*v;
			unitize(&dir); //dir now points to pixel we are raytracing

			c = trace(cop, dir, 0, AIR); 
				// yes, it IS a monochrome tracer, i have only got a sun 3/50 with b/w screen..

			if (fprintf(imagefile, "%c", (char)c*255) == EOF) error("cannot write to file.");
		}
		printf("%d rows raytraced   \r", row+1);
	}
	printf("finnished raytracing image      \n\a");
	if (fclose(imagefile) == EOF) error("cannot close file\n");
}

errors?? mail me pls..

-----------------------------------------------------
Torgeir Veimo
Studying at the University of Bergen

"...I'm gona wave my freak flag high!" (Jimi Hendrix)

"word wrap? you must be kidding..."
-----------------------------------------------------
Xref: sparky comp.graphics:10522 comp.graphics.animation:568 comp.realtime:1181 comp.sys.ibm.pc:687 comp.sys.ibm.pc.hardware:26750 comp.sys.ibm.pc.misc:13606