0
votes

i am rendering simple pixel buffer in OpenGL. First, i create a quad, then i create a texture. It works correctly if there is no changes in buffer. When i change my buffer and add new buffer into texture by glTexSubImage2D or glTexImage2D my texture's top section corrupts like image.

Corrupted image

I create my buffer like this.

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;

I create texture like this.

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);

I update texture like this.

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

For testing i change my buffer like this in render function

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;
}

Then i call tpUseShader and render my quad.

How can i fix this problem?

I changed my console size to 10x10 and run again this time i got same results but, in image you can see from bottom left 3rd pixel is dark blue. When i print printf("3rd pixel: %d- %d - %d\n", console->buffer[12], console->buffer[13], console->buffer[14]);. value i got red: 0 green: 0 blue: 0 values. That means my buffer is normal.

10x10 corrupt image

1
Not enough code to see where it fails. Seems like your console->buffer is ill formed for the second update. Different method for the first one?Ripi2
I am just updating like console->buffer[6] = 255; //6 is red in my render function for testing.Batın Evirgen
Why do you use int for your buffer but GL_UNSIGNED_BYTE for the texture?Ripi2
I used GLubyte when creating buffer, and GL_UNSIGNED_BYTE when creating texture.Batın Evirgen
If buf gets out of scope the entire data is garbage, be careful with that. How do you render it?pleluron

1 Answers

0
votes

I got the solution. As pleluron said in comments of question. I changed buf in to console->buffer, and it worked!. Now my buffer initialization code is like this:

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;
}