Ray tracing

Hello,

I am working on a ray tracer and am currently playing around with some code I found online to try to help me get to grips with how a ray tracer works. The following portion of the code I know is the function which creates a .bmp file by sort of scanning the screen but I was wondering if anyone might be able to more specifically explain what the code is doing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
struct RGBType {
	double r;
	double g;
	double b;
};


void savebmp (const char *filename, int w, int h, int dpi, RGBType *data){
	FILE *f;
	int k=w*h;
	int s=4*k;
	int filesize = 54 + s;
	
	double factor=39.375;
	int m=static_cast<int>(factor);
	
	int ppm=dpi*m;
	
	unsigned char bmpfileheader[14]={'B', 'M',0,0,0,0, 0,0,0,0, 54,0,0,0};
	unsigned char bmpinfoheader[40]={40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,24,0};
	
	bmpfileheader[ 2]=(unsigned char)(filesize);
	bmpfileheader[ 3]=(unsigned char)(filesize>>8);
	bmpfileheader[ 4]=(unsigned char)(filesize>>16);
	bmpfileheader[ 5]=(unsigned char)(filesize>>24);
	
	bmpinfoheader[4]=(unsigned char)(w);
	bmpinfoheader[5]=(unsigned char)(w>>8);
	bmpinfoheader[6]=(unsigned char)(w>>16);
	bmpinfoheader[7]=(unsigned char)(w>>24);
	
	bmpinfoheader[8]=(unsigned char)(h);
	bmpinfoheader[9]=(unsigned char)(h>>8);
	bmpinfoheader[10]=(unsigned char)(h>>16);
	bmpinfoheader[11]=(unsigned char)(h>>24);
	
	bmpinfoheader[21]=(unsigned char)(s);
	bmpinfoheader[22]=(unsigned char)(s>>8);
	bmpinfoheader[23]=(unsigned char)(s>>16);
	bmpinfoheader[24]=(unsigned char)(s>>24);
	
	bmpinfoheader[25]=(unsigned char)(ppm);
	bmpinfoheader[26]=(unsigned char)(ppm>>8);
	bmpinfoheader[27]=(unsigned char)(ppm>>16);
	bmpinfoheader[28]=(unsigned char)(ppm>>24);	
	
	bmpinfoheader[29]=(unsigned char)(ppm);
	bmpinfoheader[30]=(unsigned char)(ppm>>8);
	bmpinfoheader[31]=(unsigned char)(ppm>>16);
	bmpinfoheader[32]=(unsigned char)(ppm>>24);	
	
	f=fopen(filename,"wb");
	
	fwrite(bmpfileheader,1,14,f);
	fwrite(bmpinfoheader,1,40,f);
	
	for(int i=0; i<k; i++){
		RGBType rgb=data[i];
		
		double red=(data[i].r)*255;
		double green=(data[i].g)*255;
		double blue=(data[i].b)*255;
		
		unsigned char color[3]={(int)floor(blue),(int)floor(green),(int)floor(red)};
		
		fwrite(color,1,3,f);
	}
	
	fclose(f);

}


Any help would be greatly appreciated.

Thanks
The function takes data from an array of RGBType structs and writes it to a bmp file. It is not scanning the screen. It only uses the parameters that are passed in to make the image file. If you are unfamiliar with the structure of a bmp file, you might look here: http://en.wikipedia.org/wiki/BMP_file_format
Thank you for your response, that has helped somewhat to clear things up as I do not understand this portion of the code at all. Just to be really clear could you explain for example what a line like this line is doing specifically:

bmpinfoheader[23]=(unsigned char)(s>>16);

I assume the left hand side is accessing the 24th element of the array 'bmpinfoheader' but the right hand side I don't get.

Thanks.
Last edited on
>> 16 Is performing a bit-shift to the right by 16 bits.
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/bitshift.html
All the bit shifting is to accomplish packing all the correct bits in their correct locations for the BMP file format specification.

(unsigned char) Is an example of a typecasting operation.
http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
Just to be really clear could you explain for example what a line like this line is doing specifically:
lines 22 to 25 and the other are breaking 32 bit (int) into 8 bit (unsigned char) values.

The least significant 8 bits first (unsigned char)(s) until the most siginficant (unsigned char)(s>>24)
Thanks. This helped.
Topic archived. No new replies allowed.