gpt4 book ai didi

c++ - 翻转 glReadPixels 的 RGBA 输出(C++ OpenGL)

转载 作者:行者123 更新时间:2023-11-27 23:14:35 25 4
gpt4 key购买 nike

我想翻转 glReadPixel 的输出图像。我通过将 glReadPixels 保存到文件来检查输出。该图像是正确的,但它是颠倒的。

我尝试过的

int patchSize = 50;
std::vector<unsigned char> rgbdata(4*patchSize*patchSize);
glReadPixels(x-(patchSize/2), y-(patchSize/2)/2), patchSize,patchSize,GL_RGBA,GL_UNSIGNED_BYTE, &rgbdata[0]);

std::vector< unsigned char > temp_rgbdata = rgbdata; // Create a copy of the data
rgbdata.clear(); // Reset the array
for (int i=patchSize-1; i >= 0; i--) // Count backwards in order to flip
{
for( int j = 0; j < patchSize; j++) {

rgbdata.push_back(temp_rgbdata[i*patchSize+j*4]);
rgbdata.push_back(temp_rgbdata[i*patchSize+j*4+1]);
rgbdata.push_back(temp_rgbdata[i*patchSize+j*4+2]);
rgbdata.push_back(temp_rgbdata[i*patchSize+j*4+3]);

}
}
temp_rgbdata.clear(); // Clear the temporary array

怎么了

图像结果全错了:

Output from trying to flip image

感谢任何帮助和评论。谢谢。

解决方案(鲍里斯)

int patchSize = 50;
std::vector<unsigned char> rgbdata(4*patchSize*patchSize);
glReadPixels(x-(patchSize/2), y-(patchSize/2)), patchSize,patchSize,GL_RGBA,GL_UNSIGNED_BYTE, &rgbdata[0]);

std::vector< unsigned char > temp_rgbdata = rgbdata; // Create a copy of the data
rgbdata.clear(); // Reset the array
for (int i=patchSize-1; i >= 0; i--) // Count backwards in order to flip
{
for( int j = 0; j < patchSize; j++) {

rgbdata.push_back(temp_rgbdata[(i*patchSize+j)*4]);
rgbdata.push_back(temp_rgbdata[(i*patchSize+j)*4+1]);
rgbdata.push_back(temp_rgbdata[(i*patchSize+j)*4+2]);
rgbdata.push_back(temp_rgbdata[(i*patchSize+j)*4+3]);

}
}
temp_rgbdata.clear(); // Clear the temporary array

最佳答案

这是一个完整的例子,建立在 Stypox 的精彩片段之上——这也是实际的写作。当然,交换坐标系统是另一种解决方法但我发现左上角的 0,0 更自然。

// https://github.com/nothings/stb/blob/master/stb_image_write.h
#include "stb_image_write.h"

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadBuffer(GL_BACK_LEFT);

std::vector<uint8_t> pixels(3 * w * h);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());

for(int line = 0; line != h/2; ++line) {
std::swap_ranges(pixels.begin() + 3 * w * line,
pixels.begin() + 3 * w * (line+1),
pixels.begin() + 3 * w * (h-line-1));
}

int components = 3;
stbi_write_png("out.png", w, h, components, pixels.data(), 3 * w);
stbi_write_tga("out.tga", w, h, components, pixels.data());

关于c++ - 翻转 glReadPixels 的 RGBA 输出(C++ OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com