void GenericImage::gen_normalMap(const char* filename) { GenericImage dst; dst.width = width; dst.height = height; dst.components = 3; dst.pixels = (unsigned char*)malloc(width * height * 3); char* nmap = gen_normal(); //cast from char to unsigned char to save as an normalmap image //and be able to look at the normalmap and make sense int index=0; unsigned char* p = (unsigned char*)dst.pixels; for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { p[3*index + 0] = (unsigned char)(nmap[3*index + 0] + 127); p[3*index + 1] = (unsigned char)(nmap[3*index + 1] + 127); p[3*index + 2] = (unsigned char)(nmap[3*index + 2] + 127); //fprintf(stderr, "normal map[%d](%d, %d, %d)\n", index, nmap[3*index + 0], nmap[3*index + 1], nmap[3*index + 2]); index++; } } bool ret = dst.save(filename); if(ret) printf("success! normalmap saved to image %s\n", filename); else printf("error! normalmap save to image %s failed\n", filename); }
int main(void) { for(size_t i=0; i<10; ++i) std::cout<<gen_normal() <<std::endl; }