// Function DumpScreenToFile // void DumpScreenToFile(char* filename, int width, int height, int component); // // Reads the color or depth values from a rectangular area from (0,0) to (width,height) // on the screen and dumps it to a text file. // // Use component value as // 0 for Red color component // 1 for Green color component // 2 for Blue color component // 3 for Alpha color component // 4 for Depth component // // Example usage: // DumpScreenToFile("Screen.txt", 400, 300, 0); // #include void WriteToFile(char* filename, float* screenData, int w, int h, float scale, float bias) { FILE *fp; fopen_s(&fp,filename,"w"); for(int j=0; j < h; j++) { for(int i=0; i < w; i++) fprintf(fp,"%f ",screenData[(j*w+i)]*scale+bias); fprintf(fp,"\n"); } fclose(fp); } void DumpScreenToFile(char* filename, int width, int height, int component) { float* screenData = new float[height*width]; glReadBuffer(GL_FRONT); if (component == 0) glReadPixels(0,0,width,height,GL_RED ,GL_FLOAT,screenData); else if (component == 1) glReadPixels(0,0,width,height,GL_GREEN,GL_FLOAT,screenData); else if (component == 2) glReadPixels(0,0,width,height,GL_BLUE ,GL_FLOAT,screenData); else if (component == 3) glReadPixels(0,0,width,height,GL_ALPHA,GL_FLOAT,screenData); else if (component == 4) glReadPixels(0,0,width,height,GL_DEPTH_COMPONENT,GL_FLOAT,screenData); else return; float scale = 1; float bias = 0; if (component == 4) { glGetFloatv (GL_DEPTH_SCALE,&scale); glGetFloatv (GL_DEPTH_BIAS, &bias); } WriteToFile(filename, screenData , width , height, scale, bias); delete[] screenData; }