【问题标题】:OpenGL Texture corruptionOpenGL纹理损坏
【发布时间】:2017-07-13 13:57:14
【问题描述】:

我在 OpenGL 中渲染简单的像素缓冲区。首先,我创建一个四边形,然后创建一个纹理。如果缓冲区没有变化,它可以正常工作。当我更改缓冲区并通过glTexSubImage2DglTexImage2D 将新缓冲区添加到纹理中时,纹理的顶部会像图像一样损坏。

我这样创建缓冲区。

int length = console->width * console->height * 3;
GLubyte buf[length];

for(int i = 0; i < length; i += 3) {
    buf[i] = 0;
    buf[i + 1] = 0;
    buf[i + 2] = 0;
}

console->buffer = buf;

我创建这样的纹理。

glGenTextures(1, &console->textureID);
glBindTexture(GL_TEXTURE_2D, console->textureID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, console->width, console->height, 0, GL_RGB, GL_UNSIGNED_BYTE, console->buffer);
tpUseShader(console); // -> calls glUseShader(console->programID);
glUniform1i(glGetUniformLocation(console->programID, "texture"), 0);

我像这样更新纹理。

glBindTexture(GL_TEXTURE_2D, console->textureID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, console->width, console->height, GL_RGB, GL_UNSIGNED_BYTE, console->buffer);

为了测试,我在渲染函数中像这样更改我的缓冲区

if(console->buffer[6] == 255) {
    console->buffer[6] = 0; // 6 is second pixel's red value.
    console->buffer[10] = 255; // 10 is third pixel's green value
} else {
    console->buffer[6] = 255;
    console->buffer[10] = 0;
}

然后我调用 tpUseShader 并渲染我的四边形。

我该如何解决这个问题?

我将控制台大小更改为 10x10 并再次运行,这次我得到了相同的结果,但是,在图像中,您可以从左下角看到第 3 个像素是深蓝色。当我打印printf("3rd pixel: %d- %d - %d\n", console-&gt;buffer[12], console-&gt;buffer[13], console-&gt;buffer[14]);。值我得到红色:0 绿色:0 蓝色:0 值。这说明我的缓冲区是正常的。

【问题讨论】:

  • 没有足够的代码来查看失败的地方。似乎您的 console-&gt;buffer 在第二次更新时格式不正确。第一个方法不同?
  • 我只是在我的渲染函数中更新console-&gt;buffer[6] = 255; //6 is red 进行测试。
  • 为什么你的缓冲区使用int 而纹理使用GL_UNSIGNED_BYTE
  • 我在创建缓冲区时使用GLubyte,在创建纹理时使用GL_UNSIGNED_BYTE
  • 如果buf 超出范围,整个数据都是垃圾,请小心。你是怎么渲染的?

标签: opengl


【解决方案1】:

我得到了解决方案。正如 pleluron 在 cmets of question 中所说的那样。我将buf 更改为console-&gt;buffer,它成功了!现在我的缓冲区初始化代码是这样的:

console->buffer = malloc(sizeof(GLubyte) * length);

for(int i = 0; i < length; i += 3) {
    console->buffer[i] = 0;
    console->buffer[i + 1] = 0;
    console->buffer[i + 2] = 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2019-02-14
    • 2018-10-18
    • 1970-01-01
    相关资源
    最近更新 更多