2

代码如下(使用此处输入链接描述中的演示)来渲染字形

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

当我尝试将视口宽度设置为恰到好处的渲染文本大小时,我得到错误要么 malloc 检查标题错误 或得到图像 Misaligned Misplaced 图片,但是当我将宽度设置得足够大时,例如 1024,图像可以显示正确。

4

1 回答 1

1

当您从 GPU 内存中读取像素时,您必须设置GL_PACK_ALIGNMENT而不是GL_UNPACK_ALIGNMENT(请参阅glPixelStore):

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, picbuf);
于 2021-09-14T12:24:38.713 回答