Drawing to multiple buffers simultaneously and rendering each buffer Individually to display

649 Views Asked by At

My idea and requirement are to allow creating atmost 'n' buffers (where is n<7) during the program execution. So, ideally, I am looking for a way, where I can draw different things to different buffers and render each buffer as and when needed.

Like:

Buffer1 - Draw a rectangle

Buffer2 - Draw some texture (font/text) "Welcome"

Buffer3 - Draw some polygon

Buffer4 - Draw some texture (font/text) "Supporter of Stack overflow"

... and so on.

Here simultaneously, I want to write different things to different buffers. And then I may only want to render say contents of Buffer2, then after some time flush the display and render Buffer4.

My not working code:

GLuint VB1;
glGenBuffers(1, &VB1);
glUseProgram(ProgramObject);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
DrawRect(VB1);

GLuint VB2;
glGenBuffers(1, &VB2);
glUseProgram(ProgramObject);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
TextDraw(VB1, "Welcome");

eglSwapBuffers(egldisplay, eglsurface);

This renders everything of VB1 and VB2 both, when I say eglSwapBuffer. How can I only render one buffer.

I am doing

glBindBuffer(GL_ARRAY_BUFFER, VB)

in DrawRect() and TextDraw() definitions, which I believe should not be done.

Need help to determine how to write to each buffers separately and how to bind the required buffer contents on the display. (when needed).

DrawRect Implementation

void DrawRect(GLint VB)
{
int position_loc = glGetAttribLocation(ProgramObject, "vertex");
GLfloat Vertices[4][4] = {
              -1.0f,  1.0f, 0.0f, 1.0f,
               1.0f,  1.0f, 0.0f, 1.0f,
              -1.0f,  -1.0f, 0.0f, 1.0f,
               1.0f,  -1.0f, 0.0f, 1.0f
        };
GLfloat red[4] = {1, 0, 1, 1};
glUniform4fv(glGetUniformLocation(ProgramObject, "color"), 1, red);
glEnableVertexAttribArray(position_loc);
printf("\nAfter Enable Vertex Attrib Array");
glBindBuffer(GL_ARRAY_BUFFER, VB);
glVertexAttribPointer(position_loc, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof Vertices, Vertices, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}



void TextDraw(GLuint VB, std::string message, GLfloat x, GLfloat y, GLfloat sx, GLfloat sy)

{
int vPosition = glGetAttribLocation(ProgramObject, "vertex");
std::string::const_iterator ch;
for (ch = message.begin(); ch != message.end(); ch++)
{
    const char c = *ch;
    if (FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_COLOR))
    {
        std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
        continue;
    }

GLfloat xpos = x + (face->glyph->bitmap_left) * sx;
    GLfloat ypos = -y - (face->glyph->bitmap_top) * sy;
    GLfloat w = (face->glyph->bitmap.width) * sx;
    GLfloat h = (face->glyph->bitmap.rows) * sy;

    GLfloat vertices[4][4] = {
    {xpos,     -ypos    , 0, 0},
    {xpos + w, -ypos    , 1, 0},
    {xpos,     -ypos - h, 0, 1},
    {xpos + w, -ypos - h, 1, 1},
    };

    glActiveTexture(GL_TEXTURE0);
    GLuint text;
    glGenTextures(1, &text);
    glBindTexture(GL_TEXTURE_2D, text);
    glUniform1i(glGetUniformLocation(ProgramObject, "text"), 0);
    PrintGlError();
    GLfloat black[4] = {0, 0, 0, 1};
    glUniform4fv(glGetUniformLocation(ProgramObject, "color"), 1, black);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_ALPHA,
        face->glyph->bitmap.width,
        face->glyph->bitmap.rows,
        0,
        GL_ALPHA,
        GL_UNSIGNED_BYTE,
        face->glyph->bitmap.buffer
    );
    glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_DYNAMIC_DRAW);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    x += (face->glyph->advance.x/64) * sx;
    y += (face->glyph->advance.y/64) * sy;
    glDeleteTextures(1, &text);
    glDisableVertexAttribArray(vPosition);
}

}
0

There are 0 best solutions below