Render To Texture when using instanced drawing

I have several objects being drawn, some using "regular" methods and some using glDrawArraysInstanced and everything works very well.

I'm trying to add some post processing by rendering to texture using Frame Buffers , but when I do, if I'm drawing the instanced objects, I'm getting this result:

(You can see behind the mess the actual items).

If I comment the draw methods of the instanced objects, all is well.

Is it at all possible to use instanced drawing with render to texture?

Some code:

Instanced Object

void LifeMeter::draw(mat4 wvp)
{
    // Set the program to be used in subsequent lines:
    glUseProgram(program);

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glUniform1i(glGetUniformLocation(program, "texSampler"), /*GL_TEXTURE*/0);

    glBindVertexArray(_vao);
    glUniformMatrix4fv (glGetUniformLocation(program, "wvp") , 1, GL_FALSE, value_ptr(wvp));
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vec4)*amountOfLives, positions);
    glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, amountOfLives);

    // Unbind the Vertex Array object
    glBindVertexArray(0);
    // Cleanup, not strictly necessary
    glUseProgram(0);
}

Post Processing Buffer

void PPBuffer::init(int screen_width, int screen_height)
{
  // buffer
  // The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.

  glGenFramebuffers(1, &fbo);
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);


  /* Texture  */

  // The texture we're going to render to
  glGenTextures(1, &fbo_texture);

  // "Bind" the newly created texture : all future texture functions will modify this texture
  glBindTexture(GL_TEXTURE_2D, fbo_texture);
  glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA,screen_width, screen_height, 0,GL_RGBA, GL_UNSIGNED_BYTE, 0);

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

  glBindTexture(GL_TEXTURE_2D, 0);

  // depth buffer
  glGenRenderbuffers(1, &rbo_depth);
  glBindRenderbuffer(GL_RENDERBUFFER, rbo_depth);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, screen_width, screen_height );

  glBindRenderbuffer(GL_RENDERBUFFER, 0);

  /* Framebuffer to link everything together */
  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fbo_texture, 0); //use more of these for MRT
  // glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, fbo_texture1, 0);

  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_depth);

  // Set the list of draw buffers.
  GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};

  glDrawBuffers(1,DrawBuffers); 

  // for MRT:  glDrawBuffers(2,{GL_COLOR_ATTACHMENT0,GL_COLOR_ATTACHMENT1}); 

  GLenum status;
  if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
    fprintf(stderr, "glCheckFramebufferStatus: error %d", status);
    exit(0);
  }

  glBindFramebuffer(GL_FRAMEBUFFER, 0);

  // vertices 
  GLfloat fbo_vertices[] = {
    -1, -1,
    1, -1,
    -1,  1,
    1,  1,
  };

  // Create and bind the object's Vertex Array Object:
  glGenVertexArrays(1, &_vao);
  glBindVertexArray(_vao);

  glGenBuffers(1, &vbo_fbo_vertices);
  glBindBuffer(GL_ARRAY_BUFFER, vbo_fbo_vertices);
  glBufferData(GL_ARRAY_BUFFER, sizeof(fbo_vertices), fbo_vertices, GL_STATIC_DRAW);
  glBindBuffer(GL_ARRAY_BUFFER, 0);  

  glBindVertexArray(0);
  // shader
  programManager::sharedInstance()
    .createProgram("pp",
           SHADERS_DIR "postproc.v.glsl",
           SHADERS_DIR "postproc.f.glsl");
  program_postproc = programManager::sharedInstance().programWithID("pp");

  attribute_v_coord_postproc = glGetAttribLocation(program_postproc, "v_coord");
  uniform_fbo_texture = glGetUniformLocation(program_postproc, "fbo_texture");
}

void PPBuffer::setup() {
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
}

void PPBuffer::render() {
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  glClearColor(0.3, 0.0, 0.0, 1.0);

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  glUseProgram(program_postproc);

  glActiveTexture(GL_TEXTURE0);

  glBindTexture(GL_TEXTURE_2D, fbo_texture);
  glUniform1i(uniform_fbo_texture, /*GL_TEXTURE*/0);

  glBindVertexArray(_vao);
  glEnableVertexAttribArray(attribute_v_coord_postproc);
  glBindBuffer(GL_ARRAY_BUFFER, vbo_fbo_vertices);
  glVertexAttribPointer(
            attribute_v_coord_postproc,  // attribute
            2,                  // number of elements per vertex, here (x,y)
            GL_FLOAT,           // the type of each element
            GL_FALSE,           // take our values as-is
            0,                  // no extra data between each position
            0                   // offset of first element
            );
  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  glDisableVertexAttribArray(attribute_v_coord_postproc);
  glBindBuffer(GL_ARRAY_BUFFER,0);
  glBindVertexArray(0);
  glUseProgram(0);
}

Display Function

void display(void)
{

    _ppbuffer.setup();
    // Clear the screen buffer
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    // tell the model to draw itself...
    shuttle->draw(); // Regular object
    lifeMeter->draw(Projection ); // Instanced object


    _ppbuffer.render();

    // Swap those buffers so someone will actually see the results... //
    glutSwapBuffers();
}

I solve the problem, so if anyone stumbles across this question, the problem was the blending feature.

Removing the lines

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Solved the issue. Since I can live without them, I'm satisfied.

链接地址: http://www.djcxy.com/p/34038.html

上一篇: 绘制纹理不起作用LWJGL

下一篇: 使用实例化绘图时渲染为纹理