TerraForge3D  2.3.1
3D Terrain And Landscape Generator
ExportTexture.cpp
1#include "ExportTexture.h"
2
3#include <glad/glad.h>
4#include <stb/stb_image_write.h>
5
6void ExportTexture(int fbo, std::string path, int w, int h)
7{
8 if (path.size() < 3)
9 {
10 return;
11 }
12
13 if (path.find(".png") == std::string::npos)
14 {
15 path += ".png";
16 }
17
18 unsigned char *data = new unsigned char[w * h * 3];
19 glReadBuffer(GL_COLOR_ATTACHMENT0);
20 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
21 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
22 glBindFramebuffer(GL_FRAMEBUFFER, 0);
23 stbi_flip_vertically_on_write(true);
24 stbi_write_png(path.c_str(), w, h, 3, data, w * 3);
25 delete[] data;
26}