英文:
Texture buffer flip x and y axis
问题
OpenGL似乎假定纹理缓冲区从屏幕的左下角开始读取。这没问题,但我遇到的问题是它如何读取后续的像素。如果我像这样设置一个纹理缓冲区:
//texture_pixels[0][0][]是左下角
//texture_pixels[x][y][rgb]
unsigned char texture_pixels[1024][512][3];
for (int x = 0; x < 1024; x++) {
for (int y = 0; y < 512; y++) {
texture_pixels[x][y][0] = 0;
texture_pixels[x][y][1] = 0;
texture_pixels[x][y][2] = 0;
if (x == 0 && y < 256) {
texture_pixels[x][y][0] = 255;//将前256个值设置为红色,其他值为黑色
}
}
}
然后创建一个纹理、帧缓冲对象,并使用glBlitFramebuffer将这个纹理,我们会看到以下结果:
如果将其视为一个笛卡尔坐标系统,左下角为0,0,那么红线应该向上而不是向右。是否有办法更改OpenGL读取纹理缓冲区的方式,以使其成为这种情况,或者唯一的选择是在传递缓冲区之前翻转x和y轴(texture_pixels[y][x][rgb])?
英文:
It seems that OpenGL reads texture buffers with the assumption that the buffer starts on the bottom left corner of the screen. This is fine, but the issue I have is with how it reads subsequent pixels. If I set up a texture buffer like so:
//texture_pixels[0][0][] is the bottom left corner
//texture_pixels[x][y][rgb]
unsigned char texture_pixels[1024][512][3];
for (int x = 0; x < 1024; x++) {
for (int y = 0; y < 512; y++) {
texture_pixels[x][y][0] = 0;
texture_pixels[x][y][1] = 0;
texture_pixels[x][y][2] = 0;
if (x == 0 && y < 256) {
texture_pixels[x][y][0] = 255;//set the first 256 values to red and everything else black
}
}
}
And then create a texture, framebuffer object, and glBlitFramebuffer this texture, we see the following:
If treating this as a Cartesian coordinate system with the bottom left corner being 0,0, the red line should be going up instead of right. Is there a way to change how OpenGL reads the texture buffer so that this is the case, or is the only option to flip the x and y axis (texture_pixels[y][x][rgb]) before passing the buffer?
答案1
得分: 1
OpenGL纹理图像按行存储,但您的图像按列存储。您需要交换x和y。(每行512行,每行1024像素,每行3个颜色通道)
unsigned char texture_pixels[512][1024][3];
for (int x = 0; x < 1024; x++) {
for (int y = 0; y < 512; y++) {
texture_pixels[y][x][0] = 0;
texture_pixels[y][x][1] = 0;
texture_pixels[y][x][2] = 0;
if (x == 0 && y < 256) {
texture_pixels[y][x][0] = 255;
}
}
}
英文:
The OpenGL texture image is stored row by row, but your image is stored column by column. You need to swap x and y. (512 rows with 1024 pixels and 3 color channels each)
unsigned char texture_pixels[512][1024][3];
for (int x = 0; x < 1024; x++) {
for (int y = 0; y < 512; y++) {
texture_pixels[y][x][0] = 0;
texture_pixels[y][x][1] = 0;
texture_pixels[y][x][2] = 0;
if (x == 0 && y < 256) {
texture_pixels[y][x][0] = 255;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论