Jump to content
  • Advertisement
Sign in to follow this  
smile55

Problem with TBO

This topic is 2351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was trying to use TBO to do a simple task: First I fill a buffer object with some data; Then I want to visualize the result, so I bind the buffer object to TEXTURE_BUFFER, and post it on the screen.

here is my codes:

Initialize:


/* Just fill the buffer, color is changing in x direction */
std::vector<float> damp(WIN_WIDTH * (WIN_HEIGHT + 1));
for (int i = 0; i < WIN_WIDTH; ++i) {
for (int j = 0; j < WIN_HEIGHT; ++j) {
damp[i + j * WIN_WIDTH] = (float)i / WIN_WIDTH;
}
}

/* Create the TBO and fill the TBO with the data generated above */
glGenBuffers(1, &g_buf_levelset);
glBindBuffer(GL_TEXTURE_BUFFER, g_buf_levelset);
glBufferData(GL_TEXTURE_BUFFER, width * height * sizeof(float), &damp[0], GL_DYNAMIC_COPY);
glBindBuffer(GL_TEXTURE_BUFFER, 0);

/* Create the texture of the TBO */
glGenTextures(1, &g_tex_levelset);
glBindTexture(GL_TEXTURE_BUFFER, g_tex_levelset);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R32F, g_buf_levelset);
glBindTexture(GL_TEXTURE_BUFFER, 0);


Rendering:


/* Post the texture to the screen */
glUseProgram(g_pgm_texture);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_BUFFER, g_tex_levelset);

glUniform1i(g_uniform_texture, 0);
glUniform1i(g_uniform_win_width, WIN_WIDTH);
glUniform1i(g_uniform_win_height, WIN_HEIGHT);

glBindVertexArray(g_quad_vao);
glEnableVertexAttribArray(0);

glDrawArrays(GL_QUADS, 0, 4);

glDisableVertexAttribArray(0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_BUFFER, 0);
glUseProgram(0);


Here is the fragment shader:

#version 330
uniform samplerBuffer tex;
uniform int win_width;
uniform int win_height;
out vec4 color;
void main(void)
{
int offset = int(gl_FragCoord.x + gl_FragCoord.y * win_width);
float dist = texelFetch(tex, offset).r;

color = vec4(dist, 0.0, 0.0, 1.0);
}


There is no vertex shader , the verts are just the four corners of a screen quad. I mean they are {(-1.0, -1.0, 0.0), (1.0, -1.0, 0.0), (1.0, 1.0, 0.0), (-1.0, 1.0, 0.0)}, I set the modelview matrix and projection matrix both to identity matrices.

But the image I got was shifted: the left half part of the image was move to right half of the screen, and the right half of the image was round to left half of the screen.

I mean, the distribution of color in x direction is supposed to be:
[0.0 --------------------> 1.0]
but I got:
[0.5----->1.0 0.0 --- >0.5]

If I change the code from

glBufferData(GL_TEXTURE_BUFFER, width * height * sizeof(float), &damp[0], GL_DYNAMIC_COPY);

to

glBufferData(GL_TEXTURE_BUFFER, width * height * sizeof(float), &damp[WIN_WIDTH / 2], GL_DYNAMIC_COPY);


The result is correct. So it seems the while data is offset by WIN_WIDTH / 2.

I checked the codes for a lot times and can't figure out why, anyone has some ideas?

Thanks in advance. Edited by smile55

Share this post


Link to post
Share on other sites
Advertisement
Is it true that width == WIN_WIDTH and height == WIN_HEIGHT? Why do you allocate the damp vector of WIN_WIDTH * (WIN_HEIGHT + 1) elements, instead of WIN_WIDTH * WIN_HEIGHT?

Share this post


Link to post
Share on other sites

Is it true that width == WIN_WIDTH and height == WIN_HEIGHT? Why do you allocate the damp vector of WIN_WIDTH * (WIN_HEIGHT + 1) elements, instead of WIN_WIDTH * WIN_HEIGHT?


It's true that width == WIN_WIDTH and height == WIN_HEIGHT.

I use WIN_WIDTH * (WIN_HEIGHT + 1) because I manually offset WIN_WIDTH / 2 to bypass the problem: I copy data between damp[WIN_WIDTH / 2 : WIN_WIDTH * WIN_HEIGHT + WIN_WIDTH / 2] to TBO instead of damp[0 : WIN_WIDTH * HEIGHT].

It solve the problem, but I want to know what causes the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!