2

code show as below(use the demo from enter link description here)to render glyph

Luint rbcolor;
    glGenRenderbuffers(1, &rbcolor);
    glBindRenderbuffer(GL_RENDERBUFFER, rbcolor);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RED, width, height);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint rbds;
    glGenRenderbuffers(1, &rbds);
    glBindRenderbuffer(GL_RENDERBUFFER, rbds);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    GLuint fbo;
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbcolor);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbds);
//render
   while(...){
 ...
 glviewport(0,0,width,height);
  ...
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, picbuf);
// render in screen 
 glBindFramebuffer(GL_FRAMEBUFFER, 0);
}


// Flipping the picture vertically

    uint8_t *row_swap = (uint8_t*) malloc( width );

    for ( int iy = 0; iy < height / 2; ++iy ) {
        uint8_t* row0 = picbuf + iy * width;
        uint8_t* row1 = picbuf + ( height - 1 - iy ) * width;
        memcpy( row_swap, row0, width );
        memcpy( row0, row1, width );
        memcpy( row1, row_swap, width );
    }

    free( row_swap );

    // Saving the picture

    std::string png_filename = res_filename + ".png";
    if ( !stbi_write_png( png_filename.c_str(), width, height, 1, picbuf, 0) ) {
        std::cout << "Error writing png file." << std::endl;
        exit( 1 );
    }

when i try to set the viewport width just right the render text size, i get the error Either malloc check header error or get the image Misaligned Misplaced picture, but when i set the width big enough, such as 1024, the image can be display correctly.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
ziwei wang
  • 21
  • 2

1 Answers1

1

When you read pixel from the GPU memory you have to set GL_PACK_ALIGNMENT instead of GL_UNPACK_ALIGNMENT (see glPixelStore):

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, picbuf);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174