| 1014 | //#define STB_IMAGE_WRITE_IMPLEMENTATION |
| 1015 | #include "stb_image/stb_image_write.h" |
| 1016 | static void writeTextureToFile(int textureWidth, int textureHeight, const char* fileName, FILE* ffmpegVideo) |
| 1017 | { |
| 1018 | int numComponents = 4; |
| 1019 | //glPixelStorei(GL_PACK_ALIGNMENT,1); |
| 1020 | |
| 1021 | b3Assert(glGetError() == GL_NO_ERROR); |
| 1022 | //glReadBuffer(GL_BACK);//COLOR_ATTACHMENT0); |
| 1023 | |
| 1024 | float* orgPixels = (float*)malloc(textureWidth * textureHeight * numComponents * 4); |
| 1025 | glReadPixels(0, 0, textureWidth, textureHeight, GL_RGBA, GL_FLOAT, orgPixels); |
| 1026 | //it is useful to have the actual float values for debugging purposes |
| 1027 | |
| 1028 | //convert float->char |
| 1029 | char* pixels = (char*)malloc(textureWidth * textureHeight * numComponents); |
| 1030 | assert(glGetError() == GL_NO_ERROR); |
| 1031 | |
| 1032 | for (int j = 0; j < textureHeight; j++) |
| 1033 | { |
| 1034 | for (int i = 0; i < textureWidth; i++) |
| 1035 | { |
| 1036 | pixels[(j * textureWidth + i) * numComponents] = char(orgPixels[(j * textureWidth + i) * numComponents] * 255.f); |
| 1037 | pixels[(j * textureWidth + i) * numComponents + 1] = char(orgPixels[(j * textureWidth + i) * numComponents + 1] * 255.f); |
| 1038 | pixels[(j * textureWidth + i) * numComponents + 2] = char(orgPixels[(j * textureWidth + i) * numComponents + 2] * 255.f); |
| 1039 | pixels[(j * textureWidth + i) * numComponents + 3] = char(orgPixels[(j * textureWidth + i) * numComponents + 3] * 255.f); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (ffmpegVideo) |
| 1044 | { |
| 1045 | fwrite(pixels, textureWidth * textureHeight * numComponents, 1, ffmpegVideo); |
| 1046 | //fwrite(pixels, 100,1,ffmpegVideo);//textureWidth*textureHeight*numComponents, 1, ffmpegVideo); |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | if (1) |
| 1051 | { |
| 1052 | //swap the pixels |
| 1053 | unsigned char tmp; |
| 1054 | |
| 1055 | for (int j = 0; j < textureHeight / 2; j++) |
| 1056 | { |
| 1057 | for (int i = 0; i < textureWidth; i++) |
| 1058 | { |
| 1059 | for (int c = 0; c < numComponents; c++) |
| 1060 | { |
| 1061 | tmp = pixels[(j * textureWidth + i) * numComponents + c]; |
| 1062 | pixels[(j * textureWidth + i) * numComponents + c] = |
| 1063 | pixels[((textureHeight - j - 1) * textureWidth + i) * numComponents + c]; |
| 1064 | pixels[((textureHeight - j - 1) * textureWidth + i) * numComponents + c] = tmp; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | stbi_write_png(fileName, textureWidth, textureHeight, numComponents, pixels, textureWidth * numComponents); |
| 1070 | } |
| 1071 | |
| 1072 | free(pixels); |
| 1073 | free(orgPixels); |
no test coverage detected