纹理缓冲区翻转X和Y轴

huangapple go评论82阅读模式
英文:

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将这个纹理,我们会看到以下结果:

纹理缓冲区翻转X和Y轴

如果将其视为一个笛卡尔坐标系统,左下角为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:

纹理缓冲区翻转X和Y轴

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 &lt; 1024; x++) {
    for (int y = 0; y &lt; 512; y++) {
        texture_pixels[y][x][0] = 0;
        texture_pixels[y][x][1] = 0;
        texture_pixels[y][x][2] = 0;
        if (x == 0 &amp;&amp; y &lt; 256) {
            texture_pixels[y][x][0] = 255;
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月19日 00:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定