#include #include #include #include #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) #define clamp(x,low,high) ( min(max(x,low),high) ) int fsize (FILE * fp) { fseek(fp, 0, SEEK_END); int end = ftell(fp); rewind(fp); return end; } // image class class TGAImage { public: struct TGAHeader { unsigned char head[12]; unsigned short w; unsigned short h; unsigned char bpp; char nothing; }; TGAImage(const char * filename) { // copy the data FILE *infile=fopen(filename,"rb"); filesize=fsize(infile); filedata=new unsigned char[filesize]; fread(filedata,filesize,1,infile); fclose(infile); unsigned char tga_header_gray[12] = {0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0}; unsigned char tga_header_rgb[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; TGAHeader *header=(TGAHeader *)filedata; width = header->w; height = header->h; bpp = header->bpp/8; image = filedata+sizeof(TGAHeader); // check that it is a tga i know how to read if (memcmp(header->head, tga_header_rgb, 12) && memcmp(header->head, tga_header_gray, 12)) { printf("failed loading tga %s\n",filename); } } void Write(const char *filename) { FILE* outfile=fopen(filename,"wb"); fwrite(filedata,filesize,1,outfile); fclose(outfile); } ~TGAImage() { delete filedata; } int BytesPerPixel() { return bpp; } int Width() { return width; } int Height() { return height; } unsigned char* Pixel(int row,int col) { return image+bpp*(width*((row+height)%height)+(col+width)%width); } private: int width; int height; int bpp; unsigned char *filedata; int filesize; unsigned char *image; }; void swap(int& a, int& b) { int t=a; a=b; b=t; } // find highest elevation angle in height map along a line float raycast(TGAImage& height,int y0,int x0,float angle, int length) { int baserow=y0; int basecol=x0; int startingheight=height.Pixel(baserow,basecol)[0]; int x1 = x0+cos(angle)*length; int y1 = y0+sin(angle)*length; bool steep = abs(y1 - y0) > abs(x1 - x0); if(steep) { swap(x0, y0); swap(x1, y1); } if(x0>x1) { swap(x0, x1); swap(y0, y1); } int deltax = x1 - x0; int deltay = abs(y1 - y0); int error = deltax / 2; int y = y0; int ystep = (y0